Skip to content

Commit

Permalink
Add bug reproduce notebook.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalowitch committed Oct 15, 2020
1 parent 9f054ec commit 548afc6
Showing 1 changed file with 249 additions and 0 deletions.
249 changes: 249 additions & 0 deletions reproduce_edge_addition.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import importlib\n",
"import geopandas as gpd\n",
"from shapely.geometry.point import Point\n",
"import networkx as nx\n",
"import osmnx as ox\n",
"\n",
"from cabby.data.wikidata import query\n",
"from cabby.geo.map_processing.map_processor import map_structure\n",
"import cabby.geo.util as util\n",
"from cabby.data.metagraph import utils"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Get the existing map\n",
"base_osm_map_filepath = '/mnt/hackney/data/cabby/map/v14'\n",
"s2_level = 18\n",
"region = \"Pittsburgh\"\n",
"osm_map = map_structure.Map(region, s2_level, base_osm_map_filepath)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Prepare a new POI and prepare a dataframe row\n",
"osmid = 999111\n",
"new_df = pd.DataFrame(data={\n",
" 'name': 'Byers-Lyons House',\n",
" 'geometry': [util.point_str_to_shapely_point('Point(-80.016111 40.449722)')],\n",
" 'osmid': osmid,\n",
" 'wikidata': 'Q5004063'\n",
"}, index=[osmid])\n",
"new_df.index.rename('osmid', inplace=True)\n",
"single_poi = new_df.loc[osmid]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Run first part of map_structure.Map.add_single_poi_to_graph,\n",
"# and get the point to feed to add_single_point_edge\n",
"geometry = single_poi['geometry']\n",
"if isinstance(geometry, Point):\n",
" points = [single_poi['geometry']]\n",
"elif isinstance(geometry, Polygon):\n",
" coords = single_poi['geometry'].exterior.coords\n",
" n_points = len(coords)\n",
" \n",
" # Sample maximum 4 points.\n",
" sample_1 = Point(coords[0])\n",
" sample_2 = Point(coords[round(n_points/4)])\n",
" sample_3 = Point(coords[round(n_points/2)])\n",
" sample_4 = Point(coords[round(3*n_points/4)])\n",
" points = [sample_1, sample_2, sample_3, sample_4]\n",
" points = points[0:4]\n",
"else:\n",
" raise UnknownError\n",
"\n",
"poi_osmid = single_poi['osmid']\n",
"poi_osmid = util.concat_numbers(999, poi_osmid)\n",
"assert poi_osmid not in osm_map.poi['osmid'].tolist(), poi_osmid\n",
"osm_map.poi.loc[osm_map.poi['osmid'] ==\n",
" single_poi['osmid'], 'osmid'] = poi_osmid\n",
"\n",
"list_edges_connected_ids = []\n",
"edges_to_add = []\n",
"point = points[0]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"POINT (-80.016111 40.449722)\n",
"[]\n",
"999999111\n"
]
}
],
"source": [
"# Show arguments to map_structure.Map.add_single_point_edge\n",
"print(point)\n",
"print(list_edges_connected_ids)\n",
"print(poi_osmid)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-10-85970ce1efb1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 50\u001b[0m \u001b[0mdist_u_p\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mutil\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_distance_between_points\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mu_point\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprojected_point\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0mdist_v_p\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mutil\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_distance_between_points\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv_point\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprojected_point\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mdist_u_p\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m0\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mdist_v_p\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdist_u_p\u001b[0m\u001b[0;34m==\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0mprojected_point_osmid\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnear_edge_u\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"# Run map_structure.Map.add_single_point_edge up to assertion error\n",
"try:\n",
"\n",
" near_edge_u, near_edge_v, near_edge_key, line = \\\n",
" ox.distance.get_nearest_edge(\n",
" osm_map.nx_graph, util.tuple_from_point(point), return_geom=True, \n",
" )\n",
"\n",
"except Exception as e:\n",
" print(e)\n",
" # return []\n",
"\n",
"edge_id = (near_edge_u, near_edge_v, near_edge_key)\n",
"\n",
"if edge_id in list_edges_connected_ids: # Edge already connected\n",
" # return []\n",
" raise UnknownError\n",
"\n",
"# Get nearest points - u and v.\n",
"u_node = osm_map.nx_graph.nodes[near_edge_u]\n",
"u_point = Point(u_node['x'], u_node['y'])\n",
"\n",
"v_node = osm_map.nx_graph.nodes[near_edge_v]\n",
"v_point = Point(v_node['x'], v_node['y'])\n",
"\n",
"# Add to connected edges.\n",
"list_edges_connected_ids.append(edge_id)\n",
"\n",
"near_edge = osm_map.nx_graph.edges[edge_id]\n",
"\n",
"dist_projected = line.project(point)\n",
"projected_point = line.interpolate(line.project(point))\n",
"\n",
"cut_geometry = util.cut(line,dist_projected)\n",
"\n",
"n_lines = len(cut_geometry) \n",
"\n",
"line_1 = cut_geometry[0]\n",
"dist_1 = util.get_line_length(line_1)\n",
"\n",
"if n_lines==2:\n",
" assert projected_point==Point(line_1.coords[-1])\n",
" line_2 = cut_geometry[1]\n",
" dist_2 = util.get_line_length(line_2)\n",
"\n",
" projected_point_osmid = util.concat_numbers(\n",
" len(list_edges_connected_ids), poi_osmid)\n",
"\n",
"else: # Projected point is exactly on the end of the line (U or V).\n",
" dist_u_p = util.get_distance_between_points(u_point, projected_point)\n",
" dist_v_p = util.get_distance_between_points(v_point, projected_point)\n",
" assert dist_u_p==0 or dist_v_p==0\n",
" if dist_u_p==0:\n",
" projected_point_osmid = near_edge_u\n",
" else:\n",
" projected_point_osmid = near_edge_v"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"18.28590788244519"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dist_u_p"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28.377229757843207"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dist_v_p"
]
}
],
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 548afc6

Please sign in to comment.