Skip to content

Commit

Permalink
Merge pull request #29 from googleinterns/path-distance
Browse files Browse the repository at this point in the history
Path distance
  • Loading branch information
tzufgoogle authored Sep 10, 2020
2 parents e4234e0 + be3693b commit 6cdc47f
Show file tree
Hide file tree
Showing 8 changed files with 822 additions and 773 deletions.
17 changes: 11 additions & 6 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,
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 All @@ -118,7 +123,7 @@ def create_S2Graph(self, level: int):
self.streets[['cellids', 'osmid']].apply(
lambda x: self.s2_graph.add_street(x.cellids, x.osmid), axis=1)

def get_valid_path(self, dir_name: Text, name_ending: Text, \
def get_valid_path(self, dir_name: Text, name_ending: Text,
file_ending: Text) -> Optional[Text]:
'''Creates the file path and checks validity.
Arguments:
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.
28 changes: 23 additions & 5 deletions cabby/geo/walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,29 @@ 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 = list(outer_circle_graph.nodes.keys())
inner_circle_graph_osmid = list(inner_circle_graph.nodes.keys())

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

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

# Filter large POI.
small_poi = within_distance[within_distance['cellids'].str.len() <= 4]
small_poi = poi_in_ring[poi_in_ring['cellids'].str.len() <= 4]

if small_poi.shape[0] == 0:
return None
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

0 comments on commit 6cdc47f

Please sign in to comment.