3

I'm using Python and GDAL to iterate over the features in a GeoJSON layer and I'm seeing this strange behavior where the first iteration is repeated a second time before moving onto the next feature.

I only observe this when:
- opening the vector with gdal.GA_Update option
- including a layer.SetFeature() within the loop
- gdal version 2.4.1 (not observed in 2.2.4)

This example shows there is only 1 feature, but the print statement within the loop is called twice and prints FID 0 both times. With a GeoJSON containing more features, I observed that only the first feature iteration is repeated twice.

from osgeo import gdal

vector_path = 'test.geojson'

with open(vector_path, 'w') as file:
    file.write('{"type": "FeatureCollection", "name": "test", "features": [{ "type": "Feature", "properties": { "foo": 1 }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -1, -1 ], [ -1, 1 ], [ 1, 1 ], [ 1, -1 ], [ -1, -1 ] ] ] } }]}')

vector = gdal.OpenEx(vector_path, gdal.OF_VECTOR | gdal.GA_Update)
layer = vector.GetLayer()

print(layer.GetFeatureCount())

for feature in layer:
    print(feature.GetFID())
    layer.SetFeature(feature)

layer = None
vector = None

Is there something wrong with my GeoJSON file? Or with the GeoJSON driver?

Your Answer

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

Browse other questions tagged or ask your own question.