# DynaSlum/satsense

notebooks/Performance/JaccardIndex_Multipolygons.ipynb

### Summary

###### Test Coverage
{
"cells": [
{
"cell_type": "markdown",
"collapsed": true
},
"source": [
"# Jaccard index between two multi-polygons \n",
"## Simple example "
]
},
{
"cell_type": "markdown",
"source": [
"This notebook illustrates the computation of Jaccard similarity index between two simple multi-polygons using shapely and satsense python libraries."
]
},
{
"cell_type": "markdown",
"source": [
" ### Creating  two simple multi-polygons "
]
},
{
"cell_type": "code",
"execution_count": 1,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Multi-polygon 1 valid? True\n",
"Multi-polygon 2 valid? True\n"
]
}
],
"source": [
"# Python, shapely and satsense package imports\n",
"from matplotlib import pyplot\n",
"from shapely.geometry import MultiPolygon\n",
"from satsense.util.shapefile import show_multipolygon as shmp # visualization of multipolygons\n",
"\n",
"# define the nodes of valid multi-polygons\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",
"c = [(2,3), (4,3), (4,4), (2,4), (2,3)]\n",
"\n",
"multi1 = MultiPolygon([[a, []], [b, []] , [c, []]])\n",
"\n",
"d = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]\n",
"e = [(3, 3), (3, 4), (4, 4), (4, 3), (3, 3)]\n",
"\n",
"multi2 = MultiPolygon([[d, []], [e, []]])\n",
"\n",
"print(\"Multi-polygon 1 valid?\", multi1.is_valid)\n",
"print(\"Multi-polygon 2 valid?\", multi2.is_valid)"
]
},
{
"cell_type": "markdown",
"source": [
"### Visualizing the multi-polygons"
]
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.figure.Figure at 0x7f2455e8d320>"
]
},
"output_type": "display_data"
}
],
"source": [
"# Visualization parameters\n",
"RED = '#FF0000'\n",
"YOLK = '#FFE600'\n",
"al = 0.8\n",
"al_over = al - 0.2\n",
"show_verticies = True\n",
"\n",
"extent = [-1, -1, 5, 5] # format of extent is [xmin, ymin, xmax, ymax]\n",
"\n",
"# Visualize the multi-polygons\n",
"fig = pyplot.figure(1, dpi=90)\n",
"\n",
"shmp(multi1, ax, show_verticies, extent, RED, al, 'multi-polygon 1')\n",
"shmp(multi2, ax, show_verticies, extent, YOLK, al, 'multi-polygon 2')\n",
"shmp(multi1, ax, show_verticies, extent, RED, al_over, '') \n",
"shmp(multi2, ax, show_verticies, extent, YOLK, al_over, 'overlay multi-\\n polygons 1 and 2')    \n",
"    \n",
"pyplot.show()"
]
},
{
"cell_type": "markdown",
"collapsed": true
},
"source": [
"### Jaccard Index between the two multi-polygons"
]
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The area of the intersection between the 2 multi-polygons is  3.0\n",
"The area of the uinion between the 2 multi-polygons is  6.0\n",
"The Jaccard index between the 2 multi-polygons is  0.5\n"
]
}
],
"source": [
"# Satsense package import\n",
"from satsense.performance.jaccard_similarity import jaccard_index_multipolygons as jim # jaccard index computation\n",
"\n",
"# intersections between the multi-polygons\n",
"intersec = multi1.intersection(multi2).area\n",
"print(\"The area of the intersection between the 2 multi-polygons is \",intersec)\n",
"# union\n",
"union = multi1.union(multi2).area\n",
"print(\"The area of the uinion between the 2 multi-polygons is \",union)\n",
"     \n",
"# compute the Jaccard index (defined as intersection/union)\n",
"print(\"The Jaccard index between the 2 multi-polygons is \", jim(multi1, multi2))\n"
]
}
],
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:satelite]",
"language": "python",
"name": "conda-env-satelite-py"
},
"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.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}