I have two shapefiles. After iterating on all features of one of them, the code reads a particular field value:
from osgeo import ogr driver = ogr.GetDriverByName('ESRI Shapefile') SHP_F = myshaoefilefile1.shp SHP_CL = myshapefile2.shp SHP_F_DataSource = driver.Open(SHP_F, 1) SHP_CL_DataSource = driver.Open(SHP_CL, 1) # get the shapefiles layer SHP_F_layer = SHP_F_DataSource.GetLayer() SHP_CL_layer = SHP_CL_DataSource.GetLayer() # estraction of features from layers SHP_F_featureCount = SHP_F_layer.GetFeatureCount() SHP_CL_featureCount = SHP_CL_layer.GetFeatureCount() for i in range (0, SHP_F_featureCount ): for j in range (0, SHP_CL_featureCount ): if SHP_CL_layer.GetFeature(j).GetField(4) in ['string1', 'string2', 'string3'] # copy the feature from Layer2 to Layer1 ?? if SHP_CL_layer.GetFeature(j).GetField(4) in ['string4', 'string5', 'string6'] # update some field of the inLayer1.GetFeature(i) ??
If the field value is equal to a pre-defined string, I need to copy that feature to the first shapefile. Also I need to update some fields, both integer and string type. Is there a simple way to copy features from a shapefile to another? The two shapefiles have the same geometry type and the same projection and reference system (WGS84 UTM 33N). The code is part of a batch python script (python 2.7 version).