Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import arcpy
- raster_path = 'C:/blablabla/raster_bla.tif'
- raster = arcpy.Raster(raster_path)
- raster_data = arcpy.RasterToNumPyArray(raster)
- # import rasterio
- # raster = rasterio.open(raster_path)
- # raster_data = raster.read()
- def leaf_area_index(land_use, ndvi):
- result = ndvi.copy()
- for lu in np.unique(land_use):
- results[land_use == lu] = _calculate_lai(ndvi[land_use == lu], lu)
- return result
- def np_to_raster(array, raster_ref):
- """Utility to convert a numpy array to a Raster object using another Raster as reference
- Arguments:
- -----------
- array : np.ndarray
- data to convert
- raster_ref : arcpy.Raster, or str
- reference raster or path to reference raster
- Returns:
- -----------
- out : arcpy.Raster
- converted Raster object
- """
- if not isinstance(array, np.ndarray):
- raise TypeError('The input must be a numpy array')
- meta = arcpy.Describe(raster_ref)
- cell_size_x = meta.meanCellWidth
- cell_size_y = meta.meanCellHeight
- lowerleftcorner = meta.extent.lowerLeft
- out_raster = arcpy.NumPyArrayToRaster(array, lowerleftcorner, cell_size_x, cell_size_y, 0)
- return out_raster
- def write_img(array, raster_ref, outpath):
- newprofile = {
- 'count': 1,
- 'crs': raster_ref.crs,
- 'dtype': 'uint8',
- 'driver': 'GTiff',
- 'transform': raster_ref.transform,
- 'height': data.shape[0],
- 'width': data.shape[1],
- 'blockxsize': 256,
- 'tiled': True,
- 'blockysize': 256,
- 'nodata': None
- }
- with rasterio.open(outpath, 'w', **newprofile) as dst:
- dst.write(data)
- def run(land_use_path, ndvi_path, output_path):
- #here you load your stuff
- land_use = arcpy.Raster(land_use_path)
- lu_meta = arcpy.Describe(land_use)
- #here you test the rasters to see if they are aligned
- assert lu_meta.height == ndvi_meta.height
- #here you get the data out of the raster
- lu_data = arcpy.RasterToNumPyArray(land_use)
- #here you do your calculation
- lai_data = leaf_area_index(lu_data, ndvi_data)
- #here you convert the result to raster
- lai_raster = np_to_raster(lai_data, land_use)
- lai_raster.save(output_path)
- # or write_img(lai_data, land_use, output_path) if you are working with rasterio
Add Comment
Please, Sign In to add comment