Commit f8a0bd35 authored by Thomas Graichen's avatar Thomas Graichen

Implement each ZHV parser stage

- read stop places, areas and quays
- also extract names, DHID, latitudes and longitudes
parent 5d527711
......@@ -5,45 +5,95 @@ from lxml import etree
from zhv_parser import ZHV_Classes
from zhv_parser.RegionCode import RegionCode
from zhv_parser.ZHV_Classes import StopPlace
from zhv_parser.ZHV_Classes import StopPlace, Area, Quay, Name
namespace: str = "{http://zhv.xml.wvi/}"
tag_stop_place: str = namespace + "StopPlace"
tag_area: str = namespace + "Area"
tag_quay: str = namespace + "Quay"
event_start: str = "start"
event_end: str = "end"
def parse(xml_file):
context = etree.iterparse(xml_file, events=("start",), tag="{http://zhv.xml.wvi/}StopPlace")
for action, node in context:
stop_place_class = ZHV_Classes.StopPlace
stop_place = stop_place_class.factory()
stop_place.build(node)
if is_stop_place_in_region(RegionCode.BRANDENBURG, stop_place):
print(stop_place)
convert_to_osm(stop_place)
else:
print("Not in Region!")
context = etree.iterparse(xml_file, events=(event_start, event_end),
tag=(tag_stop_place, tag_area, tag_quay))
# TODO: Übergebe context (iterator) an nächste Stufe zur Extraktion der Daten
for action, elem in context:
if elem.tag == tag_stop_place and action == event_start:
stop_place_class = ZHV_Classes.StopPlace
stop_place = stop_place_class.factory()
stop_place.build(elem)
if is_stop_place_in_region(RegionCode.BRANDENBURG, stop_place):
print(stop_place)
stop_place_to_osm(stop_place, context)
else:
print("Not in Region!")
def is_stop_place_in_region(region_code: RegionCode, stop_place: StopPlace):
return region_code.isStopPlaceInRegion(stop_place)
def convert_to_osm(stop_place: StopPlace):
def stop_place_to_osm(stop_place: StopPlace, context):
"""Converts stop place to an OSM object """
pass
# extract stop place parameters
# TODO: Ask ZHV: Why are names double encapsulated?
stop_place_name: str = stop_place.get_Name().get_Name()[0].get_valueOf_()
for action, elem in context:
if elem.tag == tag_area and action == event_start:
area_class = ZHV_Classes.Area
area = area_class.factory()
area.build(elem)
area_to_osm(area, context)
elif elem.tag == tag_stop_place and action == event_end:
return
def area_to_osm(area: Area, context):
# extract area parameters
for action, elem in context:
if elem.tag == tag_quay and action == event_start:
quay_class = ZHV_Classes.Quay
quay = quay_class.factory()
quay.build(elem)
quay_to_osm(quay, context)
elif elem.tag == tag_area and action == event_end:
return
def quay_to_osm(quay: Quay, context):
# extract quay parameters
# TODO: Ask ZHV: Why are names double encapsulated?
quay_name: str = quay.get_Name().get_Name()[0].get_valueOf_()
quay_dhid: str = quay.get_DHID()
quay_lat: str = quay.get_Location().get_Latitude()
quay_lon: str = quay.get_Location().get_Longitude()
# return to previous structure hierarchy
for action, elem in context:
if elem.tag == tag_quay and action == event_end:
return
USAGE_TEXT = """
Usage: python ???.py <infilename>
"""
def usage():
print(USAGE_TEXT)
sys.exit(1)
def main():
args = sys.argv[1:]
if len(args) != 1:
usage()
infilename = args[0]
parse(infilename)
infile_name = args[0]
parse(infile_name)
if __name__ == '__main__':
#import pdb; pdb.set_trace()
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment