Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path distance #29

Merged
merged 6 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions cabby/geo/map_processing/map_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ def __init__(self, map_name: Text, level: int, load_directory: Text = None):

if load_directory is None:
self.poi, self.streets = self.get_poi()
self.nx_graph = ox.graph_from_polygon(self.polygon_area)
self.nx_graph = ox.graph_from_polygon(self.polygon_area, network_type = 'all')
self.nodes, _ = ox.graph_to_gdfs(self.nx_graph)

# Find closest nodes to POI.
self.poi['node'] = self.poi['centroid'].apply(lambda x: \
ox.distance.get_nearest_node(self.nx_graph, \
jasonbaldridge marked this conversation as resolved.
Show resolved Hide resolved
util.tuple_from_point(x)))

else:
self.load_map(load_directory)
Expand All @@ -82,6 +87,10 @@ def get_poi(self) -> Tuple[GeoSeries, GeoSeries]:
osm_poi_no_streets = osm_poi_named_entities[osm_highway.isnull()]
osm_poi_streets = osm_poi_named_entities[osm_highway.notnull()]

# Get centroid for POI.
osm_poi_no_streets['centroid'] = osm_poi_no_streets['geometry'].apply(
lambda x: x if isinstance(x, Point) else x.centroid)

return osm_poi_no_streets, osm_poi_streets

def create_S2Graph(self, level: int):
Expand All @@ -99,10 +108,6 @@ def create_S2Graph(self, level: int):
x, level) if isinstance(x, Point) else util.cellid_from_polyline(x,
level))

# Get centroid for POI.
self.poi['centroid'] = self.poi['geometry'].apply(
lambda x: x if isinstance(x, Point) else x.centroid)

# Filter out entities that we didn't mange to get cellids covering.
self.poi = self.poi[self.poi['cellids'].notnull()]
self.streets = self.streets[self.streets['cellids'].notnull()]
Expand Down
24 changes: 10 additions & 14 deletions cabby/geo/map_processing/map_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,29 @@ class MapTest(unittest.TestCase):

def setUp(self):

# Process the map for an area in Pittsburgh.
self.pittsburgh_map = map_structure.Map("Pittsburgh", 18)
# Process the map for an area in Bologna.
self.bologna_map = map_structure.Map("Bologna", 18)

def testSingleOutput(self):
# Verify that a known POI is present.

specific_poi_found = self.pittsburgh_map.poi[self.pittsburgh_map.poi[
'name'] == 'Frick Building']
specific_poi_found = self.bologna_map.poi[self.bologna_map.poi[
'name'] == 'Delizie di Forno']
# Check that the number of Frick Building POI found is exactly 1.
self.assertEqual(specific_poi_found.shape[0], 1)

# Check the cellid.
list_cells = self.pittsburgh_map.poi[self.pittsburgh_map.poi[
'name'] == 'Frick Building']['cellids'].tolist()[0]
expected_ids = [
9814734816715735040, 9814734845337665536, 9814734856679063552,
9814734856712617984, 9814734856746172416, 9814734856779726848,
9814734856813281280, 9814734856846835712
]
found_ids = [list_cells[i].id() for i in range(8)]
list_cells = self.bologna_map.poi[self.bologna_map.poi[
'name'] == 'Delizie di Forno']['cellids'].tolist()[0]
expected_ids = [5152070235402010624]
found_ids = [list_cells[i].id() for i in range(len(list_cells))]
for expected, found in zip(expected_ids, found_ids):
self.assertEqual(expected, found)

# Check that the POI was added correctly to the graph.
cell_to_search = list_cells[0]
node = self.pittsburgh_map.s2_graph.search(cell_to_search)
self.assertTrue(hasattr(node, 'poi') and 203322568 in node.poi)
node = self.bologna_map.s2_graph.search(cell_to_search)
self.assertTrue(hasattr(node, 'poi') and 4696883190 in node.poi)


if __name__ == "__main__":
Expand Down
Binary file modified cabby/geo/map_processing/poiTestData/pittsburgh_graph.gpickle
Binary file not shown.
1,086 changes: 338 additions & 748 deletions cabby/geo/map_processing/poiTestData/pittsburgh_nodes.geojson

Large diffs are not rendered by default.

Binary file modified cabby/geo/map_processing/poiTestData/pittsburgh_poi.pkl
Binary file not shown.
Binary file modified cabby/geo/map_processing/poiTestData/pittsburgh_streets.pkl
Binary file not shown.
26 changes: 22 additions & 4 deletions cabby/geo/walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,27 @@ def get_start_poi(map: map_structure.Map, end_point: GeoDataFrame) -> \
dist = map.poi['centroid'].apply(
lambda x: util.get_distance_km(end_point['centroid'], x))

# Get POI within a distance range.
# TODO (https://github.com/googleinterns/cabby/issues/25): Change to path
# distance.
within_distance = map.poi[(dist > 0.2) & (dist < 0.8)]

# Get closest nodes to points.
dest = ox.get_nearest_node(
map.nx_graph, util.tuple_from_point(end_point['centroid']))

# Find nodes whithin 2000 meter path distance.
outer_circle_graph = ox.truncate.truncate_graph_dist(
map.nx_graph, dest, max_dist=2000, weight='length')

# Get graph that is too close (less than 200 meter path distance)
inner_circle_graph = ox.truncate.truncate_graph_dist(
map.nx_graph, dest, max_dist=200, weight='length')

outer_circle_graph_osmid = [x for x in outer_circle_graph.nodes.keys()]
inner_circle_graph_osmid = [x for x in inner_circle_graph.nodes.keys()]
jasonbaldridge marked this conversation as resolved.
Show resolved Hide resolved

osmid_in_range = [
osmid for osmid in outer_circle_graph_osmid if osmid not in inner_circle_graph_osmid]

within_distance = map.poi[map.poi['node'].isin(osmid_in_range)]

jasonbaldridge marked this conversation as resolved.
Show resolved Hide resolved
# Filter large POI.
small_poi = within_distance[within_distance['cellids'].str.len() <= 4]

Expand Down Expand Up @@ -336,6 +353,7 @@ def generate_and_save_rvs_routes(path: Text, map: map_structure.Map, n_samples:

counter = 0
while counter < n_samples:
print (counter)
entity = get_single_sample(map)
if entity is None:
continue
Expand Down
Loading