This notebook demonstrates measurement examples
Takes two Points and finds the geographic bearing between them.
from turfpy import measurement
from geojson import Point, Feature
start = Feature(geometry=Point((-75.343, 39.984)))
end = Feature(geometry=Point((-75.534, 39.123)))
measurement.bearing(start,end)
Calculates distance between two Points.
from turfpy import measurement
from geojson import Point, Feature
start = Feature(geometry=Point((-75.343, 39.984)))
end = Feature(geometry=Point((-75.534, 39.123)))
measurement.distance(start,end)
calculates the area of the Geojson object given as input
from turfpy.measurement import area
from geojson import Feature, FeatureCollection
geometry_1 = {"coordinates": [[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]], "type": "Polygon"};
geometry_2 = {"coordinates": [[[2.38, 57.322], [23.194, -20.28], [-120.43, 19.15], [2.38, 57.322]]], "type": "Polygon"};
feature_1 = Feature(geometry=geometry_1)
feature_2 = Feature(geometry=geometry_2)
feature_collection = FeatureCollection([feature_1, feature_2])
area(feature_collection)
Generate bounding box coordinates for given geojson.
from turfpy.measurement import bbox
from geojson import Polygon
p = Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]])
bbox(p)
Generate a Polygon Feature for the bounding box generated using bbox.
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)
bbox_polygon(bb)
Takes a Feature or FeatureCollection and returns the absolute center point of all features.
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])
center(feature_collection)
Takes any number of features and returns a rectangular Polygon that encompasses all vertices.
from turfpy.measurement import envelope
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])
envelope(feature_collection)
Takes a geojson and measures its length in the specified units.
from turfpy.measurement import length
from geojson import LineString
ls = LineString([(115, -32), (131, -22), (143, -25), (150, -34)])
length(ls)
Takes a Point and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers and bearing in degrees.
from turfpy.measurement import destination
from geojson import Point, Feature
origin = Feature(geometry=Point([-75.343, 39.984]))
distance = 50
bearing = 90
options = {'units': 'mi'}
destination(origin,distance,bearing,options)
Takes one or more features and calculates the centroid using the mean of all vertices.
from turfpy.measurement import centroid
from geojson import Polygon
polygon = Polygon([[(-81, 41), (-88, 36), (-84, 31), (-80, 33), (-77, 39), (-81, 41)]])
centroid(polygon)
This function is used identify a Point at a specified distance along a LineString.
from turfpy.measurement import along
from geojson import LineString
ls = LineString([(-83, 30), (-84, 36), (-78, 41)])
along(ls,200,'mi')
Get midpoint between any the two points.
from turfpy.measurement import midpoint
from geojson import Point, Feature
point1 = Feature(geometry=Point([144.834823, -37.771257]))
point2 = Feature(geometry=Point([145.14244, -37.830937]))
midpoint(point1, point2)
Takes a reference Point Feature and FeatureCollection of point features and returns the point from the FeatureCollection closest to the reference Point Feature.
from turfpy.measurement import nearest_point
from geojson import Point, Feature, FeatureCollection
f1 = Feature(geometry=Point([28.96991729736328,41.01190001748873]))
f2 = Feature(geometry=Point([28.948459, 41.024204]))
f3 = Feature(geometry=Point([28.938674, 41.013324]))
fc = FeatureCollection([f1, f2 ,f3])
t = Feature(geometry=Point([28.973865, 41.011122]))
nearest_point(t ,fc)
Takes a Feature or FeatureCollection and returns a Point guaranteed to be on the surface of the feature.
from turfpy.measurement import point_on_feature
from geojson import Polygon, Feature
point = Polygon([[(116, -36), (131, -32), (146, -43), (155, -25), (133, -9), (111, -22), (116, -36)]])
feature = Feature(geometry=point)
point_on_feature(feature)
Takes a Point or a Point Feature and Polygon or Polygon Feature as input and returns True if Point is in given Feature.
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)
Finds the tangents of a (Multi)Polygon from a Point.
from turfpy.measurement import polygon_tangents
from geojson import Polygon, Point, Feature
point = Feature(geometry=Point([61, 5]))
polygon = Feature(geometry=Polygon([[(11, 0), (22, 4), (31, 0), (31, 11), (21, 15), (11, 11), (11, 0)]]))
polygon_tangents(point, polygon)
Returns the minimum distance between a Point and any segment of the LineString.
from turfpy.measurement import point_to_line_distance
from geojson import LineString, Point, Feature
point = Feature(geometry=Point([0, 0]))
linestring = Feature(geometry=LineString([(1, 1),(-1, 1)]))
point_to_line_distance(point, linestring)
Takes two points and finds the bearing angle between them along a Rhumb line i.e. the angle measured in degrees start the north line (0 degrees).
from turfpy.measurement import rhumb_bearing
from geojson import Feature, Point
start = Feature(geometry=Point([-75.343, 39.984]))
end = Feature(geometry=Point([-75.534, 39.123]))
rhumb_bearing(start, end, True)
Returns the destination Point having travelled the given distance along a Rhumb line from the origin Point with the (varant) given bearing.
from turfpy.measurement import rhumb_destination
from geojson import Point, Feature
start = Feature(geometry=Point([-75.343, 39.984]), properties={"marker-color": "F00"})
distance = 50
bearing = 90
rhumb_destination(start, distance, bearing, {'units':'mi', 'properties': {"marker-color": "F00"}})
Calculates the distance along a rhumb line between two points in degrees, radians, miles, or kilometers.
from turfpy.measurement import rhumb_distance
from geojson import Point, Feature
start = Feature(geometry=Point([-75.343, 39.984]))
end = Feature(geometry=Point([-75.534, 39.123]))
rhumb_distance(start, end,'mi')
Takes a bounding box and calculates the minimum square bounding box that would contain the input.
from turfpy.measurement import square
bbox = [-20, -20, -15, 0]
square(bbox)
Takes two inputs Point/Points and Polygon(s)/MultiPolygon(s) and returns all the Points with in Polygon(s)/MultiPolygon(s)
# First Visualize the Points and Polygon on Map
from geojson import Feature, FeatureCollection, Point, Polygon
from turfpy.measurement import points_within_polygon
from ipyleaflet import Map, GeoJSON
p1 = Feature(geometry=Point((-46.6318, -23.5523)))
p2 = Feature(geometry=Point((-46.6246, -23.5325)))
p3 = Feature(geometry=Point((-46.6062, -23.5513)))
p4 = Feature(geometry=Point((-46.663, -23.554)))
p5 = Feature(geometry=Point((-46.643, -23.557)))
points = FeatureCollection([p1, p2, p3, p4, p5])
poly = Polygon(
[
[
(-46.653, -23.543),
(-46.634, -23.5346),
(-46.613, -23.543),
(-46.614, -23.559),
(-46.631, -23.567),
(-46.653, -23.560),
(-46.653, -23.543),
]
]
)
m = Map(center=(-23.5523, -46.6318), zoom=13)
fc = FeatureCollection([p1, p2, p3, p4, p5, poly])
geo_json = GeoJSON(
data=fc,
style={"opacity": 1, "dashArray": "9", "fillOpacity": 0.3, "weight": 1},
hover_style={"color": "green", "dashArray": "0", "fillOpacity": 0.5},
)
m.add_layer(geo_json)
m
# Visualize the Points returned by function
result = points_within_polygon(points, poly)
data = result.copy()
data["features"].append(Feature(geometry=poly))
m = Map(center=(-23.5523, -46.6318), zoom=13)
geo_json = GeoJSON(
data=data,
style={"opacity": 1, "dashArray": "9", "fillOpacity": 0.3, "weight": 1},
hover_style={"color": "green", "dashArray": "0", "fillOpacity": 0.5},
)
m.add_layer(geo_json)
m