I am trying to change the extent of a rasterized shapefile to match that of another raster so it can act as a mask for the latter. Other posts suggest using gdalwarp. However, when using gdalwarp to create a new output it throws me a TypeError: argument of type 'float' is not iterable. Is gdalwarp unable to use float values? Although I can't see this written anywhere as a limitation of the function. Below is my code:

slope_src = r'C:\data\extraction\slope.tif'
mask_src = r'C:\data\buildings\blds.tif'
mask_new = r'C:\data\buildings\blds_mask.tif'
slope_raster = gdal.Open(slope_src)
ulx, xres, xskew, uly, yskew, yres = slope_raster.GetGeoTransform()
xres = 0.4
yres = 0.4
lrx = ulx + (slope_raster.RasterXSize * xres)
lry = uly + (slope_raster.RasterYSize * yres)
subprocess.call(["C:\\PRogram Files\\GRASS GIS 7.3\\extrabin\\gdalwarp.exe", "-te", lrx, lry, ulx, uly, mask_src, mask_new])
  • 1
    It seems to me you have wrong order in te key (-te xmin ymin xmax ymax). ulx == xmin, lrx == xmax, lry == ymin, uly == ymax. Try to swap x in your command line like this: subprocess.call(["C:\\PRogram Files\\GRASS GIS 7.3\\extrabin\\gdalwarp.exe", "-te", ulx, lry, lrx, uly, mask_src, mask_new]) Aug 7 '17 at 8:02
  • This removed the error, but now the issue is that 'mask_new' is not created by gdalwarp.
    – Tins
    Aug 7 '17 at 8:42
  • 1
    From your code I don't understand the sense of using gdalwarp. To extract part of the image the gdal_translate with -projwin ulx uly lrx lry key are applicable. gdalwarp is for reprojecting from one SRS to another or for reference based on GCP. Aug 7 '17 at 9:27
  • With the use of '.SetGeoTransform()' I was able to match the extents of the rasterized shapefile by passing it with the results of '.GetGeoTransform() applied to the raster to be masked.
    – Tins
    Aug 15 '17 at 7:06

Your Answer

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

Browse other questions tagged or ask your own question.