Using the GDAL/OGR API, I need to be able to convert various types of GDAL datasources into GeoJSON. However, I do not want to write the GeoJSON to a file, but rather I need to just return the text string within the application.

For example, if I have a set of shapefiles, I want to be able to extract the data from one of these shapefiles as a GeoJSON text string.

I want to avoid the slow I/O penalty of writing it to a file and then reading the file and then deleting the file. Such I/O would likely slow things down too much, as I would be running such tasks very frequently (if I can ever get it to work).

Is there some way to get GDAL data as GeoJSON text without writing it to a file first?

Note that I'm using the C API for GDAL/OGR within an application. So a C or C++ solution would be preferred. However, I'm also open to other suggestions which may give me other ideas.

PS . This is on iOS.

  • Write to memory perhaps? i.e. output to the virtual memory filesystem /vsimem/blahblah.json, then read with VSIFOpenL VSIFReadL – user2856 Dec 20 '16 at 5:05
  • OGRGeometry::exportToJson will give you the json for the geometry object. – Nathan W Dec 20 '16 at 5:05
  • Virtual memory file system may be a possibility. I'm not familiar with this, so will have to investigate if I can do this on iOS - unlikely, I suspect, due to sandboxing. – Son of a Beach Dec 20 '16 at 5:07
  • I think @NathanW may have the best idea. For plain C I would use OGR_G_ExportToJson() . So long as I don't need non-spatial attributes this should work: Determine all features within the required envelope, run OGR_G_ExportToJson() on the geometries for each of the features, adding results to an array, and finally join all array elements into a single GeoJSON string. I will have to experiment with this and see how it goes. – Son of a Beach Dec 20 '16 at 22:16

Based on the suggestion from @NathanW I'm going to use OGR_G_ExportToJson() for each geometry in the layer (filtered by spatial extent). I don't think I need non-spatial attributes for this, but if I do, I could theoretically build up JSON properties to include alongside the geometry. So my plan is to do it this way:

  1. Set a spatial filter on the layer with OGR_L_SetSpatialFilterRect()
  2. Cycle through the filtered features with OGR_L_GetNextFeature()
  3. Get the geometry of each feature with OGR_F_GetGeometryRef()
  4. Get GeoJSON string of each geometry with OGR_G_ExportToJson()
  5. Construct a GeoJSON feature string for each geometry based on a template string, including the GeoJSON geometry string obtained earlier, and add it to an array of GeoJSON feature strings
  6. Construct a full GeoJSON string for a feature collection, including converting the array to a GeoJSON feature list

I've implemented most of this now, but haven't actually tested it properly yet.

  • 1
    If you could upload what you have in a Gist or post it here, that'd be super helpful! – Richard Aug 11 '17 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.