turfpy.transformation module¶
This module implements some of the spatial analysis techniques and processes used to understand the patterns and relationships of geographic features. This is mainly inspired by turf.js. link: http://turfjs.org/
-
turfpy.transformation.
circle
(center, radius, steps=64, units='km', **kwargs)[source]¶ Takes a Point and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
- Parameters
center (geojson.feature.Feature) – A Point object representing center point of circle.
radius (int) – An int representing radius of the circle.
steps (int) – An int representing number of steps.
units (str) – A string representing units of distance e.g. ‘mi’, ‘km’, ‘deg’ and ‘rad’.
kwargs – A dict representing additional properties.
- Returns
A polygon feature object.
- Return type
geojson.geometry.Polygon
Example:
>>> from turfpy.transformation import circle >>> from geojson import Feature, Point >>> circle(center=Feature(geometry=Point((-75.343, 39.984))), radius=5, steps=10)
-
turfpy.transformation.
bbox_clip
(geojson, bbox)[source]¶ 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)
- Parameters
geojson (geojson.feature.Feature) –
bbox (list) –
- Return type
geojson.feature.Feature
-
turfpy.transformation.
intersect
(features)[source]¶ 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])
- Parameters
features (Union[List[geojson.feature.Feature], geojson.feature.FeatureCollection]) –
- Return type
geojson.feature.Feature
-
turfpy.transformation.
bezie_spline
(line, resolution=10000, sharpness=0.85)[source]¶ 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 bezie_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) >>> bezie_spline(f)
- Parameters
line (geojson.feature.Feature) –
- Return type
geojson.feature.Feature
-
turfpy.transformation.
union
(features)[source]¶ Given list of features or
FeatureCollection
return union of those.- Parameters
features (Union[List[geojson.feature.Feature], geojson.feature.FeatureCollection]) – A list of GeoJSON features or FeatureCollection.
- Returns
A GeoJSON Feature or FeatureCollection.
- Return type
Union[geojson.feature.Feature, geojson.feature.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"}))
-
turfpy.transformation.
_alpha_shape
(points, alpha)[source]¶ Compute the alpha shape (concave hull) of a set of points.
- Parameters
points – Iterable container of points.
alpha – alpha value to influence the gooeyness of the border. Smaller numbers don’t fall inward as much as larger numbers. Too large, and you lose everything!
-
turfpy.transformation.
concave
(features, alpha=2)[source]¶ Generate concave hull for the given feature or Feature Collection.
- Parameters
features (Union[geojson.feature.Feature, geojson.feature.FeatureCollection]) – It can be a feature or Feature Collection
alpha – Alpha determines the shape of concave hull, greater values will make shape more tighten
- Returns
Feature of concave hull polygon
Example:
>>> from turfpy.transformation import concave >>> from geojson import FeatureCollection, Feature, Point >>> f1 = Feature(geometry=Point((-63.601226, 44.642643))) >>> f2 = Feature(geometry=Point((-63.591442, 44.651436))) >>> f3 = Feature(geometry=Point((-63.580799, 44.648749))) >>> f4 = Feature(geometry=Point((-63.573589, 44.641788))) >>> f5 = Feature(geometry=Point((-63.587665, 44.64533))) >>> f6 = Feature(geometry=Point((-63.595218, 44.64765))) >>> fc = [f1, f2, f3, f4, f5, f6] >>> concave(FeatureCollection(fc), alpha=100)
-
turfpy.transformation.
convex
(features)[source]¶ Generate convex hull for the given feature or Feature Collection
- Parameters
features (Union[geojson.feature.Feature, geojson.feature.FeatureCollection]) – It can be a feature or Feature Collection
- Returns
Feature of convex hull polygon
Example:
>>> from turfpy.transformation import convex >>> from geojson import FeatureCollection, Feature, Point >>> f1 = Feature(geometry=Point((10.195312, 43.755225))) >>> f2 = Feature(geometry=Point((10.404052, 43.8424511))) >>> f3 = Feature(geometry=Point((10.579833, 43.659924))) >>> f4 = Feature(geometry=Point((10.360107, 43.516688))) >>> f5 = Feature(geometry=Point((10.14038, 43.588348))) >>> f6 = Feature(geometry=Point((10.195312, 43.755225))) >>> fc = [f1, f2, f3, f4, f5, f6] >>> convex(FeatureCollection(fc))
-
turfpy.transformation.
dissolve
(features, property_name=None)[source]¶ Take FeatureCollection or list of features to dissolve based on property_name provided. :param features: A list of GeoJSON features or FeatureCollection. :param property_name: Name of property based on which to dissolve. :return: A GeoJSON Feature or FeatureCollection.
Example:
>>> from geojson import Polygon, Feature, FeatureCollection >>> from turfpy.transformation import dissolve >>> f1 = Feature(geometry=Polygon([[ >>> [0, 0], >>> [0, 1], >>> [1, 1], >>> [1, 0], >>> [0, 0]]]), properties={"combine": "yes", "fill": "#00f"}) >>> f2 = Feature(geometry=Polygon([[ >>> [0, -1], >>> [0, 0], >>> [1, 0], >>> [1, -1], >>> [0,-1]]]), properties={"combine": "yes"}) >>> f3 = Feature(geometry=Polygon([[ >>> [1,-1], >>> [1, 0], >>> [2, 0], >>> [2, -1], >>> [1, -1]]]), properties={"combine": "no"}) >>> dissolve(FeatureCollection([f1, f2, f3]), property_name='combine')
- Parameters
features (Union[List[geojson.feature.Feature], geojson.feature.FeatureCollection]) –
property_name (str) –
- Return type
geojson.feature.FeatureCollection
-
turfpy.transformation.
difference
(feature_1, feature_2)[source]¶ 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)
- Parameters
feature_1 (geojson.feature.Feature) –
feature_2 (geojson.feature.Feature) –
- Return type
geojson.feature.Feature