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?


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! Commented Feb 7, 2017 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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