1

I have a shapefile Water_Districts.shp that consists of several water district polygons. I am trying to use the script below to 'extract' a specific polygon with the name Cache La Poudre River listed under the NAME column in the Water_Districts.shp attribute table. I have been using the python script below, but am not sure how to incorporate the SQL expression in the -where exception of the ogr2ogr command. I am getting the error: FAILURE: SetAttributeFilter(NAME = Cache La Poudre River) on layer'Water_Districts' failed.

import gdal
import subprocess

directory = r"F:\IrrigatedLands\FC_test\Water_Districts\Water_Districts.shp"
output_shp = r"F:\IrrigatedLands\FC_qgis\boundary.shp"

subprocess.call(["ogr2ogr", "-where", "NAME = Cache La Poudre River", output_shp, directory])
brgionta
  • 507
  • 5
  • 14
  • 1
    Check this out: http://gis.stackexchange.com/questions/77476/ogr2ogr-selecting-features-by-attributes – Digd Mar 28 '16 at 21:45

1 Answers1

1

The following code below worked with no errors:

import gdal
import subprocess

directory = r"F:\IrrigatedLands\FC_test\Water_Districts\Water_Districts.shp"
output_shp = r"F:\IrrigatedLands\FC_qgis\boundary.shp"

subprocess.call(["ogr2ogr", "-where", "NAME = 'Cache La Poudre River'", output_shp, directory])
brgionta
  • 507
  • 5
  • 14