I previously asked this question@ regarding finding files that contain an invalid geometry using OGR.
The following SQL command was suggested to me, which seemed to work a treat: -sql "select * from filename WHERE not ST_IsValid(geometry)"
I am now trying to get this to work under python using the following code:
from osgeo import ogr
driver = ogr.GetDriverByName('MapInfo File')
dataSource = driver.Open(file, 0)
layer = dataSource.GetLayer()
result = dataSource.ExecuteSQL("select * from file WHERE ST_IsValid(geometry)")
featureCount = result.GetFeatureCount()
print (featureCount)
Upon running the code however, I get the following error message: ERROR 1: Undefined function 'ST_IsValid' used.
Does anyone know how I go about checking ST_IsValid
under python?
-
ST_IsValid is a SQLite/Spatialite function. I believe that by adding dialect SQLite it will work gdal.org/python/osgeo.ogr.DataSource-class.html#ExecuteSQL.user30184– user301842017年02月07日 14:59:29 +00:00Commented Feb 7, 2017 at 14:59
1 Answer 1
I don't think the plain vanilla OGR SQL includes spatial functions? You can specify pszDialect in the parameters for ExecuteSQL, for example to use SQLITE's spatial functions. http://www.gdal.org/ogr_sql_sqlite.html
-
Excellent, using 'dataSource.ExecuteSQL("select * from HousingOptions WHERE ST_IsValid(geometry)", dialect = "SQLITE")' seems to have done the trick. I am getting some kind of logical output now. Thank you!JamieTasker– JamieTasker2017年02月07日 15:28:47 +00:00Commented Feb 7, 2017 at 15:28