Guest User

Untitled

a guest
Dec 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. import arcpy
  2.  
  3. raster_path = 'C:/blablabla/raster_bla.tif'
  4. raster = arcpy.Raster(raster_path)
  5. raster_data = arcpy.RasterToNumPyArray(raster)
  6. # import rasterio
  7. # raster = rasterio.open(raster_path)
  8. # raster_data = raster.read()
  9.  
  10. def leaf_area_index(land_use, ndvi):
  11. result = ndvi.copy()
  12. for lu in np.unique(land_use):
  13. results[land_use == lu] = _calculate_lai(ndvi[land_use == lu], lu)
  14. return result
  15.  
  16. def np_to_raster(array, raster_ref):
  17. """Utility to convert a numpy array to a Raster object using another Raster as reference
  18.  
  19. Arguments:
  20. -----------
  21. array : np.ndarray
  22. data to convert
  23. raster_ref : arcpy.Raster, or str
  24. reference raster or path to reference raster
  25.  
  26. Returns:
  27. -----------
  28. out : arcpy.Raster
  29. converted Raster object
  30. """
  31. if not isinstance(array, np.ndarray):
  32. raise TypeError('The input must be a numpy array')
  33. meta = arcpy.Describe(raster_ref)
  34. cell_size_x = meta.meanCellWidth
  35. cell_size_y = meta.meanCellHeight
  36. lowerleftcorner = meta.extent.lowerLeft
  37.  
  38. out_raster = arcpy.NumPyArrayToRaster(array, lowerleftcorner, cell_size_x, cell_size_y, 0)
  39. return out_raster
  40.  
  41. def write_img(array, raster_ref, outpath):
  42. newprofile = {
  43. 'count': 1,
  44. 'crs': raster_ref.crs,
  45. 'dtype': 'uint8',
  46. 'driver': 'GTiff',
  47. 'transform': raster_ref.transform,
  48. 'height': data.shape[0],
  49. 'width': data.shape[1],
  50. 'blockxsize': 256,
  51. 'tiled': True,
  52. 'blockysize': 256,
  53. 'nodata': None
  54. }
  55.  
  56. with rasterio.open(outpath, 'w', **newprofile) as dst:
  57. dst.write(data)
  58.  
  59. def run(land_use_path, ndvi_path, output_path):
  60.  
  61. #here you load your stuff
  62. land_use = arcpy.Raster(land_use_path)
  63. lu_meta = arcpy.Describe(land_use)
  64.  
  65. #here you test the rasters to see if they are aligned
  66. assert lu_meta.height == ndvi_meta.height
  67.  
  68. #here you get the data out of the raster
  69. lu_data = arcpy.RasterToNumPyArray(land_use)
  70.  
  71. #here you do your calculation
  72. lai_data = leaf_area_index(lu_data, ndvi_data)
  73.  
  74. #here you convert the result to raster
  75. lai_raster = np_to_raster(lai_data, land_use)
  76. lai_raster.save(output_path)
  77. # or write_img(lai_data, land_use, output_path) if you are working with rasterio
Add Comment
Please, Sign In to add comment