OSGeoLive-Notebooks/Shapely/shapely-pyplot-polys2.ipynb

183 lines
4.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot\n",
"from shapely.geometry import Polygon\n",
"from descartes.patch import PolygonPatch\n",
"from matplotlib.patches import Circle\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"COLOR = {\n",
" True: '#6699cc',\n",
" False: '#ff3333'\n",
" }\n",
"\n",
"def v_color(ob):\n",
" return COLOR[ob.is_valid]\n",
"\n",
"def plot_coords(ax, ob):\n",
" x, y = ob.xy\n",
" ax.plot(x, y, 'o', color='#999999', zorder=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" \n",
"fig = pyplot.figure(1, figsize=(12,20), dpi=90)\n",
"\n",
"# 1: valid polygon\n",
"ax = fig.add_subplot(121)\n",
"\n",
"ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]\n",
"int = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)][::-1]\n",
"polygon = Polygon(ext, [int])\n",
"\n",
"plot_coords(ax, polygon.interiors[0])\n",
"plot_coords(ax, polygon.exterior)\n",
"\n",
"patch = PolygonPatch(polygon, facecolor=v_color(polygon), edgecolor=v_color(polygon), alpha=0.5, zorder=2)\n",
"ax.add_patch(patch)\n",
"\n",
"ax.set_title('a) valid')\n",
"\n",
"xrange = [-1, 3]\n",
"yrange = [-1, 3]\n",
"ax.set_xlim(*xrange)\n",
"ax.set_xticks([-1, 0, 1, 2, 3])\n",
"ax.set_ylim(*yrange)\n",
"ax.set_yticks([-1, 0, 1, 2, 3])\n",
"ax.set_aspect(1)\n",
"\n",
"#2: invalid self-touching ring\n",
"ax = fig.add_subplot(122)\n",
"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 = Polygon(ext, [int])\n",
"\n",
"plot_coords(ax, polygon.interiors[0])\n",
"plot_coords(ax, polygon.exterior)\n",
"\n",
"patch = PolygonPatch(polygon, facecolor=v_color(polygon), edgecolor=v_color(polygon), alpha=0.5, zorder=2)\n",
"ax.add_patch(patch)\n",
"\n",
"ax.set_title('b) invalid')\n",
"\n",
"xrange = [-1, 3]\n",
"yrange = [-1, 3]\n",
"ax.set_xlim(*xrange)\n",
"ax.set_xticks([-1, 0, 1, 2, 3])\n",
"ax.set_ylim(*yrange)\n",
"ax.set_yticks([-1, 0, 1, 2, 3])\n",
"ax.set_aspect(1)\n",
"\n",
"pyplot.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"fig = pyplot.figure(1, figsize=(12,20), dpi=90)\n",
"\n",
"# 3: invalid polygon, ring touch along a line\n",
"ax = fig.add_subplot(121)\n",
"\n",
"ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]\n",
"int = [(0.5, 0), (1.5, 0), (1.5, 1), (0.5, 1), (0.5, 0)]\n",
"polygon = Polygon(ext, [int])\n",
"\n",
"plot_coords(ax, polygon.interiors[0])\n",
"plot_coords(ax, polygon.exterior)\n",
"\n",
"patch = PolygonPatch(polygon, facecolor=v_color(polygon), edgecolor=v_color(polygon), alpha=0.5, zorder=2)\n",
"ax.add_patch(patch)\n",
"\n",
"ax.set_title('c) invalid')\n",
"\n",
"xrange = [-1, 3]\n",
"yrange = [-1, 3]\n",
"ax.set_xlim(*xrange)\n",
"ax.set_xticks([-1, 0, 1, 2, 3])\n",
"ax.set_ylim(*yrange)\n",
"ax.set_yticks([-1, 0, 1, 2, 3])\n",
"ax.set_aspect(1)\n",
"\n",
"#4: invalid self-touching ring\n",
"ax = fig.add_subplot(122)\n",
"ext = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]\n",
"int_1 = [(0.5, 0.25), (1.5, 0.25), (1.5, 1.25), (0.5, 1.25), (0.5, 0.25)]\n",
"int_2 = [(0.5, 1.25), (1, 1.25), (1, 1.75), (0.5, 1.75)]\n",
"# int_2 = [\n",
"polygon = Polygon(ext, [int_1, int_2])\n",
"\n",
"plot_coords(ax, polygon.interiors[0])\n",
"plot_coords(ax, polygon.interiors[1])\n",
"plot_coords(ax, polygon.exterior)\n",
"\n",
"patch = PolygonPatch(polygon, facecolor=v_color(polygon), edgecolor=v_color(polygon), alpha=0.5, zorder=2)\n",
"ax.add_patch(patch)\n",
"\n",
"ax.set_title('d) invalid')\n",
"\n",
"xrange = [-1, 3]\n",
"yrange = [-1, 3]\n",
"ax.set_xlim(*xrange)\n",
"ax.set_xticks([-1, 0, 1, 2, 3])\n",
"ax.set_ylim(*yrange)\n",
"ax.set_yticks([-1, 0, 1, 2, 3])\n",
"ax.set_aspect(1)\n",
"\n",
"pyplot.show()\n"
]
},
{
"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": 2
}