I am writing a shapefile after some processing; however, in the final step I need to do some selection by attributes from the the shapefile.

I am using the command in a shell with the aim of using it in a Python script when it works.

ogr2ogr -f "ESRI Shapefile" -select * where ID="1" outfile.shp infile.shp

I am getting the error message:

Unable to open datasource `Downloads' with the following drivers.

What could I be doing wrong?

  • Is that really the exact message for that command? – BradHards Nov 14 '13 at 10:02

You miss a minus sign before where and the select is not necessary, so it should be:

ogr2ogr -where ID="1" outfile.shp infile.shp

or if you have to do more complex query on your input data:

ogr2ogr -sql "SELECT * FROM infile WHERE ID='1'" outfile.shp infile.shp

If ID is a field of Integer type, substitute ID='1' with ID=1.


  1. -f "ESRI Shapefile" is not necessary because "ESRI Shapefile" is the ogr2ogr default output format;
  2. it's convenient to skip -select and use directly the -where clause when you want to select all the fields.
  • 1
    Many thanks it works. I am using: ogr2ogr -where "ID='1'" output.shp input.shp – user2757128 Nov 14 '13 at 10:12
  • How can I use this code : ogr2ogr -sql "SELECT * FROM infile WHERE ID='1'" outfile.shp infile.shp" in python? – Shiuli Pervin Aug 11 '16 at 10:50
  • 1
    @ShiuliPervin you can use the statement in python by using os.system('''ogr2ogr ... ''') . be sure to import os at the top of your python script – geoeye Aug 29 '16 at 22:08
  • @afalciano, for the statement ogr2ogr -sql "SELECT * FROM infile WHERE ID='1'" outfile.shp infile.shp instead of just ID=1 is there a way to provide a vector or a set of numbers instead, e.g. ogr2ogr -sql "SELECT * FROM infile WHERE ID IN ['1','5','29']" outfile.shp infile.shp? – h.l.m May 11 '17 at 17:16
  • @afalciano one other thing...instead of writing out another shp file, is there a way to just get the coordinates of the selected ID polygons? – h.l.m May 11 '17 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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