[Qgis-developer] Reading GEOJSON from string using GDAL

Even Rouault even.rouault at spatialys.com
Thu Feb 18 16:35:15 PST 2016


Le vendredi 19 février 2016 01:30:59, Nyall Dawson a écrit :
> On 15 January 2016 at 06:57, Even Rouault <even.rouault at spatialys.com> 
wrote:
> >> I've got a question regarding the geojson driver you may be able to
> >> assist with. Is there any method in the OGR libraries which allow
> >> direct parsing of a string to a  layer? (Ie, without first writing it
> >> out to a file).
> >> 
> >> I'd like to add the ability to directly paste geojson text into QGIS and
> >> have it inserted as a feature in the current layer (like how you can
> >> currently paste WKT text as a feature). I don't want to have to manually
> >> parse the json (that would be a nightmare).
> > 
> > Well, the GeoJSON driver support "filenames" which are in fact GeoJSON
> > content:
> > 
> > $ ogrinfo
> > '{"type":"Feature","properties":{"foo":"bar"},"geometry":{"type":"Point"
> > ,"coordinates":[2,49]}}' -ro -al
> > 
> > Layer name: OGRGeoJSON
> > Geometry: Point
> > Feature Count: 1
> > Extent: (2.000000, 49.000000) - (2.000000, 49.000000)
> > Layer SRS WKT:
> > GEOGCS["WGS 84",
> > 
> >     DATUM["WGS_1984",
> >     
> >         SPHEROID["WGS 84",6378137,298.257223563,
> >         
> >             AUTHORITY["EPSG","7030"]],
> >         
> >         AUTHORITY["EPSG","6326"]],
> >     
> >     PRIMEM["Greenwich",0,
> >     
> >         AUTHORITY["EPSG","8901"]],
> >     
> >     UNIT["degree",0.0174532925199433,
> >     
> >         AUTHORITY["EPSG","9122"]],
> >     
> >     AUTHORITY["EPSG","4326"]]
> > 
> > foo: String (0.0)
> > OGRFeature(OGRGeoJSON):0
> > 
> >   foo (String) = bar
> >   POINT (2 49)
> > 
> > Works also with FeatureCollections.
> > Alternatively, you could also put the content in a in-memory GDAL file
> > (/vsimem/ virtual file system) and open it.
> > 
> > So you could likely instanciate a temporary layer and get a QGIS feature
> > from that.
> 
> Hi Even,
> 
> I'm currently exploring this approach (in memory file), but I'm stuck
> on how to actually write the string contents to a memory file prior to
> opening to it with GDAL. I gather for the python bindings I could
> utilise FileFromMemBuffer, but I can't find any similar replacement in
> the c api.

VSIFileFromMemBuffer :
http://www.gdal.org/cpl__vsi_8h.html#a86b6b1c37bb19d954ee3c4a7e910120c

And some code snippet here :
http://www.gdal.org/cpl__vsi_8h.html#a66e2e6f093fd42f8a941b962d4c8a19e


> 
> What's the correct approach to use here?
> 
> Nyall
> 
> > If you are just interested in geometries and not attributes, there's also
> > the C function :
> > 
> > OGRGeometryH    OGR_G_CreateGeometryFromJson (const char *);
> > 
> >> If I could somehow take advantage of OGR's geojson driver to do the
> >> heavy lifting then this work would be trivial.
> >> 
> >> Any ideas?
> >> 
> >> Nyall
> > 
> > --
> > Spatialys - Geospatial professional services
> > http://www.spatialys.com

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the Qgis-developer mailing list