1

This question already has an answer here:

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

marked as duplicate by Vince, PolyGeo Mar 29 '16 at 1:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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.