Ejemplo n.º 1
0
    def _get_city_data_from_address(self):

        if self.city_dict['cycleways'] is True:
            self.cycleways = self._get_cycleways()
        if self.city_dict['roads'] is True:
            self.roads = ox.graph_from_address(self.address,
                                               network_type='drive',
                                               dist=self.distance)
        if self.city_dict['water'] is True:
            self.water = ox.geometries_from_address(self.address,
                                                    tags=self.water_tags,
                                                    dist=self.distance)
        if self.city_dict['green'] is True:
            self.green = ox.geometries_from_address(self.address,
                                                    tags=self.green_tags,
                                                    dist=self.distance)
        if self.city_dict['buildings'] is True:
            self.buildings = ox.geometries_from_address(self.address,
                                                        tags={
                                                            "building":
                                                            True,
                                                            'landuse':
                                                            'construction'
                                                        },
                                                        dist=self.distance)
        if self.city_dict['railways'] is True:
            self.railways = ox.geometries_from_address(self.address,
                                                       tags=self.railway_tags,
                                                       dist=self.distance)
Ejemplo n.º 2
0
def test_geometries():

    # geometries_from_bbox - bounding box query to return empty GeoDataFrame
    gdf = ox.geometries_from_bbox(0.009, -0.009, 0.009, -0.009, tags={"building": True})

    # geometries_from_bbox - successful
    north, south, east, west = ox.utils_geo.bbox_from_point(location_point, dist=500)
    tags = {"landuse": True, "building": True, "highway": True}
    gdf = ox.geometries_from_bbox(north, south, east, west, tags=tags)
    fig, ax = ox.plot_footprints(gdf)

    # geometries_from_point - tests multipolygon creation
    gdf = ox.geometries_from_point((48.15, 10.02), tags={"landuse": True}, dist=2000)

    # geometries_from_place - includes test of list of places
    tags = {"amenity": True, "landuse": ["retail", "commercial"], "highway": "bus_stop"}
    gdf = ox.geometries_from_place([place1], tags=tags)

    # geometries_from_address - includes testing overpass settings and snapshot from 2019
    ox.settings.overpass_settings = '[out:json][timeout:200][date:"2019-10-28T19:20:00Z"]'
    gdf = ox.geometries_from_address(address, tags=tags)

    # geometries_from_xml - tests error handling of clipped XMLs with incomplete geometry
    gdf = ox.geometries_from_xml("tests/input_data/planet_10.068,48.135_10.071,48.137.osm")

    # test loading a geodataframe from a local .osm xml file
    with bz2.BZ2File("tests/input_data/West-Oakland.osm.bz2") as f:
        handle, temp_filename = tempfile.mkstemp(suffix=".osm")
        os.write(handle, f.read())
        os.close(handle)
    for filename in ("tests/input_data/West-Oakland.osm.bz2", temp_filename):
        gdf = ox.geometries_from_xml(filename)
        assert "Willow Street" in gdf["name"].values
    os.remove(temp_filename)
Ejemplo n.º 3
0
def _download_geometries(place, download_method, tags, crs, distance = 500.0):
    """
    The function downloads certain geometries from OSM, by means of OSMNX functions.
    It returns a GeoDataFrame, that could be empty when no geometries are found, with the provided tags.
    
    Parameters
    ----------
    place: string
        name of cities or areas in OSM: when using "OSMpolygon" please provide the name of a "relation" in OSM as an argument of "place"; when using "distance_from_address"
        provide an existing OSM address; when using "OSMplace" provide an OSM place name
    download_method: string {"polygon", "distance_from_address", "OSMplace"}
        it indicates the method that should be used for downloading the data.
    tag: dict 
        the desired OSMN tags
    crs: string
        the coordinate system of the case study area
        
    Returns
    -------
    geometries_gdf: GeoDataFrame
        the resulting GeoDataFrame
    """    
    if download_method == 'distance_from_address': 
        geometries_gdf = ox.geometries_from_address(place, tags = tags, dist = distance)
    elif download_method == 'OSMplace': 
        geometries_gdf = ox.geometries_from_place(place, tags = tags)
    else: 
        geometries_gdf = ox.geometries_from_polygon(place, tags = tags)
    
    geometries_gdf = geometries_gdf.to_crs(crs)
    return geometries_gdf
