def create_mask_rectangles(islands): for island in islands: # get path to mask raster in_raster = os.path.join(path_to_temp(), "habitats", f"{island}.tif") if not os.path.exists(in_raster): _rasterize_habitat([island]) # get rectangle extents path_to_rects = os.path.join(path_to_temp(), "rects5", f"{island}.shp") with SearchCursor(path_to_rects, ["OID@", "SHAPE@"]) as cursor: rectangles = [] for oid, rect in cursor: extent = " ".join(str(rect.extent).split()[:4]) rectangles.append((oid, extent)) # clip mask raster to rectangles and save for oid, extent in tqdm(rectangles): path_to_raster = os.path.join(path_to_masks(), f"{island}-{oid}.png") Clip_management(in_raster, extent, path_to_raster) # delete all metadata _path_to_masks = path_to_masks() for name in os.listdir(_path_to_masks): if not name.endswith("png"): os.remove(os.path.join(_path_to_masks, name))
def _envelop_shoreline_points(islands): """ Envelop point buffers (circular polygons) in rectangular polygons. """ for island in islands: in_features = os.path.join(path_to_temp(), "rects4", f"{island}.shp") out_features = os.path.join(path_to_temp(), "rects5", island) shutil.rmtree(out_features, ignore_errors=True) os.makedirs(os.path.dirname(out_features), exist_ok=True) FeatureEnvelopeToPolygon_management(in_features, out_features)
def _buffer_shoreline_points(islands, size): """ Pad the points along the shoreline with a buffer that is half the desired width of the image data. Note: Almost identical to above buffer analysis. """ for island in islands: in_features = os.path.join(path_to_temp(), "rects3", f"{island}.shp") out_features = os.path.join(path_to_temp(), "rects4", island) shutil.rmtree(out_features, ignore_errors=True) os.makedirs(os.path.dirname(out_features), exist_ok=True) Buffer_analysis(in_features, out_features, buffer_distance_or_field="{} METERS".format(size // 2))
def _filter_shoreline(islands): """ Remove the fully esconced inner buffer polygon, leaving the outer buffer polygon. """ for island in islands: in_features = os.path.join(path_to_temp(), "rects1", f"{island}.shp") out_features = os.path.join(path_to_temp(), "rects2", island) shutil.rmtree(out_features, ignore_errors=True) os.makedirs(os.path.dirname(out_features), exist_ok=True) EliminatePolygonPart_management(in_features, out_features, condition="PERCENT", part_area_percent=99, part_option="ANY")
def create_image_rectangles(islands): for island in islands: # get path to image mosaic in_raster = path_to_mosaic(island) if len(in_raster) > 1: print(f"Please merge {island} mosaics. Skipping...") continue # get rectangle extents path_to_rects = os.path.join(path_to_temp(), "rects5", f"{island}.shp") with SearchCursor(path_to_rects, ["OID@", "SHAPE@"]) as cursor: rectangles = [] for oid, rect in cursor: extent = " ".join(str(rect.extent).split()[:4]) rectangles.append((oid, extent)) # clip image mosaic to rectangles and save for oid, extent in tqdm(rectangles): path_to_raster = os.path.join(path_to_images(), f"{island}-{oid}.png") Clip_management(in_raster[0], extent, path_to_raster) # delete all metadata _path_to_images = path_to_images() for name in os.listdir(_path_to_images): if not name.endswith("png"): os.remove(os.path.join(_path_to_images, name))
def _pointilate_shoreline(islands, size, step): """ Generate points along the shoreline at a fixed interval. """ for island in islands: in_features = os.path.join(path_to_temp(), "rects2", f"{island}.shp") out_features = os.path.join(path_to_temp(), "rects3", island) shutil.rmtree(out_features, ignore_errors=True) os.makedirs(os.path.dirname(out_features), exist_ok=True) GeneratePointsAlongLines_management( in_features, out_features, Point_Placement="DISTANCE", Distance="{} METERS".format(step), )
def _rasterize_habitat(islands): for island in islands: snap_raster = path_to_mosaic(island) if len(snap_raster) > 1: print(f"Please merge {island} mosaics. Skipping...") continue # use the cell size of the mosaic for conversion to raster env.snapRaster = snap_raster[0] in_features = path_to_habitat(island) out_raster = os.path.join(path_to_temp(), "habitats", f"{island}.tif") os.makedirs(os.path.dirname(out_raster), exist_ok=True) FeatureToRaster_conversion(in_features, "M_STRUCT", out_raster, "#")
def _buffer_shoreline(islands, size): """ Pad the shoreline with a buffer that is half the desired width of the image data. """ for island in islands: in_features = path_to_shoreline(island) out_features = os.path.join(path_to_temp(), "rects1", island) shutil.rmtree(out_features, ignore_errors=True) os.makedirs(os.path.dirname(out_features), exist_ok=True) Buffer_analysis( in_features, out_features, buffer_distance_or_field="{} METERS".format(int( size * (3 / 8))), # keep the slightest bit of shoreline dissolve_option="ALL")