Refactor save() so that we can force a specific CRS.

This was brought about due to the absence of CRS information
in the navigation header outputs. We get either unqualified
easting / northing or unqualified latitude / longitude, neither
of which happen to be WGS84.

This is not a good solution as it involves manual configuration,
but allows us to move forward for the time being.
This commit is contained in:
D. Berge
2020-09-02 08:55:08 +02:00
parent ff2e30511c
commit 0a2f0b0b60

View File

@@ -64,7 +64,7 @@ async function getNearestPreplot (candidates) {
return res.rows[0].schema;
}
async function saveOnline (dataset) {
async function saveOnline (dataset, opts = {}) {
const client = await pool.connect();
@@ -119,10 +119,33 @@ async function saveOnline (dataset) {
}
}
async function saveOffline (navData) {
async function saveOffline (navData, opts = {}) {
const client = await pool.connect();
if ("latitude" in navData && "longitude" in navData) {
const epsg = navData.epsg || opts.epsg;
const schema = navData.payload && navData.payload._schema;
const hasEastNorth = "easting" in navData && "northing" in navData;
const hasLatLon = "latitude" in navData && "longitude" in navData;
if (epsg && hasEastNorth) {
const text = `
INSERT INTO real_time_inputs (tstamp, geometry, meta)
VALUES ($1, ST_Transform(ST_SetSRID(ST_MakePoint($2, $3), $5), 4326), $4);
`;
const values = [navData.tstamp, navData.easting, navData.northing, navData.payload, epsg];
await client.query(text, values)
} else if (schema && hasEastNorth) {
const text = `
INSERT INTO real_time_inputs (tstamp, geometry, meta)
VALUES ($1, ST_Transform(ST_SetSRID(ST_MakePoint($2, $3), (SELECT (data->>'epsg')::integer AS epsg FROM ${schema}.file_data)), 4326), $4);
`;
const values = [navData.tstamp, navData.longitude, navData.latitude, navData.payload];
await client.query(text, values)
} else if (hasLatLon) {
const text = `
INSERT INTO real_time_inputs (tstamp, geometry, meta)
VALUES ($1, ST_SetSRID(ST_MakePoint($2, $3), 4326), $4);
@@ -201,7 +224,7 @@ async function save (navData, opts = {}) {
// console.log("Choose nearest");
const destinationSchema = await getNearestPreplot(candidates);
if (destinationSchema) {
await saveOnline(candidates.filter(c => c.schema == destinationSchema));
await saveOnline(candidates.filter(c => c.schema == destinationSchema), opts);
navData.payload._schema = destinationSchema;
} else {
console.log("Nowhere to save to");
@@ -215,7 +238,7 @@ async function save (navData, opts = {}) {
}
}
await saveOffline(navData);
await saveOffline(navData, opts);
}
module.exports = save;