Example #1
0
def test_test_data():
    import os
    fp1 = get_data("test_pbf")
    fp2 = get_data("helsinki_pbf")
    fp3 = get_data("helsinki_region_pbf")
    assert os.path.exists(fp1)
    assert os.path.exists(fp2)
    assert os.path.exists(fp3)
Example #2
0
def test_not_available():
    try:
        get_data("file_not_existing")
    except ValueError as e:
        if "is not available" in str(e):
            pass
        else:
            raise e
    except Exception as e:
        raise e
Example #3
0
def bike_nodes_and_edges():
    from pyrosm import OSM
    # UlanBator is good small dataset for testing
    # (unmodified, i.e. not cropped)
    pbf_path = get_data("ulanbator")
    osm = OSM(pbf_path)
    return osm.get_network(nodes=True, network_type="cycling")
Example #4
0
def test_get_osm_data():
    fp = get_data("test_pbf")
    parks, drive, indust = get_osm_gata(fp)
    assert isinstance(parks, gpd.GeoDataFrame)
    assert parks.crs == CRS
    assert isinstance(drive, gpd.GeoDataFrame)
    assert drive.crs == CRS
    assert isinstance(indust, gpd.GeoDataFrame)
    assert indust.crs == CRS
Example #5
0
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
Example #6
0
def test_add_osm_data(monkeypatch):
    sensors = pd.DataFrame(data={
        'lat': [55.0, 57.0, 59.5],
        'lon': [36.0, 37.5, 38.5]
    })
    fp = get_data("test_pbf")
    monkeypatch.setattr('src.data.osmdata.fp', fp)
    result = add_osm_data(sensors)
    assert isinstance(result, pd.DataFrame)
    assert len(result) == 1
    assert 'nearest_park' in result.columns
    assert 'nearest_road' in result.columns
    assert 'nearest_indust' in result.columns
Example #7
0
 def __init__(self, region):
     super().__init__()
     self.region = region
     fp = get_data(region)
     self.osm = OSM(fp)
     self.osm.keep_node_info = True
     self.network = self.osm.get_network(
         "all",
         extra_attributes=[
             "lanes:forward",
             "lanes:backward",
             "cycleway:left",
             "cycleway:right",
         ],
     )
     self.node_tags = (self.create_node_tags_lookup()
                       )  # Used to find traffic signals, all-way stops
     self.intersections = GeometricDict(crs="EPSG:4326")
     self.build_intersections(self.intersections)
     self.links = Links(crs="EPSG:4326")
     self.build_links(self.links)
     self.look_for_stop_signs()
     logging.info("Generated %s intersections and %s links." %
                  (len(self.intersections), len(self.links)))
Example #8
0
def immutable_nodes_and_edges():
    from pyrosm import OSM
    pbf_path = get_data("test_pbf")
    osm = OSM(pbf_path)
    return osm.get_network(nodes=True)
Example #9
0
def driving_nodes_and_edges():
    from pyrosm import OSM
    pbf_path = get_data("ulanbator")
    osm = OSM(pbf_path)
    return osm.get_network(network_type="driving", nodes=True)
Example #10
0
import pathlib
from pyrosm import get_data, OSM
DATA_DIR = "../data/"
from constants import regions

regions.reverse()

regions = regions[50:]

failed_regions = []
for region in regions:
    if not (pathlib.Path(f"{DATA_DIR}{region}-nodes.parquet").is_file()
            and pathlib.Path(f"{DATA_DIR}{region}-nodes.parquet").is_file()):
        print("starting processing for region " + region)
        try:
            fp = get_data(region, directory=DATA_DIR)
            osm = OSM(fp)
        except:
            print("failed for region", region)
            failed_regions.append(region)
            continue
        print("loading network")
        nodes, edges = osm.get_network(nodes=True, network_type="driving")
        print("loading network complete")
        edges_cudf = cudf.from_pandas(edges[['u', 'v', 'length']])
        edges_cudf.columns = ["src", "dst", "length"]
        nodes_cudf = cudf.from_pandas(nodes[['lon', 'lat', 'id']])
        nodes_cudf.columns = ["x", "y", "vertex"]
        print("saving as parquet files")
        nodes_cudf.to_parquet(DATA_DIR + f"{region}-nodes.parquet")
        edges_cudf.to_parquet(DATA_DIR + f"{region}-edges.parquet")
Example #11
0
def test_bbbike_download_to_temp():
    from pyrosm import get_data
    import os
    fp = get_data("UlanBator", update=True)
    assert os.path.exists(fp)
Example #12
0
def test_bbbike_download_to_directory(directory):
    from pyrosm import get_data
    import os
    fp = get_data("UlanBator", update=True, directory=directory)
    assert os.path.exists(fp)
Example #13
0
def test_geofabrik_download_to_directory(directory):
    from pyrosm import get_data
    import os
    fp = get_data("monaco", update=True, directory=directory)
    assert os.path.exists(fp)
Example #14
0
def helsinki_pbf():
    pbf_path = get_data("helsinki_pbf")
    return pbf_path
Example #15
0
def test_geofabrik_download_to_temp():
    from pyrosm import get_data
    import os
    fp = get_data("monaco", update=True)
    assert os.path.exists(fp)
Example #16
0
def test_pbf():
    pbf_path = get_data("test_pbf")
    return pbf_path
Example #17
0
              metavar="FILE")
op.add_option("--NET_OUTPUT",
              dest="net_outfile",
              help="Output file",
              metavar="FILE")
op.add_option("--CROSSINGS_OUTPUT",
              dest="crossings_outfile",
              help="Output file",
              metavar="FILE")
op.add_option("--ERROR_OUTPUT",
              dest="errfile",
              help="Output error file",
              metavar="FILE")
(options, args) = op.parse_args()

data = pyrosm.get_data("wales", directory="../intermediate-data-scratch"
                       )  # don't worry, it caches - was great_britain

# import buffer polygon
shape = shapefile.Reader(options.buffer)
feature = shape.shapeRecords()[0]  # first (presuambly only) feature
first = feature.shape.__geo_interface__
shapely_polygon = shapely.geometry.shape(first)

osm = pyrosm.OSM(data, bounding_box=shapely_polygon)

output_tags = [
    "access", "bicycle", "bridge", "cycleway", "cycleway:left",
    "cycleway:right", "cycleway:both", "foot", "highway", "maxspeed",
    "motor_vehicle", "oneway", "sidewalk", "tunnel"
]