Utils Module

urbanpy.utils.create_duration_labels(durations)[source]

Creates inputs for pd.cut function (bins and labels) especifically for the trip durations columns.

Parameters:

durations (Pandas Series) – Series containing trip durations in minutes.

Returns:

  • custom_bins (list) – List of numbers with the inputs for the bins parameter of pd.cut function

  • custom_labels (list) – List of numbers with the inputs for the labels parameter of pd.cut function

urbanpy.utils.geo_boundary_to_polygon(x)[source]

Transform h3 geo boundary to shapely Polygon

Parameters:

x (str) – H3 hexagon index

Returns:

polygon – Polygon representing H3 hexagon area

Return type:

Polygon

urbanpy.utils.get_hdx_label(name)[source]

Get a human readable label from a HDX Facebook population density dataset filename.

Parameters:

name (str.) – HDX Facebook population density dataset filename.

Returns:

  • labels str (GeoDataFrame) – POIs from the selected type of facility.

  • df (DataFrame. Optional) – Relations metadata such as ID and tags. Returns None if ‘type_of_data’ other than ‘relation’.

Build a BallTree for nearest neighbor search based on selected distance.

Parameters:
  • tree_features (array_like) – Input features to create the search tree. Features are in lat, lon format.

  • query_features (array_like) – Points to which calculate the nearest neighbor within the tree. lat, lon coordinates expected.

  • metric (str) – Distance metric for neighorhood search. Default haversine for latlon coordinates. If haversine is selected lat, lon coordinates are converted to radians.

Returns:

  • dist (array_like) – Array with the corresponding distance in km (if haversine then distance * earth radius)

  • ind (array_like) – Array with the corresponding index of the tree_features values.

Examples

>>> x = np.random.uniform(-78,-79, 20)
>>> y = np.random.uniform(-12, -11, 20)
>>> features = np.vstack([x,y]).T
>>> query = [[-77,-12]]
>>> urbanpy.utils.nn_search(features, query)
urbanpy.utils.overpass_to_gdf(type_of_data: str, data: dict, mask: GeoDataFrame | GeoSeries | Polygon | MultiPolygon | None = None, ov_keys: list | None = None) Tuple[GeoDataFrame, DataFrame | None][source]

Process overpass response data according to type_of_data and clip to mask if given.

Parameters:
  • type_of_data (str. One of {'node', 'way', 'relation', 'rel'}) – OSM Data structure to be queried from Overpass API.

  • data (dict) – Response object’s json result from Overpass API.

  • mask (dict. Optional) – Dict contaning OSM tag filters. Dict keys can take OSM tags and Dict values can be list of strings, str or None. Example: { ‘key0’: [‘v0a’, ‘v0b’, ‘v0c’], ‘key1’: ‘v1’, ‘key2’: None } Check keys [OSM Map Features](https://wiki.openstreetmap.org/wiki/Map_features).

  • ov_keys (list. Optional) – Unique OSM keys used in Overpass query (e.g. “amenity”, “shop”, etc) to fill “poi_type” df column.

Returns:

  • gdf (GeoDataFrame) – POIs from the selected type of facility.

  • df (DataFrame. Optional) – Relations metadata such as ID and tags. Returns None if ‘type_of_data’ other than ‘relation’.

urbanpy.utils.shell_from_geometry(geometry)[source]

Util function for park and pitch processing.

urbanpy.utils.swap_xy(geom)[source]

Util function in case an x,y coordinate needs to be switched

Parameters:

geom (GeoSeries) – Input series containing the geometries needing a coordinate swap

Returns:

  • shell (list) – List containing the exterior borders of the geometry

  • holes (list) – Array of all holes within a geometry. Only for Polygon and Multipolygon

  • coords (list) – List of geomerty type with the swapped x,y coordinates

Examples

>>> p = Point(-77,-12)
>>> p_ = urbanpy.utils.swap_xy(p)
>>> print((p.x, p.y), (p_.x, p_.y))
(-77.0, -12.0) (-12.0, -77.0)
urbanpy.utils.to_overpass_query(type_of_data: str, query: dict) str[source]

Parse query dict to build Overpass QL query.

Parameters:
  • type_of_data (str) – OSM type of data (One of: relation, node, way).

  • query (dict) – OSM keys and values to be queried

Returns:

overpass_query – Formatted query string to be passed to Overpass QL.

Return type:

str