Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/42c1174287e3ed60f7bb to your computer and use it in GitHub Desktop.
Save ThomasG77/42c1174287e3ed60f7bb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:7b179609d93f35342bd7bce9a171fe451a4617853b02d3a27ee49a1b3d9175c8"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from shapely import geometry"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"geometry.Point(10,-1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"9.0 -2.0 2.0 2.0\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" <circle\n",
" cx=\"10.0\"\n",
" cy=\"-1.0\"\n",
" r=\"0.06\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.02\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
" </svg>"
],
"text": [
"<shapely.geometry.point.Point at 0x10ce19c50>"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"geometry.Point(0,0).buffer(20).intersection(geometry.Point(20,20).buffer(10))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"9.45899412842 9.45899412842 8.56095537375 8.56095537375\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" \n",
" <g fill-rule=\"evenodd\" fill=\"#00B358\" stroke=\"#007439\" stroke-width=\"0.171219107475\" opacity=\"0.6\">\n",
" <path d=\"M 10.4577895846,17.021154046 L 11.1114046604,16.6293922461 L 12.6878656833,15.4602090673 L 14.1421356237,14.1421356237 L 15.4602090673,12.6878656833 L 16.6293922461,11.1114046604 L 17.021154046,10.4577895846 L 16.1731656763,10.7612046749 L 15.2860326317,11.1807873565 L 14.4442976698,11.685303877 L 13.6560671584,12.2698954664 L 12.9289321881,12.9289321881 L 12.2698954664,13.6560671584 L 11.685303877,14.4442976698 L 11.1807873565,15.2860326317 L 10.7612046749,16.1731656763 L 10.4577895846,17.021154046 z\" />\n",
" </g>\n",
" </svg>"
],
"text": [
"<shapely.geometry.polygon.Polygon at 0x10ce25550>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"geometry.Point(0,0).buffer(10,10).difference(geometry.Point(0,0).buffer(5,5))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"-11.0 -11.0 22.0 22.0\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" \n",
" <g fill-rule=\"evenodd\" fill=\"#00B358\" stroke=\"#007439\" stroke-width=\"0.44\" opacity=\"0.6\">\n",
" <path d=\"M 10.0,0.0 L 9.87688340595,-1.5643446504 L 9.51056516295,-3.09016994375 L 8.91006524188,-4.5399049974 L 8.09016994375,-5.87785252292 L 7.07106781187,-7.07106781187 L 5.87785252292,-8.09016994375 L 4.5399049974,-8.91006524188 L 3.09016994375,-9.51056516295 L 1.5643446504,-9.87688340595 L 2.05963378428e-14,-10.0 L -1.5643446504,-9.87688340595 L -3.09016994375,-9.51056516295 L -4.5399049974,-8.91006524188 L -5.87785252292,-8.09016994375 L -7.07106781187,-7.07106781187 L -8.09016994375,-5.87785252292 L -8.91006524188,-4.5399049974 L -9.51056516295,-3.09016994375 L -9.87688340595,-1.5643446504 L -10.0,-2.78699993902e-14 L -9.87688340595,1.5643446504 L -9.51056516295,3.09016994375 L -8.91006524188,4.5399049974 L -8.09016994375,5.87785252292 L -7.07106781187,7.07106781187 L -5.87785252292,8.09016994375 L -4.5399049974,8.91006524188 L -3.09016994375,9.51056516295 L -1.5643446504,9.87688340595 L -2.84823227897e-14,10.0 L 1.5643446504,9.87688340595 L 3.09016994375,9.51056516295 L 4.5399049974,8.91006524188 L 5.87785252292,8.09016994375 L 7.07106781187,7.07106781187 L 8.09016994375,5.87785252292 L 8.91006524188,4.5399049974 L 9.51056516295,3.09016994375 L 9.87688340595,1.5643446504 L 10.0,0.0 z M 5.0,0.0 L 4.75528258148,1.54508497187 L 4.04508497187,2.93892626146 L 2.93892626146,4.04508497187 L 1.54508497187,4.75528258148 L -2.75638376904e-14,5.0 L -1.54508497187,4.75528258148 L -2.93892626146,4.04508497187 L -4.04508497187,2.93892626146 L -4.75528258148,1.54508497187 L -5.0,-2.05963378428e-14 L -4.75528258148,-1.54508497187 L -4.04508497187,-2.93892626146 L -2.93892626146,-4.04508497187 L -1.54508497187,-4.75528258148 L 9.18794589679e-15,-5.0 L 1.54508497187,-4.75528258148 L 2.93892626146,-4.04508497187 L 4.04508497187,-2.93892626146 L 4.75528258148,-1.54508497187 L 5.0,0.0 z\" />\n",
" </g>\n",
" </svg>"
],
"text": [
"<shapely.geometry.polygon.Polygon at 0x10ce25690>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"geometry.asMultiPoint(np.random.random((20, 2)))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 6,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"-0.89094002368 -0.978218150143 2.88063729539 2.93880267967\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" <circle\n",
" cx=\"0.960940221964\"\n",
" cy=\"0.510203649854\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.467696053815\"\n",
" cy=\"0.603858439519\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.989697271709\"\n",
" cy=\"0.960584529525\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.157587456456\"\n",
" cy=\"0.618304298691\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.835929970469\"\n",
" cy=\"0.822695646339\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.724812756567\"\n",
" cy=\"0.616549616068\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.164889307028\"\n",
" cy=\"0.272451282511\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.10905997632\"\n",
" cy=\"0.404393256172\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.871361952454\"\n",
" cy=\"0.927235943503\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.699138723021\"\n",
" cy=\"0.625568605927\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.291061108721\"\n",
" cy=\"0.482055898396\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.643176373153\"\n",
" cy=\"0.0217818498567\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.388566832049\"\n",
" cy=\"0.484958910588\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.129412300764\"\n",
" cy=\"0.885004249794\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.503466614705\"\n",
" cy=\"0.748796054309\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.723244303186\"\n",
" cy=\"0.269292030426\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.143722707088\"\n",
" cy=\"0.617117542845\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.918140575139\"\n",
" cy=\"0.258008306283\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.583453501959\"\n",
" cy=\"0.326157624667\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<circle\n",
" cx=\"0.797250349033\"\n",
" cy=\"0.378623571383\"\n",
" r=\"0.0881640803901\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.0293880267967\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
" </svg>"
],
"text": [
"<shapely.geometry.multipoint.MultiPointAdapter at 0x10ce25290>"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from shapely import ops\n",
"fc = { \"type\": \"FeatureCollection\",\n",
" \"features\": [\n",
" { \"type\": \"Feature\",\n",
" \"geometry\": {\"type\": \"Point\", \"coordinates\": [102.0, 0.5]},\n",
" \"properties\": {\"prop0\": \"value0\"}\n",
" },\n",
" { \"type\": \"Feature\",\n",
" \"geometry\": {\n",
" \"type\": \"LineString\",\n",
" \"coordinates\": [\n",
" [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]\n",
" ]\n",
" },\n",
" \"properties\": {\n",
" \"prop0\": \"value0\",\n",
" \"prop1\": 0.0\n",
" }\n",
" },\n",
" { \"type\": \"Feature\",\n",
" \"geometry\": {\n",
" \"type\": \"Polygon\",\n",
" \"coordinates\": [\n",
" [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],\n",
" [100.0, 1.0], [100.0, 0.0] ]\n",
" ]\n",
" },\n",
" \"properties\": {\n",
" \"prop0\": \"value0\",\n",
" \"prop1\": {\"this\": \"that\"}\n",
" }\n",
" }\n",
" ]\n",
" }"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ops.unary_union([geometry.asShape(feat[\"geometry\"]) for feat in fc[\"features\"]])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"99.0 -1.0 7.0 3.0\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" <circle\n",
" cx=\"102.0\"\n",
" cy=\"0.5\"\n",
" r=\"0.21\"\n",
" stroke=\"#007439\"\n",
" stroke-width=\"0.07\"\n",
" fill=\"#00b358\"\n",
" opacity=\".6\"\n",
" />\n",
"<polyline\n",
" fill=\"none\"\n",
" stroke=\"#007439\"\n",
" stroke-width=0.14\n",
" points=\"102.0,0.0 103.0,1.0 104.0,0.0 105.0,1.0\"\n",
" opacity=\".8\"\n",
" />\n",
"\n",
" <g fill-rule=\"evenodd\" fill=\"#00B358\" stroke=\"#007439\" stroke-width=\"0.14\" opacity=\"0.6\">\n",
" <path d=\"M 100.0,0.0 L 101.0,0.0 L 101.0,1.0 L 100.0,1.0 L 100.0,0.0 z\" />\n",
" </g>\n",
" </svg>"
],
"text": [
"<shapely.geometry.collection.GeometryCollection at 0x10ce25ad0>"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"geometry.asShape({\n",
" \"type\": \"Polygon\",\n",
" \"coordinates\": [\n",
" [\n",
" [\n",
" 89.6484375,\n",
" -21.616579336740593\n",
" ],\n",
" [\n",
" 84.72656249999999,\n",
" -27.371767300523032\n",
" ],\n",
" [\n",
" 86.1328125,\n",
" -34.30714385628803\n",
" ],\n",
" [\n",
" 92.8125,\n",
" -38.54816542304656\n",
" ],\n",
" [\n",
" 92.8125,\n",
" -34.30714385628803\n",
" ],\n",
" [\n",
" 88.24218749999999,\n",
" -29.84064389983441\n",
" ],\n",
" [\n",
" 94.21875,\n",
" -28.92163128242129\n",
" ],\n",
" [\n",
" 89.6484375,\n",
" -21.616579336740593\n",
" ]\n",
" ]\n",
" ]\n",
" })"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 9,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"83.7265638248 -39.5480212049 11.4916039264 18.931132302\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" \n",
" <g fill-rule=\"evenodd\" fill=\"#00B358\" stroke=\"#007439\" stroke-width=\"0.378622646039\" opacity=\"0.6\">\n",
" <path d=\"M 89.6484375,-21.6165793367 L 84.7265625,-27.3717673005 L 86.1328125,-34.3071438563 L 92.8125,-38.548165423 L 92.8125,-34.3071438563 L 88.2421875,-29.8406438998 L 94.21875,-28.9216312824 L 89.6484375,-21.6165793367 z\" />\n",
" </g>\n",
" </svg>"
],
"text": [
"<shapely.geometry.polygon.PolygonAdapter at 0x10ce25750>"
]
}
],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]\n",
"int = [(1, 0), (0, 1), (0.5, 1.5), (1.5, 0.5), (1, 0)][::-1]\n",
"polygon = geometry.Polygon(ext, [int])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"polygon"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"WARNING:shapely.geos:Interior is disconnected at or near point 0 1\n"
]
},
{
"output_type": "stream",
"stream": "stderr",
"text": [
"WARNING:shapely.geos:Interior is disconnected at or near point 0 1\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 11,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"-1.0 -1.0 4.0 4.0\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" \n",
" <g fill-rule=\"evenodd\" fill=\"#ff3333\" stroke=\"#a61111\" stroke-width=\"0.08\" opacity=\"0.6\">\n",
" <path d=\"M 0.0,0.0 L 0.0,2.0 L 2.0,2.0 L 2.0,0.0 L 0.0,0.0 z M 1.0,0.0 L 1.5,0.5 L 0.5,1.5 L 0.0,1.0 L 1.0,0.0 z\" />\n",
" </g>\n",
" </svg>"
],
"text": [
"<shapely.geometry.polygon.Polygon at 0x10ce30210>"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"geometry.LinearRing([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"WARNING:shapely.geos:Ring Self-intersection at or near point 1 1\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"svg": [
"<svg\n",
" preserveAspectRatio=\"xMinYMin meet\"\n",
" viewBox=\"-1.0 -1.0 4.0 4.0\"\n",
" width=\"100.0\"\n",
" height=\"100.0\"\n",
" transform=\"translate(0, 100.0),scale(1, -1)\">\n",
" <polyline\n",
" fill=\"none\"\n",
" stroke=\"#a61111\"\n",
" stroke-width=0.08\n",
" points=\"0.0,0.0 0.0,2.0 1.0,1.0 2.0,2.0 2.0,0.0 1.0,1.0 0.0,0.0\"\n",
" opacity=\".8\"\n",
" />\n",
" </svg>"
],
"text": [
"<shapely.geometry.polygon.LinearRing at 0x10ce254d0>"
]
}
],
"prompt_number": 12
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment