Exemplo n.º 1
0
def get_building_data(node_coordinates, way_records, relations,
                      tags_as_columns, custom_filter, bounding_box):
    # If custom_filter has not been defined, initialize with default
    if custom_filter is None:
        custom_filter = {"building": [True]}
    else:
        # Check that the custom filter is in correct format
        custom_filter = validate_custom_filter(custom_filter)

        # Ensure that the "building" tag exists
        if "building" not in custom_filter.keys():
            custom_filter["building"] = [True]

    # Call signature for fetching buildings
    nodes, ways, relation_ways, relations = get_osm_data(node_arrays=None,
                                                         way_records=way_records,
                                                         relations=relations,
                                                         tags_as_columns=tags_as_columns,
                                                         data_filter=custom_filter,
                                                         filter_type="keep",
                                                         osm_keys=None
                                                         )

    # If there weren't any data, return empty GeoDataFrame
    if nodes is None and ways is None and relations is None:
        warnings.warn("Could not find any building elements for given area.",
                      UserWarning,
                      stacklevel=2)
        return None

    # Prepare GeoDataFrame
    gdf = prepare_geodataframe(nodes, node_coordinates, ways,
                               relations, relation_ways, tags_as_columns,
                               bounding_box)
    return gdf
Exemplo n.º 2
0
def get_network_data(node_coordinates, way_records, tags_as_columns,
                     network_filter, bounding_box):
    # Tags to keep as separate columns
    tags_as_columns += ["id", "nodes", "timestamp", "changeset", "version"]

    # Call signature for fetching network data
    nodes, ways, relation_ways, relations = get_osm_data(
        node_arrays=None,
        way_records=way_records,
        relations=None,
        tags_as_columns=tags_as_columns,
        data_filter=network_filter,
        filter_type="exclude",
        # Keep only records having 'highway' tag
        osm_keys="highway",
    )

    # If there weren't any data, return empty GeoDataFrame
    if ways is None:
        warnings.warn("Could not find any buildings for given area.",
                      UserWarning,
                      stacklevel=2)
        return None

    # Prepare GeoDataFrame
    gdf = prepare_geodataframe(nodes, node_coordinates, ways, relations,
                               relation_ways, tags_as_columns, bounding_box)
    return gdf
Exemplo n.º 3
0
def get_poi_data(nodes, node_coordinates, way_records, relations,
                 tags_as_columns, custom_filter, bounding_box):
    # Validate filter
    custom_filter = validate_custom_filter(custom_filter)

    # Call signature for fetching POIs
    nodes, ways, relation_ways, relations = get_osm_data(
        node_arrays=nodes,
        way_records=way_records,
        relations=relations,
        tags_as_columns=tags_as_columns,
        data_filter=custom_filter,
        filter_type="keep",
        osm_keys=None,
    )

    # If there weren't any data, return empty GeoDataFrame
    if nodes is None and ways is None and relations is None:
        warnings.warn("Could not find any POIs for given area.",
                      UserWarning,
                      stacklevel=2)
        return None

    # Prepare GeoDataFrame
    gdf = prepare_geodataframe(nodes, node_coordinates, ways, relations,
                               relation_ways, tags_as_columns, bounding_box)

    return gdf
Exemplo n.º 4
0
def test_creating_building_geometries(test_pbf):
    from pyrosm import OSM
    from pyrosm.data_manager import get_osm_data
    from pyrosm.geometry import create_way_geometries
    from numpy import ndarray
    from pygeos import Geometry

    osm = OSM(filepath=test_pbf)
    osm._read_pbf()
    custom_filter = {"building": True}
    nodes, ways, relation_ways, relations = get_osm_data(
        None,
        osm._way_records,
        osm._relations,
        osm.conf.tags.building,
        custom_filter,
        filter_type="keep")
    assert isinstance(ways, dict)

    geometries, lengths, from_ids, to_ids = create_way_geometries(
        osm._node_coordinates, ways, parse_network=False)
    assert isinstance(geometries,
                      list), f"Type should be list, got {type(geometries)}."
    assert isinstance(geometries[0], Geometry)
    assert len(geometries) == len(ways["id"])
