def raster_overlap(file_A, file_B, outpath, NoData_A=None, NoData_B=None): """ Finds overlaping area between two raster images. this function examines two images and outputs a raster identifying pixels where both rasters have non-NoData values. Output raster has 1's where both images have data and 0's where one or both images are missing data. inputs: file_A the first file file_B the second file outpath the output filename for the desired output. must end in ".tif" NoData_A the NoData value of file A NoData_B the NoData value of file B This function automatically invokes clip_and_snap null_define """ if not is_rast(file_A) or not is_rast(file_B): raise Exception(' both inputs must be rasters!') # load the rasters as numpy arays. a, metaA = to_numpy(file_A) b, metaB = to_numpy(file_B) # set no_datas if NoData_A is None: NoData_A = metaA.NoData_Value if NoData_B is None: NoData_B = metaB.NoData_Value # spatially match the rasters print('preparing input rasters!') clip_and_snap(file_A, file_B, outpath.replace(".shp", ".tif"), NoData_B) # reload the rasters as numpy arrays now that spatial matching is done a, metaA = to_numpy(file_A) b, metaB = to_numpy(file_B) # create work matrix and find the overlap print('Finding overlaping pixels!') Workmatrix = a.mask + b.mask Workmatrix = Workmatrix.astype('uint8') Workmatrix[Workmatrix == 1] = 2 print('Saving overlap file!') metaA.numpy_datatype = 'uint8' from_numpy(Workmatrix, metaA, outpath.replace(".shp", ".tif"), NoData_Value=2) arcpy.RasterToPolygon_conversion(outpath.replace(".shp", ".tif"), outpath.replace(".tif", ".shp"), 'NO_SIMPLIFY') return metaA, metaB
def raster_overlap(file_A, file_B, outpath, NoData_A = None, NoData_B = None): """ Finds overlaping area between two raster images. this function examines two images and outputs a raster identifying pixels where both rasters have non-NoData values. Output raster has 1's where both images have data and 0's where one or both images are missing data. :param file_A: the first file :param file_B: the second file :param outpath: the output filename for the desired output. must end in ".tif" :param NoData_A: the NoData value of file A :param NoData_B: the NoData value of file B :return outpath: filepath to raster created by this function. This function automatically invokes * clip_and_snap * null_define """ if not is_rast(file_A) or not is_rast(file_B): raise Exception('both inputs must be rasters!') # load the rasters as numpy arays. a, metaA = to_numpy(file_A) b, metaB = to_numpy(file_B) # set no_datas if NoData_A is None: NoData_A = metaA.NoData_Value if NoData_B is None: NoData_B = metaB.NoData_Value # spatially match the rasters print('preparing input rasters!') clip_and_snap(file_A, file_B, outpath.replace(".shp",".tif"), NoData_B) # reload the rasters as numpy arrays now that spatial matching is done a, metaA = to_numpy(file_A) b, metaB = to_numpy(file_B) # create work matrix and find the overlap print('Finding overlaping pixels!') Workmatrix = a.mask + b.mask Workmatrix = Workmatrix.astype('uint8') Workmatrix[Workmatrix == 1] = 2 print('Saving overlap file!') metaA.numpy_datatype = 'uint8' from_numpy(Workmatrix, metaA, outpath.replace(".shp",".tif"), NoData_Value = 2) arcpy.RasterToPolygon_conversion(outpath.replace(".shp",".tif"), outpath.replace(".tif",".shp"), 'NO_SIMPLIFY') return outpath
def spatially_match(snap_raster, rasterlist, outdir, NoData_Value = False, resamp_type = False): """ Prepares input rasters for further numerical processing This function simply ensures all rasters in "rasterlist" are identically projected and have the same cell size, then calls the raster.clip_and_snap function to ensure that the cells are perfectly coincident and that the total spatial extents of the images are identical, even when NoData values are considered. This is useful because it allows the two images to be passed on for numerical processing as nothing more than matrices of values, and the user can be sure that any index in any matrix is exactly coincident with the same index in any other matrix. This is especially important to use when comparing different datasets from different sources outside arcmap, for example MODIS and Landsat data with an ASTER DEM. inputs: snap_raster raster to which all other images will be snapped rasterlist list of rasters, a single raster, or a directory full of tiffs which will be clipped to the extent of "snap_raster" and aligned such that the cells are perfectly coincident. outdir the output directory to save newly created spatially matched tifs. resamp_type The resampling type to use if images are not identical cell sizes. "NEAREST","BILINEAR",and "CUBIC" are the most common. this function automatically invokes clip_and_snap project_resample """ # import modules and sanitize inputs tempdir = os.path.join(outdir, 'temp') if not os.path.isdir(outdir): os.makedirs(outdir) if not os.path.isdir(tempdir): os.makedirs(tempdir) rasterlist = enf_rastlist(rasterlist) core.exists(snap_raster) usetemp = False # set the snap raster environment in arcmap. arcpy.env.snapRaster = snap_raster print('Loading snap raster {0}'.format(snap_raster)) _,snap_meta = to_numpy(snap_raster) print('Bounds of rectangle to define boundaries: [{0}]'.format(snap_meta.rectangle)) # for every raster in the raster list, snap rasters and clip. for rastname in rasterlist: _,meta = to_numpy(rastname) head,tail = os.path.split(rastname) if snap_meta.projection.projectionName != meta.projection.projectionName: print('Projection discrepancy found. Reprojecting...') project_resample(rastname, snap_raster, tempdir, resamp_type) tempname = core.create_outname(tempdir,tail,"p") usetemp = True # define an output name and run the Clip_ans_Snap_Raster function on formatted tifs outname = core.create_outname(outdir, rastname, "sm") # if a temporary file was created in previous steps, use that one for clip and snap if usetemp: clip_and_snap(snap_raster, tempname, outname, NoData_Value) else: clip_and_snap(snap_raster, rastname, outname, NoData_Value) print('Finished matching raster {0}'.format(rastname)) return
def spatially_match(snap_raster, rasterlist, outdir, NoData_Value=False, resamp_type=False): """ Prepares input rasters for further numerical processing This function simply ensures all rasters in "rasterlist" are identically projected and have the same cell size, then calls the raster.clip_and_snap function to ensure that the cells are perfectly coincident and that the total spatial extents of the images are identical, even when NoData values are considered. This is useful because it allows the two images to be passed on for numerical processing as nothing more than matrices of values, and the user can be sure that any index in any matrix is exactly coincident with the same index in any other matrix. This is especially important to use when comparing different datasets from different sources outside arcmap, for example MODIS and Landsat data with an ASTER DEM. inputs: snap_raster raster to which all other images will be snapped rasterlist list of rasters, a single raster, or a directory full of tiffs which will be clipped to the extent of "snap_raster" and aligned such that the cells are perfectly coincident. outdir the output directory to save newly created spatially matched tifs. resamp_type The resampling type to use if images are not identical cell sizes. "NEAREST","BILINEAR",and "CUBIC" are the most common. this function automatically invokes clip_and_snap project_resample """ # import modules and sanitize inputs tempdir = os.path.join(outdir, 'temp') if not os.path.isdir(outdir): os.makedirs(outdir) if not os.path.isdir(tempdir): os.makedirs(tempdir) rasterlist = enf_rastlist(rasterlist) core.exists(snap_raster) usetemp = False # set the snap raster environment in arcmap. arcpy.env.snapRaster = snap_raster print('Loading snap raster {0}'.format(snap_raster)) _, snap_meta = to_numpy(snap_raster) print('Bounds of rectangle to define boundaries: [{0}]'.format( snap_meta.rectangle)) # for every raster in the raster list, snap rasters and clip. for rastname in rasterlist: _, meta = to_numpy(rastname) head, tail = os.path.split(rastname) if snap_meta.projection.projectionName != meta.projection.projectionName: print('Projection discrepancy found. Reprojecting...') project_resample(rastname, snap_raster, tempdir, resamp_type) tempname = core.create_outname(tempdir, tail, "p") usetemp = True # define an output name and run the Clip_ans_Snap_Raster function on formatted tifs outname = core.create_outname(outdir, rastname, "sm") # if a temporary file was created in previous steps, use that one for clip and snap if usetemp: clip_and_snap(snap_raster, tempname, outname, NoData_Value) else: clip_and_snap(snap_raster, rastname, outname, NoData_Value) print('Finished matching raster {0}'.format(rastname)) return