|
2 | 2 | import csv
|
3 | 3 | import json
|
4 | 4 | import logging
|
| 5 | +import zipfile |
5 | 6 |
|
6 | 7 |
|
7 |
| -def poi_to_excluded_zones(poi_file, output_dir, instance_name): |
8 |
| - logger = logging.getLogger(__name__) |
| 8 | +def parse_file(filename): |
| 9 | + try: |
| 10 | + with open(filename) as csvfile: |
| 11 | + reader = csv.reader(csvfile, delimiter=';', quotechar='"') |
| 12 | + for row in reader: |
| 13 | + yield row |
| 14 | + except Exception as e: |
| 15 | + logging.getLogger(__name__).error( |
| 16 | + "opg_excluded_zones: Unable to read file {}, error ({})".format(filename, str(e)) |
| 17 | + ) |
| 18 | + raise |
9 | 19 |
|
| 20 | + |
| 21 | +def get_excluded_zones(path): |
| 22 | + result = {} |
| 23 | + for row in parse_file(path + "/poi_properties.txt"): |
| 24 | + if row[1].lower() != "excluded_zones": |
| 25 | + continue |
| 26 | + try: |
| 27 | + result[row[0]] = json.loads(row[2]) |
| 28 | + except Exception: |
| 29 | + logging.getLogger(__name__).error( |
| 30 | + "opg_excluded_zones: Ignored line, Invalid json ({})".format(row[2]) |
| 31 | + ) |
| 32 | + return result |
| 33 | + |
| 34 | + |
| 35 | +def get_geometries_ids(path, excluded_zones): |
| 36 | + result = {} |
| 37 | + for row in parse_file(path + "/poi.txt"): |
| 38 | + if row[0] not in excluded_zones: |
| 39 | + continue |
| 40 | + result[row[0]] = row[7] |
| 41 | + return result |
| 42 | + |
| 43 | + |
| 44 | +def get_geometries_shapes(path): |
| 45 | + result = {} |
| 46 | + for row in parse_file(path + "/geometries.txt"): |
| 47 | + result[row[0]] = row[1] |
| 48 | + return result |
| 49 | + |
| 50 | + |
| 51 | +def poi_to_excluded_zones(poi_file, output_dir, instance_name): |
10 | 52 | tmp_path = "tmp/poi_{}".format(instance_name)
|
11 |
| - import zipfile |
12 | 53 |
|
13 | 54 | with zipfile.ZipFile(poi_file, 'r') as zip_ref:
|
14 | 55 | zip_ref.extractall(tmp_path)
|
15 | 56 |
|
16 |
| - excluded_zones = {} |
17 |
| - excluded_geometries_ids = {} |
18 |
| - |
19 | 57 | # get excluded zones
|
20 |
| - with open(tmp_path + "/poi_properties.txt") as csvfile: |
21 |
| - reader = csv.reader(csvfile, delimiter=';', quotechar='"') |
22 |
| - for row in reader: |
23 |
| - if row[1].lower() != "excluded_zones": |
24 |
| - continue |
25 |
| - excluded_zones[row[0]] = json.loads(row[2]) |
| 58 | + excluded_zones = get_excluded_zones(tmp_path) |
26 | 59 |
|
27 | 60 | # find geometry id
|
28 |
| - with open(tmp_path + "/poi.txt") as csvfile: |
29 |
| - reader = csv.reader(csvfile, delimiter=';', quotechar='"') |
30 |
| - for row in reader: |
31 |
| - if row[0] not in excluded_zones: |
32 |
| - continue |
33 |
| - excluded_geometries_ids[row[0]] = row[7] |
| 61 | + excluded_geometries_ids = get_geometries_ids(tmp_path, excluded_zones) |
34 | 62 |
|
35 | 63 | if excluded_geometries_ids.keys() != excluded_zones.keys():
|
36 |
| - logger.warning("not all excluded zone's pois are found in poi.txt") |
37 |
| - logger.warning("excluded_geometries_ids: {}".format(excluded_geometries_ids.keys())) |
38 |
| - logger.warning("excluded_zones: {}".format(excluded_zones.keys())) |
| 64 | + logging.getLogger(__name__).warning("not all excluded zone's pois are found in poi.txt") |
| 65 | + logging.getLogger(__name__).warning("excluded_geometries_ids: {}".format(excluded_geometries_ids.keys())) |
| 66 | + logging.getLogger(__name__).warning("excluded_zones: {}".format(excluded_zones.keys())) |
39 | 67 |
|
40 | 68 | # read geometries
|
41 |
| - geometries_shapes = {} |
42 |
| - with open(tmp_path + "/geometries.txt") as csvfile: |
43 |
| - reader = csv.reader(csvfile, delimiter=';', quotechar='"') |
44 |
| - for row in reader: |
45 |
| - geometries_shapes[row[0]] = row[1] |
| 69 | + geometries_shapes = get_geometries_shapes(tmp_path) |
46 | 70 |
|
47 | 71 | for poi_id, zones in excluded_zones.items():
|
48 | 72 | geometry_id = excluded_geometries_ids.get(poi_id)
|
49 | 73 | if not geometry_id:
|
50 |
| - logger.error("{} could not be found in poi.txt".format(row[0])) |
| 74 | + logging.getLogger(__name__).error("{} could not be found in poi.txt".format(poi_id)) |
51 | 75 | continue
|
52 | 76 |
|
53 | 77 | shape = geometries_shapes.get(geometry_id)
|
54 | 78 | if not shape:
|
55 |
| - logger.error("{} could not be found in geometries.txt".format(geometry_id)) |
| 79 | + logging.getLogger(__name__).error("{} could not be found in geometries.txt".format(geometry_id)) |
56 | 80 | continue
|
57 | 81 |
|
58 | 82 | for i, zone in enumerate(zones):
|
|
0 commit comments