-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from klips-project/contourLines
feat: init contour-lines worker
- Loading branch information
Showing
16 changed files
with
140 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
FROM node:16-alpine | ||
|
||
RUN apk add --update nodejs npm | ||
|
||
COPY src/create-contour/package.json src/create-contour/package-lock.json /home/ | ||
COPY util/start-worker.sh util/wait-for.sh /home/ | ||
RUN chmod +x /home/start-worker.sh /home/wait-for.sh | ||
|
||
WORKDIR /home/ | ||
RUN npm install | ||
COPY src/create-contour/index.js worker/ | ||
COPY src/create-contour/get-client.js worker/ | ||
COPY src/create-contour/add-to-table.js worker/ | ||
COPY src/create-contour/contour.js worker/ | ||
COPY src/workerTemplate.js . | ||
COPY src/create-contour/child-logger.js worker/ | ||
COPY src/logger.js . | ||
|
||
HEALTHCHECK CMD pgrep node || exit 1 | ||
|
||
CMD ["./start-worker.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { getClient } from './get-client.js'; | ||
import logger from './child-logger.js'; | ||
|
||
// Add data to table | ||
export const addData = async ( | ||
datasetTimestamp, | ||
contourLine, | ||
region | ||
) => { | ||
// Check if line-vector contains all necessary properties | ||
if (!('geometry' in contourLine)) { | ||
// timestamp of dataset not valid | ||
logger.error('Object is missing geometry'); | ||
throw 'Object is missing geometry.'; | ||
} | ||
|
||
if (!('properties' in contourLine)) { | ||
// timestamp of dataset not valid | ||
logger.error('Object is missing properties'); | ||
throw 'Object is missing properties.'; | ||
} | ||
|
||
// Add to table | ||
const client = await getClient(); | ||
// TODO process.argv can potentially be removed | ||
const timestamp = process.argv[2] ?? datasetTimestamp; | ||
const geom = process.argv[2] ?? contourLine.geometry; | ||
const temp = process.argv[2] ?? contourLine.properties.TEMP; | ||
let insertRow = await client.query(`INSERT INTO ${region}_contourLines(timestamp, geom, temp) VALUES($1);`, [timestamp, geom, temp]); | ||
logger.info(`Inserted ${insertRow.rowCount} row`); | ||
await client.end(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { logger } from '../logger.js'; | ||
|
||
export default logger.child({ type: 'create-contour' }); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { exec } from 'child_process'; | ||
import fs from 'fs'; | ||
|
||
/** | ||
* Executes a shell command and return it as a Promise. | ||
* Kudos to https://ali-dev.medium.com/how-to-use-promise-with-exec-in-node-js-a39c4d7bbf77 | ||
* | ||
* @param cmd {String} The command to execute | ||
* @return {Promise<String>} A Promise returning the console output | ||
*/ | ||
const execShellCommand = (cmd) => { | ||
return new Promise((resolve, reject) => { | ||
exec(cmd, (error, stdout, stderr) => { | ||
if (error) { | ||
console.warn(error); | ||
reject(error); | ||
} | ||
resolve(stdout ? stdout : stderr); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Reclassify a GeoTIFF according to custom levels. | ||
* | ||
* Relies on GRASS | ||
* | ||
* @param {String} inputPath The path of the GeoTIFF to convert | ||
* @param {String} fileName The name of the GeoTIFF | ||
* @param {String} interval Elevation interval between contours | ||
* | ||
* @returns {Promise<String>} A Promise that resolves to the console output of the underlying GDAL process | ||
* | ||
* @throws If provided paths are invalid or conversion fails, an error is thrown | ||
*/ | ||
const createContourLines = async (inputPath, interval) => { | ||
// valdate inputPath | ||
if (! await fs.existsSync(inputPath)) { | ||
throw `Input file does not exist: ${inputPath}/`; | ||
} | ||
|
||
// build command for sub-process | ||
const contourCmd = `gdal_contour -b 2 -a TEMP -i ${interval} -f "GeoJSON" /${inputPath} /tmp/output.geojson`; | ||
|
||
return await execShellCommand(contourCmd); | ||
} | ||
|
||
export default createContourLines; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
["download-file","gunzip-file","geoserver-publish-sld","geoserver-create-and-apply-sld","geoserver-publish-layer-from-db","geonetwork-publish-metadata","send-email","dispatcher","zip-handler","upload-file", "geoserver-publish-geotiff", "geotiff-validator", "send-mattermost-message", "geoserver-publish-imagemosaic", "geoserver-create-imagemosaic-datastore", "create-file", "geotiff-optimizer", "dataset-rotation", "dataset-archive", "reclassify-geotiff"] | ||
["download-file","gunzip-file","geoserver-publish-sld","geoserver-create-and-apply-sld","geoserver-publish-layer-from-db","geonetwork-publish-metadata","send-email","dispatcher","zip-handler","upload-file", "geoserver-publish-geotiff", "geotiff-validator", "send-mattermost-message", "geoserver-publish-imagemosaic", "geoserver-create-imagemosaic-datastore", "create-file", "geotiff-optimizer", "dataset-rotation", "dataset-archive", "create-contour", "reclassify-geotiff"] |