From 46de30d19fed087d427a8e00b20a4d5c69618cbe Mon Sep 17 00:00:00 2001 From: viet nguyen Date: Mon, 19 Feb 2024 17:06:52 -0800 Subject: [PATCH] fix: create working dir fix: add missing script --- scripts/gen-tiles.sh | 16 ++++++++++++++++ src/db/utils/jobs/MapTiles/exportCmd.ts | 25 ++++++++++++++++--------- 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100755 scripts/gen-tiles.sh diff --git a/scripts/gen-tiles.sh b/scripts/gen-tiles.sh new file mode 100755 index 0000000..27eecad --- /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 68f9915..278faab 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()