{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Measurements\n", "This notebook demonstrates measurement examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bearing\n", "Takes two Points and finds the geographic bearing between them." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy import measurement\n", "from geojson import Point, Feature\n", "start = Feature(geometry=Point((-75.343, 39.984)))\n", "end = Feature(geometry=Point((-75.534, 39.123)))\n", "measurement.bearing(start,end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Distance\n", "Calculates distance between two Points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy import measurement\n", "from geojson import Point, Feature\n", "start = Feature(geometry=Point((-75.343, 39.984)))\n", "end = Feature(geometry=Point((-75.534, 39.123)))\n", "measurement.distance(start,end)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Area\n", "calculates the area of the Geojson object given as input" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import area\n", "from geojson import Feature, FeatureCollection\n", "\n", "geometry_1 = {\"coordinates\": [[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]], \"type\": \"Polygon\"};\n", "geometry_2 = {\"coordinates\": [[[2.38, 57.322], [23.194, -20.28], [-120.43, 19.15], [2.38, 57.322]]], \"type\": \"Polygon\"};\n", "feature_1 = Feature(geometry=geometry_1)\n", "feature_2 = Feature(geometry=geometry_2)\n", "feature_collection = FeatureCollection([feature_1, feature_2])\n", "\n", "area(feature_collection)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bbox\n", "Generate bounding box coordinates for given geojson." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import bbox\n", "from geojson import Polygon\n", "\n", "p = Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]])\n", "bbox(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bbox Polygon\n", "Generate a Polygon Feature for the bounding box generated using bbox." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import bbox_polygon, bbox\n", "from geojson import Polygon\n", "\n", "p = Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]])\n", "bb = bbox(p)\n", "bbox_polygon(bb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Center\n", "Takes a Feature or FeatureCollection and returns the absolute center point of all features." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import center\n", "from geojson import Feature, FeatureCollection, Point\n", "\n", "f1 = Feature(geometry=Point((-97.522259, 35.4691)))\n", "f2 = Feature(geometry=Point((-97.502754, 35.463455)))\n", "f3 = Feature(geometry=Point((-97.508269, 35.463245)))\n", "feature_collection = FeatureCollection([f1, f2, f3])\n", "center(feature_collection)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Envelope\n", "Takes any number of features and returns a rectangular Polygon that encompasses all vertices." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import envelope\n", "from geojson import Feature, FeatureCollection, Point\n", "\n", "f1 = Feature(geometry=Point((-97.522259, 35.4691)))\n", "f2 = Feature(geometry=Point((-97.502754, 35.463455)))\n", "f3 = Feature(geometry=Point((-97.508269, 35.463245)))\n", "feature_collection = FeatureCollection([f1, f2, f3])\n", "envelope(feature_collection)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Length\n", "Takes a geojson and measures its length in the specified units." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import length\n", "from geojson import LineString\n", "ls = LineString([(115, -32), (131, -22), (143, -25), (150, -34)])\n", "length(ls)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Destination\n", "Takes a Point and calculates the location of a destination point given a distance in degrees, radians, miles, or kilometers and bearing in degrees." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import destination\n", "from geojson import Point, Feature\n", "origin = Feature(geometry=Point([-75.343, 39.984]))\n", "distance = 50\n", "bearing = 90\n", "options = {'units': 'mi'}\n", "destination(origin,distance,bearing,options)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Centroid\n", "Takes one or more features and calculates the centroid using the mean of all vertices." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import centroid\n", "from geojson import Polygon\n", "polygon = Polygon([[(-81, 41), (-88, 36), (-84, 31), (-80, 33), (-77, 39), (-81, 41)]])\n", "centroid(polygon)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Along \n", "This function is used identify a Point at a specified distance along a LineString." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import along\n", "from geojson import LineString\n", "ls = LineString([(-83, 30), (-84, 36), (-78, 41)])\n", "along(ls,200,'mi')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Midpoint\n", "Get midpoint between any the two points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import midpoint\n", "from geojson import Point, Feature\n", "point1 = Feature(geometry=Point([144.834823, -37.771257]))\n", "point2 = Feature(geometry=Point([145.14244, -37.830937]))\n", "midpoint(point1, point2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nearest Point\n", "Takes a reference Point Feature and FeatureCollection of point features and returns the point from the FeatureCollection closest to the reference Point Feature." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import nearest_point\n", "from geojson import Point, Feature, FeatureCollection\n", "f1 = Feature(geometry=Point([28.96991729736328,41.01190001748873]))\n", "f2 = Feature(geometry=Point([28.948459, 41.024204]))\n", "f3 = Feature(geometry=Point([28.938674, 41.013324]))\n", "fc = FeatureCollection([f1, f2 ,f3])\n", "t = Feature(geometry=Point([28.973865, 41.011122]))\n", "nearest_point(t ,fc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Point On Feature\n", "Takes a Feature or FeatureCollection and returns a Point guaranteed to be on the surface of the feature." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import point_on_feature\n", "from geojson import Polygon, Feature\n", "point = Polygon([[(116, -36), (131, -32), (146, -43), (155, -25), (133, -9), (111, -22), (116, -36)]])\n", "feature = Feature(geometry=point)\n", "point_on_feature(feature)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Point In Polygon\n", "Takes a Point or a Point Feature and Polygon or Polygon Feature as input and returns True if Point is in given Feature." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import boolean_point_in_polygon\n", "from geojson import Point, MultiPolygon, Feature\n", "point = Feature(geometry=Point([-77, 44]))\n", "polygon = Feature(geometry=MultiPolygon([([(-81, 41), (-81, 47), (-72, 47), (-72, 41), (-81, 41)],),\n", "([(3.78, 9.28), (-130.91, 1.52), (35.12, 72.234), (3.78, 9.28)],)]))\n", "boolean_point_in_polygon(point, polygon)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tangent To Polygon\n", "Finds the tangents of a (Multi)Polygon from a Point." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import polygon_tangents\n", "from geojson import Polygon, Point, Feature\n", "point = Feature(geometry=Point([61, 5]))\n", "polygon = Feature(geometry=Polygon([[(11, 0), (22, 4), (31, 0), (31, 11), (21, 15), (11, 11), (11, 0)]]))\n", "polygon_tangents(point, polygon)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Point To Line Distance\n", "Returns the minimum distance between a Point and any segment of the LineString." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import point_to_line_distance\n", "from geojson import LineString, Point, Feature\n", "point = Feature(geometry=Point([0, 0]))\n", "linestring = Feature(geometry=LineString([(1, 1),(-1, 1)]))\n", "point_to_line_distance(point, linestring)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rhumb Bearing\n", "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)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import rhumb_bearing\n", "from geojson import Feature, Point\n", "start = Feature(geometry=Point([-75.343, 39.984]))\n", "end = Feature(geometry=Point([-75.534, 39.123]))\n", "rhumb_bearing(start, end, True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rhumb Destination\n", "Returns the destination Point having travelled the given distance along a Rhumb line from the origin Point with the (varant) given bearing." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import rhumb_destination\n", "from geojson import Point, Feature\n", "start = Feature(geometry=Point([-75.343, 39.984]), properties={\"marker-color\": \"F00\"})\n", "distance = 50\n", "bearing = 90\n", "rhumb_destination(start, distance, bearing, {'units':'mi', 'properties': {\"marker-color\": \"F00\"}})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rhumb Distance\n", "Calculates the distance along a rhumb line between two points in degrees, radians, miles, or kilometers." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import rhumb_distance\n", "from geojson import Point, Feature\n", "start = Feature(geometry=Point([-75.343, 39.984]))\n", "end = Feature(geometry=Point([-75.534, 39.123]))\n", "rhumb_distance(start, end,'mi')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Square\n", "Takes a bounding box and calculates the minimum square bounding box that would contain the input." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.measurement import square\n", "bbox = [-20, -20, -15, 0]\n", "square(bbox)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Points within Polygon\n", "Takes two inputs Point/Points and Polygon(s)/MultiPolygon(s) and returns all the Points with in Polygon(s)/MultiPolygon(s)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# First Visualize the Points and Polygon on Map\n", "\n", "from geojson import Feature, FeatureCollection, Point, Polygon\n", "from turfpy.measurement import points_within_polygon\n", "from ipyleaflet import Map, GeoJSON\n", "\n", "p1 = Feature(geometry=Point((-46.6318, -23.5523)))\n", "p2 = Feature(geometry=Point((-46.6246, -23.5325)))\n", "p3 = Feature(geometry=Point((-46.6062, -23.5513)))\n", "p4 = Feature(geometry=Point((-46.663, -23.554)))\n", "p5 = Feature(geometry=Point((-46.643, -23.557)))\n", "\n", "points = FeatureCollection([p1, p2, p3, p4, p5])\n", "\n", "poly = Polygon(\n", " [\n", " [\n", " (-46.653, -23.543),\n", " (-46.634, -23.5346),\n", " (-46.613, -23.543),\n", " (-46.614, -23.559),\n", " (-46.631, -23.567),\n", " (-46.653, -23.560),\n", " (-46.653, -23.543),\n", " ]\n", " ]\n", ")\n", "\n", "m = Map(center=(-23.5523, -46.6318), zoom=13)\n", "fc = FeatureCollection([p1, p2, p3, p4, p5, poly])\n", "\n", "geo_json = GeoJSON(\n", " data=fc,\n", " style={\"opacity\": 1, \"dashArray\": \"9\", \"fillOpacity\": 0.3, \"weight\": 1},\n", " hover_style={\"color\": \"green\", \"dashArray\": \"0\", \"fillOpacity\": 0.5},\n", ")\n", "m.add_layer(geo_json)\n", "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Visualize the Points returned by function\n", "\n", "result = points_within_polygon(points, poly)\n", "\n", "data = result.copy()\n", "data[\"features\"].append(Feature(geometry=poly))\n", "\n", "m = Map(center=(-23.5523, -46.6318), zoom=13)\n", "\n", "geo_json = GeoJSON(\n", " data=data,\n", " style={\"opacity\": 1, \"dashArray\": \"9\", \"fillOpacity\": 0.3, \"weight\": 1},\n", " hover_style={\"color\": \"green\", \"dashArray\": \"0\", \"fillOpacity\": 0.5},\n", ")\n", "m.add_layer(geo_json)\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }