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?
Answer
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())
No comments:
Post a Comment