transformation.md

September 7, 2020 ยท View on GitHub

Transformation Examples :

  • Circle : Takes a Point and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
ArgumentTypeDescription
CenterFeatureCenter Point
radiusIntradius of the circle
stepsIntNumber of steps
unitsstr(optional)Unit of distance, default is 'km' refer Units type section
kwargsdict(optional)optional properties
ReturnTypeDescription
FeatureFeatureA circle polygon
from turfpy.transformation import circle
from geojson import Point, Feature
circle(center=Feature(geometry=Point((-75.343, 39.984))), radius=5, steps=10)
  • bbox_clip : Takes a Feature or geometry and a bbox and clips the feature to the bbox.
ArgumentTypeDescription
geojsonFeatureGeojson data
bboxListBounding Box which is used to clip the geojson
ReturnTypeDescription
bb_clipFeatureClipped geojson
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)
  • bezier_spline : Takes a line and returns a curved version by applying a Bezier spline algorithm.
ArgumentTypeDescription
lineFeatureLineString Feature which is used to draw the curve
resolutionFloatTime in milliseconds between points
sharpnessFloatA measure of how curvy the path should be between splines
ReturnTypeDescription
CurveFeatureCurve as LineString Feature
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)
  • concave : Generate concave hull for the given feature or Feature Collection.
ArgumentTypeDescription
featuresFeature or FeatureCollectionIt can be a feature or Feature Collection
alphaFloatAlpha determines the shape of concave hull, greater values will make shape more tighten.
ReturnTypeDescription
concave hullFeatureFeature of concave hull polygon
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)
  • convex : Generate convex hull for the given feature or Feature Collection.
ArgumentTypeDescription
featuresFeature or FeatureCollectionIt can be a feature or Feature Collection
ReturnTypeDescription
convex hullFeatureFeature of convex hull polygon
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))
  • intersect : Takes polygons and finds their intersection.
ArgumentTypeDescription
featuresList[Feature] or FeatureCollectionList of features of Feature Collection
ReturnTypeDescription
intersectionFeatureIntersection Geojson Feature
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])
  • union : Given list of features or FeatureCollection return union of those.
ArgumentTypeDescription
featuresList[Feature] or FeatureCollectionA list of GeoJSON features or FeatureCollection.
ReturnTypeDescription
unionFeatureA GeoJSON Feature or FeatureCollection
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"}))
  • dissolve : Take FeatureCollection or list of features to dissolve based on property_name provided.
ArgumentTypeDescription
featuresList[Feature], FeatureCollectionA list of GeoJSON features or FeatureCollection.
property_namestrName of property based on which to dissolve.
ReturnTypeDescription
dissolvedFeature or FeatureCollectionA GeoJSON Feature or FeatureCollection.
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')
  • difference : Find the difference between given two features.
ArgumentTypeDescription
feature_1FeatureA GeoJSON feature
feature_2FeatureA GeoJSON feature
ReturnTypeDescription
differenceFeatureA GeoJSON feature
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)
  • transform rotate : Rotates any geojson Feature or Geometry of a specified angle, around its centroid or a given pivot point; all rotations follow the right-hand rule
ArgumentTypeDescription
featureFeatureA GeoJSON feature
anglefloatangle of rotation (along the vertical axis), from North in decimal degrees, negative clockwise
pivotlist(optional)point around which the rotation will be performed, deafult values is centroid
mutateboolean(optional)allows GeoJSON input to be mutated (significant performance increase if True), deafult value is False
ReturnTypeDescription
geojsonFeatureThe rotated GeoJSON
from turfpy.transformation import transform_rotate
from geojson import Polygon, Feature
f = Feature(geometry=Polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]))
pivot = [0, 25]
transform_rotate(f, 10, pivot)
  • transform translate : Moves any geojson Feature or Geometry of a specified distance along a Rhumb Line on the provided direction angle.
