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.
OGRGeometry::exportToJson
will give you the json for the geometry object.OGR_G_ExportToJson()
. So long as I don't need non-spatial attributes this should work: Determine all features within the required envelope, runOGR_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.