SHARE
TWEET

subtractraster

a guest Feb 18th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from osgeo import gdal, gdalconst
  2. import numpy as np
  3.  
  4. # open the geoTIFFs from disk
  5. ds1 = gdal.Open('data/file1.tif', gdalconst.GA_ReadOnly)
  6. ds2 = gdal.Open('data/file2.tif', gdalconst.GA_ReadOnly)
  7.  
  8. # make numpy arrays out of them
  9. arr1 = ds1.ReadAsArray()
  10. arr2 = ds2.ReadAsArray()
  11.  
  12. # subtract every value from both rasters
  13. result = np.subtract(arr1, arr2)
  14.  
  15. # get attributes from input raster 1 and set them to the output raster
  16. driver = gdal.GetDriverByName("GTiff")
  17. output = driver.Create('result.tif', ds1.RasterXSize, ds1.RasterYSize, 1, ds1.GetRasterBand(1).DataType)
  18. output.SetGeoTransform(ds1.GetGeoTransform())
  19. output.SetProjection(ds1.GetProjection())
  20.  
  21. # you can set your desired noData value here if you like
  22. output.GetRasterBand(1).SetNoDataValue(-9999)
  23.  
  24. # write raster to disk
  25. output.GetRasterBand(1).WriteArray(result)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top