hub/imports/geometry/osm_subway.py

58 lines
1.9 KiB
Python
Raw Normal View History

2020-06-30 16:51:49 -04:00
"""
OsmSubway module parses osm files and import the metro location into the city model structure
SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2020 Project Author Guille Gutierrez guillermo.gutierrezmorote@concordia.ca
"""
2021-08-27 12:51:30 -04:00
import sys
2020-06-30 16:51:49 -04:00
import xmltodict
2021-08-27 12:51:30 -04:00
from pyproj import Transformer
from city_model_structure.city import City
2020-06-30 18:06:27 -04:00
from city_model_structure.subway_entrance import SubwayEntrance
2020-06-30 16:51:49 -04:00
class OsmSubway:
2021-08-27 12:51:30 -04:00
"""
Open street map subway
"""
2020-06-30 16:51:49 -04:00
def __init__(self, path):
2021-08-27 12:51:30 -04:00
self._city = None
2020-06-30 18:06:27 -04:00
self._subway_entrances = []
2020-06-30 16:51:49 -04:00
with open(path) as osm:
self._osm = xmltodict.parse(osm.read(), force_list='tag')
for node in self._osm['osm']['node']:
if 'tag' not in node:
continue
for tag in node['tag']:
if '@v' not in tag:
continue
if tag['@v'] == 'subway_entrance':
2020-06-30 18:06:27 -04:00
subway_entrance = SubwayEntrance(node['@id'], node['@lat'], node['@lon'])
self._subway_entrances.append(subway_entrance)
@property
2021-08-27 12:51:30 -04:00
def city(self) -> City:
"""
Get a city with subway entrances
2021-08-27 12:51:30 -04:00
"""
transformer = Transformer.from_crs("EPSG:4326", "EPSG:3857")
lower_corner = [sys.float_info.max, sys.float_info.max, 0]
upper_corner = [sys.float_info.min, sys.float_info.min, 0]
x = 0
y = 1
for subway_entrance in self._subway_entrances:
coordinate = transformer.transform(subway_entrance.longitude, subway_entrance.latitude)
if coordinate[x] >= upper_corner[x]:
upper_corner[x] = coordinate[x]
if coordinate[y] >= upper_corner[y]:
upper_corner[y] = coordinate[y]
if coordinate[x] < lower_corner[x]:
lower_corner[x] = coordinate[x]
if coordinate[y] < lower_corner[y]:
lower_corner[y] = coordinate[y]
2021-09-23 09:17:23 -04:00
city = City(lower_corner, upper_corner, 'unknown')
for subway_entrance in self._subway_entrances:
city.add_city_object(subway_entrance)
return city