[gdal-dev] Python ogr DataSource.ExecuteSQL syntax?

Homme Zwaagstra hrz at geodata.soton.ac.uk
Tue Feb 5 00:59:03 PST 2013


Hello Dave,

On Mon, Feb 04, 2013 at 06:32:11PM -0900, David Verbyla wrote:
> I am trying to figure out the syntax for .ExecuteSQL using Python ogr.

I have successfully been using `DataSource.ExecuteSQL` from Python
using gdal 1.9.0.

> For example:
> fireLayer.SetAttributeFilter(None) #returns all 2623 polygons
> fireLayer.SetAttributeFilter(" FIREID < 75 ") #returns 4 polygons
> 
> I am trying to get the same results using the ExecuteSQL:
> testLayer = dataSource.ExecuteSQL("select * from fireLayer")
> type(testLayer) #retuns NoneType
> testLayer = dataSource.ExecuteSQL("select * from fireLayer where FIREID <
> 75")
> type(testLayer) #retuns NoneType

`None` seems to be returned when the SQL does not generate any
features.  Therefore the only thing I can think that might be wrong is
that the identifier `fireLayer` in the SQL above is not matching the
layer name in `dataSource`. Amending to the following *should* work:

    dataSource.ExecuteSQL('select * from "%s"' % fireLayer.GetName())

Cheers,

Homme

> Thanks in advance.
> 
> Dave Verbyla
> 
> On Sat, Dec 22, 2012 at 7:17 AM, David Verbyla <dlverbyla at alaska.edu> wrote:
> 
> >
> > I am a newbie to OGR/OSR using Python 2.7.2, please excuse my ignorance:
> >> I am trying to find the distance to the closest polygon for each point.
> >> Is there a more efficient way than simply computing the distance to all
> >> polygons for each point?  For example:
> >>
> >
> > nPts = pointsLayer.GetFeatureCount()
> > nPolys = polysLayer.GetFeatureCount()
> >
> > for pt in range(0,nPts):
> >       minDist = 1000000
> >       pointFeature = pointsLayer.GetFeature(pt)
> >       for poly in range(0,nPolys):
> >             polyFeature = polysLayer.GetFeature(poly)
> >             Dist = pointFeature.GetGeometryRef().Distance(
> > polyFeature.GetGeometryRef() )
> >             if (Dist < minDist):
> >                   minDist = Dist
> >       print (pt,minDist)
> >
> > Thank you.
> >
> > Dave Verbyla
> >

> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
Homme Zwaagstra
GeoData Institute
University of Southampton

Tel: +44(0)2380 59 8709


More information about the gdal-dev mailing list