Skip to content

Index

Filters.

merge_osm_tags_filter(osm_tags_filter)

Merge OSM tags filter into OsmTagsFilter type.

Optionally merges GroupedOsmTagsFilter into OsmTagsFilter to allow loaders to load all defined groups during single operation.

PARAMETER DESCRIPTION
osm_tags_filter

OSM tags filter definition.

TYPE: Union[OsmTagsFilter, GroupedOsmTagsFilter, Iterable[OsmTagsFilter], Iterable[GroupedOsmTagsFilter]]

RAISES DESCRIPTION
AttributeError

When provided tags don't match both OsmTagsFilter or GroupedOsmTagsFilter.

RETURNS DESCRIPTION
OsmTagsFilter

Merged filters.

TYPE: OsmTagsFilter

Source code in srai/loaders/osm_loaders/filters/_typing.py
def merge_osm_tags_filter(
    osm_tags_filter: Union[
        OsmTagsFilter, GroupedOsmTagsFilter, Iterable[OsmTagsFilter], Iterable[GroupedOsmTagsFilter]
    ],
) -> OsmTagsFilter:
    """
    Merge OSM tags filter into `OsmTagsFilter` type.

    Optionally merges `GroupedOsmTagsFilter` into `OsmTagsFilter` to allow loaders to load all
    defined groups during single operation.

    Args:
        osm_tags_filter: OSM tags filter definition.

    Raises:
        AttributeError: When provided tags don't match both
            `OsmTagsFilter` or `GroupedOsmTagsFilter`.

    Returns:
        OsmTagsFilter: Merged filters.
    """
    if is_expected_type(osm_tags_filter, OsmTagsFilter):
        return cast(OsmTagsFilter, osm_tags_filter)
    elif is_expected_type(osm_tags_filter, GroupedOsmTagsFilter):
        return _merge_grouped_osm_tags_filter(cast(GroupedOsmTagsFilter, osm_tags_filter))
    elif is_expected_type(osm_tags_filter, Iterable):
        return _merge_multiple_osm_tags_filters(
            [
                merge_osm_tags_filter(
                    cast(Union[OsmTagsFilter, GroupedOsmTagsFilter], sub_osm_tags_filter)
                )
                for sub_osm_tags_filter in osm_tags_filter
            ]
        )

    raise AttributeError(
        "Provided tags don't match required type definitions"
        " (OsmTagsFilter or GroupedOsmTagsFilter)."
    )

Download the OSM's most popular tags from taginfo api.

This is a wrapper around the popular taginfo api endpoint [1]. It queries the API, and optionally filters the results according to argument values.

PARAMETER DESCRIPTION
in_wiki_only

If True, only return results tags that have at least one wiki page. Defaults to False.

TYPE: bool DEFAULT: False

min_count(int,

Minimum number of objects in OSM with this tag attached, to include the tag in the results. Defaults to 0 (no filtering).

TYPE: optional

min_fraction(float,

What fraction of all objects have to have this tag attached, to include it in the results. Defaults to 0.0 (no filtering).

TYPE: optional

RETURNS DESCRIPTION
OsmTagsFilter

Dict[str, List[str]]: dictionary containing the downloaded popular tags.

References
  1. https://taginfo.openstreetmap.org/taginfo/apidoc#api_4_tags_popular
Source code in srai/loaders/osm_loaders/filters/popular.py
def get_popular_tags(
    in_wiki_only: bool = False, min_count: int = 0, min_fraction: float = 0.0
) -> OsmTagsFilter:
    """
    Download the OSM's most popular tags from taginfo api.

    This is a wrapper around the `popular` taginfo api endpoint [1].
    It queries the API, and optionally filters the results
    according to argument values.

    Args:
        in_wiki_only (bool, optional): If True, only return results tags
            that have at least one wiki page. Defaults to False.
        min_count(int, optional): Minimum number of objects in OSM with this tag attached,
            to include the tag in the results. Defaults to 0 (no filtering).
        min_fraction(float, optional): What fraction of all objects have to have this tag attached,
            to include it in the results. Defaults to 0.0 (no filtering).

    Returns:
        Dict[str, List[str]]: dictionary containing the downloaded popular tags.

    References:
        1. https://taginfo.openstreetmap.org/taginfo/apidoc#api_4_tags_popular
    """
    taginfo_api_response = requests.get(
        _TAGINFO_API_TAGS,
        headers={"User-Agent": "SRAI Python package (https://github.com/kraina-ai/srai)"},
    )
    taginfo_api_response.raise_for_status()
    taginfo_data = taginfo_api_response.json()["data"]
    return _parse_taginfo_response(taginfo_data, in_wiki_only, min_count, min_fraction)