2019-03-14 04:23:36 +00:00
|
|
|
#! /usr/bin/python3
|
2019-03-14 15:03:54 +00:00
|
|
|
# Copyright (C) 2018-2019 Merspieler, merspieler _at_ airmail.cc
|
2018-11-21 20:17:10 +00:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2019-03-14 04:23:36 +00:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from math import ceil
|
|
|
|
import time
|
2018-11-13 15:31:53 +00:00
|
|
|
|
2019-03-14 04:23:36 +00:00
|
|
|
argc = len(sys.argv)
|
|
|
|
i = 1
|
|
|
|
first = 1
|
|
|
|
create_zip = False
|
2019-03-15 04:34:38 +00:00
|
|
|
build_all = True
|
|
|
|
build_fix = False
|
|
|
|
build_continue = False
|
2019-03-14 04:23:36 +00:00
|
|
|
man_threads = ""
|
|
|
|
project = ""
|
2019-04-13 04:26:49 +00:00
|
|
|
chunk_size = 1
|
2019-03-14 04:23:36 +00:00
|
|
|
while i < argc:
|
|
|
|
if sys.argv[i] == "-t" or sys.argv[i] == "--threads":
|
|
|
|
i += 1
|
2019-04-02 09:41:59 +00:00
|
|
|
man_threads = "-t " + sys.argv[i]
|
2019-03-14 04:23:36 +00:00
|
|
|
elif sys.argv[i] == "-z" or sys.argv[i] == "--zip":
|
|
|
|
create_zip = True
|
2019-03-15 04:34:38 +00:00
|
|
|
elif sys.argv[i] == "-c" or sys.argv[i] == "--continue":
|
|
|
|
build_all = False
|
2019-04-25 01:38:11 +00:00
|
|
|
build_continue = True
|
2019-03-15 04:34:38 +00:00
|
|
|
elif sys.argv[i] == "-f" or sys.argv[i] == "--fix":
|
|
|
|
build_all = False
|
|
|
|
build_fix = True
|
2019-04-13 04:26:49 +00:00
|
|
|
elif sys.argv[i] == "-S" or sys.argv[i] == "--chunk-size":
|
|
|
|
i += 1
|
|
|
|
chunk_size = sys.argv[i]
|
2019-03-14 04:23:36 +00:00
|
|
|
elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
|
|
|
|
print("usage: build <project> [OPTIONS]")
|
|
|
|
print("Builds the tiles with osm2city")
|
|
|
|
print("")
|
|
|
|
print("OPTIONS")
|
2019-04-13 04:26:49 +00:00
|
|
|
print(" -h, --help Shows this help and exit")
|
|
|
|
print(" -t, --threads Number of threads used for building")
|
|
|
|
print(" This will overwrite the value from the general-settings file")
|
|
|
|
print(" -z, --zip Create a ready to distribute zip file on success")
|
|
|
|
print(" -S, --chunk-size Chunks in which the scenery is build.")
|
|
|
|
print(" Smaller numbers allow better continuation on interruption")
|
2019-03-15 04:34:38 +00:00
|
|
|
print("")
|
2019-04-13 04:26:49 +00:00
|
|
|
print(" -c, --continue Continue interrupted build")
|
|
|
|
print(" -f, --fix Try to rebuild tiles wich had exceptions in the previous run")
|
2019-03-14 04:23:36 +00:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
if first == 1:
|
|
|
|
first = 0
|
|
|
|
project = sys.argv[i]
|
|
|
|
else:
|
|
|
|
print("Unknown option " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
|
|
|
i += 1
|
2018-11-13 17:31:24 +00:00
|
|
|
|
2019-03-14 04:23:36 +00:00
|
|
|
if project == "":
|
|
|
|
print("No project was given. See build -h for details")
|
|
|
|
sys.exit(1)
|
2019-03-04 08:45:14 +00:00
|
|
|
|
2019-03-15 04:36:25 +00:00
|
|
|
# Takes a file as input and returns a list of bounds
|
2019-03-15 03:06:49 +00:00
|
|
|
def get_bounds(file_path):
|
|
|
|
try:
|
|
|
|
with open(file_path) as f:
|
2019-03-16 02:19:02 +00:00
|
|
|
hits = []
|
2019-03-15 03:06:49 +00:00
|
|
|
lines = f.readlines()
|
|
|
|
for line in lines:
|
2019-03-15 07:47:23 +00:00
|
|
|
match = re.findall("(-?[0-9]{1,3}\.?[0-9]{0,4})_(-?[0-9]{1,2}\.?[0-9]{0,4})_(-?[0-9]{1,3}\.?[0-9]{0,4})_(-?[0-9]{1,2}\.?[0-9]{0,4})", line)
|
2019-03-15 08:00:13 +00:00
|
|
|
if match != []:
|
2019-03-15 07:47:23 +00:00
|
|
|
bounds = { "west": float(match[0][0]), "south": float(match[0][1]), "east": float(match[0][2]), "north": float(match[0][3]) }
|
2019-03-16 02:19:02 +00:00
|
|
|
hits.append(bounds)
|
|
|
|
# Filter duplicates
|
|
|
|
ret = []
|
|
|
|
ret.append(hits[0])
|
|
|
|
for hit in hits:
|
|
|
|
duplicate = False
|
|
|
|
for coords in ret:
|
|
|
|
if coords == hit:
|
|
|
|
duplicate = True
|
2019-03-18 01:03:05 +00:00
|
|
|
if not duplicate:
|
2019-03-16 02:19:02 +00:00
|
|
|
ret.append(coords)
|
2019-03-15 03:06:49 +00:00
|
|
|
return ret
|
|
|
|
except:
|
|
|
|
print("WARNING: Getting bounds failed")
|
2019-03-15 06:10:53 +00:00
|
|
|
return []
|
2019-03-15 03:06:49 +00:00
|
|
|
|
2018-11-13 15:31:53 +00:00
|
|
|
|
2019-04-13 04:17:41 +00:00
|
|
|
def run_build(west, south, east, north, log_done=False, start_w=None, start_s=None, chunk_size=1):
|
2019-03-15 04:36:25 +00:00
|
|
|
width = ceil(east - west)
|
|
|
|
height = ceil(north - south)
|
2018-11-13 15:31:53 +00:00
|
|
|
|
2019-03-17 04:38:33 +00:00
|
|
|
if start_w != None and start_s != None:
|
|
|
|
start_offset = True
|
|
|
|
s = ceil(north - start_s)
|
|
|
|
w = ceil(east - start_w)
|
|
|
|
|
|
|
|
# Case most east tile has been build last
|
|
|
|
if w == width:
|
|
|
|
w = 0
|
|
|
|
s += 1
|
|
|
|
else:
|
|
|
|
start_offset = False
|
|
|
|
w = 0
|
|
|
|
s = 0
|
|
|
|
|
|
|
|
while w < width:
|
2019-03-15 04:36:25 +00:00
|
|
|
build_w = west + w
|
2019-04-13 04:17:41 +00:00
|
|
|
build_e = build_w + chunk_size
|
2019-03-15 04:36:25 +00:00
|
|
|
if build_e > east:
|
|
|
|
build_e = east
|
2018-11-13 15:31:53 +00:00
|
|
|
|
2019-03-15 04:36:25 +00:00
|
|
|
if build_w < 0:
|
|
|
|
build_w = "*" + str(build_w)
|
2018-11-13 17:31:24 +00:00
|
|
|
|
2019-03-17 04:38:33 +00:00
|
|
|
if start_offset:
|
|
|
|
start_offset = False
|
|
|
|
else:
|
|
|
|
s = 0
|
|
|
|
|
|
|
|
while s < height:
|
2019-03-15 04:36:25 +00:00
|
|
|
build_s = south + s
|
2019-04-13 04:17:41 +00:00
|
|
|
build_n = build_s + chunk_size
|
2019-03-15 04:36:25 +00:00
|
|
|
if build_n > north:
|
|
|
|
build_n = north
|
2018-11-13 15:31:53 +00:00
|
|
|
|
2019-04-04 06:34:03 +00:00
|
|
|
run_command("bash build_tile " + project + " " + man_threads + " -b " + str(build_w) + "_" + str(build_s) + "_" + str(build_e) + "_" + str(build_n))
|
2019-03-15 06:10:53 +00:00
|
|
|
|
|
|
|
if log_done:
|
|
|
|
try:
|
|
|
|
f = open("projects/" + project + "/done", "w")
|
|
|
|
f.write(str(build_w) + "_" + str(build_s) + "_" + str(build_e) + "_" + str(build_n))
|
|
|
|
f.close()
|
|
|
|
except:
|
|
|
|
print("WARNING: Writing done file failed")
|
2019-04-25 01:41:21 +00:00
|
|
|
s += chunk_size
|
|
|
|
w += chunk_size
|
2018-11-13 15:31:53 +00:00
|
|
|
|
2019-04-04 06:34:03 +00:00
|
|
|
def run_command(command):
|
|
|
|
exit_code = os.system(command)
|
|
|
|
if exit_code == 0:
|
|
|
|
return
|
2019-04-04 06:51:21 +00:00
|
|
|
elif exit_code == 1:
|
|
|
|
print("An argument problem occured while running '" + command + "'")
|
|
|
|
sys.exit(1)
|
2019-04-04 06:34:03 +00:00
|
|
|
elif exit_code == 130:
|
|
|
|
print("Interrupted by user. Aborting...")
|
2019-04-04 06:48:30 +00:00
|
|
|
sys.exit(130)
|
2019-04-04 06:34:03 +00:00
|
|
|
|
2019-03-15 04:36:25 +00:00
|
|
|
start_time = time.time()
|
|
|
|
|
2019-03-16 02:19:27 +00:00
|
|
|
try:
|
|
|
|
if build_all:
|
|
|
|
bounds = get_bounds("projects/" + project + "/settings")
|
2019-03-15 04:36:25 +00:00
|
|
|
if bounds == []:
|
2019-03-15 06:10:53 +00:00
|
|
|
print("Unable to read project settings")
|
|
|
|
sys.exit(1)
|
2019-04-04 06:34:03 +00:00
|
|
|
run_command("rm -f projects/" + project + "/osm2city-exceptions.log")
|
2019-03-16 02:19:27 +00:00
|
|
|
run_build(bounds[0]["west"], bounds[0]["south"], bounds[0]["east"], bounds[0]["north"], log_done=True)
|
|
|
|
else:
|
|
|
|
if build_fix:
|
|
|
|
bounds = get_bounds("projects/" + project + "/osm2city-exceptions.log")
|
2019-04-04 06:34:03 +00:00
|
|
|
run_command("rm -f projects/" + project + "/osm2city-exceptions.log")
|
2019-03-16 02:19:27 +00:00
|
|
|
if bounds == []:
|
|
|
|
print("INFO: Skip fix run: no exceptions found")
|
|
|
|
else:
|
|
|
|
for area in bounds:
|
|
|
|
run_build(area["west"], area["south"], area["east"], area["north"])
|
2019-04-25 01:38:11 +00:00
|
|
|
|
2019-03-16 02:19:27 +00:00
|
|
|
if build_continue:
|
|
|
|
start_bounds = get_bounds("projects/" + project + "/done")
|
|
|
|
if start_bounds == []:
|
|
|
|
print("No start bounds found. Please rerun without -c")
|
|
|
|
sys.exit(1)
|
|
|
|
project_bounds = get_bounds("projects/" + project + "/settings")
|
|
|
|
if project_bounds == []:
|
|
|
|
print("Unable to read project settings")
|
2019-04-04 06:48:30 +00:00
|
|
|
sys.exit(2)
|
2019-03-17 04:38:33 +00:00
|
|
|
start_bounds = start_bounds[0]
|
|
|
|
project_bounds = project_bounds[0]
|
2019-04-25 01:16:25 +00:00
|
|
|
chunks = start_bounds["east"] - start_bounds["west"]
|
2019-03-16 02:19:27 +00:00
|
|
|
if start_bounds["north"] == project_bounds["north"] and start_bounds["east"] == project_bounds["east"]:
|
2019-04-13 04:17:41 +00:00
|
|
|
print("It seems like the project build is complete. If you wish to rerun please do so without -c")
|
2019-03-16 02:19:27 +00:00
|
|
|
else:
|
2019-04-25 01:50:38 +00:00
|
|
|
run_build(project_bounds["west"], project_bounds["south"], project_bounds["east"], project_bounds["north"], log_done=True, start_w=start_bounds["east"], start_s=start_bounds["south"], chunk_size=chunks)
|
2019-03-16 02:19:27 +00:00
|
|
|
except KeyboardInterrupt:
|
2019-03-17 04:38:33 +00:00
|
|
|
print("Interrupted by user!")
|
2019-04-25 01:16:25 +00:00
|
|
|
sys.exit(130)
|
|
|
|
|
|
|
|
except:
|
|
|
|
print("An unknown error ocurred!")
|
|
|
|
sys.exit(4)
|
|
|
|
|
2018-11-13 15:31:53 +00:00
|
|
|
|
2019-03-14 04:23:36 +00:00
|
|
|
# Get build time
|
|
|
|
end_time = time.time()
|
|
|
|
elapsed = end_time - start_time
|
|
|
|
seconds = elapsed % 60
|
|
|
|
elapsed = (elapsed - seconds) / 60
|
|
|
|
minutes = elapsed % 60
|
|
|
|
elapsed = (elapsed - minutes) / 60
|
|
|
|
hours = elapsed % 24
|
|
|
|
days = (elapsed - hours) / 24
|
2019-03-04 08:45:14 +00:00
|
|
|
|
2019-03-15 03:06:49 +00:00
|
|
|
time = str(int(hours)) + " Hours, " + str(int(minutes)) + " Minutes and " + str(int(seconds)) + " Seconds"
|
2019-03-14 04:23:36 +00:00
|
|
|
if days > 0:
|
2019-03-15 03:06:49 +00:00
|
|
|
time = str(int(days)) + " Days, " + time
|
2019-03-04 08:45:14 +00:00
|
|
|
|
2019-03-15 03:06:49 +00:00
|
|
|
print("Building " + project + " took " + time)
|
2019-03-14 04:23:36 +00:00
|
|
|
|
2019-03-14 14:16:49 +00:00
|
|
|
if os.path.isfile("projects/" + project + "/osm2city-exceptions.log"):
|
|
|
|
print("There have been exceptionss in the build.")
|
|
|
|
print("Please check the osm2city-exceptions.log")
|
2019-03-15 04:36:25 +00:00
|
|
|
print("If you want to rebuild only failed tiles, run with the -f parameter")
|
2019-03-14 14:16:49 +00:00
|
|
|
elif create_zip == True:
|
|
|
|
print("Creating zip file...")
|
2019-04-04 06:34:03 +00:00
|
|
|
run_command("zip -rq projects/" + project + "/" + project + ".zip projects/" + project + "/scenery/")
|