def trim_grid_to_aoi(grid_dto: GridDTO) -> geojson.FeatureCollection:
"""
Removes grid squares not intersecting with the aoi. Optionally leaves partialy intersecting task squares
complete or clips them exactly to the AOI outline
:param grid_dto: the dto containing
:return: geojson.FeatureCollection trimmed task grid
"""
# get items out of the dto
grid = grid_dto.grid
aoi = grid_dto.area_of_interest
clip_to_aoi = grid_dto.clip_to_aoi
# create a shapely shape from the aoi
aoi_multi_polygon_geojson = GridService.merge_to_multi_polygon(aoi, dissolve=True)
aoi_multi_polygon = shapely.geometry.shape(aoi_multi_polygon_geojson)
intersecting_features = []
for feature in grid['features']:
# create a shapely shape for the tile
tile = shapely.geometry.shape(feature['geometry'])
if aoi_multi_polygon.contains(tile):
# tile is completely within aoi, use as is
intersecting_features.append(feature)
else:
intersection = aoi_multi_polygon.intersection(tile)
if intersection.is_empty or intersection.geom_type not in ['Polygon', 'MultiPolygon']:
continue # this intersections which are not polygons or which are completely outside aoi
# tile is partially intersecting the aoi
clipped_feature = GridService._update_feature(clip_to_aoi, feature, intersection)
intersecting_features.append(clipped_feature)
return geojson.FeatureCollection(intersecting_features)
评论列表
文章目录