Ejemplo n.º 4
0
def get_urban_rail_fromOSM(place, download_method, epsg, distance=7000):
    """
    The function downloads and creates a simplified OSMNx graph for a selected area's urban rail network. 
    Afterwards, GeoDataFrames for nodes and edges are created, assigning new nodeID and edgeID identifiers.
        
    Parameters
    ----------
    place: string
        name of cities or areas in OSM: when using "OSMpolygon" please provide the name of a "relation" in OSM as an argument of "place"; when using "distance_from_address"
        provide an existing OSM address; when using "OSMplace" provide an OSM place name
    download_method: string {"polygon", "distance_from_address", "OSMplace"}
        it indicates the method that should be used for downloading the data. When 'polygon' the shape to get network data within. coordinates should be in
        unprojected latitude-longitude degrees (EPSG:4326).
    epsg: int
        epsg of the area considered; if None OSMNx is used for the projection
    distance: float
        it is used only if download_method == "distance from address"
        
    Returns
    -------
    nodes_gdf, edges_gdf: Tuple of GeoDataFrames
        the nodes and edges GeoDataFrames
    """

    crs = 'EPSG:' + str(epsg)
    tag = {'railway': True}

    if download_method == 'distance_from_address':
        railways_gdf = ox.geometries_from_address(place,
                                                  tags=tag,
                                                  dist=distance)
    elif download_method == 'OSMplace':
        railways_gdf = ox.geometries_from_place(place, tags=tag)
    else:
        railways_gdf = ox.geometries_from_polygon(place, tags=tag)

    print(railways_gdf.columns)
    railways_gdf = railways_gdf.to_crs(crs)
    to_keep = ["rail", "light_rail", "subway"]
    railways_gdf = railways_gdf[railways_gdf.railway.isin(to_keep)]
    railways_gdf['type'] = railways_gdf['railway']

    railways_gdf['length'] = railways_gdf.geometry.length
    railways_gdf = railways_gdf[[
        "geometry", "length", "name", "type", "bridge", "tunnel"
    ]]
    railways_gdf.reset_index(inplace=True, drop=True)
    railways_gdf['edgeID'] = railways_gdf.index.values.astype(int)
    railways_gdf.index.name = None
    return railways_gdf
Ejemplo n.º 5
0
def osm_gdf_from_address(address, tags, dist=1000):
    """Create GeoDataFrame of OSM entities within some distance N, S, E, W of address.

    Args:
        address (str): The address to geocode and use as the central point around which to get the geometries.
        tags (dict): Dict of tags used for finding objects in the selected area. Results returned are the union, not intersection of each individual tag. Each result matches at least one given tag. The dict keys should be OSM tags, (e.g., building, landuse, highway, etc) and the dict values should be either True to retrieve all items with the given tag, or a string to get a single tag-value combination, or a list of strings to get multiple values for the given tag. For example, tags = {‘building’: True} would return all building footprints in the area. tags = {‘amenity’:True, ‘landuse’:[‘retail’,’commercial’], ‘highway’:’bus_stop’} would return all amenities, landuse=retail, landuse=commercial, and highway=bus_stop.
        dist (int, optional): Distance in meters. Defaults to 1000.

    Returns:
        GeoDataFrame: A GeoDataFrame of OSM entities.
    """
    check_package("osmnx",
                  "https://osmnx.readthedocs.io/en/stable/#installation")
    import osmnx as ox

    gdf = ox.geometries_from_address(address, tags, dist)
    return gdf
Ejemplo n.º 6
0
def get_historical_buildings_fromOSM(place,
                                     download_method,
                                     epsg=None,
                                     distance=1000):
    """    
    The function downloads and cleans buildings footprint geometries and create a buildings GeoDataFrames for the area of interest.
    The function exploits OSMNx functions for downloading the data as well as for projecting it.
    The land use classification for each building is extracted. Only relevant columns are kept.   
            
    Parameters
    ----------
    place: string, tuple
        name of cities or areas in OSM: when using "from point" please provide a (lat, lon) tuple to create the bounding box around it; when using "distance_from_address"
        provide an existing OSM address; when using "OSMplace" provide an OSM place name
    download_method: string, {"from_point", "distance_from_address", "OSMplace"}
        it indicates the method that should be used for downloading the data.
    epsg: int
        epsg of the area considered; if None OSMNx is used for the projection
    distance: float
        Specify distance from address or point
    
    Returns
    -------
    GeoDataFrames
    """

    columns = ['geometry', 'historic']

    if download_method == "distance_from_address":
        historic_buildings = ox.geometries_from_address(
            address=place, dist=distance, tags={"building": True})
    elif download_method == "OSMplace":
        historic_buildings = ox.geometries_from_place(place,
                                                      tags={"building": True})
    elif download_method == "from_point":
        historic_buildings = ox.geometries_from_point(center_point=place,
                                                      dist=distance,
                                                      tags={"building": True})
    else:
        raise downloadError(
            'Provide a download method amongst {"from_point", "distance_from_address", "OSMplace"}'
        )

    if 'heritage' in historic_buildings:
        columns.append('heritage')
    historic_buildings = historic_buildings[columns]

    if 'heritage' in historic_buildings:
        historic_buildings = historic_buildings[~(
            historic_buildings.historic.isnull()
            & historic_buildings.heritage.isnull())]
    else:
        historic_buildings = historic_buildings[~historic_buildings.historic.
                                                isnull()]

    if epsg is None:
        historic_buildings = ox.projection.project_gdf(historic_buildings)
    else:
        crs = 'EPSG:' + str(epsg)
        historic_buildings = historic_buildings.to_crs(crs)

    historic_buildings["historic"] = 1
    historic_buildings["historic"][historic_buildings["historic"] != 0] = 1
    historic_buildings = historic_buildings[['geometry', 'historic']]
    historic_buildings['area'] = historic_buildings.geometry.area

    return historic_buildings
