def test_passing_incorrect_custom_filter(test_pbf): from pyrosm import OSM osm = OSM(filepath=test_pbf) try: osm.get_pois(custom_filter="wrong") except ValueError as e: if "dictionary" in str(e): pass except Exception as e: raise e
def test_adding_extra_attribute(helsinki_pbf): from pyrosm import OSM from geopandas import GeoDataFrame osm = OSM(filepath=helsinki_pbf) gdf = osm.get_pois() extra_col = "wikidata" extra = osm.get_pois(extra_attributes=[extra_col]) # The extra should have one additional column compared to the original one assert extra.shape[1] == gdf.shape[1] + 1 # Should have same number of rows assert extra.shape[0] == gdf.shape[0] assert extra_col in extra.columns assert len(extra[extra_col].dropna().unique()) > 0 assert isinstance(gdf, GeoDataFrame)
def test_using_rare_tag(helsinki_pbf): from pyrosm import OSM from geopandas import GeoDataFrame osm = OSM(filepath=helsinki_pbf) # There aren't any but should not raise an error still (#47) gdf = osm.get_pois({"park_ride": ["yes"]}) assert gdf is None
def test_pdgraph_connectivity(): from pyrosm.graphs import to_pandana import pandas as pd from pyrosm import OSM osm = OSM(get_data("helsinki_pbf")) nodes, edges = osm.get_network(nodes=True) # Prerare some test data for aggregations restaurants = osm.get_pois(custom_filter={"amenity": ["restaurant"]}) restaurants = restaurants.loc[restaurants["osm_type"] == "node"] restaurants["employee_cnt"] = 1 x = restaurants["lon"] y = restaurants["lat"] g = to_pandana(nodes, edges, retain_all=False) # Nodes and edges should be in DataFrames assert isinstance(g.nodes_df, pd.DataFrame) assert isinstance(g.edges_df, pd.DataFrame) # Precompute up to 1000 meters g.precompute(1000) # Link restaurants to graph g.set_pois("restaurants", 1000, 5, x, y) # Find the distance to nearest 5 restaurants from each node nearest_restaurants = g.nearest_pois(1000, "restaurants", num_pois=5) assert isinstance(nearest_restaurants, pd.DataFrame) assert nearest_restaurants.shape == (5750, 5) # Get closest node_ids for each restaurant node_ids = g.get_node_ids(x, y) assert isinstance(node_ids, pd.Series) assert node_ids.min() > 0 restaurants["node_id"] = node_ids # Attach employee counts to the graph g.set(node_ids, variable=restaurants.employee_cnt, name="employee_cnt") # Aggregate the number of employees within 500 meters from each node access = g.aggregate(500, type="sum", decay="linear", name="employee_cnt") assert isinstance(access, pd.Series) assert len(access) == 5750 # Test shortest path calculations shortest_distances = g.shortest_path_lengths(node_ids[0:100], node_ids[100:200], imp_name="length") assert isinstance(shortest_distances, list) assert len(shortest_distances) == 100 shortest_distances = pd.Series(shortest_distances) assert shortest_distances.min().round(0) == 22 assert shortest_distances.max().round(0) == 2453 assert shortest_distances.mean().round(0) == 856
def test_reading_pois_from_area_having_none(helsinki_pbf): from pyrosm import OSM from geopandas import GeoDataFrame # Bounding box for area that does not have any data bbox = [24.940514, 60.173849, 24.942, 60.175892] osm = OSM(filepath=helsinki_pbf, bounding_box=bbox) # The tool should warn if no buildings were found with pytest.warns(UserWarning) as w: gdf = osm.get_pois() # Check the warning text if "could not find any buildings" in str(w): pass # Result should be None assert gdf is None
def test_using_multiple_filters(helsinki_pbf): from pyrosm import OSM from geopandas import GeoDataFrame osm = OSM(filepath=helsinki_pbf) gdf = osm.get_pois({"shop": ["alcohol"], "amenity": ["pub"]}) # shop and amenity columns should only contain alcohol and pub as requested # (in addition to None values) shop = gdf["shop"].unique().tolist() shop = [item for item in shop if isinstance(item, str)] amenity = gdf["amenity"].unique().tolist() amenity = [item for item in amenity if isinstance(item, str)] assert isinstance(gdf, GeoDataFrame) assert shop == ["alcohol"] assert amenity == ["pub"] assert gdf.shape == (59, 32)
def test_pois(test_pbf): from pyrosm import OSM from geopandas import GeoDataFrame osm = OSM(test_pbf) gdf = osm.get_pois() assert isinstance(gdf, GeoDataFrame)