Skip to content

Commit 18e2832

Browse files
authored
Implement quick tile generation with imputing (#383)
Improve `generate-tiles` script. By default, this script does NOT change current tile generation behavior except for two things: * it will take into account the min/max zooms as defined in the tileset yaml file, and pass them to the `tilelive-pgquery`, so that the tilelive info block is correctly generated. * it will run mbtiles-tools meta-generate at the end to update the info block with the actual zoom ranges and other tileset yaml data (this step is currently done by OMT makefile) ### New functionality If MID_ZOOM env variable is set, this script will first generate all tiles from MIN_ZOOM to MID_ZOOM. Afterwards, the script will generate one zoom level at a time, making sure to only generate non-empty tiles. A non-empty tile is a tile that has some data in the previous zoom. All other tiles will be imputed using "mbtiles-tools impute" command. Lastly, this also changes the version to 0.0.0 to avoid accidental publishing (the workflow will automatically fix this when publishing)
1 parent 0dab317 commit 18e2832

File tree

3 files changed

+92
-39
lines changed

3 files changed

+92
-39
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ RUN set -eux ;\
124124
npm install -g \
125125
126126
127-
tilelive-pgquery@1.1.0 ;\
127+
tilelive-pgquery@1.2.0 ;\
128128
\
129129
/bin/bash -c 'echo ""; echo ""; echo "##### Cleaning up"' >&2 ;\
130130
rm -rf /var/lib/apt/lists/*

bin/generate-tiles

Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,59 @@ set -o errexit
33
set -o pipefail
44
set -o nounset
55

6+
#
7+
# Generate tiles from postgres using tilelive-copy and tilelive-pgquery
8+
#
9+
# If run without MID_ZOOM parameter, this script will generate all tiles from MIN_ZOOM to MAX_ZOOM
10+
# If MID_ZOOM is set, it will first generate all tiles from MIN_ZOOM to MID_ZOOM. Afterwards,
11+
# the script will generate one zoom level at a time, making sure to only generate non-empty tiles.
12+
# A non-empty tile is a tile that has some data in the previous zoom. All other tiles will be imputed
13+
# using "mbtiles-tools impute" command.
14+
#
15+
616
# For backward compatibility, allow both PG* and POSTGRES_* forms,
717
# with the non-standard POSTGRES_* form taking precedence.
818
# An error will be raised if neither form is given, except for the PGPORT
9-
export PGDATABASE="${POSTGRES_DB:-${PGDATABASE?}}"
10-
export PGUSER="${POSTGRES_USER:-${PGUSER?}}"
11-
export PGPASSWORD="${POSTGRES_PASSWORD:-${PGPASSWORD?}}"
12-
export PGPORT="${POSTGRES_PORT:-${PGPORT:-5432}}"
19+
: "${PGDATABASE:=${POSTGRES_DB:-${PGDATABASE?}}}"
20+
: "${PGUSER:=${POSTGRES_USER:-${PGUSER?}}}"
21+
: "${PGPASSWORD:=${POSTGRES_PASSWORD:-${PGPASSWORD?}}}"
22+
: "${PGPORT:=${POSTGRES_PORT:-${PGPORT:-5432}}}"
1323

1424
# List of postgres servers
1525
# "xxx.xxx.xxx.xxx&host=xxx.xxx.xxx.xxx&host=..."
16-
if [[ -z "${PGHOSTS_LIST}" ]]; then
17-
export HOST_COUNT=1
18-
export PGHOSTS="${POSTGRES_HOST:-${PGHOST?}}"
26+
if [[ -z "${PGHOSTS_LIST-}" ]]; then
27+
: "${HOST_COUNT:=1}"
28+
: "${PGHOSTS:=${POSTGRES_HOST:-${PGHOST?}}}"
1929
else
20-
HOST_COUNT=$(awk -F"&" '{print NF}' <<< "${PGHOSTS_LIST}")
21-
export HOST_COUNT
22-
export PGHOSTS="${PGHOSTS_LIST}"
30+
: "${HOST_COUNT:=$(awk -F"&" '{print NF}' <<< "${PGHOSTS_LIST}")}"
31+
: "${PGHOSTS:=${PGHOSTS_LIST}}"
2332
fi
2433

25-
export FUNC_ZXY=${FUNC_ZXY:-getmvt}
26-
export COPY_CONCURRENCY=${COPY_CONCURRENCY:-1} # number of CPUs per postgres server
27-
export MAX_HOST_CONNECTIONS=${MAX_HOST_CONNECTIONS:-${COPY_CONCURRENCY}}
28-
export ALL_STREAMS=$(( MAX_HOST_CONNECTIONS * HOST_COUNT ))
34+
: "${FUNC_ZXY:=${FUNC_ZXY:-getmvt}}"
35+
: "${COPY_CONCURRENCY:=${COPY_CONCURRENCY:-1}}" # number of CPUs per postgres server
36+
: "${MAX_HOST_CONNECTIONS:=${MAX_HOST_CONNECTIONS:-${COPY_CONCURRENCY}}}"
37+
: "${ALL_STREAMS:=$(( MAX_HOST_CONNECTIONS * HOST_COUNT ))}"
38+
39+
: "${EXPORT_DIR:=${EXPORT_DIR:-/export}}"
40+
: "${MBTILES_FILE:=${MBTILES_FILE:-tiles.mbtiles}}"
41+
: "${MBTILES_PATH:=${MBTILES_PATH:-${EXPORT_DIR}/${MBTILES_FILE}}}"
42+
43+
: "${RETRY:=${RETRY:-2}}"
44+
: "${BBOX:=${BBOX:-"-180.0,-85.0511,180.0,85.0511"}}"
45+
: "${RENDER_SCHEME:=${RENDER_SCHEME:-pyramid}}"
46+
: "${MIN_ZOOM:=${MIN_ZOOM:-0}}"
47+
: "${MAX_ZOOM:=${MAX_ZOOM:-14}}"
2948

30-
export EXPORT_DIR=${EXPORT_DIR:-/export}
31-
export MBTILES_FILE=${MBTILES_FILE:-tiles.mbtiles}
32-
export RENDER_SCHEME=${RENDER_SCHEME:-pyramid}
33-
export RETRY=${RETRY:-2}
34-
export BBOX=${BBOX:-"-180.0,-85.0511,180.0,85.0511"}
35-
export TIMEOUT=${TIMEOUT:-1800000}
36-
export MIN_ZOOM=${MIN_ZOOM:-0}
37-
export MAX_ZOOM=${MAX_ZOOM:-14}
49+
if [[ -z "${TILESET_FILE:-}" ]]; then
50+
echo "WARNING: Env var TILESET_FILE is not set to a valid tileset yaml file. Unable to load min/max zooms. Metadata will not be generated"
51+
elif [[ ! -f "${TILESET_FILE:-}" ]]; then
52+
echo "Invalid tileset file: TILESET_FILE='$TILESET_FILE'"
53+
exit 1
54+
else
55+
# Get tileset min/max zooms for pgquery info. If the yaml file cannot be parsed, will use min/max zooms from above
56+
: "${TILESET_MIN_ZOOM:=${TILESET_MIN_ZOOM:-$(grep -Poh '(?<=minzoom:)[^\n]+' < "$TILESET_FILE" |xargs)}}"
57+
: "${TILESET_MAX_ZOOM:=${TILESET_MAX_ZOOM:-$(grep -Poh '(?<=maxzoom:)[^\n]+' < "$TILESET_FILE" |xargs)}}"
58+
fi
3859

3960
PGQUERY="pgquery://\
4061
?database=${PGDATABASE}\
@@ -44,28 +65,60 @@ PGQUERY="pgquery://\
4465
&password=${PGPASSWORD}\
4566
&funcZXY=${FUNC_ZXY}\
4667
&maxpool=${MAX_HOST_CONNECTIONS}\
47-
&minzoom=${MIN_ZOOM}\
48-
&maxzoom=${MAX_ZOOM}\
68+
&minzoom=${TILESET_MIN_ZOOM:-$MIN_ZOOM}\
69+
&maxzoom=${TILESET_MAX_ZOOM:-$MAX_ZOOM}\
4970
${GZIP:+&gzip=${GZIP}}\
5071
${NOGZIP:+&nogzip=${NOGZIP}}\
5172
${USE_KEY_COLUMN:+&key=${USE_KEY_COLUMN}}\
5273
${TEST_ON_STARTUP_TILE:+&testOnStartup=${TEST_ON_STARTUP}}"
5374

54-
export PGQUERY
55-
56-
function export_local_mbtiles() {
57-
echo "Generating zoom $MIN_ZOOM..$MAX_ZOOM from $HOST_COUNT servers, using $MAX_HOST_CONNECTIONS connections per server, $ALL_STREAMS parallel streams..."
58-
set -x
59-
exec tilelive-copy \
60-
--scheme="$RENDER_SCHEME" \
75+
function run_tilelive_copy() {
76+
set -x -o errexit
77+
tilelive-copy "${@}" \
78+
--exit \
6179
--retry="$RETRY" \
62-
--bounds="$BBOX" \
63-
--timeout="$TIMEOUT" \
64-
--minzoom="$MIN_ZOOM" \
65-
--maxzoom="$MAX_ZOOM" \
6680
--concurrency="$ALL_STREAMS" \
6781
"$PGQUERY" \
68-
"mbtiles://${EXPORT_DIR}/${MBTILES_FILE}"
82+
"mbtiles://${MBTILES_PATH}"
83+
{ set +x ;} 2> /dev/null
6984
}
7085

71-
export_local_mbtiles
86+
87+
if [[ -z "${MID_ZOOM-}" ]]; then
88+
89+
# One pass zoom - generate all tiles in one pass
90+
echo "$(date '+%Y-%m-%d %H-%M-%S') Generating zoom $MIN_ZOOM..$MAX_ZOOM from $HOST_COUNT servers, using $MAX_HOST_CONNECTIONS connections per server, $ALL_STREAMS parallel streams..."
91+
run_tilelive_copy --scheme="$RENDER_SCHEME" --bounds="$BBOX" --minzoom="$MIN_ZOOM" --maxzoom="$MAX_ZOOM" --timeout="${TIMEOUT:-1800000}"
92+
93+
else
94+
95+
# Generate all tiles up to MID_ZOOM. Afterwards only generate those tiles where zoom-1 is not empty
96+
echo "$(date '+%Y-%m-%d %H-%M-%S') Generating zoom $MIN_ZOOM..$MID_ZOOM pyramid from $HOST_COUNT servers, using $MAX_HOST_CONNECTIONS connections per server, $ALL_STREAMS parallel streams..."
97+
run_tilelive_copy --scheme="$RENDER_SCHEME" --bounds="$BBOX" --minzoom="$MIN_ZOOM" --maxzoom="$MID_ZOOM" --timeout="${TIMEOUT:-1800000}"
98+
99+
# Do not print extra info more than once
100+
PGQUERY="${PGQUERY}&serverInfo=&specInfo="
101+
102+
for (( ZOOM=MID_ZOOM+1; ZOOM<=MAX_ZOOM; ZOOM++ )); do
103+
LIST_FILE="$EXPORT_DIR/tiles_$ZOOM.txt"
104+
echo "$(date '+%Y-%m-%d %H-%M-%S') Imputing tiles for zoom $ZOOM"
105+
set -x
106+
mbtiles-tools impute "$MBTILES_PATH" --zoom "$ZOOM" --output "$LIST_FILE" --verbose
107+
{ set +x ;} 2> /dev/null
108+
echo "$(date '+%Y-%m-%d %H-%M-%S') Generating zoom $ZOOM using a tile list $LIST_FILE from $HOST_COUNT servers, using $MAX_HOST_CONNECTIONS connections per server, $ALL_STREAMS streams"
109+
# Use smaller timeout by default because high zooms should generate faster
110+
run_tilelive_copy --scheme=list "--list=$LIST_FILE" --timeout="${TIMEOUT:-180000}"
111+
done
112+
113+
fi
114+
115+
if [[ -z "${TILESET_FILE:-}" ]]; then
116+
echo "WARNING: Env var TILESET_FILE is not set to a valid tileset yaml file. Skipping metadata generation"
117+
else
118+
echo "$(date '+%Y-%m-%d %H-%M-%S') Updating generated tile metadata from $TILESET_FILE"
119+
set -x
120+
mbtiles-tools meta-generate "$MBTILES_PATH" "$TILESET_FILE" --auto-minmax --show-ranges
121+
{ set +x ;} 2> /dev/null
122+
fi
123+
124+
echo "$(date '+%Y-%m-%d %H-%M-%S') Tile generation complete!"

openmaptiles/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '5.3.2'
1+
__version__ = '0.0.0'

0 commit comments

Comments
 (0)