7

I want try use Python and GDAL to create some SQL queries in my shapefiles. I try something and it looks good, but doesn't work. I get a none result.

Here is the code :

from osgeo import ogr
ogr_ds = ogr.Open('line.shp')
TEST=3
sql = "SELECT id FROM {} where id='{}'".format(ogr_ds,TEST)
layer = ogr_ds.ExecuteSQL(sql)
print layer
Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Apr 26, 2017 at 20:12
7
  • 1
    Does your SQL find something when you run it with ogrinfo? Commented Apr 26, 2017 at 20:24
  • no I need like this in editor with python but I sure to have value Commented Apr 26, 2017 at 20:30
  • I meant that do you know that the SQL part is OK? Test with ogrinfo -sql "SELECT id FROM line where id=3" line.shp. If you get "3" it is OK and the issue can only be in your Python code. Commented Apr 26, 2017 at 20:35
  • 1
    yes I get it I take correct result now how to do in python ? Commented Apr 26, 2017 at 20:36
  • I guess that you have only opened the GDAL datasource but not the layer and you should get first a layer to query with something like layer = dataSource.GetLayer(). Taken from an example pcjericks.github.io/py-gdalogr-cookbook/…. Commented Apr 26, 2017 at 20:45

1 Answer 1

4

You are probably using the input layer in a wrong way; furthermore, also the .format{} operation doesn't seem correct.

You may try the following code:

from osgeo import ogr
filepath = 'C:/Users/path_to_the_shapefile/line.shp' # set the filepath
layer_name = filepath[:-4].split('/')[-1] # get the layer name
driver = ogr.GetDriverByName ("ESRI Shapefile")
ogr_ds = driver.Open(filepath)
TEST=3
sql = "SELECT id FROM %s WHERE id=%s" %(layer_name,TEST)
layer = ogr_ds.ExecuteSQL(sql)
print layer

I tested it with another shapefile (and a similar query) and it returns what expected:

print layer
<osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at 0x18A4D8F0> >

EDIT

If you want to print the id value, you may add these lines:

feat = layer.GetNextFeature()
val = feat.GetField(0)
print val

but this will only returns one feature and not all the queried features (this kind of operation would be useful if you are only interested in knowing if a specific value for the id field is stored in the input layer). Furthermore, you will get an error if there isn't any id value equals to the TEST variable.

answered Apr 26, 2017 at 21:20
3
  • show me driver not define Commented Apr 26, 2017 at 21:31
  • @jessiejes Sorry, there was a missing line. I edited the answer. Commented Apr 26, 2017 at 21:33
  • now I take this <osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at 0x03166D88> > but I need the id for example if select id is 6 then I need to take only this value 6 in the print layer Commented Apr 26, 2017 at 21:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.