bwscheddebrock_trassenplaner/setup_wea_db.js

95 lines
2.4 KiB
JavaScript

const { Pool } = require('pg');
require('dotenv').config();
const pool = new Pool({
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
const weaData = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [7.458648469357766, 52.12395112598604]
},
"properties": { "id": 3, "NH": 169, "RD": 162, "GH": 250, "WEA": 11 }
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [7.471394709318756, 52.11134006270081]
},
"properties": { "id": 4, "NH": 169, "RD": 162, "GH": 250, "WEA": 12 }
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [7.442653079085219, 52.11178210921146]
},
"properties": { "id": null, "NH": 169, "RD": 162, "GH": null, "WEA": 13 }
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [7.445899475376124, 52.10830024407835]
},
"properties": { "id": null, "NH": 112, "RD": 136, "GH": null, "WEA": 14 }
}
]
};
async function setupWEA() {
const client = await pool.connect();
try {
await client.query("SET search_path TO bw_scheddebrock, public;");
// Create Table
await client.query(`
CREATE TABLE IF NOT EXISTS "WEA" (
id SERIAL PRIMARY KEY,
geom GEOMETRY(Point, 25832),
"NH" NUMERIC,
"RD" NUMERIC,
"GH" NUMERIC,
"WEA" INTEGER
)
`);
// Clear old data
await client.query('DELETE FROM "WEA"');
// Insert data
for (const f of weaData.features) {
const coords = f.geometry.coordinates;
const props = f.properties;
// Transform 4326 to 25832
const query = `
INSERT INTO "WEA" (geom, "NH", "RD", "GH", "WEA")
VALUES (
ST_Transform(ST_SetSRID(ST_Point($1, $2), 4326), 25832),
$3, $4, $5, $6
)
`;
await client.query(query, [coords[0], coords[1], props.NH, props.RD, props.GH, props.WEA]);
}
console.log("WEA table created and populated in bw_scheddebrock.");
} catch (err) {
console.error("Error:", err);
} finally {
client.release();
await pool.end();
}
}
setupWEA();