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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
"""
Node operations for the OpenStreetMap API.
"""
from typing import Any, Optional, TYPE_CHECKING, cast
from xml.dom.minidom import Element
from . import dom
if TYPE_CHECKING:
from .OsmApi import OsmApi
class NodeMixin:
"""Mixin providing node-related operations with pythonic method names."""
def node_get(
self: "OsmApi", node_id: int, node_version: int = -1
) -> dict[str, Any]:
"""
Returns node with `node_id` as a dict:
#!python
{
'id': id of node,
'lat': latitude of node,
'lon': longitude of node,
'tag': {},
'changeset': id of changeset of last change,
'version': version number of node,
'user': username of user that made the last change,
'uid': id of user that made the last change,
'timestamp': timestamp of last change,
'visible': True|False
}
If `node_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/node/{node_id}"
if node_version != -1:
uri += f"/{node_version}"
data = self._session._get(uri)
node_element = cast(
Element, dom.OsmResponseToDom(data, tag="node", single=True)
)
return dom.dom_parse_node(node_element)
def node_create(
self: "OsmApi", node_data: dict[str, Any]
) -> Optional[dict[str, Any]]:
"""
Creates a node based on the supplied `node_data` dict:
#!python
{
'lat': latitude of node,
'lon': longitude of node,
'tag': {},
}
Returns updated `node_data` (without timestamp):
#!python
{
'id': id of node,
'lat': latitude of node,
'lon': longitude of node,
'tag': dict of tags,
'changeset': id of changeset of last change,
'version': version number of node,
'user': username of last change,
'uid': id of user of last change,
'visible': True|False
}
If no authentication information are provided,
`OsmApi.UsernamePasswordMissingError` is raised.
If there is no open changeset,
`OsmApi.NoChangesetOpenError` is raised.
If the supplied information contain an existing node,
`OsmApi.OsmTypeAlreadyExistsError` is raised.
If the changeset is already closed,
`OsmApi.ChangesetClosedApiError` is raised.
"""
return self._do("create", "node", node_data)
def node_update(
self: "OsmApi", node_data: dict[str, Any]
) -> Optional[dict[str, Any]]:
"""
Updates node with the supplied `node_data` dict:
#!python
{
'id': id of node,
'lat': latitude of node,
'lon': longitude of node,
'tag': {},
'version': version number of node,
}
Returns updated `node_data` (without timestamp):
#!python
{
'id': id of node,
'lat': latitude of node,
'lon': longitude of node,
'tag': dict of tags,
'changeset': id of changeset of last change,
'version': version number of node,
'user': username of last change,
'uid': id of user of last change,
'visible': True|False
}
If no authentication information are provided,
`OsmApi.UsernamePasswordMissingError` is raised.
If there is no open changeset,
`OsmApi.NoChangesetOpenError` is raised.
If there is already an open changeset,
`OsmApi.ChangesetAlreadyOpenError` is raised.
If the changeset is already closed,
`OsmApi.ChangesetClosedApiError` is raised.
"""
return self._do("modify", "node", node_data)
def node_delete(
self: "OsmApi", node_data: dict[str, Any]
) -> Optional[dict[str, Any]]:
"""
Delete node with `node_data`:
#!python
{
'id': id of node,
'lat': latitude of node,
'lon': longitude of node,
'tag': dict of tags,
'version': version number of node,
}
Returns updated `node_data` (without timestamp):
#!python
{
'id': id of node,
'lat': latitude of node,
'lon': longitude of node,
'tag': dict of tags,
'changeset': id of changeset of last change,
'version': version number of node,
'user': username of last change,
'uid': id of user of last change,
'visible': True|False
}
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", "node", node_data)
def node_history(self: "OsmApi", node_id: int) -> dict[int, dict[str, Any]]:
"""
Returns dict with version as key:
#!python
{
1: dict of node version 1,
2: dict of node version 2,
...
}
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
uri = f"/api/0.6/node/{node_id}/history"
data = self._session._get(uri)
node_list = cast(list[Element], dom.OsmResponseToDom(data, tag="node"))
result = {}
for node in node_list:
node_data = dom.dom_parse_node(node)
result[node_data["version"]] = node_data
return result
def node_ways(self: "OsmApi", node_id: int) -> list[dict[str, Any]]:
"""
Returns list of dicts of ways that use the node with `node_id`:
#!python
[
{
'id': id of way,
'nd': list of node ids,
'tag': dict of tags,
'changeset': id of changeset of last change,
'version': version number of way,
'user': username of user that made the last change,
'uid': id of user that made the last change,
'timestamp': timestamp of last change,
'visible': True|False
},
...
]
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
uri = f"/api/0.6/node/{node_id}/ways"
data = self._session._get(uri)
way_list = cast(
list[Element], dom.OsmResponseToDom(data, tag="way", allow_empty=True)
)
return [dom.dom_parse_way(way) for way in way_list]
def node_relations(self: "OsmApi", node_id: int) -> list[dict[str, Any]]:
"""
Returns list of dicts of relations that use the node with `node_id`:
#!python
[
{
'id': id of relation,
'member': [
{
'ref': reference id,
'role': role,
'type': node|way|relation
},
...
],
'tag': dict of tags,
'changeset': id of changeset of last change,
'version': version number of relation,
'user': username of user that made the last change,
'uid': id of user that made the last change,
'timestamp': timestamp of last change,
'visible': True|False
},
...
]
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
uri = f"/api/0.6/node/{node_id}/relations"
data = self._session._get(uri)
relation_list = cast(
list[Element], dom.OsmResponseToDom(data, tag="relation", allow_empty=True)
)
return [dom.dom_parse_relation(rel) for rel in relation_list]
def nodes_get(self: "OsmApi", node_id_list: list[int]) -> dict[int, dict[str, Any]]:
"""
Returns dict with id as key:
#!python
{
node_id: dict of node,
...
}
If the requested element can not be found,
`OsmApi.ElementNotFoundApiError` is raised.
"""
nodes = ",".join([str(x) for x in node_id_list])
uri = f"/api/0.6/nodes?nodes={nodes}"
data = self._session._get(uri)
node_list = cast(list[Element], dom.OsmResponseToDom(data, tag="node"))
result = {}
for node in node_list:
node_data = dom.dom_parse_node(node)
result[node_data["id"]] = node_data
return result
|