I am now using arcpy: arcpy.sa.ExtractByMask(weight_raster,elev) both weight_raster and elev are raster data in different size.

I want to use functions in GDAL to replace this arcpy methods. Does anyone know how to do it?


GDAL_Calc will perform this operation:

gdal_calc.py -A weight_raster -B elev --outfile=result.tif --calc="B*(A>0)" --NoDataValue=0

This multiplies B (elev) by A > 0 which is 0 for less than or equal to and 1 for greater than (boolean 0=False, 1=True) so the raster will be 0 where B fails the test and A where B passes the test. Note that this will cause problems where 0 is inside the valid range as any 0 values from the input will now be nodata, you can filter these by performing a preliminary calc on elev:

gdal_calc.py -A elev --outfile=elev_offset.tif --calc="A+0.00001*(A==0)"

Which adds (offsets) 0.00001 to cells that are true 0 to make them not 0 any more, this shouldn't affect the raster too much as it's 1/10th of a milimetre.

Normally extract operations are done with a binary raster, which can be created with GDAL_Rasterize from a shapefile or other OGR format.

Another limitation of GDAL_Calc is that the rasters must be the same size and origin, you can clip them to the same extent using GDAL_Translate with the -projwin (in dataset coordinates) or -srcwin options (in cells).

|improve this answer|||||

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.