|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
try: |
|
4
|
|
|
import logging |
|
5
|
|
|
import sys |
|
6
|
|
|
import os |
|
7
|
|
|
import pandas as pd |
|
8
|
|
|
from lxml import etree |
|
9
|
|
|
from osm_poi_matchmaker.dao.data_handlers import insert_city_dataframe, insert_street_type_dataframe |
|
|
|
|
|
|
10
|
|
|
from osm_poi_matchmaker.libs.xml import save_downloaded_xml |
|
11
|
|
|
except ImportError as err: |
|
12
|
|
|
logging.error('Error %s import module: %s', __name__, err) |
|
13
|
|
|
logging.exception('Exception occurred') |
|
14
|
|
|
|
|
15
|
|
|
sys.exit(128) |
|
16
|
|
|
|
|
17
|
|
|
POI_COLS_CITY = ['city_post_code', 'city_name'] |
|
18
|
|
|
POI_COLS_STREET_TYPE = ['street_type'] |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class hu_city_postcode(): |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def __init__(self, session, link): |
|
24
|
|
|
self.session = session |
|
25
|
|
|
self.link = link |
|
26
|
|
|
|
|
27
|
|
|
def process(self): |
|
|
|
|
|
|
28
|
|
|
xl = pd.ExcelFile(self.link) |
|
|
|
|
|
|
29
|
|
|
df = xl.parse("Települések") |
|
|
|
|
|
|
30
|
|
|
del df['Településrész'] |
|
31
|
|
|
insert_city_dataframe(self.session, df) |
|
32
|
|
|
big_cities = [['Budapest', 'Bp.u.'], |
|
33
|
|
|
['Miskolc', 'Miskolc u.'], |
|
34
|
|
|
['Debrecen', 'Debrecen u.'], |
|
35
|
|
|
['Szeged', 'Szeged u.'], |
|
36
|
|
|
['Pécs', 'Pécs u.'], |
|
37
|
|
|
['Győr', 'Győr u.'] |
|
38
|
|
|
] |
|
39
|
|
|
for city, sheet in big_cities: |
|
40
|
|
|
df = xl.parse(sheet) |
|
|
|
|
|
|
41
|
|
|
df.columns.values[0] = 'city_post_code' |
|
42
|
|
|
df['city_name'] = city |
|
43
|
|
|
df = df[['city_post_code', 'city_name']] |
|
|
|
|
|
|
44
|
|
|
df.drop_duplicates('city_post_code', keep='first', inplace=True) |
|
45
|
|
|
insert_city_dataframe(self.session, df) |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
class hu_city_postcode_from_xml(): |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
def __init__(self, session, link, download_cache, filename='hu_city_postcode.xml'): |
|
51
|
|
|
self.session = session |
|
52
|
|
|
self.link = link |
|
53
|
|
|
self.download_cache = download_cache |
|
54
|
|
|
self.filename = filename |
|
55
|
|
|
|
|
56
|
|
|
def process(self): |
|
|
|
|
|
|
57
|
|
|
xml = save_downloaded_xml('{}'.format(self.link), os.path.join( |
|
58
|
|
|
self.download_cache, self.filename)) |
|
59
|
|
|
insert_data = [] |
|
60
|
|
|
root = etree.fromstring(xml) |
|
|
|
|
|
|
61
|
|
|
for e in root.findall('zipCode'): |
|
|
|
|
|
|
62
|
|
|
cities = e[1].text |
|
63
|
|
|
postcode = e[0].text.strip() |
|
64
|
|
|
for i in cities.split('|'): |
|
65
|
|
|
insert_data.append([postcode, i.strip()]) |
|
66
|
|
|
df = pd.DataFrame(insert_data) |
|
|
|
|
|
|
67
|
|
|
df.columns = POI_COLS_CITY |
|
68
|
|
|
insert_city_dataframe(self.session, df) |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
class hu_street_types_from_xml(): |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
def __init__(self, session, link, download_cache, filename='hu_street_types.xml'): |
|
74
|
|
|
self.session = session |
|
75
|
|
|
self.link = link |
|
76
|
|
|
self.download_cache = download_cache |
|
77
|
|
|
self.filename = filename |
|
78
|
|
|
|
|
79
|
|
|
def process(self): |
|
|
|
|
|
|
80
|
|
|
xml = save_downloaded_xml('{}'.format(self.link), os.path.join( |
|
81
|
|
|
self.download_cache, self.filename)) |
|
82
|
|
|
insert_data = [] |
|
83
|
|
|
root = etree.fromstring(xml) |
|
|
|
|
|
|
84
|
|
|
for e in root.findall('streetType'): |
|
|
|
|
|
|
85
|
|
|
if e.text is not None: |
|
86
|
|
|
street_type = e.text.strip() |
|
87
|
|
|
insert_data.append(street_type) |
|
88
|
|
|
df = pd.DataFrame(insert_data) |
|
|
|
|
|
|
89
|
|
|
df.columns = POI_COLS_STREET_TYPE |
|
90
|
|
|
insert_street_type_dataframe(self.session, df) |
|
91
|
|
|
|