Last active
November 3, 2021 07:49
-
-
Save wassname/9f08c8a1c7f6d7fd377069b56f603e09 to your computer and use it in GitHub Desktop.
Convert any geojson to shapely
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-07-13T02:57:55.750183Z", | |
"start_time": "2017-07-13T10:57:55.735299+08:00" | |
}, | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import shapely.geometry\n", | |
"def geojson2shp(geojson):\n", | |
" \"\"\"Convert geometry of any geojson to a shapely object.\"\"\"\n", | |
" if geojson['type'] == 'Feature':\n", | |
" return geojson2shp(geojson[\"geometry\"])\n", | |
" elif geojson['type'] == 'FeatureCollection':\n", | |
" geometries = [geojson2shp(geom) for geom in geojson['features']]\n", | |
" return shapely.geometry.GeometryCollection(geometries)\n", | |
" elif geojson['type'] == 'Point':\n", | |
" return shapely.geometry.Point(geojson['coordinates'])\n", | |
" elif geojson['type'] == 'MultiPoint':\n", | |
" points = [\n", | |
" geojson2shp(dict(type='Point', coordinates=poly))\n", | |
" for poly in geojson['coordinates']\n", | |
" ]\n", | |
" return shapely.geometry.MultiPoint(points)\n", | |
" elif geojson['type'] == 'LineString':\n", | |
" return shapely.geometry.LineString(geojson['coordinates'])\n", | |
" elif geojson['type'] == 'MultiLineString':\n", | |
" linestrings = [\n", | |
" geojson2shp(dict(type='LineString', coordinates=poly))\n", | |
" for poly in geojson['coordinates']\n", | |
" ]\n", | |
" return shapely.geometry.MultiLineString(linestrings)\n", | |
" elif geojson['type'] == 'Polygon':\n", | |
" return shapely.geometry.Polygon(geojson['coordinates'][0])\n", | |
" elif geojson['type'] == 'MultiPolygon':\n", | |
" polygons = [\n", | |
" geojson2shp(dict(type='Polygon', coordinates=poly))\n", | |
" for poly in geojson['coordinates']\n", | |
" ]\n", | |
" return shapely.geometry.MultiPolygon(polygons)\n", | |
" elif geojson['type'] == 'GeometryCollection':\n", | |
" geometries = [geojson2shp(geom) for geom in geojson['geometries']]\n", | |
" return shapely.geometry.GeometryCollection(geometries)\n", | |
" else:\n", | |
" raise Exception('Unknown geojson type: %s' % geojson)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 65, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2017-07-13T03:05:22.358068Z", | |
"start_time": "2017-07-13T11:05:22.126116+08:00" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Polygon\n" | |
] | |
}, | |
{ | |
"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=\"-118.0641364 33.7815536 0.0166428 0.0129828\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.000332856\" opacity=\"0.6\" d=\"M -118.06335,33.79202 L -118.06352,33.78217 L -118.05532,33.78217 L -118.04811,33.79036 L -118.055,33.79392 L -118.06335,33.79202 z\" /></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.polygon.Polygon at 0x7f2ce4f15dd0>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Point\n" | |
] | |
}, | |
{ | |
"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=\"-119.06335 32.79202 2.0 2.0\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.58404)\"><circle cx=\"-118.06335\" cy=\"33.79202\" r=\"0.06\" stroke=\"#555555\" stroke-width=\"0.02\" fill=\"#66cc99\" opacity=\"0.6\" /></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.point.Point at 0x7f2ce4a221d0>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MultiPoint\n" | |
] | |
}, | |
{ | |
"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=\"-118.063914 33.781776 0.000957999999997 0.010638\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><g><circle cx=\"-118.06335\" cy=\"33.79202\" r=\"0.00031914\" stroke=\"#555555\" stroke-width=\"0.00010638\" fill=\"#66cc99\" opacity=\"0.6\" /><circle cx=\"-118.06352\" cy=\"33.78217\" r=\"0.00031914\" stroke=\"#555555\" stroke-width=\"0.00010638\" fill=\"#66cc99\" opacity=\"0.6\" /></g></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.multipoint.MultiPoint at 0x7f2ce4a22690>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"LineString\n" | |
] | |
}, | |
{ | |
"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=\"-118.063914 33.781776 0.000957999999997 0.010638\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.00021276\" points=\"-118.06335,33.79202 -118.06352,33.78217\" opacity=\"0.8\" /></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.linestring.LineString at 0x7f2ce4a221d0>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MultiLineString\n" | |
] | |
}, | |
{ | |
"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=\"-118.0641364 33.7815536 0.0166428 0.0110828\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.000332856\" points=\"-118.05532,33.78217 -118.04811,33.79036\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.000332856\" points=\"-118.06335,33.79202 -118.06352,33.78217\" opacity=\"0.8\" /></g></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.multilinestring.MultiLineString at 0x7f2ce4a22690>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Polygon\n" | |
] | |
}, | |
{ | |
"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=\"-118.0641364 33.7815536 0.0166428 0.0129828\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.000332856\" opacity=\"0.6\" d=\"M -118.06335,33.79202 L -118.06352,33.78217 L -118.05532,33.78217 L -118.04811,33.79036 L -118.055,33.79392 L -118.06335,33.79202 z\" /></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.polygon.Polygon at 0x7f2d04049bd0>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MultiPolygon\n" | |
] | |
}, | |
{ | |
"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=\"-118.0641364 33.7815536 0.0166428 0.0129828\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><g><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.000332856\" opacity=\"0.6\" d=\"M -118.06335,33.79202 L -118.06352,33.78217 L -118.05532,33.78217 L -118.04811,33.79036 L -118.055,33.79392 L -118.06335,33.79202 z\" /></g></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.multipolygon.MultiPolygon at 0x7f2ce4a22690>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"GeometryCollection\n" | |
] | |
}, | |
{ | |
"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=\"-118.0641364 33.7815536 0.0166428 0.0129828\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><g><g><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.000332856\" opacity=\"0.6\" d=\"M -118.06335,33.79202 L -118.06352,33.78217 L -118.05532,33.78217 L -118.04811,33.79036 L -118.055,33.79392 L -118.06335,33.79202 z\" /></g><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.000332856\" opacity=\"0.6\" d=\"M -118.06335,33.79202 L -118.06352,33.78217 L -118.05532,33.78217 L -118.04811,33.79036 L -118.055,33.79392 L -118.06335,33.79202 z\" /></g></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.collection.GeometryCollection at 0x7f2d04049bd0>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"FeatureCollection\n" | |
] | |
}, | |
{ | |
"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=\"99.8 -0.2 5.4 1.4\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,1.0)\"><g><circle cx=\"102.0\" cy=\"0.5\" r=\"0.162\" stroke=\"#555555\" stroke-width=\"0.054\" fill=\"#66cc99\" opacity=\"0.6\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.108\" points=\"102.0,0.0 103.0,1.0 104.0,0.0 105.0,1.0\" opacity=\"0.8\" /><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.108\" opacity=\"0.6\" 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\" /></g></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.collection.GeometryCollection at 0x7f2ce4a22690>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MultiLineString\n" | |
] | |
}, | |
{ | |
"data": { | |
"image/svg+xml": [ | |
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"300\" height=\"100.0\" viewBox=\"-194.4 30.6 388.8 28.8\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,90.0)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"2.592\" points=\"170.0,45.0 180.0,45.0\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"2.592\" points=\"-180.0,45.0 -170.0,45.0\" opacity=\"0.8\" /></g></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.multilinestring.MultiLineString at 0x7f2d04049bd0>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MultiPolygon\n" | |
] | |
}, | |
{ | |
"data": { | |
"image/svg+xml": [ | |
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"300\" height=\"100.0\" viewBox=\"-194.4 25.6 388.8 38.8\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,90.0)\"><g><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"2.592\" opacity=\"0.6\" d=\"M 180.0,40.0 L 180.0,50.0 L 170.0,50.0 L 170.0,40.0 L 180.0,40.0 z\" /><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"2.592\" opacity=\"0.6\" d=\"M -170.0,40.0 L -170.0,50.0 L -180.0,50.0 L -180.0,40.0 L -170.0,40.0 z\" /></g></g></svg>" | |
], | |
"text/plain": [ | |
"<shapely.geometry.multipolygon.MultiPolygon at 0x7f2ce4a22510>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# test\n", | |
"data=[\n", | |
" {\"type\": \"Polygon\", \"coordinates\":[[[-118.06335, 33.79202], [-118.06352, 33.78217], [-118.05532, 33.78217], [-118.04811, 33.79036], [-118.055, 33.79392], [-118.06335, 33.79202]]]},\n", | |
" {\"type\": \"Point\", \"coordinates\":[-118.06335, 33.79202]},\n", | |
" {\"type\": \"MultiPoint\", \"coordinates\":[[-118.06335, 33.79202], [-118.06352, 33.78217]]},\n", | |
" {\"type\": \"LineString\", \"coordinates\":[[-118.06335, 33.79202], [-118.06352, 33.78217]]},\n", | |
" {\"type\": \"MultiLineString\", \"coordinates\":[[[-118.05532, 33.78217], [-118.04811, 33.79036]],[[-118.06335, 33.79202], [-118.06352, 33.78217]]]},\n", | |
" {\"type\": \"Polygon\", \"coordinates\":[[[-118.06335, 33.79202], [-118.06352, 33.78217], [-118.05532, 33.78217], [-118.04811, 33.79036], [-118.055, 33.79392], [-118.06335, 33.79202]]]},\n", | |
" {\"type\": \"MultiPolygon\", \"coordinates\":[[[[-118.06335, 33.79202], [-118.06352, 33.78217], [-118.05532, 33.78217], [-118.04811, 33.79036], [-118.055, 33.79392], [-118.06335, 33.79202]]]]},\n", | |
" {\"type\":\"GeometryCollection\", \"geometries\":[\n", | |
" {\"type\": \"MultiPolygon\", \"coordinates\":[[[[-118.06335, 33.79202], [-118.06352, 33.78217], [-118.05532, 33.78217], [-118.04811, 33.79036], [-118.055, 33.79392], [-118.06335, 33.79202]]]]},\n", | |
" {\"type\": \"Polygon\", \"coordinates\":[[[-118.06335, 33.79202], [-118.06352, 33.78217], [-118.05532, 33.78217], [-118.04811, 33.79036], [-118.055, 33.79392], [-118.06335, 33.79202]]]}\n", | |
" ]},\n", | |
" {\n", | |
" \"type\": \"FeatureCollection\",\n", | |
" \"features\": [{\n", | |
" \"type\": \"Feature\",\n", | |
" \"geometry\": {\n", | |
" \"type\": \"Point\",\n", | |
" \"coordinates\": [102.0, 0.5]\n", | |
" },\n", | |
" \"properties\": {\n", | |
" \"prop0\": \"value0\"\n", | |
" }\n", | |
" }, {\n", | |
" \"type\": \"Feature\",\n", | |
" \"geometry\": {\n", | |
" \"type\": \"LineString\",\n", | |
" \"coordinates\": [\n", | |
" [102.0, 0.0],\n", | |
" [103.0, 1.0],\n", | |
" [104.0, 0.0],\n", | |
" [105.0, 1.0]\n", | |
" ]\n", | |
" },\n", | |
" \"properties\": { \"prop0\": \"value0\",\n", | |
" \"prop1\": 0.0\n", | |
" }\n", | |
" }, {\n", | |
" \"type\": \"Feature\",\n", | |
" \"geometry\": {\n", | |
" \"type\": \"Polygon\",\n", | |
" \"coordinates\": [\n", | |
" [\n", | |
" [100.0, 0.0],\n", | |
" [101.0, 0.0],\n", | |
" [101.0, 1.0],\n", | |
" [100.0, 1.0],\n", | |
" [100.0, 0.0]\n", | |
" ]\n", | |
" ]\n", | |
" },\n", | |
" \"properties\": {\n", | |
" \"prop0\": \"value0\",\n", | |
" \"prop1\": {\n", | |
" \"this\": \"that\"\n", | |
" }\n", | |
" }\n", | |
" }]\n", | |
" },\n", | |
" {\n", | |
" \"type\": \"MultiLineString\",\n", | |
" \"coordinates\": [\n", | |
" [\n", | |
" [170.0, 45.0], [180.0, 45.0]\n", | |
" ], [\n", | |
" [-180.0, 45.0], [-170.0, 45.0]\n", | |
" ]\n", | |
" ]\n", | |
" }, \n", | |
" {\n", | |
" \"type\": \"MultiPolygon\",\n", | |
" \"coordinates\": [\n", | |
" [\n", | |
" [\n", | |
" [180.0, 40.0], [180.0, 50.0], [170.0, 50.0],\n", | |
" [170.0, 40.0], [180.0, 40.0]\n", | |
" ]\n", | |
" ],\n", | |
" [\n", | |
" [\n", | |
" [-170.0, 40.0], [-170.0, 50.0], [-180.0, 50.0],\n", | |
" [-180.0, 40.0], [-170.0, 40.0]\n", | |
" ]\n", | |
" ]\n", | |
" ]\n", | |
" }\n", | |
"]\n", | |
"\n", | |
"from IPython.display import display\n", | |
"for geojson in data:\n", | |
" shp = geojson2shp(geojson)\n", | |
" print(geojson['type'])\n", | |
" \n", | |
" assert shp\n", | |
" assert shp.is_valid\n", | |
" assert not shp.is_empty \n", | |
" display(shp)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "ipython2", | |
"language": "python", | |
"name": "ipython2" | |
}, | |
"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.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment