1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
|
|
try: |
4
|
|
|
import logging |
5
|
|
|
import sys |
6
|
|
|
import os |
7
|
|
|
from osm_poi_matchmaker.libs.file_output import save_csv_file, generate_osm_xml |
8
|
|
|
from osm_poi_matchmaker.utils import config |
9
|
|
|
except ImportError as err: |
10
|
|
|
logging.error('Error %s import module: %s', __name__, err) |
11
|
|
|
logging.exception('Exception occurred') |
12
|
|
|
|
13
|
|
|
sys.exit(128) |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def export_raw_poi_data(addr_data, comm_data, postfix=''): |
|
|
|
|
17
|
|
|
logging.info('Exporting CSV files ...') |
18
|
|
|
# And merge and them into one Dataframe and save it to a CSV file |
19
|
|
|
save_csv_file(config.get_directory_output(), 'poi_common{}.csv'.format(postfix), comm_data, 'poi_common') |
|
|
|
|
20
|
|
|
save_csv_file(config.get_directory_output(), 'poi_address{}.csv'.format(postfix), addr_data, 'poi_address') |
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def export_raw_poi_data_xml(addr_data, postfix=''): |
|
|
|
|
24
|
|
|
with open(os.path.join(config.get_directory_output(), 'poi_address{}.osm'.format(postfix)), 'wb') as oxf: |
|
|
|
|
25
|
|
|
oxf.write(generate_osm_xml(addr_data)) |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def export_grouped_poi_data(data): |
|
|
|
|
29
|
|
|
try: |
30
|
|
|
# Generating CSV files group by poi_code |
31
|
|
|
output_dir = data[0] |
32
|
|
|
filename = data[1] |
33
|
|
|
rows = data[2] |
34
|
|
|
table = data[3] |
35
|
|
|
# Generating CSV files group by poi_code |
36
|
|
|
save_csv_file(output_dir, '{}.csv'.format(filename), rows, table) |
37
|
|
|
with open(os.path.join(output_dir, '{}.osm'.format(filename)), 'wb') as oxf: |
38
|
|
|
oxf.write(generate_osm_xml(rows)) |
39
|
|
|
except Exception as e: |
|
|
|
|
40
|
|
|
logging.error(e) |
41
|
|
|
logging.exception('Exception occurred') |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def export_grouped_poi_data_with_postcode_groups(data): |
|
|
|
|
45
|
|
|
try: |
46
|
|
|
# Generating CSV files group by poi_code and postcode |
47
|
|
|
output_dir = data[0] |
48
|
|
|
filename = data[1] |
49
|
|
|
rows = data[2] |
50
|
|
|
# Maximum number of items in one file |
51
|
|
|
batch = 100 |
52
|
|
|
# Minimum difference between postcode grouped data sets |
53
|
|
|
postcode_gap = 200 |
54
|
|
|
# Postcode minimum value |
55
|
|
|
postcode_start = 1000 |
56
|
|
|
# Postcode maximum value |
57
|
|
|
postcode_stop = 9999 |
58
|
|
|
if len(rows) > batch: |
59
|
|
|
# Create sliced data output |
60
|
|
|
i = postcode_start |
61
|
|
|
for i in range(postcode_start, postcode_stop, postcode_gap): |
62
|
|
|
stop = i + postcode_gap - 1 |
63
|
|
|
xml_export = rows[rows['poi_postcode'].between(i, stop)] |
64
|
|
|
print(xml_export.to_string()) |
65
|
|
|
if len(xml_export) != 0: |
66
|
|
|
with open(os.path.join(output_dir, '{}_{:04d}-{:04d}.osm'.format(filename, i, stop)), 'wb') as oxf: |
|
|
|
|
67
|
|
|
oxf.write(generate_osm_xml(xml_export)) |
68
|
|
|
i += postcode_gap |
69
|
|
|
except Exception as e: |
|
|
|
|
70
|
|
|
logging.error(e) |
71
|
|
|
logging.exception('Exception occurred') |
72
|
|
|
|