{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from shapely.geometry import LineString, Polygon, Point, box, shape\n",
"from descartes import PolygonPatch\n",
"\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.collections import PatchCollection\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"BLUE = '#6699cc'\n",
"GRAY = '#999999'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"boxes_l = [\n",
" [(1, 1),\n",
" (3, 3)],\n",
" [(0.5, 2.5),\n",
" (1.5, 3.5)],\n",
" [(1.25, 2.25),\n",
" (1.75, 2.75)],\n",
" [(1.125, 2.125),\n",
" (1.375, 2.375)],\n",
" [(1.125, 2.625),\n",
" (1.375, 2.875)],\n",
" [(1.25, 3.25),\n",
" (1.75, 3.75)],\n",
" [(1.125, 3.125),\n",
" (1.375, 3.375)],\n",
" [(1.125, 3.625),\n",
" (1.375, 3.875)],\n",
" [(0.25, 2.25),\n",
" (0.75, 2.75)],\n",
" [(0.125, 2.125),\n",
" (0.375, 2.375)],\n",
" [(0.125, 2.625),\n",
" (0.375, 2.875)],\n",
" [(0.625, 2.625),\n",
" (0.875, 2.875)],\n",
" [(0.625, 2.125),\n",
" (0.875, 2.375)],\n",
" [(0.25, 3.25),\n",
" (0.75, 3.75)],\n",
" [(0.125, 3.125),\n",
" (0.375, 3.375)],\n",
" [(0.625, 3.625),\n",
" (0.875, 3.875)],\n",
" [(0.625, 3.125),\n",
" (0.875, 3.375)],\n",
" [(0.125, 3.625),\n",
" (0.375, 3.875)],\n",
" [(2.5, 0.5),\n",
" (3.5, 1.5)],\n",
" [(3.25, 0.25),\n",
" (3.75, 0.75)],\n",
" [(3.125, 0.125),\n",
" (3.375, 0.375)],\n",
" [(3.625, 0.625),\n",
" (3.875, 0.875)],\n",
" [(3.625, 0.125),\n",
" (3.875, 0.375)],\n",
" [(3.125, 0.625),\n",
" (3.375, 0.875)],\n",
" [(2.25, 0.25),\n",
" (2.75, 0.75)],\n",
" [(2.125, 0.125),\n",
" (2.375, 0.375)],\n",
" [(2.625, 0.625),\n",
" (2.875, 0.875)],\n",
" [(2.625, 0.125),\n",
" (2.875, 0.375)],\n",
" [(2.125, 0.625),\n",
" (2.375, 0.875)],\n",
" [(0.5, 0.5),\n",
" (1.5, 1.5)],\n",
" [(0.25, 1.25),\n",
" (0.75, 1.75)],\n",
" [(0.125, 1.125),\n",
" (0.375, 1.375)],\n",
" [(0.625, 1.625),\n",
" (0.875, 1.875)],\n",
" [(0.625, 1.125),\n",
" (0.875, 1.375)],\n",
" [(0.125, 1.625),\n",
" (0.375, 1.875)],\n",
" [(0.25, 0.25),\n",
" (0.75, 0.75)],\n",
" [(0.125, 0.125),\n",
" (0.375, 0.375)],\n",
" [(0.125, 0.625),\n",
" (0.375, 0.875)],\n",
" [(0.625, 0.125),\n",
" (0.875, 0.375)],\n",
" [(0.625, 0.625),\n",
" (0.875, 0.875)],\n",
" [(0.5625, 0.5625),\n",
" (0.6875, 0.6875)],\n",
" [(1.25, 0.25),\n",
" (1.75, 0.75)]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"boxes_m = [\n",
" [(1.3571428571428572, 1.3571428571428572), (3.357142857142857, 3.357142857142857)],\n",
" [(1, 3), (2, 4)],\n",
" [(1, 3), (1.5, 3.5)],\n",
" [(1, 3), (1.25, 3.25)],\n",
" [(1, 3), (1.125, 3.125)],\n",
" [(1, 3), (1.125, 3.125)],\n",
" [(1, 3), (1.25, 3.25)],\n",
" [(1, 3), (1.125, 3.125)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.75, 2.75)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.75, 2.75)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (1, 3)],\n",
" [(0.5, 2.5), (0.75, 2.75)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.75, 2.75)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (1, 3)],\n",
" [(0.5, 2.5), (0.75, 2.75)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.75, 2.75)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.5, 2.5), (0.625, 2.625)],\n",
" [(0.625, 0.625), (0.75, 0.75)],\n",
" [(0.5625, 0.5625), (0.625, 0.625)]\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"boxes = [box(item[0][0], item[0][1], item[1][0], item[1][1]) for item in boxes_m]\n",
"# create a multipolygon?\n",
"box_polygons = [\n",
" PolygonPatch(box, fc='none', ec='#ff0000', alpha=1., lw=0.5, zorder=0) for box in boxes\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"main_polygon = Polygon([(0.0, 0.0), (4.0, 0.0), (4.0, 1.0), (1.0, 1.0), (1.0, 4.0), (0.0, 4.0),\n",
" (0.0, 0.0)])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"main_polygon_patch = PolygonPatch(main_polygon, fc=BLUE, ec=GRAY, alpha=0.5, lw=0.25, zorder=0)\n",
"centroid_patch = PolygonPatch(main_polygon.centroid.buffer(.05), fc='#ff0000', ec='#000000', alpha=1., lw=.5, zorder=0)\n",
"best = PolygonPatch(Point(0.5625, 0.5625).buffer(.05), fc='#ff0000', ec='#000000', alpha=1., lw=.5, zorder=0)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAEBCAYAAAC+DNNtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAACwFJREFUeJzt3V2MVPUZx/HfgeXFRoRl8YJaDSxpjakkIvYlDU2IS21s\nL3xZeJqmN7VhF9A2qU2MmFTBl8RtIbF31YXa9qr6tGBiYtJKF03sRWPAUOlFGwM1rbSmFgWbWFgW\nTi84RIR1zlmY/5w5PN9PYpxZDsOj2e+c//zn7JDleS4Al75pdQ8AoDOIHQiC2IEgiB0IgtiBIEpj\nN7OR4t9D6ccBkEqVM/uwmb0h6UDqYQCk01PhmLXuvjP5JACSqnJmn29mA2Z2X/JpACSTVb2Crnjt\n/qK77047EoAUWi7ji025w8Uy/rCkfkmTxj42NsZ1t0BNBgYGsrJjyl6zH5C0p7jdJ2nXxf6BANqr\n6om25Wv2Ysn+FTMblPQfd9/XjuEAdF7pbry77+jEIADS4go6IAhiB4IgdiAIYgeCIHYgCGIHgiB2\nIAhiB4IgdiAIYgeCIHYgCGIHgiB2IAhiB4IgdiAIYgeCIHYgCGIHgiB2IAhiB4IgdiAIYgeCIHYg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OpnZFZJMp1dQ/U1ZkTRNceKS\nTre3tdWxXC4LBMEGHRAEsQNBEDsQBLEDQRA7EASxA0EQOxAEsQNB/B+FVl4WCWjZggAAAABJRU5E\nrkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x1062accd0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.clf()\n",
"fig = plt.figure(1.5, figsize=(30, 30), dpi=300)\n",
"ax = fig.add_subplot(111, aspect=\"equal\", axisbg=\"none\")\n",
"ax.set_xlim(0.0, 5.0)\n",
"ax.set_ylim(0.0, 5.0)\n",
"\n",
"ax.add_collection(PatchCollection([main_polygon_patch], match_original=True))\n",
"ax.add_collection(PatchCollection([centroid_patch], match_original=True))\n",
"for i, box in enumerate(box_polygons):\n",
" ax.add_collection(PatchCollection([box], match_original=True))\n",
" plt.savefig('output/box_%s.png' % i, format=\"png\", bbox_inches='tight', alpha=True, transparent=True, dpi=100)\n",
"ax.add_collection(PatchCollection([best], match_original=True))\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"10.5"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(boxes) / 4."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ls = LineString([[1.0, 1.0], [2.0, 2.0]])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<shapely.coords.CoordinateSequence at 0x105ef92d0>"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ls.coords"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"p = Polygon([[0, 0], [1, 1], [2, 0]])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'POLYGON ((0 0, 1 1, 2 0, 0 0))'"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.wkt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}