def difference(feature_1: Feature, feature_2: Feature) -> Feature: """ Find the difference between given two features. :param feature_1: A GeoJSON feature :param feature_2: A GeoJSON feature :return: A GeoJSON feature Example: >>> from geojson import Polygon, Feature >>> from turfpy.transformation import difference >>> f1 = Feature(geometry=Polygon([[ >>> [128, -26], >>> [141, -26], >>> [141, -21], >>> [128, -21], >>> [128, -26]]]), properties={"combine": "yes", "fill": "#00f"}) >>> f2 = Feature(geometry=Polygon([[ >>> [126, -28], >>> [140, -28], >>> [140, -20], >>> [126, -20], >>> [126, -28]]]), properties={"combine": "yes"}) >>> difference(f1, f2) """ properties_list = [] if "properties" in feature_1.keys(): properties_list.append(feature_1["properties"]) if "properties" in feature_2.keys(): properties_list.append(feature_2["properties"]) shape_1 = shape(get_geom(feature_1)) shape_2 = shape(get_geom(feature_2)) difference_result = shape_1.difference(shape_2) difference_result = mapping(difference_result) if len(difference_result["coordinates"]) == 0: return None properties = merge_dict(properties_list) difference_feature = Feature(geometry=difference_result, properties=properties) return difference_feature
def intersect(geojson_1: Feature, geojson_2: Feature): """ Takes two polygons and finds their intersection :param geojson_1: Geojson data :param geojson_2: Geojson data :return: Intersection Geojson Feature Example: >>> from turfpy.transformation import intersect >>> from geojson import Feature >>> f = Feature(geometry={"coordinates": [ >>> [[-122.801742, 45.48565], [-122.801742, 45.60491], >>> [-122.584762, 45.60491], [-122.584762, 45.48565], >>> [-122.801742, 45.48565]]], "type": "Polygon"}) >>> b = Feature(geometry={"coordinates": [ >>> [[-122.520217, 45.535693], [-122.64038, 45.553967], >>> [-122.720031, 45.526554], [-122.669906, 45.507309], >>> [-122.723464, 45.446643], [-122.532577, 45.408574], >>> [-122.487258, 45.477466], [-122.520217, 45.535693] >>> ]], "type": "Polygon"}) >>> inter = intersect(f, b) """ geometry_1 = get_geom(geojson_1) geometry_2 = get_geom(geojson_2) shape_1 = shape(geometry_1) shape_2 = shape(geometry_2) if not shape_1.is_valid: shape_1 = shape_1.buffer(0) if not shape_2.is_valid: shape_2 = shape_2.buffer(0) intersection = shape_1.intersection(shape_2) intersection = mapping(intersection) if len(intersection["coordinates"]) == 0: return None intersection_feature = Feature(geometry=intersection) return intersection_feature
def boolean_point_in_polygon(point, polygon, ignore_boundary=False): """ Takes a Point or a Point Feature and Polygon or Polygon Feature as input and returns True if Point is in given Feature. :param point: Point or Point Feature. :param polygon: Polygon or Polygon Feature. :param ignore_boundary: [Optional] default value is False, specify whether to exclude boundary of the given polygon or not. :return: True if the given Point is in Polygons else False Example: >>> from turfpy.measurement import boolean_point_in_polygon >>> from geojson import Point, MultiPolygon, Feature >>> point = Feature(geometry=Point((-77, 44))) >>> polygon = Feature(geometry=MultiPolygon([([(-81, 41), (-81, 47), (-72, 47), (-72, 41), (-81, 41)],), >>> ([(3.78, 9.28), (-130.91, 1.52), (35.12, 72.234), (3.78, 9.28)],)])) >>> boolean_point_in_polygon(point, polygon) """ if not point: raise Exception("point is required") if not polygon: raise Exception("polygon is required") pt = get_coord(point) geom = get_geom(polygon) geo_type = geom["type"] bbox = polygon.get("bbox", None) polys = geom["coordinates"] if bbox and not in_bbox(pt, bbox): return False if geo_type == "Polygon": polys = [polys] inside_poly = False for i in range(0, len(polys)): if in_ring(pt, polys[i][0], ignore_boundary): in_hole = False k = 1 while k < len(polys[i]) and not in_hole: if in_ring(pt, polys[i][k], not ignore_boundary): in_hole = True k += 1 if not in_hole: inside_poly = True return inside_poly
def bezier_spline(line: Feature, resolution=10000, sharpness=0.85) -> Feature: """ Takes a line and returns a curved version by applying a Bezier spline algorithm :param line: LineString Feature which is used to draw the curve :param resolution: time in milliseconds between points :param sharpness: a measure of how curvy the path should be between splines :return: Curve as LineString Feature Example: >>> from geojson import LineString, Feature >>> from turfpy.transformation import bezier_spline >>> ls = LineString([(-76.091308, 18.427501), >>> (-76.695556, 18.729501), >>> (-76.552734, 19.40443), >>> (-74.61914, 19.134789), >>> (-73.652343, 20.07657), >>> (-73.157958, 20.210656)]) >>> f = Feature(geometry=ls) >>> bezier_spline(f) """ coords = [] points = [] geom = get_geom(line) for c in geom["coordinates"]: points.append({"x": c[0], "y": c[1]}) spline = Spline(points_data=points, resolution=resolution, sharpness=sharpness) i = 0 while i < spline.duration: pos = spline.pos(i) if floor(i / 100) % 2 == 0: coords.append((pos["x"], pos["y"])) i = i + 10 return Feature(geometry=LineString(coords))
def union(features: List[Feature]) -> Feature: """ Given list of two or more `Polygons` return union of those. :param features: A list of GeoJSON features(Polygons). :return: union GeoJSON Feature. """ shapes = [] for f in features: if f.geometry.type != "Polygon": raise ValueError("All the features in the list must be Polygon") poly = get_geom(f) s = shape(poly) shapes.append(s) result = cascaded_union(shapes) result = mapping(result) if not result["coordinates"]: return None return Feature(geometry=result)
def union( features: Union[List[Feature], FeatureCollection] ) -> Union[Feature, FeatureCollection]: """ Given list of features or ``FeatureCollection`` return union of those. :param features: A list of GeoJSON features or FeatureCollection. :return: A GeoJSON Feature or FeatureCollection. Example: >>> from turfpy.transformation import union >>> from geojson import Feature, Polygon, FeatureCollection >>> f1 = Feature(geometry=Polygon([[ ... [-82.574787, 35.594087], ... [-82.574787, 35.615581], ... [-82.545261, 35.615581], ... [-82.545261, 35.594087], ... [-82.574787, 35.594087] ... ]]), properties={"fill": "#00f"}) >>> f2 = Feature(geometry=Polygon([[ ... [-82.560024, 35.585153], ... [-82.560024, 35.602602], ... [-82.52964, 35.602602], ... [-82.52964, 35.585153], ... [-82.560024, 35.585153]]]), properties={"fill": "#00f"}) >>> union(FeatureCollection([f1, f2], properties={"combine": "yes"})) """ shapes = [] properties_list = [] if isinstance(features, list): for f in features: if f.type != "Feature": raise Exception("Not a valid feature") geom = get_geom(f) s = shape(geom) shapes.append(s) if "properties" in f.keys(): properties_list.append(f["properties"]) else: if "features" not in features.keys(): raise Exception("Invalid FeatureCollection") if "properties" in features.keys(): properties_list.append(features["properties"]) for f in features["features"]: geom = get_geom(f) s = shape(geom) shapes.append(s) if "properties" in f.keys(): properties_list.append(f["properties"]) result = cascaded_union(shapes) result = mapping(result) properties = merge_dict(properties_list) if result["type"] == "GeometryCollection": features = [] for geom in result["geometries"]: features.append(Feature(geometry=geom)) return FeatureCollection(features, properties=properties) return Feature(geometry=result, properties=properties)
def intersect(features: Union[List[Feature], FeatureCollection]) -> Feature: """ Takes polygons and finds their intersection :param features: List of features of Feature Collection :return: Intersection Geojson Feature Example: >>> from turfpy.transformation import intersect >>> from geojson import Feature >>> f = Feature(geometry={"coordinates": [ >>> [[-122.801742, 45.48565], [-122.801742, 45.60491], >>> [-122.584762, 45.60491], [-122.584762, 45.48565], >>> [-122.801742, 45.48565]]], "type": "Polygon"}) >>> b = Feature(geometry={"coordinates": [ >>> [[-122.520217, 45.535693], [-122.64038, 45.553967], >>> [-122.720031, 45.526554], [-122.669906, 45.507309], >>> [-122.723464, 45.446643], [-122.532577, 45.408574], >>> [-122.487258, 45.477466], [-122.520217, 45.535693] >>> ]], "type": "Polygon"}) >>> inter = intersect([f, b]) """ properties_list = [] if isinstance(features, list): shapes = [] for f in features: poly = get_geom(f) s = shape(poly) shapes.append(s) if "properties" in f.keys(): properties_list.append(f["properties"]) else: if "features" not in features.keys(): raise Exception("Invalid FeatureCollection") if "properties" in features.keys(): properties_list.append(features["properties"]) shapes = [] for f in features["features"]: poly = get_geom(f) s = shape(poly) shapes.append(s) if "properties" in f.keys(): properties_list.append(f["properties"]) intersection = shapes[0] for shape_value in shapes: intersection = shape_value.intersection(intersection) intersection = mapping(intersection) if len(intersection["coordinates"]) == 0: return None properties = merge_dict(properties_list) intersection_feature = Feature(geometry=intersection, properties=properties) return intersection_feature