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

Even Rouault even.rouault at mines-paris.org
Tue Oct 30 05:43:08 PDT 2012


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.

Even


More information about the gdal-dev mailing list