Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge-trees-to-database #15

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { runMerge } from "../src/index.js";
await runMerge();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"convert": "node bin/convert.js",
"normalize": "node bin/normalize.js",
"concatenate": "node bin/concatenate.js",
"merge": "node bin/merge.js",
"save": "node bin/save.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge will get rid of the need for the save step I think. Do you want to handle that or should I assign myself?

"tile": "node bin/tile.js",
"upload": "node bin/upload.js",
Expand Down
14 changes: 14 additions & 0 deletions src/db/db-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* https://github.com/vitaly-t/pg-promise/wiki/Connection-Syntax
*/
export const dbConfig = {
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
};

// console.log('dbConfig',dbConfig, process.env);

//module.exports = dbConfig;
7 changes: 7 additions & 0 deletions src/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pgp from 'pg-promise';
import { dbConfig } from './db-config.js';
import { pgPromiseConfig } from './pg-promise-config.js';

export const pgPromise = pgp(pgPromiseConfig);
export const db = pgPromise(dbConfig);

27 changes: 27 additions & 0 deletions src/db/pg-promise-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
import pgp from 'pg-promise';

/**
* source:
* https://github.com/vitaly-t/pg-promise/issues/78#issuecomment-171951303
*/
function camelizeColumns(data) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, we have this function replicated here: https://github.com/waterthetrees/wtt_server/blob/68e22644d31da834a69b4435373e8b9aeac9ad5e/server/db/pg-promise-config.js

Probably not worth calling between repos tho since its a common utility function.

const tmp = data[0];
for (const prop in tmp) {
const camel = pgp.utils.camelize(prop);
if (!(camel in tmp)) {
for (let i = 0; i < data.length; i++) {
const d = data[i];
d[camel] = d[prop];
delete d[prop];
}
}
}
}

export const pgPromiseConfig = {
capSQL: true,
receive: (data) => camelizeColumns(data),
};

5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "path";
import * as download from "./stages/download.js";
import * as convert from "./stages/convert.js";
import * as normalize from "./stages/normalize.js";
import * as merge from "./stages/merge.js";
import * as save from "./stages/save.js";
import * as concatenate from "./stages/concatenate.js";
import * as tile from "./stages/tile.js";
Expand All @@ -23,6 +24,10 @@ export const runNormalize = async () => {
await normalize.normalizeSources(sources);
};

export const runMerge = async () => {
await merge.mergeSources(sources);
};

export const runConcatenate = async () => {
const filenames = await utils.asyncReadDir(config.NORMALIZED_DIRECTORY);
const filepaths = filenames.map((f) =>
Expand Down
76 changes: 76 additions & 0 deletions src/stages/merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { db } from '../db/index.js';
import { exec } from 'child_process';
import { dbConfig } from '../db/db-config.js';
import * as utils from "../core/utils.js";


export const mergeSource = async (source) => {
console.log('source.id', source.id);
if (
!source.destinations ||
!source.destinations.geojson ||
!source.destinations.normalized
) {
throw new Error(`No destinations for source: "${source}"`);
}

const normalizedExists = await utils.asyncFileExists(
source.destinations.normalized.path
);
if (!normalizedExists) {
console.log(
`The normalized file '${source.destinations.normalized.path}' does not exists. Skipping...`
);
return `NO FILE for ${source.id}`; // Early Return
}

console.log(`Running for ${source.destinations.normalized.path}`);
// FIXME use the async version of exec, but that means a new dependecy
const command = `ogr2ogr -f "PostgreSQL" PG:"host=${dbConfig.host} user=${dbConfig.user} password=${dbConfig.password} dbname=${dbConfig.database}" ${source.destinations.normalized.path} -nln tree_staging -geomfield geom -append`
exec(command, async (error, stdout, stderr) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer spawn here as it has larger buffer for longer running processes. Could use spawnSync.

if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}

console.log(`Running the stored proc for ${source.id}`);
const result = await db.proc('public.merge_treedata', [source.id]);
console.log(result);
console.log(`stdout: ${stdout}`);
});
/*
*/

const context = {
source,
nullGeometry: 0,
invalidGeometry: 0,
};

return context;
};


export const mergeSources = async (list) => {
if (process.argv.length > 2) {
list = list.filter(m => m.id == process.argv[2]);
}

const limit = pLimit(5);
const promises = list.map((source) =>
limit(() => mergeSource(source))
);
const results = await Promise.allSettled(promises);
console.log("Finished uploading to the database...");
results.forEach((l) => {
if (l && l.forEach) {
l.forEach(console.log);
} else {
console.log(l);
}
});
};