Skip to content

Instantly share code, notes, and snippets.

@AsgerPetersen
Created February 8, 2017 18:00
Show Gist options
  • Save AsgerPetersen/a1c86769eaf0f2da5345cc540fb452c1 to your computer and use it in GitHub Desktop.
Save AsgerPetersen/a1c86769eaf0f2da5345cc540fb452c1 to your computer and use it in GitHub Desktop.
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": 126,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def wedge_buffer(centre, radius, azimuth, opening_angle, inner_radius = 0, angle_resolution = 10):\n",
" # make Azimuth 0 north and positive clockwise\n",
" azimuth = -1 * 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": 127,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from shapely.geometry import Polygon, Point, GeometryCollection"
]
},
{
"cell_type": "code",
"execution_count": 128,
"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": 129,
"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": 130,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[(5.965925826289069, 9.74118095489748),\n",
" (5.996194698091745, 9.912844257252342),\n",
" (5.996194698091745, 10.087155742747658),\n",
" (5.965925826289069, 10.258819045102522),\n",
" (5.90630778703665, 10.4226182617407),\n",
" (5.819152044288992, 10.573576436351047),\n",
" (5.707106781186548, 10.707106781186548),\n",
" (5.573576436351046, 10.819152044288991),\n",
" (5.422618261740699, 10.90630778703665),\n",
" (5.258819045102521, 10.965925826289068),\n",
" (5.087155742747658, 10.996194698091745),\n",
" (4.912844257252342, 10.996194698091745),\n",
" (4.741180954897479, 10.965925826289068),\n",
" (5, 10),\n",
" (5.965925826289069, 9.74118095489748)]"
]
},
"execution_count": 130,
"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": 131,
"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=\"4.69098040517 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.96592582629,9.7411809549 L 5.99619469809,9.91284425725 L 5.99619469809,10.0871557427 L 5.96592582629,10.2588190451 L 5.90630778704,10.4226182617 L 5.81915204429,10.5735764364 L 5.70710678119,10.7071067812 L 5.57357643635,10.8191520443 L 5.42261826174,10.906307787 L 5.2588190451,10.9659258263 L 5.08715574275,10.9961946981 L 4.91284425725,10.9961946981 L 4.7411809549,10.9659258263 L 5.0,10.0 L 5.96592582629,9.7411809549 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x144d7e3d0>"
]
},
"execution_count": 131,
"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": 132,
"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=\"-0.30901959483 -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.965925826289,-0.258819045103 L 0.996194698092,-0.0871557427477 L 0.996194698092,0.0871557427477 L 0.965925826289,0.258819045103 L 0.906307787037,0.422618261741 L 0.819152044289,0.573576436351 L 0.707106781187,0.707106781187 L 0.573576436351,0.819152044289 L 0.422618261741,0.906307787037 L 0.258819045103,0.965925826289 L 0.0871557427477,0.996194698092 L -0.0871557427477,0.996194698092 L -0.258819045103,0.965925826289 L -0.129409522551,0.482962913145 L -0.0435778713738,0.498097349046 L 0.0435778713738,0.498097349046 L 0.129409522551,0.482962913145 L 0.21130913087,0.453153893518 L 0.286788218176,0.409576022144 L 0.353553390593,0.353553390593 L 0.409576022144,0.286788218176 L 0.453153893518,0.21130913087 L 0.482962913145,0.129409522551 L 0.498097349046,0.0435778713738 L 0.498097349046,-0.0435778713738 L 0.482962913145,-0.129409522551 L 0.965925826289,-0.258819045103 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x144d7e090>"
]
},
"execution_count": 132,
"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": 133,
"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=\"-0.30901959483 -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.965925826289,-0.258819045103 L 0.996194698092,0.0871557427477 L 0.906307787037,0.422618261741 L 0.707106781187,0.707106781187 L 0.422618261741,0.906307787037 L 0.0871557427477,0.996194698092 L -0.258819045103,0.965925826289 L -0.129409522551,0.482962913145 L 0.0435778713738,0.498097349046 L 0.21130913087,0.453153893518 L 0.353553390593,0.353553390593 L 0.453153893518,0.21130913087 L 0.498097349046,0.0435778713738 L 0.482962913145,-0.129409522551 L 0.965925826289,-0.258819045103 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x10e354950>"
]
},
"execution_count": 133,
"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": 134,
"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=\"-0.309171806907 -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.965925826289,-0.258819045103 L 0.984807753012,-0.173648177667 L 0.996194698092,-0.0871557427477 L 1.0,2.77555756156e-17 L 0.996194698092,0.0871557427477 L 0.984807753012,0.173648177667 L 0.965925826289,0.258819045103 L 0.939692620786,0.342020143326 L 0.906307787037,0.422618261741 L 0.866025403784,0.5 L 0.819152044289,0.573576436351 L 0.766044443119,0.642787609687 L 0.707106781187,0.707106781187 L 0.642787609687,0.766044443119 L 0.573576436351,0.819152044289 L 0.5,0.866025403784 L 0.422618261741,0.906307787037 L 0.342020143326,0.939692620786 L 0.258819045103,0.965925826289 L 0.173648177667,0.984807753012 L 0.0871557427477,0.996194698092 L 5.05321549807e-16,1.0 L -0.0871557427477,0.996194698092 L -0.173648177667,0.984807753012 L -0.258819045103,0.965925826289 L -0.258819045103,0.965925826289 L -0.129409522551,0.482962913145 L -0.0868240888335,0.492403876506 L -0.0435778713738,0.498097349046 L -1.91428434946e-16,0.5 L 0.0435778713738,0.498097349046 L 0.0868240888335,0.492403876506 L 0.129409522551,0.482962913145 L 0.171010071663,0.469846310393 L 0.21130913087,0.453153893518 L 0.25,0.433012701892 L 0.286788218176,0.409576022144 L 0.321393804843,0.383022221559 L 0.353553390593,0.353553390593 L 0.383022221559,0.321393804843 L 0.409576022144,0.286788218176 L 0.433012701892,0.25 L 0.453153893518,0.21130913087 L 0.469846310393,0.171010071663 L 0.482962913145,0.129409522551 L 0.492403876506,0.0868240888335 L 0.498097349046,0.0435778713738 L 0.5,4.57966997658e-16 L 0.498097349046,-0.0435778713738 L 0.492403876506,-0.0868240888335 L 0.482962913145,-0.129409522551 L 0.482962913145,-0.129409522551 L 0.965925826289,-0.258819045103 z\" /></g></svg>"
],
"text/plain": [
"<shapely.geometry.polygon.Polygon at 0x10e3546d0>"
]
},
"execution_count": 134,
"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