Exemplo n.º 5
0
def get_boundary_data(node_coordinates, way_records, relations,
                      tags_as_columns, custom_filter, boundary_type, name,
                      bounding_box):

    if boundary_type == "all":
        boundary_type = True
    else:
        boundary_type = [boundary_type]

    # If custom_filter has not been defined, initialize with default
    if custom_filter is None:
        custom_filter = {"boundary": boundary_type}

    if "boundary" not in custom_filter.keys():
        custom_filter["boundary"] = True

    # Check that the custom filter is in correct format
    custom_filter = validate_custom_filter(custom_filter)

    # Call signature for fetching buildings
    nodes, ways, relation_ways, relations = get_osm_data(
        node_arrays=None,
        way_records=way_records,
        relations=relations,
        tags_as_columns=tags_as_columns,
        data_filter=custom_filter,
        filter_type="keep",
        osm_keys=None)

    # If there weren't any data, return empty GeoDataFrame
    if nodes is None and ways is None and relations is None:
        warnings.warn("Could not find any boundaries for given area.",
                      UserWarning,
                      stacklevel=2)
        return None

    # Prepare GeoDataFrame
    gdf = prepare_geodataframe(nodes, node_coordinates, ways, relations,
                               relation_ways, tags_as_columns, bounding_box)

    if gdf is None:
        return None

    # Filter by name
    # (use Pandas for filtering, which allows using 'contains' more easily)
    if name is not None:
        if "name" not in gdf.columns:
            raise ValueError(
                "Could not filter by name from given area. "
                "Any of the OSM elements did not have a name tag.")
        gdf = gdf.dropna(subset=["name"])
        gdf = gdf.loc[gdf["name"].str.contains(name)].reset_index(
            drop=True).copy()

    return gdf
Exemplo n.º 6
0
def get_user_defined_data(nodes, node_coordinates, way_records, relations,
                          tags_as_columns, custom_filter, osm_keys,
                          filter_type, keep_nodes, keep_ways, keep_relations,
                          bounding_box):

    if not keep_nodes:
        nodes = None

    # If wanting to parse relations but not ways,
    # it is still necessary to parse ways as well at this point
    if keep_ways is False and keep_relations is True:
        pass
    # If ways are not wanted, neither should relations be parsed
    elif not keep_ways:
        way_records = None
        relations = None

    if not keep_relations:
        relations = None

    # Call signature for fetching POIs
    nodes, ways, relation_ways, relations = get_osm_data(
        node_arrays=nodes,
        way_records=way_records,
        relations=relations,
        tags_as_columns=tags_as_columns,
        data_filter=custom_filter,
        filter_type=filter_type,
        osm_keys=osm_keys,
    )

    # If there weren't any data, return empty GeoDataFrame
    if nodes is None and ways is None and relations is None:
        warnings.warn("Could not find any OSM data for given area.",
                      UserWarning,
                      stacklevel=2)
        return None

    # Ensure that ways are None if returning those are not requested
    if not keep_ways:
        ways = None

    # Prepare GeoDataFrame
    gdf = prepare_geodataframe(nodes, node_coordinates, ways, relations,
                               relation_ways, tags_as_columns, bounding_box)

    return gdf
Exemplo n.º 7
0
def test_parsing_building_elements(test_pbf):
    from pyrosm import OSM
    from pyrosm.data_manager import get_osm_data
    osm = OSM(filepath=test_pbf)
    osm._read_pbf()
    custom_filter = {"building": True}
    nodes, ways, relation_ways, relations = get_osm_data(
        None,
        osm._way_records,
        osm._relations,
        osm.conf.tags.building,
        custom_filter,
        filter_type="keep")
    assert isinstance(ways, dict)

    # Required keys
    required = ['id', 'nodes']
    for col in required:
        assert col in ways.keys()

    # Test shape
    assert len(ways["id"]) == 2219
Exemplo n.º 8
0
def test_creating_building_geometries(test_pbf):
    from pyrosm import OSM
    from pyrosm.data_manager import get_osm_data
    from pyrosm.geometry import create_way_geometries
    from numpy import ndarray
    from shapely.geometry import Polygon

    osm = OSM(filepath=test_pbf)
    osm._read_pbf()
    custom_filter = {"building": True}
    nodes, ways, relation_ways, relations = get_osm_data(
        None,
        osm._way_records,
        osm._relations,
        osm.conf.tags.building,
        custom_filter,
        filter_type="keep")
    assert isinstance(ways, dict)

    geometries = create_way_geometries(osm._node_coordinates, ways)
    assert isinstance(geometries, ndarray)
    assert isinstance(geometries[0], Polygon)
    assert len(geometries) == len(ways["id"])