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

114 lines
2.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot\n",
"from shapely.geometry.polygon import LinearRing\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)\n",
"\n",
"def plot_line(ax, ob):\n",
" x, y = ob.xy\n",
" ax.plot(x, y, color=v_color(ob), alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"fig = pyplot.figure(1, figsize=(12,20), dpi=90)\n",
"\n",
"# 1: valid ring\n",
"ax = fig.add_subplot(121)\n",
"ring = LinearRing([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 0.8), (0, 0)])\n",
"\n",
"plot_coords(ax, ring)\n",
"plot_line(ax, ring)\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",
"ring2 = LinearRing([(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)])\n",
"\n",
"plot_coords(ax, ring2)\n",
"plot_line(ax, ring2)\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": []
}
],
"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
}