Skip to content

Instantly share code, notes, and snippets.

@sgillies
Created November 26, 2014 22:49
Show Gist options
  • Save sgillies/546c9250e3bd7ba33519 to your computer and use it in GitHub Desktop.
Save sgillies/546c9250e3bd7ba33519 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:91a210a2a0d661958454a82f079deaac6b16d381ec86e18baa3982a651135d2e"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from shapely.geometry import Polygon"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"p1 = [(339, 346), (459,346), (399,311), (340, 277), (399, 173), (280, 242), (339, 415), (280, 381), (460, 207), (339, 346)] \n",
"p2 = [(339, 207), (280, 311), (460, 138), (399, 242), (459, 277), (459, 415), (399, 381), (519, 311), (520, 242), (519, 173), (399, 450), (339, 207)]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Polygon(p1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"WARNING:shapely.geos:Self-intersection at or near point 379.33333333333331 299.66666666666669\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"279.000270263 206.000201433 181.999383373 209.999063762\"\n",
" width=\"181.999383373\"\n",
" height=\"209.999063762\"\n",
" transform=\"translate(0, 181.999383373),scale(1, -1)\">\n",
" \n",
" <g fill-rule=\"evenodd\" fill=\"#ff3333\" stroke=\"#555555\" \n",
" stroke-width=\"2.0\" opacity=\"0.6\">\n",
" <path d=\"M 339.0,346.0 L 459.0,346.0 L 399.0,311.0 L 340.0,277.0 L 399.0,173.0 L 280.0,242.0 L 339.0,415.0 L 280.0,381.0 L 460.0,207.0 L 339.0,346.0 z\" />\n",
" </g>\n",
" </svg>"
],
"text": [
"<shapely.geometry.polygon.Polygon at 0x10eed9e50>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Polygon(p2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"WARNING:shapely.geos:Self-intersection at or near point 439 357.66666666666669\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 5,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"347.328657169 137.000319492 113.67068835 313.999117655\"\n",
" width=\"113.67068835\"\n",
" height=\"300\"\n",
" transform=\"translate(0, 113.67068835),scale(1, -1)\">\n",
" \n",
" <g fill-rule=\"evenodd\" fill=\"#ff3333\" stroke=\"#555555\" \n",
" stroke-width=\"2.09332745103\" opacity=\"0.6\">\n",
" <path d=\"M 339.0,207.0 L 280.0,311.0 L 460.0,138.0 L 399.0,242.0 L 459.0,277.0 L 459.0,415.0 L 399.0,381.0 L 519.0,311.0 L 520.0,242.0 L 519.0,173.0 L 399.0,450.0 L 339.0,207.0 z\" />\n",
" </g>\n",
" </svg>"
],
"text": [
"<shapely.geometry.polygon.Polygon at 0x10eed9f10>"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Polygon(p1).within(Polygon(p2))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"ERROR:shapely.geos:TopologyException: side location conflict at 459 346\n"
]
},
{
"ename": "PredicateError",
"evalue": "Failed to evaluate <_FuncPtr object at 0x10eea4c80>",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mPredicateError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-8-ace1c0f27267>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mPolygon\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwithin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mPolygon\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/sean/code/Shapely/shapely/geometry/base.pyc\u001b[0m in \u001b[0;36mwithin\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m 624\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mwithin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 625\u001b[0m \u001b[0;34m\"\"\"Returns True if geometry is within the other, else False\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 626\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimpl\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'within'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 627\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 628\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mequals_exact\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/sean/code/Shapely/shapely/predicates.pyc\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, this, other, *args)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_validate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mthis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_validate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mother\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstop_prepared\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mthis\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_geom\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_geom\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mRelateOp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mDelegating\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/sean/code/Shapely/shapely/geos.pyc\u001b[0m in \u001b[0;36merrcheck_predicate\u001b[0;34m(result, func, argtuple)\u001b[0m\n\u001b[1;32m 498\u001b[0m \u001b[0;34m'''Result is 2 on exception, 1 on True, 0 on False'''\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 499\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 500\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mPredicateError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Failed to evaluate %s\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mrepr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 501\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 502\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mPredicateError\u001b[0m: Failed to evaluate <_FuncPtr object at 0x10eea4c80>"
]
}
],
"prompt_number": 8
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment