Advertisement
Guest User

subtractraster

a guest
Feb 18th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement