Process comment markers server-side.

Replace @POS@, @POSITION@ and @DMS@ in the remarks
with the event's position (sexagesimal degrees for
the last one).
This commit is contained in:
D. Berge
2021-05-15 01:54:07 +02:00
parent 838b45ef26
commit 142a820ed7
4 changed files with 95 additions and 1 deletions

View File

@@ -1,5 +1,22 @@
const { setSurvey } = require('../connection');
const { geometryAsString } = require('../../utils');
function replaceMarkers (item, opts={}) {
const textkey = opts.text || "remarks";
const text = item[textkey];
if (text && typeof text === "string") {
item[textkey] = text
.replace(/@POS(ITION)?@/g, geometryAsString(item, opts) || "(position unknown)")
.replace(/@DMS@/g, geometryAsString(item, {...opts, dms:true}) || "(position unknown)")
}
return item;
}
async function list (projectId, opts = {}) {
const client = await setSurvey(projectId);
@@ -29,7 +46,7 @@ async function list (projectId, opts = {}) {
const res = await client.query(text, filter[1]);
client.release();
return res.rows;
return res.rows.map(i => replaceMarkers(i));
}
module.exports = list;

View File

@@ -0,0 +1,36 @@
function dms (lat, lon) {
const λh = lat < 0 ? "S" : "N";
const φh = lon < 0 ? "W" : "E";
const λn = Math.abs(lat);
const φn = Math.abs(lon);
const λi = Math.trunc(λn);
const φi = Math.trunc(φn);
const λf = λn - λi;
const φf = φn - φi;
const λs = ((λf*3600)%60).toFixed(1);
const φs = ((φf*3600)%60).toFixed(1);
const λm = Math.trunc(λf*60);
const φm = Math.trunc(φf*60);
const λ =
String(λi).padStart(2, "0") + "°" +
String(λm).padStart(2, "0") + "'" +
String(λs).padStart(4, "0") + '" ' +
λh;
const φ =
String(φi).padStart(3, "0") + "°" +
String(φm).padStart(2, "0") + "'" +
String(φs).padStart(4, "0") + '" ' +
φh;
return λ+" "+φ;
}
module.exports = dms;

View File

@@ -0,0 +1,36 @@
const dms = require('./dms');
function geometryAsString (item, opts = {}) {
const key = "key" in opts ? opts.key : "geometry";
const formatDMS = opts.dms;
let str = "";
if (key in item) {
const geometry = item[key];
if (geometry && "coordinates" in geometry) {
if (geometry.type == "Point") {
if (formatDMS) {
str = dms(geometry.coordinates[1], geometry.coordinates[0]);
} else {
str = `${geometry.coordinates[1].toFixed(6)}, ${geometry.coordinates[0].toFixed(6)}`;
}
}
if (str) {
if (opts.url) {
if (typeof opts.url === 'string') {
str = `[${str}](${opts.url.replace("$x", geometry.coordinates[0]).replace("$y", geometry.coordinates[1])})`;
} else {
str = `[${str}](geo:${geometry.coordinates[0]},${geometry.coordinates[1]})`;
}
}
}
}
}
return str;
}
module.exports = geometryAsString;

View File

@@ -0,0 +1,5 @@
module.exports = {
geometryAsString: require('./geometryAsString'),
dms: require('./dms')
};