Skip to content

Commit d5cce15

Browse files
authored
Merge pull request #4223 from hove-io/fix_excluded_zones_files
Fix excluded_zones generator
2 parents d96241d + f10f705 commit d5cce15

File tree

3 files changed

+79
-45
lines changed

3 files changed

+79
-45
lines changed

source/navitiacommon/navitiacommon/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,7 @@ def files_exists_in_zipfile(poi_zipfile, set_files):
201201
z = zipfile.ZipFile(poi_zipfile)
202202
files_from_zip = {member.filename for member in z.infolist()}
203203
return not bool(len(set_files.difference(files_from_zip)))
204+
205+
206+
def is_empty_directory(directory, ext="json"):
207+
return len(glob.glob("{}/*.{}".format(directory, ext))) == 0 if os.path.isdir(directory) else True

source/tyr/tyr/binarisation.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import navitiacommon.task_pb2
5151
from tyr import celery, redis
5252
from tyr.rabbit_mq_handler import RabbitMqHandler
53-
from navitiacommon import models
53+
from navitiacommon import models, utils
5454
from tyr.helper import get_instance_logger, get_named_arg, get_autocomplete_instance_logger, get_task_logger
5555
from contextlib import contextmanager
5656
import glob
@@ -1249,25 +1249,31 @@ def poi2asgard(self, instance_config, filename, job_id, dataset_uid):
12491249
shutil.rmtree(excluded_zone_dir)
12501250

12511251
os.mkdir(excluded_zone_dir)
1252-
poi_to_excluded_zones(filename, excluded_zone_dir, instance.name)
1253-
12541252
try:
1255-
with collect_metric("poi2Asgard", job, dataset_uid):
1256-
asgard_bucket = current_app.config.get('MINIO_ASGARD_BUCKET_NAME', None)
1257-
if not asgard_bucket:
1258-
dataset.state = "failed"
1259-
return
1260-
1261-
bash_command = (
1262-
"env REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt "
1263-
"aws s3 sync ./{excluded_zone_dir} s3://{asgard_bucket}/excluded_zones".format(
1264-
excluded_zone_dir=excluded_zone_dir, asgard_bucket=asgard_bucket
1253+
poi_to_excluded_zones(filename, excluded_zone_dir, instance.name)
1254+
if utils.is_empty_directory(excluded_zone_dir):
1255+
logger.warning(
1256+
"opg_excluded_zones: Impossible to push excluded zones to S3 for instance {}, empty directory".format(
1257+
instance.name
12651258
)
12661259
)
1267-
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
1268-
output, error = process.communicate()
1269-
if error:
1270-
raise Exception("Error occurred when putting excluded zones to asgard: {}".format(error))
1260+
else:
1261+
with collect_metric("poi2Asgard", job, dataset_uid):
1262+
asgard_bucket = current_app.config.get('MINIO_ASGARD_BUCKET_NAME', None)
1263+
if not asgard_bucket:
1264+
dataset.state = "failed"
1265+
return
1266+
1267+
bash_command = (
1268+
"env REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt "
1269+
"aws s3 sync ./{excluded_zone_dir} s3://{asgard_bucket}/excluded_zones".format(
1270+
excluded_zone_dir=excluded_zone_dir, asgard_bucket=asgard_bucket
1271+
)
1272+
)
1273+
process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
1274+
output, error = process.communicate()
1275+
if error:
1276+
raise Exception("Error occurred when putting excluded zones to asgard: {}".format(error))
12711277
except:
12721278
logger.exception("")
12731279
job.state = "failed"

source/tyr/tyr/poi_to_excluded_zones.py

+52-28
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,81 @@
22
import csv
33
import json
44
import logging
5+
import zipfile
56

67

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
919

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):
1052
tmp_path = "tmp/poi_{}".format(instance_name)
11-
import zipfile
1253

1354
with zipfile.ZipFile(poi_file, 'r') as zip_ref:
1455
zip_ref.extractall(tmp_path)
1556

16-
excluded_zones = {}
17-
excluded_geometries_ids = {}
18-
1957
# 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)
2659

2760
# 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)
3462

3563
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()))
3967

4068
# 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)
4670

4771
for poi_id, zones in excluded_zones.items():
4872
geometry_id = excluded_geometries_ids.get(poi_id)
4973
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))
5175
continue
5276

5377
shape = geometries_shapes.get(geometry_id)
5478
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))
5680
continue
5781

5882
for i, zone in enumerate(zones):

0 commit comments

Comments
 (0)