0

I am trying to count features that have the text "Fehler" in a certain field. This only works, when using the OGRSQL-dialect, but returns None when using SQLITE. I want to use the SQLITE-dialect, because I am looping over several SQL-Strings, and some of them contain SUM(LENGTH)/1000, which only works in SQLITE, and not with OGRSQL. I am using Python 2.7.5 an gdal 2.1.3 Here is the code that I am using:

import os

try:
    from osgeo import ogr
    from osgeo import gdal
except:
    sys.exit("Failed to import OGR-module. Please check the GDAL-Installation!")

srcFile = r"D:\Didi\Tmp\OGR_Select_Bug\Export.shp"
driver = ogr.GetDriverByName('ESRI Shapefile')
destFileDS = driver.Open(srcFile, 0)
sql = u"SELECT COUNT(*) as Anzahl from %s WHERE rstatus like '%%Fehler'" % (os.path.basename(srcFile)[:-4])

#Works
actLayer =  destFileDS.ExecuteSQL(statement=sql.encode('utf-8'), dialect='OGRSQL')
actFeat = actLayer.GetNextFeature()
destFileDS.ReleaseResultSet(actLayer)

#Returns none
actLayer =  destFileDS.ExecuteSQL(statement=sql.encode('utf-8'), dialect='SQLITE')
actFeat = actLayer.GetNextFeature()
destFileDS.ReleaseResultSet(actLayer)
  • 1
    I don't know about `OGRSQL`, but `like '%%Fehler'` will not be true in Standard SQL when there are trailing blanks, e.g. 'hier ist ein Fehler ' doesn't match. Try either `trim(rstatus)' or `like `'%%Fehler%%'` – dnoeth Apr 23 '18 at 15:12
  • Thanks, you gave the essential hint. Allthough there are no spaces (I double-checked it), your suggestion with `trim(rstatus)>` solved the Problem. `'%%Fehler%%'` does not work, which in my oppinion, is a bug in ogr. – user7882389 Apr 24 '18 at 06:38
  • This was a bug in GDAL 2.1.3 and has been fixed with GDAL 2.2.4. Here is the link to the bug-entry on github: https://github.com/OSGeo/gdal/issues/490 – user7882389 Apr 30 '18 at 06:34

0 Answers0