-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (58 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { Client } = require('pg');
const wkx = require('wkx');
const QueryStream = require('pg-query-stream');
const client = new Client();
const client2 = new Client();
let num = 0;
const sourceTable = 'cont_dmr';
const destTable = 'cont_dmr_split';
const fn = async () => {
await Promise.all([
client.connect(),
client2.connect(),
]);
const qs = new QueryStream(`
SELECT
${sourceTable}.id AS id,
${sourceTable}.height AS height,
st_asbinary(st_simplify(${sourceTable}.wkb_geometry, 0.1, true)) AS wkb_geometry
FROM ${sourceTable} LEFT JOIN ${destTable} ON ${destTable}.id = ${sourceTable}.id
WHERE ${destTable}.id IS NULL
`);
const stream = client.query(qs);
for await (const row of stream) {
if (num % 1000 === 0) {
if (num > 0) {
await client2.query('COMMIT');
}
console.log('ROW', num);
await client2.query('BEGIN');
}
num++;
const gj = wkx.Geometry.parse(row.wkb_geometry).toGeoJSON();
// console.log(gj);
const len = gj.coordinates.length;
const n = Math.ceil(len / 1000);
const size = len / n;
let from = 0;
for (let i = 0; i < n; i++) {
const sliceCoords = gj.coordinates.slice(Math.round(from) - (from > 0 ? 1 : 0), Math.round(from + size));
const sliceGeom = new wkx.LineString(sliceCoords.map(([x, y]) => (new wkx.Point(x, y)))).toWkb();
await client2.query(
`INSERT INTO ${destTable} (id, height, wkb_geometry) VALUES ($1, $2, ST_GeomFromWKB($3, 3857))`,
[
row.id,
row.height,
sliceGeom,
],
);
from += size;
}
}
await client2.query('COMMIT');
client.end();
client2.end();
};
fn().catch(err => {
console.log(err);
});