-2

I don't have that much skill with GDAL. I have multiple rasters that I need to clip among other things. I've made the following code which works for a single raster, however I do not know how to integrate this into a loop.

gdalwarp -overwrite -ot Int16 -of GTiff -tr 926.6254331383334 -926.625433139166 -tap -cutline GEOFT_COUNTRY.shp -cl GEOFT_COUNTRY -crop_to_cutline -dstnodata -999999999.0 -co COMPRESS=DEFLATE -co PREDICTOR=2 -co Z LE VEL=9 gf_Day2009_001.tif OUTPUT . tif

0

1 Answer 1

1

Here are a couple of ideas/options. I have yet to try them, but hopefully it points you in the right direction.

  1. Use subprocess.call() to open a new instance to run what you have. With each pass through, update the raster path as well as saved output. If you are having problems with f-strings or are using python2.7, check out this post.

For an example and check out this post.

import os
import subprocess

raster_path = "C:/Users/user/Desktop/CLIPPED"

for i, raster in enumerate(os.listdir(raster_path)):
    image = os.path.join(raster_path, raster)

    subprocess.call(f'gdal_warp -overwrite -ot Int16 -of GTiff -tr 926.6254331383334 -926.625433139166 -tap -cutline GEOFT_COUNTRY.shp -cl GEOFT_COUNTRY -crop_to_cutline -dstnodata -999999999.0 -co COMPRESS=DEFLATE -co PREDICTOR=2 -co Z LE VEL=9 {image} OUTPUT_{i}.tif')
  1. Use gdal.warp in a python script. There are multiple questions on this. You can look here and here. The best source of information is going to be the docs.

The subprocess method is probably easiest to use at first, but through my experience and findings, creating a quality function and learning how to incorporate library functions within a python script is better (option 2).

2
  • Muito obrigado amigo, fiz algumas alterações e funcionou bem.
    – Davi Luca
    May 10, 2023 at 13:29
  • @DaviLuca Please accept if it solved your problem
    – Binx
    May 10, 2023 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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