def bbox_clip(geojson: Feature, bbox: list) -> Feature: """ Takes a Feature or geometry and a bbox and clips the feature to the bbox :param geojson: Geojson data :param bbox: Bounding Box which is used to clip the geojson :return: Clipped geojson Example: >>> from turfpy.transformation import bbox_clip >>> from geojson import Feature >>> f = Feature(geometry={"coordinates": [[[2, 2], [8, 4], >>> [12, 8], [3, 7], [2, 2]]], "type": "Polygon"}) >>> bbox = [0, 0, 10, 10] >>> clip = bbox_clip(f, bbox) """ bb_polygon = bbox_polygon(bbox) bb_clip = intersect([geojson, bb_polygon]) if not bb_clip: return bb_clip if "properties" in geojson: bb_clip.properties = geojson["properties"] return bb_clip
def test_bbox_polygon_feature(): p = Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]]) bbox_poly = bbox_polygon(bbox(p)) assert bbox_poly["geometry"]["coordinates"] == [[ [-120.43, -20.28], [23.194, -20.28], [23.194, 57.322], [-120.43, 57.322], [-120.43, -20.28], ]]
def divide_bbox( feature: dict, cell_width: Optional[float] = None, units: Optional[str] = "m", ): """ Divides the given feature into grid boxes as per given cell width :param feature: Feature to be divide in grid :param cell_width: Width of each grid boxs :param units: Units for the width of grid boxs :return: List of features in which the input feature is divided """ bb = bbox(feature) bbox_polygon_feature = bbox_polygon(bb) if not cell_width: gr = grid( bb, length(bbox_polygon_feature, units=units) / 4, length(bbox_polygon_feature, units=units) / 4, units, ) else: gr = grid(bb, cell_width, cell_width, units) final = [] for f in gr["features"]: try: inter = intersect([f, feature]) if inter: final.append(inter) except Exception: logger.debug("The intersection geometry is incorrect") return final