This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
6_buffered_geometry-2_buffer.js
executable file
·149 lines (122 loc) · 4.11 KB
/
6_buffered_geometry-2_buffer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env node
'use strict'
const { simpleCluster } = require('big-data-tools');
const { readFileSync, renameSync, rmSync, existsSync, createWriteStream } = require('fs');
const turf = require('@turf/turf');
const miss = require('mississippi2');
const config = require('../config.js');
const { getSpawn, calcTemporaryFilename } = require('../lib/helper.js');
const { ogrWrapFileDriver, ogrLoadGpkgAsGeojsonStream, ogrGenerateSQL, unionAndClipFeaturesDC, convertGzippedGeoJSONSeq2Anything } = require('../lib/geohelper.js');
const { createGzip } = require('zlib');
simpleCluster(async runWorker => {
const { ruleTypes, bundeslaender } = JSON.parse(readFileSync(config.getFilename.bufferedGeometry('index.json')));
let todos = [];
ruleTypes.forEach(ruleType => {
if (ruleType.slug === 'wohngebaeude') return;
ruleType.regions.forEach(region => {
let bundesland = bundeslaender.find(b => b.ags === region.ags);
todos.push({
bundesland,
ruleType,
region,
radius: region.radius / 1000,
filenameIn: ruleType.filenameIn,
filenameOut: region.filenameBase + '.gpkg',
})
})
})
todos = todos.filter(t => !existsSync(t.filenameOut));
//todos = todos.filter(t => [2,4,6,11].includes(t.bundesland.ags));
await todos.forEachParallel(runWorker);
console.log('finished')
process.exit();
}, async todo => {
let name = '\x1b[90m'+todo.ruleType.slug+' '+todo.region.ags+'/'+todo.bundesland.name+'\x1b[0m';
console.log('process', name);
let filenameIn = todo.filenameIn + '.gpkg';
if (todo.radius > 0) {
let bbox = turf.bboxPolygon(todo.bundesland.bbox);
bbox = turf.buffer(bbox, todo.radius, { steps: 18 });
bbox = turf.bbox(bbox);
let filenameGeoGz = todo.region.filenameBase + '.tmp.geojsonl.gz';
if (!existsSync(filenameGeoGz)) {
console.log(' 1/3 extract and buffer', name);
let filenameTmp = calcTemporaryFilename(filenameGeoGz)
await new Promise(res => {
ogrLoadGpkgAsGeojsonStream(filenameIn, {
dropProperties: true,
bbox,
})
.pipe(calcBuffer(todo.radius))
.pipe(createGzip())
.pipe(createWriteStream(filenameTmp))
.once('close', () => res())
});
renameSync(filenameTmp, filenameGeoGz);
}
let filenameTmp = todo.region.filenameBase + '.tmp.gpkg';
if (!existsSync(filenameTmp)) {
console.log(' 2/3 convert to gpkg', name);
await convertGzippedGeoJSONSeq2Anything(filenameGeoGz, filenameTmp, { dropProperties: true });
}
console.log(' 3/3 union', name);
await unionAndClipFeaturesDC(filenameTmp, todo.bundesland.filename, todo.filenameOut);
rmSync(filenameTmp);
rmSync(filenameGeoGz);
} else {
let filenameTmp = calcTemporaryFilename(todo.filenameOut);
console.log(' 1/1 extract only', name);
let cp = getSpawn('ogr2ogr', [
'-a_srs', 'EPSG:4326',
'-dialect', 'SQLite',
'-sql', ogrGenerateSQL({
dropProperties: true,
bbox: todo.bundesland.bbox,
makeValid: true,
}),
'-clipdst', todo.bundesland.filename,
'-explodecollections',
'-nlt', 'MULTIPOLYGON',
'-nln', 'layer',
'-lco', 'GEOMETRY_NAME=geometry',
filenameTmp,
ogrWrapFileDriver(filenameIn),
]);
await new Promise(res => cp.once('close', res))
renameSync(filenameTmp, todo.filenameOut);
}
function calcBuffer(radius) {
return miss.through.obj(function (line, enc, cb) {
if (line.length === 0) return cb();
let f0 = JSON.parse(line);
turf.flattenEach(f0, f1 => {
f1 = turf.buffer(f1, radius, { steps: 18 });
turf.flattenEach(f1, f2 => {
cleanupFeature(f2);
try {
f2 = turf.unkinkPolygon(f2);
} catch (e) {
console.dir(f2, { depth: 10 });
throw e;
}
f2.features.forEach(f3 => {
this.push(JSON.stringify(f3) + '\n');
})
})
})
cb();
})
function cleanupFeature(feature) {
if (feature.geometry.type !== 'Polygon') throw Error(feature.geometry.type);
feature.geometry.coordinates = feature.geometry.coordinates.map(ring => {
let lastp = [];
return ring.filter(p => {
p = p.map(v => Math.round(v * 1e8) / 1e8);
if ((p[0] === lastp[0]) && (p[1] === lastp[1])) return false;
lastp = p;
return true;
})
})
}
}
})