{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Transformation\n", "This notebook demonstrates all the examples of transformation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Circle\n", "Takes a Point and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geojson import Feature, Point\n", "from turfpy.transformation import circle\n", "\n", "center = Feature(geometry=Point((19.0760, 72.8777)))\n", "circle(center, radius=5, steps=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bbox clip\n", "Takes a Feature or geometry and a bbox and clips the feature to the bbox." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.transformation import bbox_clip\n", "from geojson import Feature\n", "f = Feature(geometry={\"coordinates\": [[[2, 2], [8, 4],\n", "[12, 8], [3, 7], [2, 2]]], \"type\": \"Polygon\"})\n", "bbox = [0, 0, 10, 10]\n", "bbox_clip(f, bbox)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bezie spline\n", "Takes a line and returns a curved version by applying a Bezier spline algorithm." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geojson import LineString, Feature\n", "from turfpy.transformation import bezie_spline\n", "ls = LineString([(-76.091308, 18.427501),\n", " (-76.695556, 18.729501),\n", " (-76.552734, 19.40443),\n", " (-74.61914, 19.134789),\n", " (-73.652343, 20.07657),\n", " (-73.157958, 20.210656)])\n", "f = Feature(geometry=ls)\n", "bezie_spline(f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Concave Hull\n", "Generate concave hull for the given feature or Feature Collection." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.transformation import concave\n", "from geojson import FeatureCollection, Feature, Point\n", "f1 = Feature(geometry=Point((-63.601226, 44.642643)))\n", "f2 = Feature(geometry=Point((-63.591442, 44.651436)))\n", "f3 = Feature(geometry=Point((-63.580799, 44.648749)))\n", "f4 = Feature(geometry=Point((-63.573589, 44.641788)))\n", "f5 = Feature(geometry=Point((-63.587665, 44.64533)))\n", "f6 = Feature(geometry=Point((-63.595218, 44.64765)))\n", "fc = [f1, f2, f3, f4, f5, f6]\n", "concave(FeatureCollection(fc), alpha=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convex Hull\n", "Generate convex hull for the given feature or Feature Collection." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.transformation import convex\n", "from geojson import FeatureCollection, Feature, Point\n", "f1 = Feature(geometry=Point((10.195312, 43.755225)))\n", "f2 = Feature(geometry=Point((10.404052, 43.8424511)))\n", "f3 = Feature(geometry=Point((10.579833, 43.659924)))\n", "f4 = Feature(geometry=Point((10.360107, 43.516688)))\n", "f5 = Feature(geometry=Point((10.14038, 43.588348)))\n", "f6 = Feature(geometry=Point((10.195312, 43.755225)))\n", "fc = [f1, f2, f3, f4, f5, f6]\n", "convex(FeatureCollection(fc))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Intersect\n", "Takes polygons and finds their intersection." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.transformation import intersect\n", "from geojson import Feature\n", "f = Feature(geometry={\"coordinates\": [\n", "[[-122.801742, 45.48565], [-122.801742, 45.60491],\n", "[-122.584762, 45.60491], [-122.584762, 45.48565],\n", "[-122.801742, 45.48565]]], \"type\": \"Polygon\"})\n", "b = Feature(geometry={\"coordinates\": [\n", "[[-122.520217, 45.535693], [-122.64038, 45.553967],\n", "[-122.720031, 45.526554], [-122.669906, 45.507309],\n", "[-122.723464, 45.446643], [-122.532577, 45.408574],\n", "[-122.487258, 45.477466], [-122.520217, 45.535693]\n", "]], \"type\": \"Polygon\"})\n", "intersect([f, b])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Union\n", "Given list of features or FeatureCollection return union of those." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from turfpy.transformation import union\n", "from geojson import Feature, Polygon, FeatureCollection\n", "f1 = Feature(geometry=Polygon([[\n", " [-82.574787, 35.594087],\n", " [-82.574787, 35.615581],\n", " [-82.545261, 35.615581],\n", " [-82.545261, 35.594087],\n", " [-82.574787, 35.594087]\n", "]]), properties={\"fill\": \"#00f\"})\n", "f2 = Feature(geometry=Polygon([[\n", " [-82.560024, 35.585153],\n", " [-82.560024, 35.602602],\n", " [-82.52964, 35.602602],\n", " [-82.52964, 35.585153],\n", " [-82.560024, 35.585153]]]), properties={\"fill\": \"#00f\"})\n", "union(FeatureCollection([f1, f2], properties={\"combine\": \"yes\"}))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dissolve\n", "Take FeatureCollection or list of features to dissolve based on property_name provided." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geojson import Polygon, Feature, FeatureCollection\n", "from turfpy.transformation import dissolve\n", "f1 = Feature(geometry=Polygon([[\n", " [0, 0],\n", " [0, 1],\n", " [1, 1],\n", " [1, 0],\n", " [0, 0]]]), properties={\"combine\": \"yes\", \"fill\": \"#00f\"})\n", "f2 = Feature(geometry=Polygon([[\n", " [0, -1],\n", " [0, 0],\n", " [1, 0],\n", " [1, -1],\n", " [0,-1]]]), properties={\"combine\": \"yes\"})\n", "f3 = Feature(geometry=Polygon([[\n", " [1,-1],\n", " [1, 0],\n", " [2, 0],\n", " [2, -1],\n", " [1, -1]]]), properties={\"combine\": \"no\"})\n", "dissolve(FeatureCollection([f1, f2, f3]), property_name='combine')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Difference\n", "Find the difference between given two features." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from geojson import Polygon, Feature\n", "from turfpy.transformation import difference\n", "f1 = Feature(geometry=Polygon([[\n", " [128, -26],\n", " [141, -26],\n", " [141, -21],\n", " [128, -21],\n", " [128, -26]]]), properties={\"combine\": \"yes\", \"fill\": \"#00f\"})\n", "f2 = Feature(geometry=Polygon([[\n", " [126, -28],\n", " [140, -28],\n", " [140, -20],\n", " [126, -20],\n", " [126, -28]]]), properties={\"combine\": \"yes\"})\n", "difference(f1, f2)" ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }