diff --git a/scripts/gen-tiles.sh b/scripts/gen-tiles.sh new file mode 100755 index 00000000..27eecada --- /dev/null +++ b/scripts/gen-tiles.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# Generate map tiles from geojson exports +# See also https://github.com/felt/tippecanoe + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +. ${SCRIPT_DIR}/../.env +. ${SCRIPT_DIR}/../.env.local + +echo "Generating crags tiles file" +tippecanoe --force -o ${MAPTILES_WORKING_DIR}/crags.mbtiles -l crags -n "Crags" -zg ${MAPTILES_WORKING_DIR}/crags.*.geojson + +echo "Generating crag group tiles file" +tippecanoe --force -o ${MAPTILES_WORKING_DIR}/crag-groups.mbtiles -l crag-groups -n "Crag groups" -zg ${MAPTILES_WORKING_DIR}/crag-groups.geojson + +exit $? \ No newline at end of file diff --git a/src/db/utils/jobs/MapTiles/exportCmd.ts b/src/db/utils/jobs/MapTiles/exportCmd.ts index 68f9915d..278faab8 100644 --- a/src/db/utils/jobs/MapTiles/exportCmd.ts +++ b/src/db/utils/jobs/MapTiles/exportCmd.ts @@ -1,4 +1,4 @@ -import { WriteStream, createWriteStream } from 'node:fs' +import { WriteStream, createWriteStream, existsSync, mkdirSync } from 'node:fs' import { point, feature, @@ -27,14 +27,7 @@ import { workingDir } from './init.js' async function exportLeafCrags (): Promise { const model = getAreaModel() - let features: Array< - Feature< - Point, - { - name: string - } - > - > = [] + let features: Array> = [] let fileIndex = 0 let stream: WriteStream = createWriteStream(`${workingDir}/crags.${fileIndex}.geojson`, { @@ -219,8 +212,22 @@ async function exportCragGroups (): Promise { stream.close() } +/** + * Create working directory if it does not exist + */ +function prepareWorkingDir (): void { + if (!existsSync(workingDir)) { + logger.info(`Working dir doesn't exist. Creating ${workingDir}`) + mkdirSync(workingDir, { recursive: true }) + } +} + +/** + * Export crag data as Geojson + */ async function onDBConnected (): Promise { logger.info('Start exporting crag data as Geojson') + prepareWorkingDir() await exportLeafCrags() await exportCragGroups() await gracefulExit()