def callback(current_coords, previous_coords): segment = Feature(geometry=LineString( [previous_coords, current_coords]), properties=properties) segment.bbox = bbox(previous_coords, current_coords) segments.append(segment) return previous_coords
def bbox_polygon(bbox: list, properties: dict = {}) -> Feature: """ To generate a Polygon Feature for the bounding box generated using bbox. :param bbox: bounding box generated for a geojson. :param properties: properties to be added to the returned feature. :return: polygon for the given bounding box coordinates. Example : >>> from turfpy.measurement import bbox_polygon, bbox >>> from geojson import Polygon >>> p = Polygon([((2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), ... (2.38, 57.322))]) >>> bb = bbox(p) >>> feature = bbox_polygon(bb) """ west = float(bbox[0]) south = float(bbox[1]) east = float(bbox[2]) north = float(bbox[3]) if len(bbox) == 6: raise Exception("bbox-polygon does not support BBox with 6 positions") low_left = (west, south) top_left = (west, north) top_right = (east, north) low_right = (east, south) bbox_polygon = Polygon([(low_left, low_right, top_right, top_left, low_left)]) feature_bbox = Feature(geometry=bbox_polygon) if "properties" in properties: feature_bbox.properties = properties["properties"] elif "properties" not in properties: feature_bbox.properties = {} if "id" in properties: feature_bbox.id = properties["id"] if "bbox" in properties: feature_bbox.bbox = properties["bbox"] return feature_bbox
def center(geojson, properties: Optional[dict] = None) -> Feature: """ Takes a Feature or FeatureCollection and returns the absolute center point of all features. :param geojson: GeoJSON for which centered to be calculated. :param properties: Optional parameters to be set to the generated feature. :return: Point feature for the center. Example : >>> from turfpy.measurement import center >>> from geojson import Feature, FeatureCollection, Point >>> f1 = Feature(geometry=Point((-97.522259, 35.4691))) >>> f2 = Feature(geometry=Point((-97.502754, 35.463455))) >>> f3 = Feature(geometry=Point((-97.508269, 35.463245))) >>> feature_collection = FeatureCollection([f1, f2, f3]) >>> feature = center(feature_collection) """ bounding_box = bbox(geojson) x = (bounding_box[0] + bounding_box[2]) / 2 y = (bounding_box[1] + bounding_box[3]) / 2 point = Point((x, y)) center_feature = Feature(geometry=point) if properties is None: properties = dict() if "properties" in properties: center_feature.properties = properties["properties"] elif "properties" not in properties: center_feature.properties = {} if "id" in properties: center_feature.id = properties["id"] if "bbox" in properties: center_feature.bbox = properties["bbox"] return center_feature