OSGeoLive-Notebooks/Fiona/fiona-ipynb-interop.ipynb

148 lines
3.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"import fiona\n",
"\n",
"from matplotlib.patches import Polygon\n",
"from shapely.geometry import shape, box\n",
"from shapely.ops import cascaded_union\n",
"\n",
"## Fiona, IPython Notebook interaction\n",
"## Live 8.5 * darkblue-b\n",
"##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Terminal Commands\n",
"----------------------\n",
"``Shell script can be executed with results stored into python variables``\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"shps = !ls /home/user/data/north_carolina/shape/*shp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Task: quickly examine the bounding areas of a directory of shapefiles\n",
"------------------------------------------------------------------\n",
"* use ``fiona.open()`` to read data files on disk\n",
"* save the filename and bounding box into a python ``dict``\n",
"\n",
".\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"td = {}\n",
"\n",
"for shp in shps:\n",
" with fiona.open( shp, 'r') as inp:\n",
" td[ inp.name ] = inp.bounds\n",
"\n",
"## Fiona inp.bounds => ( lower_lng, lower_lat, upper_lng, upper_lat)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Create shapely geometry from the coords\n",
"## shapely/geometry/geo.py\n",
"## box(minx, miny, maxx, maxy, ccw=True)\n",
"\n",
"nboxes = []\n",
"for k,v in td.items():\n",
" nboxes.append( box( v[0], v[1], v[2], v[3]) )\n",
"\n",
"print( 'Found BBOXs: ',len(nboxes))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## create a single cascaded UNION too\n",
"dst_poly = cascaded_union(nboxes)\n",
"dst_poly.bounds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Draw every BBOX for all files, transparently\n",
"## use matplotlib.Polygon to draw; let autoscale calculate the area\n",
"\n",
"fig, ax = plt.subplots(figsize=(12,12))\n",
"for polygon in nboxes:\n",
" mpl_poly = Polygon(np.array(polygon.exterior), facecolor=\"y\", alpha=0.02)\n",
" ax.add_patch(mpl_poly)\n",
"\n",
"## Indicate the exterior of the study area with a heavy line\n",
"ax.add_patch( Polygon(np.array(dst_poly.exterior), fill=False, lw=4, ec=\"b\", alpha=0.9) )\n",
"\n",
"ax.relim()\n",
"ax.autoscale()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}