Skip to content

Instantly share code, notes, and snippets.

@AsgerPetersen
Last active February 4, 2016 13:31
Show Gist options
  • Save AsgerPetersen/e9af4bc7a432635a7e8d to your computer and use it in GitHub Desktop.
Save AsgerPetersen/e9af4bc7a432635a7e8d to your computer and use it in GitHub Desktop.
Wedge buffers in python

Wedge buffers in python

Copyright (c) 2016, Asger Sigurd Skovbo Petersen

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Constructing \"Wedge buffer"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import math\n",
"RAD2DEGREE = 180 / math.pi\n",
"DEGREE2RAD = math.pi / 180"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def polar_to_cartesian(centre, angle, radius):\n",
" x = math.cos(angle) * radius + centre[0]\n",
" y = math.sin(angle) * radius + centre[1]\n",
" return (x,y)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def make_arc(centre, radius, rad_from_azimuth, rad_to_azimuth, rad_angle_resolution):\n",
" cartesian = []\n",
" rad_az = rad_from_azimuth\n",
" if rad_from_azimuth < rad_to_azimuth:\n",
" while rad_az < rad_to_azimuth:\n",
" cartesian.append(polar_to_cartesian(centre, rad_az, radius))\n",
" rad_az = rad_az + rad_angle_resolution\n",
" else:\n",
" while rad_az > rad_to_azimuth:\n",
" cartesian.append(polar_to_cartesian(centre, rad_az, radius))\n",
" rad_az = rad_az - rad_angle_resolution\n",
" cartesian.append(polar_to_cartesian(centre, rad_to_azimuth, radius))\n",
" return cartesian"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def wedge_buffer(centre, radius, azimuth, opening_angle, inner_radius = 0, angle_resolution = 10):\n",
" # make Azimuth 0 north\n",
" azimuth += 90\n",
" rad_from_azimuth = (azimuth - opening_angle * 0.5) * DEGREE2RAD\n",
" rad_to_azimuth = (azimuth + opening_angle * 0.5) * DEGREE2RAD\n",
" rad_angle_res = angle_resolution * DEGREE2RAD\n",
" \n",
" cartesian_coords = make_arc(centre, radius, rad_from_azimuth, rad_to_azimuth, rad_angle_res)\n",
" \n",
" if inner_radius <= 0:\n",
" cartesian_coords.append(centre)\n",
" else:\n",
" # Reverse arc at inner radius\n",
" cartesian_coords += make_arc(centre, inner_radius, rad_to_azimuth, rad_from_azimuth, rad_angle_res)\n",
" \n",
" # Close ring\n",
" cartesian_coords.append(cartesian_coords[0])\n",
" return cartesian_coords"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Demonstrate"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from shapely.geometry import Polygon, Point, GeometryCollection"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import matplotlib as ml\n",
"from matplotlib import pyplot\n",
"# For ipython to display pyplots inline\n",
"%matplotlib inline\n",
"# Default figure size\n",
"pyplot.rcParams['figure.figsize'] = (10.0, 10.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simplest case"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"centre = (5,10)\n",
"radius = 1\n",
"azimuth = 45\n",
"opening_angle = 120\n",
"poly = wedge_buffer(centre, radius, azimuth, opening_angle)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`poly` is just a list of coordinates forming the polygon"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[(5.258819045102521, 10.965925826289068),\n",
" (5.087155742747658, 10.996194698091745),\n",
" (4.912844257252342, 10.996194698091745),\n",
" (4.741180954897479, 10.965925826289068),\n",
" (4.577381738259301, 10.90630778703665),\n",
" (4.426423563648954, 10.819152044288993),\n",
" (4.292893218813453, 10.707106781186548),\n",
" (4.180847955711009, 10.573576436351047),\n",
" (4.093692212963351, 10.4226182617407),\n",
" (4.034074173710932, 10.258819045102522),\n",
" (4.003805301908255, 10.08715574274766),\n",
" (4.003805301908255, 9.912844257252344),\n",
" (4.034074173710931, 9.74118095489748),\n",
" (4.034074173710931, 9.741180954897478),\n",
" (5, 10),\n",
" (5.258819045102521, 10.965925826289068)]"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"poly"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using shapely it can be visualized in iPython"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"3.95360475218 9.69098040517 1.35541484265 1.35541484265\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,20.737375653)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.027108296853\" opacity=\"0.6\" d=\"M 5.2588190451,10.9659258263 L 5.08715574275,10.9961946981 L 4.91284425725,10.9961946981 L 4.7411809549,10.9659258263 L 4.57738173826,10.906307787 L 4.42642356365,10.8191520443 L 4.29289321881,10.7071067812 L 4.18084795571,10.5735764364 L 4.09369221296,10.4226182617 L 4.03407417371,10.2588190451 L 4.00380530191,10.0871557427 L 4.00380530191,9.91284425725 L 4.03407417371,9.7411809549 L 4.03407417371,9.7411809549 L 5.0,10.0 L 5.2588190451,10.9659258263 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x144d75d50>"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Polygon(poly)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `inner_radius` will cut away the inner part"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"-1.04639524782 -0.30901959483 1.35541484265 1.35541484265\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,0.737375652989)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.027108296853\" opacity=\"0.6\" d=\"M 0.258819045103,0.965925826289 L 0.0871557427477,0.996194698092 L -0.0871557427477,0.996194698092 L -0.258819045103,0.965925826289 L -0.422618261741,0.906307787037 L -0.573576436351,0.819152044289 L -0.707106781187,0.707106781187 L -0.819152044289,0.573576436351 L -0.906307787037,0.422618261741 L -0.965925826289,0.258819045103 L -0.996194698092,0.0871557427477 L -0.996194698092,-0.0871557427477 L -0.965925826289,-0.258819045103 L -0.965925826289,-0.258819045103 L -0.482962913145,-0.129409522551 L -0.498097349046,-0.0435778713738 L -0.498097349046,0.0435778713738 L -0.482962913145,0.129409522551 L -0.453153893518,0.21130913087 L -0.409576022144,0.286788218176 L -0.353553390593,0.353553390593 L -0.286788218176,0.409576022144 L -0.21130913087,0.453153893518 L -0.129409522551,0.482962913145 L -0.0435778713738,0.498097349046 L 0.0435778713738,0.498097349046 L 0.129409522551,0.482962913145 L 0.129409522551,0.482962913145 L 0.258819045103,0.965925826289 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x144d7e190>"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inner_radius = 0.5\n",
"poly = wedge_buffer(pnt, radius, azimuth, opening_angle, inner_radius)\n",
"Polygon(poly)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`angle_resolution` defines the density of points used to approximate the arc"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"-1.04639524782 -0.30901959483 1.35541484265 1.35541484265\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,0.737375652989)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.027108296853\" opacity=\"0.6\" d=\"M 0.258819045103,0.965925826289 L -0.0871557427477,0.996194698092 L -0.422618261741,0.906307787037 L -0.707106781187,0.707106781187 L -0.906307787037,0.422618261741 L -0.996194698092,0.0871557427477 L -0.965925826289,-0.258819045103 L -0.482962913145,-0.129409522551 L -0.498097349046,0.0435778713738 L -0.453153893518,0.21130913087 L -0.353553390593,0.353553390593 L -0.21130913087,0.453153893518 L -0.0435778713738,0.498097349046 L 0.129409522551,0.482962913145 L 0.258819045103,0.965925826289 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x144d7ed90>"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"angle_resolution = 20\n",
"Polygon(wedge_buffer(pnt, radius, azimuth, opening_angle, inner_radius, angle_resolution))"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"100.0\" height=\"100.0\" viewBox=\"-1.0503527618 -0.309171806907 1.35952456871 1.35952456871\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,0.741180954897)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0271904913742\" opacity=\"0.6\" d=\"M 0.258819045103,0.965925826289 L 0.173648177667,0.984807753012 L 0.0871557427477,0.996194698092 L 2.83276944882e-16,1.0 L -0.0871557427477,0.996194698092 L -0.173648177667,0.984807753012 L -0.258819045103,0.965925826289 L -0.342020143326,0.939692620786 L -0.422618261741,0.906307787037 L -0.5,0.866025403784 L -0.573576436351,0.819152044289 L -0.642787609687,0.766044443119 L -0.707106781187,0.707106781187 L -0.766044443119,0.642787609687 L -0.819152044289,0.573576436351 L -0.866025403784,0.5 L -0.906307787037,0.422618261741 L -0.939692620786,0.342020143326 L -0.965925826289,0.258819045103 L -0.984807753012,0.173648177667 L -0.996194698092,0.0871557427477 L -1.0,1.89882151931e-15 L -0.996194698092,-0.0871557427477 L -0.984807753012,-0.173648177667 L -0.965925826289,-0.258819045103 L -0.965925826289,-0.258819045103 L -0.482962913145,-0.129409522551 L -0.492403876506,-0.0868240888335 L -0.498097349046,-0.0435778713738 L -0.5,-1.60812264968e-16 L -0.498097349046,0.0435778713738 L -0.492403876506,0.0868240888335 L -0.482962913145,0.129409522551 L -0.469846310393,0.171010071663 L -0.453153893518,0.21130913087 L -0.433012701892,0.25 L -0.409576022144,0.286788218176 L -0.383022221559,0.321393804843 L -0.353553390593,0.353553390593 L -0.321393804843,0.383022221559 L -0.286788218176,0.409576022144 L -0.25,0.433012701892 L -0.21130913087,0.453153893518 L -0.171010071663,0.469846310393 L -0.129409522551,0.482962913145 L -0.0868240888335,0.492403876506 L -0.0435778713738,0.498097349046 L -1.07960685465e-15,0.5 L 0.0435778713738,0.498097349046 L 0.0868240888335,0.492403876506 L 0.129409522551,0.482962913145 L 0.129409522551,0.482962913145 L 0.258819045103,0.965925826289 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x144d7e510>"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"angle_resolution = 5\n",
"Polygon(wedge_buffer(pnt, radius, azimuth, opening_angle, inner_radius, angle_resolution))"
]
}
],
"metadata": {
"celltoolbar": "Raw Cell Format",
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment