Add documentation.

This commit is contained in:
Sergey Vartanov 2021-06-06 02:54:20 +03:00
parent 6a9af15b05
commit 20b2ec1871
3 changed files with 45 additions and 34 deletions

View file

@ -170,20 +170,20 @@ class Constructor:
Construct Röntgen ways.
"""
way_number: int = 0
for way_id in self.map_.way_map: # type: int
for way_id in self.map_.ways: # type: int
ui.progress_bar(
way_number,
len(self.map_.way_map),
len(self.map_.ways),
step=10,
text="Constructing ways",
)
way_number += 1
way: OSMWay = self.map_.way_map[way_id]
way: OSMWay = self.map_.ways[way_id]
if not self.check_level(way.tags):
continue
self.construct_line(way, [], [way.nodes])
ui.progress_bar(-1, len(self.map_.way_map), text="Constructing ways")
ui.progress_bar(-1, len(self.map_.ways), text="Constructing ways")
def construct_line(
self,
@ -294,8 +294,8 @@ class Constructor:
"""
Construct Röntgen ways from OSM relations.
"""
for relation_id in self.map_.relation_map:
relation: OSMRelation = self.map_.relation_map[relation_id]
for relation_id in self.map_.relations:
relation: OSMRelation = self.map_.relations[relation_id]
tags = relation.tags
if not self.check_level(tags):
continue
@ -306,11 +306,11 @@ class Constructor:
for member in relation.members: # type: OSMMember
if member.type_ == "way":
if member.role == "inner":
if member.ref in self.map_.way_map:
inner_ways.append(self.map_.way_map[member.ref])
if member.ref in self.map_.ways:
inner_ways.append(self.map_.ways[member.ref])
elif member.role == "outer":
if member.ref in self.map_.way_map:
outer_ways.append(self.map_.way_map[member.ref])
if member.ref in self.map_.ways:
outer_ways.append(self.map_.ways[member.ref])
else:
print(f'Unknown member role "{member.role}".')
if outer_ways:
@ -325,8 +325,8 @@ class Constructor:
node_number: int = 0
sorted_node_ids: Iterator[int] = sorted(
self.map_.node_map.keys(),
key=lambda x: -self.map_.node_map[x].coordinates[0],
self.map_.nodes.keys(),
key=lambda x: -self.map_.nodes[x].coordinates[0],
)
missing_tags = Counter()
@ -334,9 +334,9 @@ class Constructor:
for node_id in sorted_node_ids: # type: int
node_number += 1
ui.progress_bar(
node_number, len(self.map_.node_map), text="Constructing nodes"
node_number, len(self.map_.nodes), text="Constructing nodes"
)
node: OSMNode = self.map_.node_map[node_id]
node: OSMNode = self.map_.nodes[node_id]
flung = self.flinger.fling(node.coordinates)
tags = node.tags
@ -379,4 +379,4 @@ class Constructor:
if key not in icon_set.processed
)
ui.progress_bar(-1, len(self.map_.node_map), text="Constructing nodes")
ui.progress_bar(-1, len(self.map_.nodes), text="Constructing nodes")

View file

@ -59,6 +59,9 @@ class OSMNode(Tagged):
@classmethod
def from_xml_structure(cls, element, is_full: bool = False) -> "OSMNode":
"""
Parse node from OSM XML `<node>` element.
"""
node = cls()
attributes = element.attrib
node.id_ = int(attributes["id"])
@ -114,6 +117,9 @@ class OSMWay(Tagged):
@classmethod
def from_xml_structure(cls, element, nodes, is_full: bool) -> "OSMWay":
"""
Parse way from OSM XML `<way>` element.
"""
way = cls(int(element.attrib["id"]))
if is_full:
way.visible = element.attrib["visible"]
@ -188,6 +194,9 @@ class OSMRelation(Tagged):
@classmethod
def from_xml_structure(cls, element, is_full: bool) -> "OSMRelation":
"""
Parse relation from OSM XML `<relation>` element.
"""
attributes = element.attrib
relation = cls(int(attributes["id"]))
if is_full:
@ -245,39 +254,39 @@ class Map:
"""
def __init__(self):
self.node_map: Dict[int, OSMNode] = {}
self.way_map: Dict[int, OSMWay] = {}
self.relation_map: Dict[int, OSMRelation] = {}
self.nodes: Dict[int, OSMNode] = {}
self.ways: Dict[int, OSMWay] = {}
self.relations: Dict[int, OSMRelation] = {}
self.authors: Set[str] = set()
self.time: MinMax = MinMax()
self.boundary_box: List[MinMax] = [MinMax(), MinMax()]
def add_node(self, node: OSMNode):
def add_node(self, node: OSMNode) -> None:
"""
Add node and update map parameters.
"""
self.node_map[node.id_] = node
self.nodes[node.id_] = node
if node.user:
self.authors.add(node.user)
self.time.update(node.timestamp)
self.boundary_box[0].update(node.coordinates[0])
self.boundary_box[1].update(node.coordinates[1])
def add_way(self, way: OSMWay):
def add_way(self, way: OSMWay) -> None:
"""
Add way and update map parameters.
"""
self.way_map[way.id_] = way
self.ways[way.id_] = way
if way.user:
self.authors.add(way.user)
self.time.update(way.timestamp)
def add_relation(self, relation: OSMRelation):
def add_relation(self, relation: OSMRelation) -> None:
"""
Add relation and update map parameters.
"""
self.relation_map[relation.id_] = relation
self.relations[relation.id_] = relation
class OverpassReader:
@ -321,6 +330,8 @@ class OverpassReader:
class OSMReader:
"""
OpenStreetMap XML file parser.
See https://wiki.openstreetmap.org/wiki/OSM_XML
"""
def __init__(
@ -375,7 +386,7 @@ class OSMReader:
if element.tag == "way" and self.parse_ways:
self.map_.add_way(
OSMWay.from_xml_structure(
element, self.map_.node_map, self.is_full
element, self.map_.nodes, self.is_full
)
)
if element.tag == "relation" and self.parse_relations:

View file

@ -19,8 +19,8 @@ def test_node() -> None:
<node id="42" lon="5" lat="10" />
</osm>"""
)
assert 42 in map_.node_map
node: OSMNode = map_.node_map[42]
assert 42 in map_.nodes
node: OSMNode = map_.nodes[42]
assert node.id_ == 42
assert np.allclose(node.coordinates, np.array([10, 5]))
@ -38,8 +38,8 @@ def test_node_with_tag() -> None:
</node>
</osm>"""
)
assert 42 in map_.node_map
node: OSMNode = map_.node_map[42]
assert 42 in map_.nodes
node: OSMNode = map_.nodes[42]
assert node.id_ == 42
assert np.allclose(node.coordinates, np.array([10, 5]))
assert node.tags["key"] == "value"
@ -56,8 +56,8 @@ def test_way() -> None:
<way id="42" />
</osm>"""
)
assert 42 in map_.way_map
way: OSMWay = map_.way_map[42]
assert 42 in map_.ways
way: OSMWay = map_.ways[42]
assert way.id_ == 42
@ -76,7 +76,7 @@ def test_nodes() -> None:
</way>
</osm>"""
)
way: OSMWay = map_.way_map[2]
way: OSMWay = map_.ways[2]
assert len(way.nodes) == 1
assert way.nodes[0].id_ == 1
assert way.tags["key"] == "value"
@ -100,8 +100,8 @@ def test_relation() -> None:
</relation>
</osm>"""
)
assert 3 in map_.relation_map
relation: OSMRelation = map_.relation_map[3]
assert 3 in map_.relations
relation: OSMRelation = map_.relations[3]
assert relation.id_ == 3
assert relation.tags["key"] == "value"
assert len(relation.members) == 1