2

Environment: Windows 10, Python 3.7, Numpy 1.18 and GDAL 3.0.2

enter image description here

Data required:

  1. Single band raster
  2. Polygons on same area with an attribute "name"

enter image description here

The code below works fine and cuts the data for all 3 polygons

ds = gdal.Warp('cut.tif', 'ndvi.tif', format = 'GTiff', cutlineDSName = 'parcel.shp')

if ds is None:
    print('Failed')
else:
    print('Success')
# prints Success

enter image description here

But this does not work

ds = gdal.Warp('first-wrong.tif', 'ndvi.tif', format = 'GTiff', \
               cutlineDSName = 'parcel.shp', \
               cutlineSQL = 'SELECT * FROM parcel', \
               cutlineWhere = 'name = "first"')

if ds is None:
    print('Failed')
else:
    print('Success')
# prints Success but gives same result as previous

Expected output: enter image description here

Is this a bug or am I missing something?

5
  • In case you wish to reproduce it with my data, it is here (Zipped download <400KB) drive.google.com/drive/folders/… – PKG Jul 29 '20 at 15:09
  • Note your shapefile did not include the parcel.dbf – user2856 Jul 31 '20 at 21:43
  • Thank you noticing. Fixed that. The missing file does not affect the problem. – PKG Aug 1 '20 at 11:49
  • Actually it does, because the code will not work at all without the attribute table (the .dbf file) – user2856 Aug 1 '20 at 12:10
  • Okay. Thank you. – PKG Aug 1 '20 at 12:50
1

Your where clause is using double quotes for the string literal.

The OGR SQL documentation specifies single quotes are required for strings. Double quotes are used for field names (only required if they contain special characters or reserved words):

String literals and identifiers quoting

Strict SQL92 rules are applied regarding string literals and identifiers quoting.

String literals (constants) must be surrounded with single-quote characters. e.g. WHERE a_field = 'a_value' Identifiers (column names and tables names) can be used unquoted if they don’t contain special characters or are not a SQL reserved keyword. Otherwise they must be surrounded with double-quote characters. e.g. WHERE "from" = 5.

This works:

ds = gdal.Warp('first-right.tif', 'ndvi.tif', format = 'GTiff',
                cutlineDSName='parcel.shp',
                cutlineWhere='name = \'first\'')

Or

ds = gdal.Warp('first-right.tif', 'ndvi.tif', format = 'GTiff',
                cutlineDSName='parcel.shp',
                cutlineWhere="name = 'first'")

enter image description here

1
  • OGR SQL dialect documentation is a treasure trove! Thank you for finding the issue. – PKG Aug 1 '20 at 12:48

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.