mirror of
https://github.com/enzet/map-machine.git
synced 2025-06-13 16:21:55 +02:00
Issue #14: refactor OSM reading.
This commit is contained in:
parent
2d90ed23fc
commit
ed6a6c4858
2 changed files with 23 additions and 25 deletions
|
@ -471,11 +471,11 @@ def main():
|
||||||
if options.mode in ["user-coloring", "time"]:
|
if options.mode in ["user-coloring", "time"]:
|
||||||
full = True
|
full = True
|
||||||
|
|
||||||
osm_reader = OSMReader(input_file_name)
|
osm_reader = OSMReader()
|
||||||
|
|
||||||
map_ = osm_reader.parse_osm_file(
|
map_ = osm_reader.parse_osm_file(
|
||||||
parse_ways=options.draw_ways, parse_relations=options.draw_ways,
|
input_file_name, parse_ways=options.draw_ways,
|
||||||
full=full)
|
parse_relations=options.draw_ways, full=full)
|
||||||
|
|
||||||
output_file = svg.SVG(open(options.output_file_name, "w+"))
|
output_file = svg.SVG(open(options.output_file_name, "w+"))
|
||||||
|
|
||||||
|
|
|
@ -142,35 +142,33 @@ def get_value(key: str, text: str):
|
||||||
|
|
||||||
|
|
||||||
class Map:
|
class Map:
|
||||||
def __init__(self, node_map, way_map, relation_map):
|
def __init__(self):
|
||||||
self.node_map = node_map
|
self.node_map: Dict[int, OSMNode] = {}
|
||||||
self.way_map = way_map
|
self.way_map: Dict[int, OSMWay] = {}
|
||||||
self.relation_map = relation_map
|
self.relation_map: Dict[int, OSMRelation] = {}
|
||||||
|
|
||||||
|
|
||||||
class OSMReader:
|
class OSMReader:
|
||||||
"""
|
"""
|
||||||
OSM XML representation reader.
|
OSM XML representation reader.
|
||||||
"""
|
"""
|
||||||
def __init__(self, file_name: str):
|
def __init__(self):
|
||||||
self.file_name = file_name
|
self.map_ = Map()
|
||||||
|
|
||||||
def parse_osm_file(
|
def parse_osm_file(
|
||||||
self, parse_nodes: bool = True, parse_ways: bool = True,
|
self, file_name: str, parse_nodes: bool = True,
|
||||||
parse_relations: bool = True, full: bool = False) -> Map:
|
parse_ways: bool = True, parse_relations: bool = True,
|
||||||
|
full: bool = False) -> Map:
|
||||||
"""
|
"""
|
||||||
Parse OSM XML representation.
|
Parse OSM XML representation.
|
||||||
"""
|
"""
|
||||||
node_map: Dict[int, OSMNode] = {}
|
print(f"Line number counting for {file_name}...")
|
||||||
way_map: Dict[int, OSMWay] = {}
|
with open(file_name) as f:
|
||||||
relation_map: Dict[int, OSMRelation] = {}
|
|
||||||
print(f"Line number counting for {self.file_name}...")
|
|
||||||
with open(self.file_name) as f:
|
|
||||||
for lines_number, _ in enumerate(f):
|
for lines_number, _ in enumerate(f):
|
||||||
pass
|
pass
|
||||||
print("Done.")
|
print("Done.")
|
||||||
print(f"Parsing OSM file {self.file_name}...")
|
print(f"Parsing OSM file {file_name}...")
|
||||||
input_file = open(self.file_name)
|
input_file = open(file_name)
|
||||||
line = input_file.readline()
|
line = input_file.readline()
|
||||||
line_number = 0
|
line_number = 0
|
||||||
|
|
||||||
|
@ -188,11 +186,11 @@ class OSMReader:
|
||||||
break
|
break
|
||||||
if line[-3] == "/":
|
if line[-3] == "/":
|
||||||
node: OSMNode = OSMNode().parse_from_xml(line[7:-3], full)
|
node: OSMNode = OSMNode().parse_from_xml(line[7:-3], full)
|
||||||
node_map[node.id_] = node
|
self.map_.node_map[node.id_] = node
|
||||||
else:
|
else:
|
||||||
element = OSMNode().parse_from_xml(line[7:-2], full)
|
element = OSMNode().parse_from_xml(line[7:-2], full)
|
||||||
elif line in [" </node>\n", "\t</node>\n", " </node>\n"]:
|
elif line in [" </node>\n", "\t</node>\n", " </node>\n"]:
|
||||||
node_map[element.id_] = element
|
self.map_.node_map[element.id_] = element
|
||||||
|
|
||||||
# Way parsing.
|
# Way parsing.
|
||||||
|
|
||||||
|
@ -204,11 +202,11 @@ class OSMReader:
|
||||||
break
|
break
|
||||||
if line[-3] == '/':
|
if line[-3] == '/':
|
||||||
way = OSMWay().parse_from_xml(line[6:-3], full)
|
way = OSMWay().parse_from_xml(line[6:-3], full)
|
||||||
way_map[way.id_] = way
|
self.map_.way_map[way.id_] = way
|
||||||
else:
|
else:
|
||||||
element = OSMWay().parse_from_xml(line[6:-2], full)
|
element = OSMWay().parse_from_xml(line[6:-2], full)
|
||||||
elif line in [' </way>\n', '\t</way>\n'] or line == " </way>\n":
|
elif line in [' </way>\n', '\t</way>\n'] or line == " </way>\n":
|
||||||
way_map[element.id_] = element
|
self.map_.way_map[element.id_] = element
|
||||||
|
|
||||||
# Relation parsing.
|
# Relation parsing.
|
||||||
|
|
||||||
|
@ -218,12 +216,12 @@ class OSMReader:
|
||||||
break
|
break
|
||||||
if line[-3] == "/":
|
if line[-3] == "/":
|
||||||
relation = OSMRelation().parse_from_xml(line[11:-3])
|
relation = OSMRelation().parse_from_xml(line[11:-3])
|
||||||
relation_map[relation.id_] = relation
|
self.map_.relation_map[relation.id_] = relation
|
||||||
else:
|
else:
|
||||||
element = OSMRelation().parse_from_xml(line[11:-2])
|
element = OSMRelation().parse_from_xml(line[11:-2])
|
||||||
elif line in [" </relation>\n", "\t</relation>\n"] or \
|
elif line in [" </relation>\n", "\t</relation>\n"] or \
|
||||||
line == " </relation>\n":
|
line == " </relation>\n":
|
||||||
relation_map[element.id_] = element
|
self.map_.relation_map[element.id_] = element
|
||||||
|
|
||||||
# Elements parsing.
|
# Elements parsing.
|
||||||
|
|
||||||
|
@ -241,4 +239,4 @@ class OSMReader:
|
||||||
|
|
||||||
ui.write_line(-1, lines_number) # Complete progress bar.
|
ui.write_line(-1, lines_number) # Complete progress bar.
|
||||||
|
|
||||||
return Map(node_map, way_map, relation_map)
|
return self.map_
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue