Skip to content

rgdal - read geoJSON with readOGR

2 messages · Emmanuel Blondel (GMAIL), Barry Rowlingson

#
Dear all,

I tried to use the readOGR from package rgdal to read a geoJSON file 
from a Geoserver WFS.
I can get a SpatialPointDataFrame with this code:

url<-"http://localhost:8080/geoserver/wfs?service=WFS&version=1.0.0&request=GetFeature&outputFormat=json&typeName=mylayer"
ogrInfo(dsn=url,layer = 'OGRGeoJSON')
layer<-readOGR(dsn=url,layer = 'OGRGeoJSON')

but my problem is regarding the attributes. I have a numeric (real) 
field that is converted to integer. To see why readOGR had this 
behaviour, I downloaded my geoJSON file to explore the data: It appears 
that the first value for this attribute is 1, so the field is converted 
in integer with readOGR, and all values between 0 and 1 are set to 0.

Is there any way to force numeric attributes to real type?

I thank you in advance,

Cheers,
Emmanuel
#
On Wed, May 11, 2011 at 8:27 AM, Emmanuel Blondel
<emmanuel.blondel1 at gmail.com> wrote:
Some experiments with the command-line ogrinfo tools seem to indicate
that the geoJSON driver in OGR has this behaviour. I can't see this
explicitly in the source code from a brief look, however.

 The JSON spec only has a single 'number' concept (no differentiating
between ints and reals) so I'd argue that the geoJSON driver should
read anything numeric in as 'real'. (RFC 4627).

 If I get five minutes today I'll create some test samples and submit
a bug report to the OGR guys.

 A massively kludgy workaround would be to use readOGR to get the
geometry, and "fromJSON" from the rjson package to get the attributes
(which would need massaging to get into a data frame) - this does
preserve the floating point nature of data even if the first point
doesn't have a decimal point in it! If the geometry is simple enough
(points?) you could do it all with fromJSON fairly easily.

 Final thought: Geoserver WFS should maybe generate geoJSON where real
values always have a decimal point in them (I'm not sure if Geoserver
is creating these from anothe data source or just passing through
already-created files..).

Barry