[gdal-dev] [User] OGR2OGR append update duplicates

Even Rouault even.rouault at mines-paris.org
Tue Oct 30 13:32:36 PDT 2012


Le mardi 30 octobre 2012 13:43:08, Even Rouault a écrit :
> Selon Emmanuel Séguin <emmanuel.seguin at ign.fr>:
> > Hi,
> > 
> > I'm using ogr2ogr to merge shapefiles (ogr2ogr -append -update -f "ESRI
> > Shapefile" file1.shp file2.shp -nln file1.shp).
> > It seems to me that duplicate records are possible in the resulting
> > shapefile (based on tests using two identical shapefiles).
> > => Is there a way to merge two shapefiles with gdal/ogr and avoid
> > duplicate records to be created in the resulting shapefile ?
> 
> Emmanuel,
> 
> -append just ... appends ;-) It does not make any attempt to check if there
> are existing similar features in the target datasource.
> 
> For that, you would need to define a uniqueness criterion (typically the
> primary key = FID in OGR data model, or possibly some attribute field that
> can also serves a a primary key).
> 
> That can be done with a few lines of OGR Python scripting for example.
> Basically you iterate over the features of the source dataset. For each
> feature, you request the target datasource if a similar feature exists. If
> it is based on the FID, a dstLayer.GetFeature(fid) is sufficient,
> otherwise you need to set an attribute filter on dstLayer, and call
> GetNextFeature() to check if there's a feature matching the filter. If
> not, you append the feature.
> 
> I must have a script that does that somewhere. I'll try to find it.

You'll find in
http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/ogrupdate.py
the OGR Python script that I mentionned above. Hopefully the included usage 
message will be enough to use it.

> 
> Even
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list