[gdal-dev] THANK YOU!

David Verbyla dlverbyla at alaska.edu
Tue Feb 5 02:21:33 PST 2013


OK..I figured it out that the layername is not the layer object,
it is the name property of the layer object and has to be enclosed
in double quotes.

For example:
test = ds.ExecuteSQL('select * from "three_points"  where Class IN (1,3)
')  #returns 2 of 3 point features

http://www.gdal.org/ogr/ogr_sql.html would be more helpful if there were
some Python examples...
for example:
it is not clear from the first example that polylayer is the name of the
layer, not the layer object, and needs to be enclosed in
double quotes:

SELECT * FROM polylayer

Thanks!

Dave Verbyla

On Tue, Feb 5, 2013 at 12:50 AM, Homme Zwaagstra <hrz at geodata.soton.ac.uk>wrote:

> Dave,
>
> Try the following modified version of your code snippet below. Note
> that `ExecuteSQL` expects a simple SQL string - it does not do any
> interpretation of local python variables! See
> http://www.gdal.org/ogr/ogr_sql.html for more information on the SQL.
>
>     import os
>     from osgeo import ogr
>     ogr.UseExceptions()         # if something goes wrong, we want to know
> about it
>     driver = ogr.GetDriverByName('ESRI Shapefile')
>     ds = driver.Open('three_points.shp', 0)#0--read-only
>     layer = ds.GetLayer()
>     layerName =  layer.GetName() #returns string 'three_points'
>     test = ds.ExecuteSQL('select * from "%s"' % layerName)
>
> Cheers,
>
> Homme
>
> On Tue, Feb 05, 2013 at 12:39:50AM -0900, David Verbyla wrote:
> > I am not understanding some key aspect of this function.
> > For example:
> > #three_points.shp contains
> > #Class, Point_X, Point_Y
> > # 1,301665.4,1701522.8
> > # 2,302240.3,1701992.1
> > # 3,301559.8,1701112.1
> > import os
> > from osgeo import ogr
> > driver = ogr.GetDriverByName('ESRI Shapefile')
> > ds = driver.Open('three_points.shp',0)#0--read-only
> > layer = ds.GetLayer()
> > layerName =  layer.GetName() #returns string 'three_points'
> > test = ds.ExecuteSQL('select from layerName *')
> > print type(test) #returns <type 'NoneType'>
> >
> > test = ds.ExecuteSQL('select from layer.GetName')
> > print type(test) #returns <type 'NoneType'>
> >
> > ds.ExecuteSQL('select from layerName where Class > 0')
> > print type (test) #returns <type 'NoneType'>
> >
> > Thanks in advance.
> >
> > Dave Verbyla
> >
> >
> > On Mon, Feb 4, 2013 at 11:59 PM, Homme Zwaagstra <
> hrz at geodata.soton.ac.uk>wrote:
> >
> > > 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
> > >
>
> --
> Homme Zwaagstra
> GeoData Institute
> University of Southampton
>
> Tel: +44(0)2380 59 8709
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20130205/dbb890c2/attachment.html>


More information about the gdal-dev mailing list