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])
1

1 Answer 1

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])

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