py-gdalogr-cookbook icon indicating copy to clipboard operation
py-gdalogr-cookbook copied to clipboard

Filter and Select Input Shapefile to New Output Shapefile Like ogr2ogr CLI bug

Open AlainFELER opened this issue 9 years ago • 0 comments

The # Add features to the ouput Layer part fills the out shp without with the n first attributes values whatever you ask. Eg IN.SHP(a1,a2,a3,a4,a5) OUT.SHP(a1,a3,a5) will receive a1,a2,a3 values in a1,a3,a5 fields if I do python xxxx.py a1 a3 a5.

I did correct this with these lines (excuses for the ... I don't know how to indent) : # Add field values from input Layer for i in range(0, inLayerDefn.GetFieldCount()): ....infieldDefn = inLayerDefn.GetFieldDefn(i) ....infieldName = infieldDefn.GetName() ....for j in range(0, outLayerDefn.GetFieldCount()) : ........outfieldDefn = outLayerDefn.GetFieldDefn(j) ........outfieldName = outfieldDefn.GetName() ........if outfieldName == infieldName : ............outFeature.SetField(outLayerDefn.GetFieldDefn(j).GetNameRef(),inFeature.GetField(i))

instead of

    # Add field values from input Layer
    for i in range(0, outLayerDefn.GetFieldCount()):
        fieldDefn = outLayerDefn.GetFieldDefn(i)
        fieldName = fieldDefn.GetName()
        if fieldName not in field_name_target:
            continue

        outFeature.SetField(outLayerDefn.GetFieldDefn(i).GetNameRef(),inFeature.GetField(i))

(Python 2.7 with GDAL 1.11)

Hope this helps.

Yours, AF

AlainFELER avatar Sep 21 '15 12:09 AlainFELER