-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy path__init__.py
1663 lines (1369 loc) · 55.2 KB
/
__init__.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime
from decimal import Decimal
from functools import partial
from urllib.request import urlopen
from urllib.error import HTTPError
from xml.sax import handler, make_parser
import xml.etree.ElementTree
import json
import re
import time
from typing import Any, Callable, ClassVar, Dict, List, NoReturn, Optional, Tuple, Type, TypeVar, Union
from overpy import exception
# Ignore flake8 F401 warning for unused vars
from overpy.__about__ import ( # noqa: F401
__author__, __copyright__, __email__, __license__, __summary__, __title__,
__uri__, __version__
)
ElementTypeVar = TypeVar("ElementTypeVar", bound="Element")
XML_PARSER_DOM = 1
XML_PARSER_SAX = 2
# Try to convert some common attributes
# http://wiki.openstreetmap.org/wiki/Elements#Common_attributes
GLOBAL_ATTRIBUTE_MODIFIERS: Dict[str, Callable] = {
"changeset": int,
"timestamp": lambda ts: datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ"),
"uid": int,
"version": int,
"visible": lambda v: v.lower() == "true"
}
GLOBAL_ATTRIBUTE_SERIALIZERS: Dict[str, Callable] = {
"timestamp": lambda dt: datetime.strftime(dt, "%Y-%m-%dT%H:%M:%SZ"),
}
def _attributes_to_json(attributes: dict):
def attr_serializer(k):
return GLOBAL_ATTRIBUTE_SERIALIZERS.get(k, lambda v: v)
return {k: attr_serializer(k)(v) for k, v in attributes.items()}
def is_valid_type(
element: Union["Area", "Node", "Relation", "Way"],
cls: Type[Union["Area", "Element", "Node", "Relation", "Way"]]) -> bool:
"""
Test if an element is of a given type.
:param element: The element instance to test
:param cls: The element class to test
:return: False or True
"""
return isinstance(element, cls) and element.id is not None
class Overpass:
"""
Class to access the Overpass API
:param read_chunk_size: Max size of each chunk read from the server response
:param url: Optional URL of the Overpass server. Defaults to http://overpass-api.de/api/interpreter
:param xml_parser: The xml parser to use
:param max_retry_count: Max number of retries (Default: default_max_retry_count)
:param retry_timeout: Time to wait between tries (Default: default_retry_timeout)
"""
#: Global max number of retries (Default: 0)
default_max_retry_count: ClassVar[int] = 0
#: Max size of each chunk read from the server response
default_read_chunk_size: ClassVar[int] = 4096
#: Global time to wait between tries (Default: 1.0s)
default_retry_timeout: ClassVar[float] = 1.0
#: Default URL of the Overpass server
default_url: ClassVar[str] = "http://overpass-api.de/api/interpreter"
def __init__(
self,
read_chunk_size: Optional[int] = None,
url: Optional[str] = None,
xml_parser: int = XML_PARSER_SAX,
max_retry_count: int = None,
retry_timeout: float = None):
#: URL to use for this instance
self.url = self.default_url
if url is not None:
self.url = url
self._regex_extract_error_msg = re.compile(br"\<p\>(?P<msg>\<strong\s.*?)\</p\>")
self._regex_remove_tag = re.compile(b"<[^>]*?>")
if read_chunk_size is None:
read_chunk_size = self.default_read_chunk_size
#: The chunk size for this instance
self.read_chunk_size = read_chunk_size
if max_retry_count is None:
max_retry_count = self.default_max_retry_count
#: Max retry count for this instance
self.max_retry_count = max_retry_count
if retry_timeout is None:
retry_timeout = self.default_retry_timeout
#: The retry timeout for this instance
self.retry_timeout = retry_timeout
#: The XML parser to use for this instance
self.xml_parser = xml_parser
@staticmethod
def _handle_remark_msg(msg: str) -> NoReturn:
"""
Try to parse the message provided with the remark tag or element.
:param msg: The message
:raises overpy.exception.OverpassRuntimeError: If message starts with 'runtime error:'
:raises overpy.exception.OverpassRuntimeRemark: If message starts with 'runtime remark:'
:raises overpy.exception.OverpassUnknownError: If we are unable to identify the error
"""
msg = msg.strip()
if msg.startswith("runtime error:"):
raise exception.OverpassRuntimeError(msg=msg)
elif msg.startswith("runtime remark:"):
raise exception.OverpassRuntimeRemark(msg=msg)
raise exception.OverpassUnknownError(msg=msg)
def query(self, query: Union[bytes, str]) -> "Result":
"""
Query the Overpass API
:param query: The query string in Overpass QL
:return: The parsed result
"""
if not isinstance(query, bytes):
query = query.encode("utf-8")
retry_exceptions: List[exception.OverPyException] = []
for run in range(self.max_retry_count + 1):
if run:
time.sleep(self.retry_timeout)
response = b""
try:
with urlopen(self.url, query) as f:
f_read = partial(f.read, self.read_chunk_size)
for data in iter(f_read, b""):
response += data
except HTTPError as exc:
f = exc
current_exception: exception.OverPyException
if f.code == 200:
content_type = f.getheader("Content-Type")
if content_type == "application/json":
return self.parse_json(response)
elif content_type == "application/osm3s+xml":
return self.parse_xml(response)
else:
current_exception = exception.OverpassUnknownContentType(content_type)
elif f.code == 400:
msgs: List[str] = []
for msg_raw in self._regex_extract_error_msg.finditer(response):
msg_clean_bytes = self._regex_remove_tag.sub(b"", msg_raw.group("msg"))
try:
msg = msg_clean_bytes.decode("utf-8")
except UnicodeDecodeError:
msg = repr(msg_clean_bytes)
msgs.append(msg)
current_exception = exception.OverpassBadRequest(query, msgs=msgs)
elif f.code == 429:
current_exception = exception.OverpassTooManyRequests()
elif f.code == 504:
current_exception = exception.OverpassGatewayTimeout()
else:
current_exception = exception.OverpassUnknownHTTPStatusCode(f.code)
if not self.max_retry_count:
raise current_exception
retry_exceptions.append(current_exception)
raise exception.MaxRetriesReached(retry_count=run + 1, exceptions=retry_exceptions)
def parse_json(
self,
data: Union[bytes, str],
encoding: str = "utf-8"
) -> "Result":
"""
Parse raw response from Overpass service.
:param data: Raw JSON Data
:param encoding: Encoding to decode byte string
:return: Result object
"""
if isinstance(data, bytes):
data = data.decode(encoding)
data_parsed: dict = json.loads(data, parse_float=Decimal)
if "remark" in data_parsed:
self._handle_remark_msg(msg=data_parsed.get("remark"))
return Result.from_json(data_parsed, api=self)
def parse_xml(
self,
data: Union[bytes, str],
encoding: str = "utf-8",
parser: Optional[int] = None
) -> "Result":
"""
:param data: Raw XML Data
:param encoding: Encoding to decode byte string
:param parser: The XML parser to use
:return: Result object
"""
if parser is None:
parser = self.xml_parser
if isinstance(data, bytes):
data = data.decode(encoding)
m = re.compile("<remark>(?P<msg>[^<>]*)</remark>").search(data)
if m:
self._handle_remark_msg(m.group("msg"))
return Result.from_xml(data, api=self, parser=parser)
class Result:
"""
Class to handle the result.
:param elements: List of elements to initialize the result with
:param api: The API object to load additional resources and elements
"""
def __init__(
self,
elements: Optional[List[Union["Area", "Node", "Relation", "Way"]]] = None,
api: Optional[Overpass] = None):
if elements is None:
elements = []
self._areas: Dict[int, Union["Area", "Node", "Relation", "Way"]] = {
element.id: element for element in elements if is_valid_type(element, Area)
}
self._nodes = {
element.id: element for element in elements if is_valid_type(element, Node)
}
self._ways = {
element.id: element for element in elements if is_valid_type(element, Way)
}
self._relations = {
element.id: element for element in elements if is_valid_type(element, Relation)
}
self._class_collection_map: Dict[Any, Any] = {
Node: self._nodes,
Way: self._ways,
Relation: self._relations,
Area: self._areas
}
#: The API to use if we need to resolve additional information
self.api: Optional[Overpass] = api
def expand(self, other: "Result"):
"""
Add all elements from another result to the list of elements of this result object.
It is used by the auto resolve feature.
:param other: Expand the result with the elements from this result.
:raises ValueError: If provided parameter is not instance of :class:`overpy.Result`
"""
if not isinstance(other, Result):
raise ValueError("Provided argument has to be instance of overpy:Result()")
other_collection_map: Dict[Type["Element"], List[Union["Area", "Node", "Relation", "Way"]]] = {
Area: other.areas,
Node: other.nodes,
Relation: other.relations,
Way: other.ways
}
for element_type, own_collection in self._class_collection_map.items():
for element in other_collection_map[element_type]:
if is_valid_type(element, element_type) and element.id not in own_collection:
own_collection[element.id] = element
def append(self, element: Union["Area", "Node", "Relation", "Way"]):
"""
Append a new element to the result.
:param element: The element to append
"""
if is_valid_type(element, Element):
self._class_collection_map[element.__class__].setdefault(element.id, element)
def get_elements(
self,
filter_cls: Type[ElementTypeVar],
elem_id: Optional[int] = None) -> List[ElementTypeVar]:
"""
Get a list of elements from the result and filter the element type by a class.
:param filter_cls:
:param elem_id: ID of the object
:return: List of available elements
"""
result: List[ElementTypeVar] = []
if elem_id is not None:
try:
result = [self._class_collection_map[filter_cls][elem_id]]
except KeyError:
result = []
else:
for e in self._class_collection_map[filter_cls].values():
result.append(e)
return result
def get_ids(
self,
filter_cls: Type[Union["Area", "Node", "Relation", "Way"]]) -> List[int]:
"""
Get all Element IDs
:param filter_cls: Only IDs of elements with this type
:return: List of IDs
"""
return list(self._class_collection_map[filter_cls].keys())
def get_node_ids(self) -> List[int]:
return self.get_ids(filter_cls=Node)
def get_way_ids(self) -> List[int]:
return self.get_ids(filter_cls=Way)
def get_relation_ids(self) -> List[int]:
return self.get_ids(filter_cls=Relation)
def get_area_ids(self) -> List[int]:
return self.get_ids(filter_cls=Area)
@classmethod
def from_json(cls, data: dict, api: Optional[Overpass] = None) -> "Result":
"""
Create a new instance and load data from json object.
:param data: JSON data returned by the Overpass API
:param api:
:return: New instance of Result object
"""
result = cls(api=api)
elem_cls: Type[Union["Area", "Node", "Relation", "Way"]]
for elem_cls in [Node, Way, Relation, Area]:
for element in data.get("elements", []):
e_type = element.get("type")
if hasattr(e_type, "lower") and e_type.lower() == elem_cls._type_value:
result.append(elem_cls.from_json(element, result=result))
return result
def to_json(self) -> dict:
def elements_to_json():
for elem_cls in [Node, Way, Relation, Area]:
for element in self.get_elements(elem_cls):
yield element.to_json()
return {
"version": 0.6,
"generator": "Overpy Serializer",
"elements": list(elements_to_json())
}
@classmethod
def from_xml(
cls,
data: Union[str, xml.etree.ElementTree.Element],
api: Optional[Overpass] = None,
parser: Optional[int] = None) -> "Result":
"""
Create a new instance and load data from xml data or object.
.. note::
If parser is set to None, the functions tries to find the best parse.
By default the SAX parser is chosen if a string is provided as data.
The parser is set to DOM if an xml.etree.ElementTree.Element is provided as data value.
:param data: Root element
:param api: The instance to query additional information if required.
:param parser: Specify the parser to use(DOM or SAX)(Default: None = autodetect, defaults to SAX)
:return: New instance of Result object
"""
if parser is None:
if isinstance(data, str):
parser = XML_PARSER_SAX
else:
parser = XML_PARSER_DOM
result = cls(api=api)
if parser == XML_PARSER_DOM:
import xml.etree.ElementTree as ET
if isinstance(data, str):
root = ET.fromstring(data)
elif isinstance(data, ET.Element):
root = data
else:
raise exception.OverPyException("Unable to detect data type.")
elem_cls: Type[Union["Area", "Node", "Relation", "Way"]]
for elem_cls in [Node, Way, Relation, Area]:
for child in root:
if child.tag.lower() == elem_cls._type_value:
result.append(elem_cls.from_xml(child, result=result))
elif parser == XML_PARSER_SAX:
from io import StringIO
if not isinstance(data, str):
raise ValueError("data must be of type str if using the SAX parser")
source = StringIO(data)
sax_handler = OSMSAXHandler(result)
sax_parser = make_parser()
sax_parser.setContentHandler(sax_handler)
sax_parser.parse(source)
else:
# ToDo: better exception
raise Exception("Unknown XML parser")
return result
def get_area(self, area_id: int, resolve_missing: bool = False) -> "Area":
"""
Get an area by its ID.
:param area_id: The area ID
:param resolve_missing: Query the Overpass API if the area is missing in the result set.
:return: The area
:raises overpy.exception.DataIncomplete: The requested way is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the area can't be resolved.
"""
areas = self.get_areas(area_id=area_id)
if len(areas) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing area is disabled")
query = ("\n"
"[out:json];\n"
f"area({area_id});\n"
"out body;\n"
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
areas = self.get_areas(area_id=area_id)
if len(areas) == 0:
raise exception.DataIncomplete("Unable to resolve requested areas")
return areas[0]
def get_areas(self, area_id: Optional[int] = None) -> List["Area"]:
"""
Alias for get_elements() but filter the result by Area
:param area_id: The Id of the area
:return: List of elements
"""
return self.get_elements(Area, elem_id=area_id)
def get_node(self, node_id: int, resolve_missing: bool = False) -> "Node":
"""
Get a node by its ID.
:param node_id: The node ID
:param resolve_missing: Query the Overpass API if the node is missing in the result set.
:return: The node
:raises overpy.exception.DataIncomplete: At least one referenced node is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and at least one node can't be resolved.
"""
nodes = self.get_nodes(node_id=node_id)
if len(nodes) == 0:
if not resolve_missing:
raise exception.DataIncomplete("Resolve missing nodes is disabled")
query = ("\n"
"[out:json];\n"
f"node({node_id});\n"
"out body;\n"
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
nodes = self.get_nodes(node_id=node_id)
if len(nodes) == 0:
raise exception.DataIncomplete("Unable to resolve all nodes")
return nodes[0]
def get_nodes(self, node_id: Optional[int] = None) -> List["Node"]:
"""
Alias for get_elements() but filter the result by Node()
:param node_id: The Id of the node
:type node_id: Integer
:return: List of elements
"""
return self.get_elements(Node, elem_id=node_id)
def get_relation(self, rel_id: int, resolve_missing: bool = False) -> "Relation":
"""
Get a relation by its ID.
:param rel_id: The relation ID
:param resolve_missing: Query the Overpass API if the relation is missing in the result set.
:return: The relation
:raises overpy.exception.DataIncomplete: The requested relation is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the relation can't be resolved.
"""
relations = self.get_relations(rel_id=rel_id)
if len(relations) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing relations is disabled")
query = ("\n"
"[out:json];\n"
f"relation({rel_id});\n"
"out body;\n"
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
relations = self.get_relations(rel_id=rel_id)
if len(relations) == 0:
raise exception.DataIncomplete("Unable to resolve requested reference")
return relations[0]
def get_relations(self, rel_id: int = None) -> List["Relation"]:
"""
Alias for get_elements() but filter the result by Relation
:param rel_id: Id of the relation
:return: List of elements
"""
return self.get_elements(Relation, elem_id=rel_id)
def get_way(self, way_id: int, resolve_missing: bool = False) -> "Way":
"""
Get a way by its ID.
:param way_id: The way ID
:param resolve_missing: Query the Overpass API if the way is missing in the result set.
:return: The way
:raises overpy.exception.DataIncomplete: The requested way is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the way can't be resolved.
"""
ways = self.get_ways(way_id=way_id)
if len(ways) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing way is disabled")
query = ("\n"
"[out:json];\n"
f"way({way_id});\n"
"out body;\n"
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
ways = self.get_ways(way_id=way_id)
if len(ways) == 0:
raise exception.DataIncomplete("Unable to resolve requested way")
return ways[0]
def get_ways(self, way_id: Optional[int] = None) -> List["Way"]:
"""
Alias for get_elements() but filter the result by Way
:param way_id: The Id of the way
:return: List of elements
"""
return self.get_elements(Way, elem_id=way_id)
area_ids = property(get_area_ids)
areas = property(get_areas)
node_ids = property(get_node_ids)
nodes = property(get_nodes)
relation_ids = property(get_relation_ids)
relations = property(get_relations)
way_ids = property(get_way_ids)
ways = property(get_ways)
class Element:
"""
Base element
:param attributes: Additional attributes
:param result: The result object this element belongs to
:param tags: List of tags
"""
_type_value: str
def __init__(self, attributes: Optional[dict] = None, result: Optional[Result] = None, tags: Optional[Dict] = None):
self._result = result
self.attributes = attributes
# ToDo: Add option to modify attribute modifiers
attribute_modifiers: Dict[str, Callable] = dict(GLOBAL_ATTRIBUTE_MODIFIERS.items())
for n, m in attribute_modifiers.items():
if n in self.attributes:
self.attributes[n] = m(self.attributes[n])
#: The ID of the element form the OSM data
self.id: int
#: The tags of the element
self.tags: Optional[Dict] = tags
@classmethod
def get_center_from_json(cls, data: dict) -> Tuple[Decimal, Decimal]:
"""
Get center information from json data
:param data: json data
:return: tuple with two elements: lat and lon
"""
center_lat = None
center_lon = None
center = data.get("center")
if isinstance(center, dict):
center_lat = center.get("lat")
center_lon = center.get("lon")
if center_lat is None or center_lon is None:
raise ValueError("Unable to get lat or lon of way center.")
center_lat = Decimal(center_lat)
center_lon = Decimal(center_lon)
return center_lat, center_lon
@classmethod
def get_center_from_xml_dom(cls, sub_child: xml.etree.ElementTree.Element) -> Tuple[Decimal, Decimal]:
center_lat_str: str = sub_child.attrib.get("lat")
center_lon_str: str = sub_child.attrib.get("lon")
if center_lat_str is None or center_lon_str is None:
raise ValueError("Unable to get lat or lon of way center.")
center_lat = Decimal(center_lat_str)
center_lon = Decimal(center_lon_str)
return center_lat, center_lon
@classmethod
def from_json(cls: Type[ElementTypeVar], data: dict, result: Optional[Result] = None) -> ElementTypeVar:
"""
Create new Element() from json data
:param data:
:param result:
:return:
"""
raise NotImplementedError
def to_json(self) -> dict:
d = {"type": self._type_value, "id": self.id, "tags": self.tags}
d.update(_attributes_to_json(self.attributes))
return d
@classmethod
def from_xml(
cls: Type[ElementTypeVar],
child: xml.etree.ElementTree.Element,
result: Optional[Result] = None) -> ElementTypeVar:
"""
Create new Element() element from XML data
"""
raise NotImplementedError
class Area(Element):
"""
Class to represent an element of type area
:param area_id: Id of the area element
:param kwargs: Additional arguments are passed directly to the parent class
"""
_type_value = "area"
def __init__(self, area_id: Optional[int] = None, **kwargs):
Element.__init__(self, **kwargs)
#: The id of the way
self.id = area_id
def __repr__(self) -> str:
return f"<overpy.Area id={self.id}>"
@classmethod
def from_json(cls, data: dict, result: Optional[Result] = None) -> "Area":
"""
Create new Area element from JSON data
:param data: Element data from JSON
:param result: The result this element belongs to
:return: New instance of Way
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match.
"""
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
area_id = data.get("id")
attributes = {}
ignore = ["id", "tags", "type"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(area_id=area_id, attributes=attributes, tags=tags, result=result)
@classmethod
def from_xml(cls, child: xml.etree.ElementTree.Element, result: Optional[Result] = None) -> "Area":
"""
Create new way element from XML data
:param child: XML node to be parsed
:param result: The result this node belongs to
:return: New Way oject
:raises overpy.exception.ElementDataWrongType: If name of the xml child node doesn't match
:raises ValueError: If the ref attribute of the xml node is not provided
:raises ValueError: If a tag doesn't have a name
"""
if child.tag.lower() != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=child.tag.lower()
)
tags = {}
for sub_child in child:
if sub_child.tag.lower() == "tag":
name = sub_child.attrib.get("k")
if name is None:
raise ValueError("Tag without name/key.")
value = sub_child.attrib.get("v")
tags[name] = value
area_id_str: Optional[str] = child.attrib.get("id")
area_id: Optional[int] = None
if area_id_str is not None:
area_id = int(area_id_str)
attributes = {}
ignore = ["id"]
for n, v in child.attrib.items():
if n in ignore:
continue
attributes[n] = v
return cls(area_id=area_id, attributes=attributes, tags=tags, result=result)
class Node(Element):
"""
Class to represent an element of type node
:param lat: Latitude
:param lon: Longitude
:param node_id: Id of the node element
:param kwargs: Additional arguments are passed directly to the parent class
"""
_type_value = "node"
def __init__(
self,
node_id: Optional[int] = None,
lat: Optional[Union[Decimal, float]] = None,
lon: Optional[Union[Decimal, float]] = None,
**kwargs):
Element.__init__(self, **kwargs)
#: The node ID from the OSM
self.id = node_id
#: Latitude of the node
self.lat = lat
#: Longitude of the node
self.lon = lon
def __repr__(self) -> str:
return f"<overpy.Node id={self.id} lat={self.lat} lon={self.lon}>"
@classmethod
def from_json(cls, data: dict, result: Optional[Result] = None) -> "Node":
"""
Create new Node element from JSON data
:param data: Element data from JSON
:param result: The result this element belongs to
:return: New instance of Node
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match.
"""
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
node_id = data.get("id")
lat = data.get("lat")
lon = data.get("lon")
attributes = {}
ignore = ["type", "id", "lat", "lon", "tags"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(node_id=node_id, lat=lat, lon=lon, tags=tags, attributes=attributes, result=result)
def to_json(self) -> dict:
d = super().to_json()
d["lat"] = self.lat
d["lon"] = self.lon
return d
@classmethod
def from_xml(cls, child: xml.etree.ElementTree.Element, result: Optional[Result] = None) -> "Node":
"""
Create new way element from XML data
:param child: XML node to be parsed
:param result: The result this node belongs to
:return: New Way oject
:raises overpy.exception.ElementDataWrongType: If name of the xml child node doesn't match
:raises ValueError: If a tag doesn't have a name
"""
if child.tag.lower() != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=child.tag.lower()
)
tags = {}
for sub_child in child:
if sub_child.tag.lower() == "tag":
name = sub_child.attrib.get("k")
if name is None:
raise ValueError("Tag without name/key.")
value = sub_child.attrib.get("v")
tags[name] = value
node_id: Optional[int] = None
node_id_str: Optional[str] = child.attrib.get("id")
if node_id_str is not None:
node_id = int(node_id_str)
lat: Optional[Decimal] = None
lat_str: Optional[str] = child.attrib.get("lat")
if lat_str is not None:
lat = Decimal(lat_str)
lon: Optional[Decimal] = None
lon_str: Optional[str] = child.attrib.get("lon")
if lon_str is not None:
lon = Decimal(lon_str)
attributes = {}
ignore = ["id", "lat", "lon"]
for n, v in child.attrib.items():
if n in ignore:
continue
attributes[n] = v
return cls(node_id=node_id, lat=lat, lon=lon, tags=tags, attributes=attributes, result=result)
class Way(Element):
"""
Class to represent an element of type way
:param center_lat: The latitude of the center of the way (optional depending on query)
:param center_lon: The longitude of the center of the way (optional depending on query)
:param node_ids: List of node IDs
:param way_id: Id of the way element
:param kwargs: Additional arguments are passed directly to the parent class
"""
_type_value = "way"
def __init__(
self,
way_id: Optional[int] = None,
center_lat: Optional[Union[Decimal, float]] = None,
center_lon: Optional[Union[Decimal, float]] = None,
node_ids: Optional[Union[List[int], Tuple[int]]] = None,
**kwargs):
Element.__init__(self, **kwargs)
#: The id of the way
self.id = way_id
#: List of Ids of the associated nodes
self._node_ids = node_ids
#: The latitude of the center of the way (optional depending on query)
self.center_lat = center_lat
#: The longitude of the center of the way (optional depending on query)
self.center_lon = center_lon
def __repr__(self):
return f"<overpy.Way id={self.id} nodes={self._node_ids}>"
@property
def nodes(self) -> List[Node]:
"""
List of nodes associated with the way.
"""
return self.get_nodes()
def get_nodes(self, resolve_missing: bool = False) -> List[Node]:
"""
Get the nodes defining the geometry of the way
:param resolve_missing: Try to resolve missing nodes.
:return: List of nodes
:raises overpy.exception.DataIncomplete: At least one referenced node is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and at least one node can't be resolved.
"""
result = []
resolved = False
for node_id in self._node_ids:
try:
node = self._result.get_node(node_id)
except exception.DataIncomplete:
node = None
if node is not None:
result.append(node)
continue
if not resolve_missing:
raise exception.DataIncomplete("Resolve missing nodes is disabled")
# We tried to resolve the data but some nodes are still missing
if resolved:
raise exception.DataIncomplete("Unable to resolve all nodes")
query = ("\n"
"[out:json];\n"
f"way({self.id});\n"
"node(w);\n"
"out body;\n"
)
tmp_result = self._result.api.query(query)
self._result.expand(tmp_result)
resolved = True
try:
node = self._result.get_node(node_id)
except exception.DataIncomplete:
node = None
if node is None:
raise exception.DataIncomplete("Unable to resolve all nodes")
result.append(node)
return result
@classmethod
def from_json(cls, data: dict, result: Optional[Result] = None) -> "Way":
"""
Create new Way element from JSON data
:param data: Element data from JSON
:param result: The result this element belongs to
:return: New instance of Way
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match.
"""
if data.get("type") != cls._type_value: