1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
"""
Relation operations for the OpenStreetMap API.
This module provides pythonic (snake_case) methods for working with OSM relations.
"""
from typing import Any, Optional, TYPE_CHECKING, cast
from xml.dom.minidom import Element
from . import dom, parser
if TYPE_CHECKING:
from .OsmApi import OsmApi
class RelationMixin:
"""Mixin providing relation-related operations with pythonic method names."""
def relation_get(
self: "OsmApi", relation_id: int, relation_version: int = -1
) -> dict[str, Any]:
"""
Returns relation with `relation_id` as a dict.
If `relation_version` is supplied, this specific version is returned,
otherwise the latest version is returned.
If the requested element has been deleted,
`OsmApi.ElementDeletedApiError` is raised.
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
uri = f"/api/0.6/relation/{relation_id}"
if relation_version != -1:
uri += f"/{relation_version}"
data = self._session._get(uri)
relation = cast(
Element, dom.OsmResponseToDom(data, tag="relation", single=True)
)
return dom.dom_parse_relation(relation)
def relation_create(
self: "OsmApi", relation_data: dict[str, Any]
) -> Optional[dict[str, Any]]:
"""
Creates a relation based on the supplied `relation_data` dict.
If no authentication information are provided,
`OsmApi.UsernamePasswordMissingError` is raised.
If the supplied information contain an existing relation,
`OsmApi.OsmTypeAlreadyExistsError` is raised.
If there is no open changeset,
`OsmApi.NoChangesetOpenError` is raised.
If the changeset is already closed,
`OsmApi.ChangesetClosedApiError` is raised.
"""
return self._do("create", "relation", relation_data)
def relation_update(
self: "OsmApi", relation_data: dict[str, Any]
) -> Optional[dict[str, Any]]:
"""
Updates relation with the supplied `relation_data` dict.
If no authentication information are provided,
`OsmApi.UsernamePasswordMissingError` is raised.
If there is no open changeset,
`OsmApi.NoChangesetOpenError` is raised.
If the changeset is already closed,
`OsmApi.ChangesetClosedApiError` is raised.
"""
return self._do("modify", "relation", relation_data)
def relation_delete(
self: "OsmApi", relation_data: dict[str, Any]
) -> Optional[dict[str, Any]]:
"""
Delete relation with `relation_data`.
If no authentication information are provided,
`OsmApi.UsernamePasswordMissingError` is raised.
If there is no open changeset,
`OsmApi.NoChangesetOpenError` is raised.
If the changeset is already closed,
`OsmApi.ChangesetClosedApiError` is raised.
"""
return self._do("delete", "relation", relation_data)
def relation_history(self: "OsmApi", relation_id: int) -> dict[int, dict[str, Any]]:
"""
Returns dict with version as key.
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
uri = f"/api/0.6/relation/{relation_id}/history"
data = self._session._get(uri)
relations = cast(list[Element], dom.OsmResponseToDom(data, tag="relation"))
result: dict[int, dict[str, Any]] = {}
for relation in relations:
relation_data = dom.dom_parse_relation(relation)
result[relation_data["version"]] = relation_data
return result
def relation_relations(self: "OsmApi", relation_id: int) -> list[dict[str, Any]]:
"""
Returns a list of dicts of relation data containing relation `relation_id`.
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
uri = f"/api/0.6/relation/{relation_id}/relations"
data = self._session._get(uri)
relations = cast(
list[Element], dom.OsmResponseToDom(data, tag="relation", allow_empty=True)
)
result: list[dict[str, Any]] = []
for relation in relations:
relation_data = dom.dom_parse_relation(relation)
result.append(relation_data)
return result
def relation_full_recur(self: "OsmApi", relation_id: int) -> list[dict[str, Any]]:
"""
Returns the full data (all levels) for relation `relation_id` as list of dicts.
This function is useful for relations containing other relations.
If you don't need all levels, use `relation_full` instead,
which return only 2 levels.
If any relation (on any level) has been deleted,
`OsmApi.ElementDeletedApiError` is raised.
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
data = []
todo = [relation_id]
done = []
while todo:
rid = todo.pop(0)
done.append(rid)
temp = self.relation_full(rid)
for item in temp:
if item["type"] != "relation":
continue
if item["data"]["id"] in done:
continue
todo.append(item["data"]["id"])
data += temp
return data
def relation_full(self: "OsmApi", relation_id: int) -> list[dict[str, Any]]:
"""
Returns the full data (two levels) for relation `relation_id` as list of dicts.
If you need all levels, use `relation_full_recur`.
If the requested element has been deleted,
`OsmApi.ElementDeletedApiError` is raised.
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
uri = f"/api/0.6/relation/{relation_id}/full"
data = self._session._get(uri)
return parser.parse_osm(data)
def relations_get(
self: "OsmApi", relation_id_list: list[int]
) -> dict[int, dict[str, Any]]:
"""
Returns dict with the id of the relation as a key
for each relation in `relation_id_list`.
`relation_id_list` is a list containing unique identifiers
for multiple relations.
"""
relation_list = ",".join([str(x) for x in relation_id_list])
uri = f"/api/0.6/relations?relations={relation_list}"
data = self._session._get(uri)
relations = cast(list[Element], dom.OsmResponseToDom(data, tag="relation"))
result: dict[int, dict[str, Any]] = {}
for relation in relations:
relation_data = dom.dom_parse_relation(relation)
result[relation_data["id"]] = relation_data
return result
|