1

This question already has an answer here:

Having the following GeoJSON:

{"geometry": {"coordinates": [[[0.0, -89.9999999999], [0.0, -87.5], [120.0, -87.5], [120.0, -89.9999999999], [0.0, -89.9999999999]]], "type": "Polygon"}, "properties": {"grid_cell_index": 1}, "type": "Feature"}

I'm trying to load it with GDAL and to perform a VectorTranslate on it. The objective is to guarantee that is compliant to RFC 7946. My code is running on Python 2.7.x with GDAL 2.2.1:

from osgeo import gdal

srcDS = gdal.OpenEx('input.geojson')
ds = gdal.VectorTranslate('output.json', srcDS=srcDS, format = 'GeoJSON', layerCreationOptions = ['RFC7946=YES', 'WRITE_BBOX=YES'])

When I count the objects on the input:

srcDS.GetLayer(0).GetFeatureCount()

Returning 1.

However, when I do the same count after transforming:

ds.GetLayer(0).GetFeatureCount()

It returns me 0, and also the output.json file is empty. I've inverted my point coordinates but it seems that is not a right-hand error. Any ideas on what could be going on?

marked as duplicate by user2856, tinlyx, ArMoraer, lynxlynxlynx, PolyGeo Sep 18 '17 at 8:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

This is a known python GDAL "gotcha". The data doesn't get written until the dataset is closed and dereferenced.

This works:

from osgeo import gdal
gdal.UseExceptions()
srcDS = gdal.OpenEx('input.geojson')
ds = gdal.VectorTranslate('output.geojson', srcDS=srcDS, format = 'GeoJSON', layerCreationOptions = ['RFC7946=YES', 'WRITE_BBOX=YES'])

#Dereference and close dataset, then reopen.
del ds
ds = gdal.OpenEx('output.geojson')

print(srcDS.GetLayer(0).GetFeatureCount())
print(ds.GetLayer(0).GetFeatureCount())
  • Indeed it is a gotcha. Do you know the reason behind it? (Not that flushing an object is necessarily a problem) – Ricardo Barros Lourenço Sep 19 '17 at 13:19
  • I think it's a design choice of the underlying C++ library. The python bindings mostly just mirror the C++ rather than building a pythonic interface on top. A much more pythonic alternative is rasterio which is also built on top of gdal. – user2856 Sep 20 '17 at 20:51

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