[gdal-dev] Field values are applied to incorrect fields in merging 2 or more Shapefiles

Cord Thomas cord.thomas at gmail.com
Wed Jan 23 12:37:56 PST 2019


I am merging 2 or more Shapefiles pulled from OpenStreetMap.   In the
merging, the values of fields are getting applied to the wrong output
fields in the merged shapefile.   I have been looking at this code for a
while and cannot figure out the error.  As a note, my downloaded source
files don't all seem to have the exact same list of fields; not sure why.

As an example, the name field value from the source is assigned to the
maxspeed value of the target.  This happens consistently for an entire
source, so not random.

Code:

def copy_feature_attribute_values(inFeature, outLayerDefn, outFeature):
  for i in range(0, outLayerDefn.GetFieldCount()):
    fieldName = outLayerDefn.GetFieldDefn(i).GetNameRef()
    field = inFeature.GetField(i)
    outFeature.SetField(fieldName, field)


*... There is some code here to create the layer from the first of the*

* shapefiles to be merged*

la_streets_osm_layer_defn = la_streets_osm_layer.GetLayerDefn()

for sub_network_key in sub_network_segments:

  sub_network_src = config['SPATIAL']['BASE_STREET_PATH'] + config['SPATIAL'] \
                  ['CA_Street_Centerlines_OSM'] + \
                  sub_network_key + '/edges/edges.shp'

  sub_network_ds = ogr.Open(sub_network_src)
  sub_network_layer = sub_network_ds.GetLayer()

  for sub_network_feature in sub_network_layer:
    outFeature = ogr.Feature(la_streets_osm_layer_defn)

    copy_feature_attribute_values(sub_network_feature,
la_streets_osm_layer_defn, outFeature)
    newGeom = sub_network_feature.GetGeometryRef()
    outFeature.SetGeometry(newGeom.Clone())

    la_streets_osm_layer.CreateFeature(outFeature)
    outFeature = None
    la_streets_osm_layer.SyncToDisk()

ReplyForward
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20190123/fcc1ed5c/attachment.html>


More information about the gdal-dev mailing list