ArgumentTypeDescription
featureFeatureA GeoJSON feature
distancefloatlength of the motion, negative values determine motion in opposite direction
directionfloatof the motion, angle from North in decimal degrees, positive clockwise
unitsstr(optional)Unit of distance, default is 'km' refer Units type section
z_translationfloat(optional)length of the vertical motion, same unit of distance, default value is 0
mutateboolean(optional)allows GeoJSON input to be mutated (significant performance increase if True), deafult value is False
ReturnTypeDescription
geojsonFeatureThe translated GeoJSON
from turfpy.transformation import transform_translate
from geojson import Polygon, Feature
f = Feature(geometry=Polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]))
transform_translate(f, 100, 35, mutate=True)
  • transform scale : Scale a GeoJSON from a given point by a factor of scaling (ex: factor=2 would make the GeoJSON 200% larger). If a FeatureCollection is provided, the origin point will be calculated based on each individual Feature.
ArgumentTypeDescription
featureFeatureFeatureCollection
factorfloatfactor of scaling, positive or negative values greater than 0
originstr or listPoint from which the scaling will occur (string options: sw/se/nw/ne/center/centroid), can also provide a point, deafult value is centroid
mutateboolean(optional)allows GeoJSON input to be mutated (significant performance increase if True), deafult value is False
ReturnTypeDescription
geojsonFeatureThe scaled GeoJSON
from turfpy.transformation import transform_scale
from geojson import Polygon, Feature
f = Feature(geometry=Polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]))
transform_scale(f, 3, origin=[0, 29])
  • tesselate : Tesselates a Feature into a FeatureCollection of triangles using earcut.
ArgumentTypeDescription
polyFeature(Polygon)the polygon to tesselate
ReturnTypeDescription
geojsonFeatureCollectiona geometrycollection feature
from geojson import Feature
from turfpy.transformation import tesselate
polygon = Feature(geometry={"coordinates": [[[11, 0], [22, 4], [31, 0], [31, 11],[21, 15], [11, 11], [11, 0]]], "type": "Polygon"})
tesselate(polygon)
  • line offset : Takes a linestring or multilinestring and returns a line at offset by the specified distance.
ArgumentTypeDescription
geojsonFeature(Line or MultiLineString)input GeoJSON of Line or MutliLineString
distancefloatdistance to offset the line (can be of negative value)
unitstr(Optional)Unit of distance, default is 'km' refer Units type section
ReturnTypeDescription
geojsonFeatureLine feature offset from the input line
from geojson import MultiLineString, Feature
from turfpy.transformation import line_offset
ls = Feature(geometry=MultiLineString([
     [(3.75, 9.25), (-130.95, 1.52)],
     [(23.15, -34.25), (-1.35, -4.65), (3.45, 77.95)]
 ]))
line_offset(ls, 2, unit='mi')
  • voronoi : Takes a FeatureCollection of points, and a bounding box, and returns a FeatureCollection of Voronoi polygons.
ArgumentTypeDescription
pointsFeatureCollection or List of Pointspoints to find the Voronoi polygons around.
bboxlistA bounding box to clip
ReturnTypeDescription
geojsonFeatureA GeoJSON Feature.
from turfpy.transformation import voronoi

points = [
    [-66.9703, 40.3183],
    [-63.7763, 40.4500],
    [-65.4196, 42.13985310302137],
    [-69.5813, 43.95405461286195],
    [-65.66337553550034, 55.97088945355232],
    [-60.280418548905, 56.240669185466146],
    [-68.5129561347689, 50.12984589640148],
    [-64.2393519226657, 59.66235385923687],
]
bbox = [-70, 40, -60, 60]
result = voronoi(points, bbox)

Units Type

Some functionalities support units as a parameter, default values of units is kilometers for the functionalities that have units are parameters. The values for it are:

'km' = kilometers
'm' = meters
'mi = miles
'ft' = feets
'in' = inches
'deg' = degrees
'cen' = centimeters
'rad' = radians
'naut' = nauticals
'yd' = yards