Ejemplo n.º 7
0
def get_buildings_fromOSM(place, download_method, epsg=None, distance=1000):
    """    
    The function downloads and cleans buildings footprint geometries and create a buildings GeoDataFrames for the area of interest.
    The function exploits OSMNx functions for downloading the data as well as for projecting it.
    The land use classification for each building is extracted. Only relevant columns are kept.   
            
    Parameters
    ----------
    place: string, tuple
        name of cities or areas in OSM: when using "from point" please provide a (lat, lon) tuple to create the bounding box around it; when using "distance_from_address"
        provide an existing OSM address; when using "OSMplace" provide an OSM place name
    download_method: string, {"from_point", "distance_from_address", "OSMplace"}
        it indicates the method that should be used for downloading the data.
    epsg: int
        epsg of the area considered; if None OSMNx is used for the projection
    distance: float
        Specify distance from address or point
    
    Returns
    -------
    buildings_gdf: Polygon GeoDataFrame
        the buildings GeoDataFrame
    """

    columns_to_keep = [
        'amenity', 'building', 'geometry', 'historic', 'land_use_raw'
    ]

    if download_method == "distance_from_address":
        buildings_gdf = ox.geometries_from_address(address=place,
                                                   dist=distance,
                                                   tags={"building": True})
    elif download_method == "OSMplace":
        buildings_gdf = ox.geometries_from_place(place,
                                                 tags={"building": True})
    elif download_method == "from_point":
        buildings_gdf = ox.geometries_from_point(center_point=place,
                                                 dist=distance,
                                                 tags={"building": True})
    elif download_method == "OSMpolygon":
        buildings_gdf = ox.geometries_from_polygon(place,
                                                   tags={"building": True})
    else:
        raise downloadError(
            'Provide a download method amongst {"from_point", "distance_from_address", "OSMplace", "OSMpolygon}'
        )

    if epsg is None:
        buildings_gdf = ox.projection.project_gdf(buildings_gdf)
    else:
        crs = 'EPSG:' + str(epsg)
        buildings_gdf = buildings_gdf.to_crs(crs)

    buildings_gdf['land_use_raw'] = None
    for column in buildings_gdf.columns:
        if column.startswith('building:use:'):
            buildings_gdf.loc[pd.notnull(buildings_gdf[column]),
                              'land_use_raw'] = column[13:]
        if column not in columns_to_keep:
            buildings_gdf.drop(column, axis=1, inplace=True)

    buildings_gdf = buildings_gdf[~buildings_gdf['geometry'].is_empty]
    buildings_gdf['building'].replace('yes', np.nan, inplace=True)
    buildings_gdf['building'][
        buildings_gdf['building'].isnull()] = buildings_gdf['amenity']
    buildings_gdf['land_use_raw'][
        buildings_gdf['land_use_raw'].isnull()] = buildings_gdf['building']
    buildings_gdf['land_use_raw'][
        buildings_gdf['land_use_raw'].isnull()] = 'residential'

    buildings_gdf = buildings_gdf[['geometry', 'historic', 'land_use_raw']]
    buildings_gdf['area'] = buildings_gdf.geometry.area
    buildings_gdf = buildings_gdf[buildings_gdf['area'] >= 50]

    # reset index
    buildings_gdf = buildings_gdf.reset_index(drop=True)
    buildings_gdf['buildingID'] = buildings_gdf.index.values.astype('int')

    return buildings_gdf