1

I'm trying to calculate a field ('duplicates') to reflect the identical records, here's a part of the code:

d = []
def isDuplicate(t):
     import string
     global d
     iD = 0
     for item in d:
         if item == t:
             iD = 1
             continue
     if iD == 1:
         return 1
     elif iD == 0:
         d.append(t)
         return 0

I'm calling the function isDuplicate() in the code below:

ogrinfo input.shp -dialect SQLite -sql "UPDATE input SET duplicates = isDuplicate('account_No')"

It doesn't output anything and it doesn't give any errors either. Can anyone see the problem?

2

isDuplicate() is not an SQL function. OGR doesn't use python functions in SQL statements as far as I am aware. Not only that, as ogrinfo is a command line executable in a separate process, it won't even be aware that the python function exists.

You need to loop through each feature and use feature.SetField(field,value).

from osgeo import ogr

ds = ogr.Open(filepath, 1)
ogr.UseExceptions()

layer = ds.GetLayer()

values = set()
for feature in layer:
    value = feature.GetField("account_No")
    duplicate = int(value in values)
    feature.SetField('Duplicate', duplicate)
    layer.SetFeature(feature)
    values.add(value)

del layer, ds
  • Thank you @Luke. It works now. This works on shapefiles, how about FileGDB feature classes? – Matt Oct 21 '16 at 0:17
  • @Matt If your GDAL/OGR has the read/write "Esri FileGDB driver" compiled in (unlikely unless you compile GDAL/OGR yourself) or installed as a plugin, then yes it should work. It will not work with the read only GDAL/OGR "OpenFileGDB" driver that is compiled in by default (as of GDAL/OGR 1.11+). Getting the ESRI FileGDB driver working in your GDAL/OGR installation is a whole new question... It's been asked a few times, so have a search of the site. – user2856 Oct 21 '16 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.