batch

Posted

Some interesting snippets from a larger batch script where several gdal raster utilities are called upon for rasterizing, raster calculations and raster aggregations.

create timestamp

C:\> SET timestamp=%date:~0, 4%%date:~5,2%%date:~8,2%t%time:~0,2%%time:~3,2%%time:~6,2%
C:\> echo %timestamp%
20210623t135528

vector 2 raster

gdal_rasterize -at -l  %input_layer% -of GTiff -te 10000 300000 280000 625000 -tr 2.5 2.5 -ot Byte -a_srs epsg:28992 -co COMPRESS=LZW -burn 1 %input_gdb% %input_layer% %output_name%_%timestamp%.tif

Target resolution and extent are specified with -tr and -te. Vector shapes are set to value 1 with -burn 1.

raster calculations

python "%gdal_dir%/gdal_calc.py" --co COMPRESS=LZW -A %sinput_A% -B %input_B% -C %input_C% --outfile=%output_name%_%timestamp%.tif --calc="numpy.where((A+B+C)>=1, 1, 0)"

Add three 0/1 rasters together and set to 1 where the sum is equal to or larger than 1, else 0. The gdal_calc.py utility is called with python, which of course only works if a proper python environment is activated:

call c:\apps\Miniconda3\Scripts\activate.bat
call conda activate environment_name

raster aggregations

gdalwarp -co COMPRESS=LZW -tr 25 25 -te 10000 300000 280000 625000 -r sum %input%.tif %output_name%_%timestamp%_25m.tif 

Aggregating from 2.5 to 25m, again by setting -tr and -te. Resampling method is sum, which is available in gdalwarp but not in gdal_translate

Author
Categories gdal