Skip to content

Instantly share code, notes, and snippets.

@mbjones
Created April 15, 2025 22:24
Show Gist options
  • Save mbjones/36508254c229dc869d8c6ddf780e8b0a to your computer and use it in GitHub Desktop.
Save mbjones/36508254c229dc869d8c6ddf780e8b0a to your computer and use it in GitHub Desktop.
geojson2bbox
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "06dfb1d8",
"metadata": {},
"source": [
"# geojson to bbox conversion\n",
"\n",
"Derived from: https://gist.github.com/wassname/9f08c8a1c7f6d7fd377069b56f603e09\n",
"\n",
"Demonstrates conversion of many geojson shapes to bounding boxes."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "53acbe47",
"metadata": {},
"outputs": [],
"source": [
"# sample geojson data of various types\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"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "e4664c40",
"metadata": {},
"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": 29,
"id": "9631a576",
"metadata": {},
"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.016642799999999625 0.012982799999996075\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0003328559999999925\" 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": [
"<POLYGON ((-118.063 33.792, -118.064 33.782, -118.055 33.782, -118.048 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Polygon bbox\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.016642799999999625 0.012982799999996075\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0003328559999999925\" opacity=\"0.6\" d=\"M -118.06352,33.78217 L -118.04811,33.78217 L -118.04811,33.79392 L -118.06352,33.79392 L -118.06352,33.78217 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-118.064 33.782, -118.048 33.782, -118.048 33.794, -118.064 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-118.06352 33.78217, -118.04811 33.78217, -118.04811 33.79392, -118.06352 33.79392, -118.06352 33.78217))\n",
"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": [
"<POINT (-118.063 33.792)>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Point bbox\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": [
"<POINT (-118.063 33.792)>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POINT (-118.06335 33.79202)\n",
"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.0009579999999971278 0.010638000000000147\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><g><circle cx=\"-118.06335\" cy=\"33.79202\" r=\"0.00031914000000000443\" stroke=\"#555555\" stroke-width=\"0.00010638000000000147\" fill=\"#66cc99\" opacity=\"0.6\" /><circle cx=\"-118.06352\" cy=\"33.78217\" r=\"0.00031914000000000443\" stroke=\"#555555\" stroke-width=\"0.00010638000000000147\" fill=\"#66cc99\" opacity=\"0.6\" /></g></g></svg>"
],
"text/plain": [
"<MULTIPOINT (-118.063 33.792, -118.064 33.782)>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MultiPoint bbox\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.0009579999999971278 0.010638000000000147\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.00021276000000000294\" opacity=\"0.6\" d=\"M -118.06352,33.78217 L -118.06335,33.78217 L -118.06335,33.79202 L -118.06352,33.79202 L -118.06352,33.78217 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-118.064 33.782, -118.063 33.782, -118.063 33.792, -118.064 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-118.06352 33.78217, -118.06335 33.78217, -118.06335 33.79202, -118.06352 33.79202, -118.06352 33.78217))\n",
"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.0009579999999971278 0.010638000000000147\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.00021276000000000294\" points=\"-118.06335,33.79202 -118.06352,33.78217\" opacity=\"0.8\" /></g></svg>"
],
"text/plain": [
"<LINESTRING (-118.063 33.792, -118.064 33.782)>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"LineString bbox\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.0009579999999971278 0.010638000000000147\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.00021276000000000294\" opacity=\"0.6\" d=\"M -118.06352,33.78217 L -118.06335,33.78217 L -118.06335,33.79202 L -118.06352,33.79202 L -118.06352,33.78217 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-118.064 33.782, -118.063 33.782, -118.063 33.792, -118.064 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-118.06352 33.78217, -118.06335 33.78217, -118.06335 33.79202, -118.06352 33.79202, -118.06352 33.78217))\n",
"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.016642799999999625 0.01108279999999695\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><g><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.0003328559999999925\" points=\"-118.05532,33.78217 -118.04811,33.79036\" opacity=\"0.8\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.0003328559999999925\" points=\"-118.06335,33.79202 -118.06352,33.78217\" opacity=\"0.8\" /></g></g></svg>"
],
"text/plain": [
"<MULTILINESTRING ((-118.055 33.782, -118.048 33.79), (-118.063 33.792, -118....>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MultiLineString bbox\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.016642799999999625 0.01108279999999695\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57419)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0003328559999999925\" opacity=\"0.6\" d=\"M -118.06352,33.78217 L -118.04811,33.78217 L -118.04811,33.79202 L -118.06352,33.79202 L -118.06352,33.78217 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-118.064 33.782, -118.048 33.782, -118.048 33.792, -118.064 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-118.06352 33.78217, -118.04811 33.78217, -118.04811 33.79202, -118.06352 33.79202, -118.06352 33.78217))\n",
"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.016642799999999625 0.012982799999996075\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0003328559999999925\" 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": [
"<POLYGON ((-118.063 33.792, -118.064 33.782, -118.055 33.782, -118.048 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Polygon bbox\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.016642799999999625 0.012982799999996075\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0003328559999999925\" opacity=\"0.6\" d=\"M -118.06352,33.78217 L -118.04811,33.78217 L -118.04811,33.79392 L -118.06352,33.79392 L -118.06352,33.78217 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-118.064 33.782, -118.048 33.782, -118.048 33.794, -118.064 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-118.06352 33.78217, -118.04811 33.78217, -118.04811 33.79392, -118.06352 33.79392, -118.06352 33.78217))\n",
"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.016642799999999625 0.012982799999996075\" 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.0003328559999999925\" 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": [
"<MULTIPOLYGON (((-118.063 33.792, -118.064 33.782, -118.055 33.782, -118.048...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MultiPolygon bbox\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.016642799999999625 0.012982799999996075\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0003328559999999925\" opacity=\"0.6\" d=\"M -118.06352,33.78217 L -118.04811,33.78217 L -118.04811,33.79392 L -118.06352,33.79392 L -118.06352,33.78217 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-118.064 33.782, -118.048 33.782, -118.048 33.794, -118.064 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-118.06352 33.78217, -118.04811 33.78217, -118.04811 33.79392, -118.06352 33.79392, -118.06352 33.78217))\n",
"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.016642799999999625 0.012982799999996075\" 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.0003328559999999925\" 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.0003328559999999925\" 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": [
"<GEOMETRYCOLLECTION (MULTIPOLYGON (((-118.063 33.792, -118.064 33.782, -118....>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"GeometryCollection bbox\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.016642799999999625 0.012982799999996075\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,67.57609)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.0003328559999999925\" opacity=\"0.6\" d=\"M -118.06352,33.78217 L -118.04811,33.78217 L -118.04811,33.79392 L -118.06352,33.79392 L -118.06352,33.78217 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-118.064 33.782, -118.048 33.782, -118.048 33.794, -118.064 33.79...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-118.06352 33.78217, -118.04811 33.78217, -118.04811 33.79392, -118.06352 33.79392, -118.06352 33.78217))\n",
"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.400000000000006 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.16200000000000017\" stroke=\"#555555\" stroke-width=\"0.054000000000000055\" fill=\"#66cc99\" opacity=\"0.6\" /><polyline fill=\"none\" stroke=\"#66cc99\" stroke-width=\"0.10800000000000011\" 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.10800000000000011\" 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": [
"<GEOMETRYCOLLECTION (POINT (102 0.5), LINESTRING (102 0, 103 1, 104 0, 105 1...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"FeatureCollection bbox\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.400000000000006 1.4\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,1.0)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"0.10800000000000011\" opacity=\"0.6\" d=\"M 100.0,0.0 L 105.0,0.0 L 105.0,1.0 L 100.0,1.0 L 100.0,0.0 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((100 0, 105 0, 105 1, 100 1, 100 0))>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((100 0, 105 0, 105 1, 100 1, 100 0))\n",
"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.799999999999997\" 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": [
"<MULTILINESTRING ((170 45, 180 45), (-180 45, -170 45))>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MultiLineString bbox\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.799999999999997\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,90.0)\"><path fill-rule=\"evenodd\" fill=\"#ff3333\" stroke=\"#555555\" stroke-width=\"2.592\" opacity=\"0.6\" d=\"M -180.0,45.0 L 180.0,45.0 L 180.0,45.0 L -180.0,45.0 L -180.0,45.0 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-180 45, 180 45, 180 45, -180 45, -180 45))>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-180 45, 180 45, 180 45, -180 45, -180 45))\n",
"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.800000000000004\" 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": [
"<MULTIPOLYGON (((180 40, 180 50, 170 50, 170 40, 180 40)), ((-170 40, -170 5...>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"MultiPolygon bbox\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.800000000000004\" preserveAspectRatio=\"xMinYMin meet\"><g transform=\"matrix(1,0,0,-1,0,90.0)\"><path fill-rule=\"evenodd\" fill=\"#66cc99\" stroke=\"#555555\" stroke-width=\"2.592\" opacity=\"0.6\" d=\"M -180.0,40.0 L 180.0,40.0 L 180.0,50.0 L -180.0,50.0 L -180.0,40.0 z\" /></g></svg>"
],
"text/plain": [
"<POLYGON ((-180 40, 180 40, 180 50, -180 50, -180 40))>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bounding box is: POLYGON ((-180 40, 180 40, 180 50, -180 50, -180 40))\n"
]
}
],
"source": [
"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)\n",
" print(geojson['type']+' bbox')\n",
" bbox = shp.envelope\n",
" display(bbox)\n",
" print(\"Bounding box is: \", bbox)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "scomp",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment