135 lines
3.4 KiB
Plaintext
135 lines
3.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from matplotlib import pyplot\n",
|
|
"from shapely.geometry import MultiPolygon\n",
|
|
"from descartes.patch import PolygonPatch\n",
|
|
"\n",
|
|
"%matplotlib inline\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\n",
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
" \n",
|
|
"fig = pyplot.figure( 1, figsize=(12,20), dpi=90 )\n",
|
|
"\n",
|
|
"# 1: valid multi-polygon\n",
|
|
"ax = fig.add_subplot(121)\n",
|
|
"\n",
|
|
"a = [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]\n",
|
|
"b = [(1, 1), (1, 2), (2, 2), (2, 1), (1, 1)]\n",
|
|
"\n",
|
|
"multi1 = MultiPolygon([ [a, []], [b, []] ])\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",
|
|
"p0 = multi1[0]\n",
|
|
"plot_coords(ax, p0.exterior)\n",
|
|
"patch = PolygonPatch( p0, facecolor=v_color(multi1), edgecolor=v_color(multi1), alpha=0.5, zorder=2)\n",
|
|
"ax.add_patch(patch)\n",
|
|
"\n",
|
|
"p1 = multi1[1]\n",
|
|
"plot_coords(ax, p1.exterior)\n",
|
|
"patch = PolygonPatch( p1, facecolor=v_color(multi1), edgecolor=v_color(multi1), alpha=0.5, zorder=2)\n",
|
|
"ax.add_patch(patch)\n",
|
|
" \n",
|
|
" \n",
|
|
"#2: invalid self-touching ring\n",
|
|
"ax = fig.add_subplot(122)\n",
|
|
"\n",
|
|
"c = [(0, 0), (0, 1.5), (1, 1.5), (1, 0), (0, 0)]\n",
|
|
"d = [(1, 0.5), (1, 2), (2, 2), (2, 0.5), (1, 0.5)]\n",
|
|
"\n",
|
|
"multi2 = MultiPolygon([[c, []], [d, []]])\n",
|
|
"\n",
|
|
"ax.set_title('b) invalid')\n",
|
|
"\n",
|
|
"p20 = multi2[0]\n",
|
|
"plot_coords(ax, p20.exterior)\n",
|
|
"patch = PolygonPatch( p20, facecolor=v_color(multi1), edgecolor=v_color(multi1), alpha=0.5, zorder=2)\n",
|
|
"ax.add_patch(patch)\n",
|
|
"\n",
|
|
"p21 = multi2[1]\n",
|
|
"plot_coords(ax, p21.exterior)\n",
|
|
"patch = PolygonPatch( p21, facecolor=v_color(multi1), edgecolor=v_color(multi1), alpha=0.5, zorder=2)\n",
|
|
"ax.add_patch(patch)\n",
|
|
"\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
|
|
}
|