Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SergiyKolesnikov/45b6dad2480f9e3b7f390567d58954e6 to your computer and use it in GitHub Desktop.
Save SergiyKolesnikov/45b6dad2480f9e3b7f390567d58954e6 to your computer and use it in GitHub Desktop.
Distance based calculation of connection costs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import shapely.geometry as spg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We specify location of a cleint and location of an access hub as points. The two locations are different, so obviously they do not intersect."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n"
]
}
],
"source": [
"client_location = spg.point.Point(0, 1.6) # this will come from the geocoder lib \n",
"access_hub1 = spg.point.Point(0, 0) # this will come from the KML files\n",
"print(client_location.intersects(access_hub1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we use `buffer()` to define three circles around the access hub point. Each circle denotes costs for connecting to the access hub for the points inside the circle. The lcient is located inside the medium cost circle, but not in the low cost circle, so the connection costs will be medium."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n",
"True\n"
]
}
],
"source": [
"access_hub1_low_costs = access_hub1.buffer(1)\n",
"access_hub1_medium_costs = access_hub1.buffer(2)\n",
"access_hub1_high_costs = access_hub1.buffer(3)\n",
"\n",
"print(client_location.intersects(access_hub1_low_costs))\n",
"print(client_location.intersects(access_hub1_medium_costs))\n",
"print(client_location.intersects(access_hub1_high_costs))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With many access hub points and cost levels iterating over all them and checking for intersection may become too slow, so we will use GIS index to speed up the lookup."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"access_hub2 = spg.point.Point(0, 4.5) # this will come from the KML files\n",
"access_hub2_low_costs = access_hub2.buffer(1)\n",
"access_hub2_medium_costs = access_hub2.buffer(2)\n",
"access_hub2_high_costs = access_hub2.buffer(3)\n",
"\n",
"access_hub1_low_costs.tag = 'hub1_low'\n",
"access_hub1_medium_costs.tag = 'hub1_medium'\n",
"access_hub1_high_costs.tag = 'hub1_high'\n",
"access_hub2_low_costs.tag = 'hub2_low'\n",
"access_hub2_medium_costs.tag = 'hub2_medium'\n",
"access_hub2_high_costs.tag = 'hub2_high'\n",
"\n",
"all_costs = [\n",
" access_hub1_low_costs,\n",
" access_hub1_medium_costs,\n",
" access_hub1_high_costs,\n",
" access_hub2_low_costs,\n",
" access_hub2_medium_costs,\n",
" access_hub2_high_costs\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from shapely.strtree import STRtree\n",
"\n",
"gis_index = STRtree(all_costs)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hub1_medium\n",
"hub1_high\n",
"hub2_high\n"
]
}
],
"source": [
"possible_costs = gis_index.query(client_location)\n",
"for pc in possible_costs:\n",
" if client_location.intersects(pc):\n",
" print(pc.tag)"
]
}
],
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment