2019-04-29 05:57:48 +00:00
|
|
|
#! /usr/bin/python
|
2019-03-25 04:45:03 +00:00
|
|
|
# Copyright (C) 2018-2019 Merspieler, merspieler _at_ airmail.cc
|
|
|
|
#
|
|
|
|
# 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-24 06:59:16 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2019-03-25 04:45:03 +00:00
|
|
|
import math
|
2019-03-25 04:50:10 +00:00
|
|
|
import re
|
2019-04-27 14:32:17 +00:00
|
|
|
import json
|
2019-04-28 04:41:56 +00:00
|
|
|
import time
|
2019-03-25 04:45:03 +00:00
|
|
|
|
2019-04-27 14:41:59 +00:00
|
|
|
chunk_size = 5
|
2019-04-28 04:45:12 +00:00
|
|
|
threads = 5
|
2019-05-09 01:40:57 +00:00
|
|
|
cont = 0
|
2019-06-28 03:40:57 +00:00
|
|
|
exclude = []
|
2019-08-07 15:39:05 +00:00
|
|
|
pbf_path = ""
|
2019-04-25 09:20:59 +00:00
|
|
|
|
2019-03-25 04:45:03 +00:00
|
|
|
argc = len(sys.argv)
|
|
|
|
i = 1
|
|
|
|
first = 1
|
|
|
|
while i < argc:
|
2019-05-09 01:40:57 +00:00
|
|
|
if sys.argv[i] == "-s" or sys.argv[i] == "--chunk-size":
|
2019-04-27 14:41:59 +00:00
|
|
|
i += 1
|
|
|
|
chunk_size = sys.argv[i]
|
2019-05-09 01:40:57 +00:00
|
|
|
elif sys.argv[i] == "-c" or sys.argv[i] == "--continue":
|
|
|
|
i += 1
|
2019-05-09 01:43:38 +00:00
|
|
|
cont = int(sys.argv[i])
|
2019-04-28 09:54:47 +00:00
|
|
|
elif sys.argv[i] == "-t" or sys.argv[i] == "--threads":
|
2019-04-28 04:45:12 +00:00
|
|
|
i += 1
|
|
|
|
threads = sys.argv[i]
|
2019-06-28 03:40:57 +00:00
|
|
|
elif sys.argv[i] == "-e" or sys.argv[i] == "--exclude":
|
|
|
|
i += 1
|
|
|
|
if os.path.isfile(sys.argv[i]):
|
|
|
|
try:
|
|
|
|
with open(sys.argv[i]) as json_data:
|
|
|
|
exclude += json.load(json_data)
|
|
|
|
except ValueError:
|
|
|
|
print("Exclude file '" + sys.argv[i] + "' is no proper JSON file.")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
print("File not found: " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
2019-04-27 14:41:59 +00:00
|
|
|
elif sys.argv[i] == "-p" or sys.argv[i] == "--progress":
|
2019-03-25 04:45:03 +00:00
|
|
|
try:
|
|
|
|
with open("projects/worldbuild/done") as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
for line in lines:
|
2019-05-08 06:57:32 +00:00
|
|
|
match = re.findall("(-?[0-9]{1,3}\.?[0-9]{0,4})_(-?[0-9]{1,3}\.?[0-9]{0,4})_(-?[0-9]{1,3}\.?[0-9]{0,4})_(-?[0-9]{1,3}\.?[0-9]{0,4})", line)
|
2019-03-25 04:45:03 +00:00
|
|
|
if match != []:
|
|
|
|
|
2019-03-25 04:56:50 +00:00
|
|
|
w = float(match[0][0])
|
2019-05-08 06:57:32 +00:00
|
|
|
s = float(match[0][1])
|
|
|
|
e = float(match[0][2])
|
|
|
|
n = float(match[0][3])
|
2019-03-25 04:45:03 +00:00
|
|
|
|
2019-05-08 06:57:32 +00:00
|
|
|
if n == 90:
|
|
|
|
world = 1
|
2019-05-11 01:59:51 +00:00
|
|
|
tile = "N/A"
|
2019-05-08 06:57:32 +00:00
|
|
|
elif n == -80:
|
|
|
|
world = 2
|
2019-05-11 01:59:51 +00:00
|
|
|
tile = "N/A"
|
2019-05-08 06:57:32 +00:00
|
|
|
else:
|
|
|
|
wm = w % 10
|
|
|
|
sm = s % 10
|
|
|
|
em = e % 10
|
|
|
|
nm = n % 10
|
2019-03-24 06:59:16 +00:00
|
|
|
|
2019-05-08 06:57:32 +00:00
|
|
|
wM = (int(w) - wm) / 10
|
|
|
|
sM = (int(s) - sm) / 10
|
|
|
|
eM = (int(e) - em) / 10
|
|
|
|
nM = (int(n) - nm) / 10
|
2019-03-28 12:30:25 +00:00
|
|
|
|
2019-05-08 08:04:52 +00:00
|
|
|
cs = n - s
|
2019-05-08 06:57:32 +00:00
|
|
|
if nm == 0:
|
2019-05-08 08:04:52 +00:00
|
|
|
nm = 10
|
2019-06-23 00:29:37 +00:00
|
|
|
tile = (wm % 10) * 10 + cs * cs * (nm / cs)
|
2019-03-25 04:45:03 +00:00
|
|
|
|
2019-05-08 06:57:32 +00:00
|
|
|
|
2019-03-30 03:28:04 +00:00
|
|
|
rows = 0
|
2019-05-08 03:02:04 +00:00
|
|
|
while sM > -8:
|
2019-03-30 03:28:04 +00:00
|
|
|
rows += 1
|
2019-05-08 03:02:04 +00:00
|
|
|
sM -= 1
|
2019-03-28 12:30:25 +00:00
|
|
|
|
2019-05-08 03:02:04 +00:00
|
|
|
world = 3 + 36 * rows + wM + 18
|
2019-03-28 12:30:25 +00:00
|
|
|
|
|
|
|
print("Current worldbuild tile is " + str(int(world)) + "/578")
|
2019-03-25 04:45:03 +00:00
|
|
|
print("Current tile " + str(tile) + "% complete")
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
print("Unable to get progress")
|
|
|
|
sys.exit(1)
|
|
|
|
except IOError:
|
|
|
|
print("Unable to get progress")
|
|
|
|
sys.exit(1)
|
|
|
|
elif sys.argv[i] == "-h" or sys.argv[i] == "--help":
|
2019-05-08 00:58:57 +00:00
|
|
|
print("usage: worldbuild <pbf-path> [OPTIONS]")
|
|
|
|
print("Builds the world")
|
2019-03-25 04:45:03 +00:00
|
|
|
print("")
|
|
|
|
print("OPTIONS")
|
2019-05-09 01:40:57 +00:00
|
|
|
print(" -s, --chunk-size Sets chunk size, default 5")
|
2019-04-28 04:45:12 +00:00
|
|
|
print(" -t, --threads Number of threads to run")
|
2019-05-09 01:40:57 +00:00
|
|
|
print(" -c, --continue Contine build from tile number <n>")
|
2019-06-28 03:40:57 +00:00
|
|
|
print(" -e, --exclude Files containing JSON array naming tiles not to be build")
|
|
|
|
print(" Can be used multiple times.")
|
|
|
|
print(" If not given projects/worldbuild/exclude will be used")
|
2019-04-27 14:41:59 +00:00
|
|
|
print(" -h, --help Shows this help and exit")
|
|
|
|
print(" -p, --progress Shows progress and exit")
|
2019-03-25 04:45:03 +00:00
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
if first == 1:
|
|
|
|
first = 0
|
|
|
|
pbf_path = sys.argv[i]
|
|
|
|
else:
|
|
|
|
print("Unknown option " + sys.argv[i])
|
|
|
|
sys.exit(1)
|
|
|
|
i += 1
|
2019-03-24 06:59:16 +00:00
|
|
|
|
2019-08-07 15:39:05 +00:00
|
|
|
if pbf_path == "":
|
|
|
|
print("No pbf-path was given, exiting...")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-04-04 06:54:42 +00:00
|
|
|
def run(command):
|
2019-04-30 04:43:17 +00:00
|
|
|
exit_code = os.system(command)
|
2019-04-04 06:54:42 +00:00
|
|
|
if exit_code == 0:
|
|
|
|
return
|
2019-04-13 05:01:22 +00:00
|
|
|
elif exit_code == 130:
|
2019-04-04 06:54:42 +00:00
|
|
|
print("Aborted!")
|
|
|
|
sys.exit(130)
|
2019-04-29 05:41:30 +00:00
|
|
|
else:
|
2019-04-30 04:41:36 +00:00
|
|
|
print("Sub process '" + command + "'exited with code " + str(exit_code) + ". Aborting!")
|
2019-04-29 05:41:30 +00:00
|
|
|
sys.exit(4)
|
2019-04-04 06:54:42 +00:00
|
|
|
|
|
|
|
run("mkdir -p projects/worldbuild/output/error")
|
2019-03-24 06:59:16 +00:00
|
|
|
|
2019-05-09 01:40:57 +00:00
|
|
|
def build_tile(name, west, south, east, north, chunk_size, threads, cont=False):
|
2019-04-28 09:51:58 +00:00
|
|
|
global pbf_path
|
2019-03-24 06:59:16 +00:00
|
|
|
if west < 0:
|
|
|
|
west = "*" + str(west)
|
|
|
|
else:
|
|
|
|
west = str(west)
|
|
|
|
south = str(south)
|
|
|
|
east = str(east)
|
|
|
|
north = str(north)
|
|
|
|
|
2019-04-04 06:54:42 +00:00
|
|
|
run("./read-pbf worldbuild " + pbf_path + name + ".osm.pbf")
|
2019-04-28 10:21:21 +00:00
|
|
|
run('echo "bounds=' + west + "_" + south + "_" + east + "_" + north + '" > projects/worldbuild/settings')
|
2019-04-28 10:03:07 +00:00
|
|
|
run("./build worldbuild --chunk-size " + str(chunk_size) + " -t " + str(threads))
|
2019-03-24 06:59:16 +00:00
|
|
|
|
|
|
|
def after_build(name):
|
|
|
|
if os.path.isfile("projects/worldbuild/osm2city-exceptions.log"):
|
2019-04-04 06:54:42 +00:00
|
|
|
run("mv projects/worldbuild/osm2city-exceptions.log projects/worldbuild/output/error/" + name + ".exceptions.log")
|
|
|
|
run("zip -rq projects/worldbuild/output/error/" + name + ".zip projects/worldbuild/scenery/ ")
|
2019-04-24 07:29:01 +00:00
|
|
|
|
|
|
|
# Trigger failed after build script
|
2019-04-25 09:18:11 +00:00
|
|
|
if os.path.isfile("./scripts/afterbuild-failed"):
|
|
|
|
os.system("./scripts/afterbuild-failed " + name + " &")
|
2019-03-24 06:59:16 +00:00
|
|
|
else:
|
2019-04-04 06:54:42 +00:00
|
|
|
run("zip -rq projects/worldbuild/output/" + name + ".zip projects/worldbuild/scenery/ ")
|
2019-04-24 07:29:01 +00:00
|
|
|
|
|
|
|
# Trigger after build script
|
2019-04-25 09:18:11 +00:00
|
|
|
if os.path.isfile("./scripts/afterbuild-success"):
|
|
|
|
os.system("./scripts/afterbuild-success " + name + " &")
|
2019-04-24 07:29:01 +00:00
|
|
|
|
2019-03-24 06:59:16 +00:00
|
|
|
def prepare():
|
2019-04-04 06:54:42 +00:00
|
|
|
run("./delete-db worldbuild")
|
|
|
|
run("./create-db worldbuild")
|
2019-03-24 06:59:16 +00:00
|
|
|
|
2019-05-10 09:46:57 +00:00
|
|
|
run("rm -rf projects/worldbuild/scenery/*")
|
|
|
|
run("./clear-cache-files worldbuild")
|
|
|
|
|
2019-05-09 01:45:23 +00:00
|
|
|
def run_all(name, w, s, e, n, chunk_size, threads, cont=False):
|
2019-04-30 04:25:06 +00:00
|
|
|
global pbf_path
|
2019-04-30 04:39:43 +00:00
|
|
|
if os.system("ls -l " + pbf_path + name + ".osm.pbf | grep ' 73 ' > /dev/null") != 0:
|
2019-08-07 16:51:14 +00:00
|
|
|
if cont == False:
|
|
|
|
prepare()
|
2019-05-09 01:45:23 +00:00
|
|
|
build_tile(name, w, s, e, n, chunk_size, threads, cont)
|
2019-04-30 04:25:06 +00:00
|
|
|
after_build(name)
|
|
|
|
else:
|
|
|
|
print("INFO: Skipping " + name + " because pbf file is empty")
|
2019-03-24 06:59:16 +00:00
|
|
|
|
|
|
|
def norm(num, length):
|
|
|
|
num = str(num)
|
|
|
|
while len(num) < length:
|
|
|
|
num = "0" + num
|
|
|
|
return num
|
|
|
|
|
2019-04-30 04:28:47 +00:00
|
|
|
def print_build_time(start_time, end_time):
|
2019-04-28 04:41:56 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
time = str(int(hours)) + " Hours, " + str(int(minutes)) + " Minutes and " + str(int(seconds)) + " Seconds"
|
|
|
|
if days > 0:
|
|
|
|
time = str(int(days)) + " Days, " + time
|
|
|
|
|
|
|
|
print("Running worldbuild took " + time)
|
|
|
|
|
2019-04-27 14:32:17 +00:00
|
|
|
# Get exclude file
|
2019-06-28 03:40:57 +00:00
|
|
|
if os.path.isfile("projects/worldbuild/exclude") and exclude == []:
|
|
|
|
try:
|
|
|
|
with open("projects/worldbuild/exclude") as json_data:
|
|
|
|
exclude = json.load(json_data)
|
|
|
|
except ValueError:
|
|
|
|
print("Exclude file 'projects/worldbuild/exclude' is no proper JSON file.")
|
|
|
|
sys.exit(1)
|
2019-04-27 14:32:17 +00:00
|
|
|
|
2019-04-28 04:41:56 +00:00
|
|
|
start_time = time.time()
|
|
|
|
|
2019-05-09 01:40:57 +00:00
|
|
|
if cont != 0:
|
|
|
|
tile = cont - 2
|
|
|
|
tile_in_row = ((tile - 1) % 36) - 18
|
|
|
|
ii = (tile - tile_in_row - 19) / 36 - 8
|
|
|
|
else:
|
|
|
|
ii = -8
|
2019-05-11 01:47:03 +00:00
|
|
|
# Build poles first
|
|
|
|
if not "n-pole" in exclude and cont <= 1:
|
|
|
|
run_all("n-pole", -180, 80, 180, 90, 360, threads)
|
|
|
|
|
|
|
|
if not "s-pole" in exclude and cont <= 2:
|
|
|
|
run_all("s-pole", -180, -90, 180, -80, 360, threads)
|
|
|
|
|
2019-05-09 01:40:57 +00:00
|
|
|
while ii < 8:
|
|
|
|
i = ii * 10
|
|
|
|
if cont != 0:
|
|
|
|
jj = tile_in_row
|
|
|
|
else:
|
|
|
|
jj = -18
|
|
|
|
while jj < 18:
|
|
|
|
j = jj * 10
|
2019-03-24 06:59:16 +00:00
|
|
|
if i >= 0:
|
|
|
|
ns = "n"
|
|
|
|
else:
|
|
|
|
ns = "s"
|
|
|
|
if j >= 0:
|
|
|
|
ew = "e"
|
|
|
|
else:
|
|
|
|
ew = "w"
|
|
|
|
|
2019-04-22 00:14:42 +00:00
|
|
|
name = ew + norm(abs(j), 3) + ns + norm(abs(i), 2)
|
2019-03-24 06:59:16 +00:00
|
|
|
|
2019-05-08 03:02:04 +00:00
|
|
|
if not name in exclude:
|
2019-05-09 01:40:57 +00:00
|
|
|
if cont != 0:
|
|
|
|
run_all(name, j, i, j + 10, i + 10, chunk_size, threads, cont=True)
|
|
|
|
cont = 0
|
|
|
|
else:
|
2019-05-09 01:45:23 +00:00
|
|
|
run_all(name, j, i, j + 10, i + 10, chunk_size, threads, cont=False)
|
2019-05-09 01:40:57 +00:00
|
|
|
jj += 1
|
|
|
|
ii += 1
|
2019-03-24 06:59:16 +00:00
|
|
|
|
2019-04-30 04:28:47 +00:00
|
|
|
print_build_time(start_time, time.time())
|
2019-03-24 06:59:16 +00:00
|
|
|
|
|
|
|
|