OSGeoLive-Notebooks/Shapely/shapely-pyplot-collections....

124 lines
3.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot\n",
"from shapely.geometry import LineString\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"BLUE = '#6699cc'\n",
"YELLOW = '#ffcc33'\n",
"GREEN = '#339933'\n",
"GRAY = '#999999'\n",
"\n",
"def plot_coords(ax, ob):\n",
" x, y = ob.xy\n",
" ax.plot(x, y, 'o', color=GRAY, zorder=1)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"fig = pyplot.figure(1, figsize=(12,20), dpi=90) #1, figsize=(10, 4), dpi=180)\n",
"\n",
"a = LineString([(0, 0), (1, 1), (1,2), (2,2)])\n",
"b = LineString([(0, 0), (1, 1), (2,1), (2,2)])\n",
"\n",
"# 1: disconnected multilinestring\n",
"ax = fig.add_subplot(121)\n",
"\n",
"plot_coords(ax, a)\n",
"plot_coords(ax, b)\n",
"\n",
"x, y = a.xy\n",
"ax.plot(x, y, color=YELLOW, alpha=0.5, linewidth=3, solid_capstyle='round', zorder=2)\n",
"\n",
"x, y = b.xy\n",
"ax.plot(x, y, color=GREEN, alpha=0.5, linewidth=3, solid_capstyle='round', zorder=2)\n",
"\n",
"ax.set_title('a) lines')\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",
"\n",
"x, y = a.xy\n",
"ax.plot(x, y, color=GRAY, alpha=0.7, linewidth=1, solid_capstyle='round', zorder=1)\n",
"x, y = b.xy\n",
"ax.plot(x, y, color=GRAY, alpha=0.7, linewidth=1, solid_capstyle='round', zorder=1)\n",
"\n",
"for ob in a.intersection(b):\n",
" x, y = ob.xy\n",
" if len(x) == 1:\n",
" ax.plot(x, y, 'o', color=BLUE, zorder=2)\n",
" else:\n",
" ax.plot(x, y, color=BLUE, alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2)\n",
"\n",
"ax.set_title('b) collection')\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
}