-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (123 loc) · 3.74 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
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
150
151
152
153
154
155
156
157
158
159
160
161
const gdal = require("gdal-next");
const { create } = require("xmlbuilder2");
// "/media/martin/ecf9e826-7b6b-4992-adad-71232022b316/martin/dmr5/R_02_17_s.tif"
const wgs84 = gdal.SpatialReference.fromEPSG(4326);
const srs = gdal.SpatialReference.fromProj4(
"+proj=krovak +lat_0=49.5 +lon_0=24.8333333333333 +alpha=30.2881397527778 +k=0.9999 +x_0=0 +y_0=0 +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0 +units=m +no_defs"
);
const coord_transform = new gdal.CoordinateTransformation(wgs84, srs);
const coord_transform1 = new gdal.CoordinateTransformation(srs, wgs84);
const https = require("https");
const chunks = [];
// [bbox:{{bbox}}]
const query = Buffer.from(`[out:json][timeout:90];
(
node[natural=peak]["source:position"!="ÚGKK SR DMR5.0"](area:3600014296);
);
(._;>;);
out meta;
`);
const req = https.request(
{
hostname: "overpass.freemap.sk",
port: 443,
path: "/api/interpreter",
method: "POST",
headers: {
"Content-Length": query.length,
},
},
(res) => {
res.on("data", (d) => {
chunks.push(d);
});
res.on("end", () => {
adjust(JSON.parse(chunks.join("")).elements);
});
}
);
req.write(query);
req.end();
function adjust(elements) {
const root = create({ version: "1.0" }).ele("osm", {
version: "0.6",
generator: "peak-position-adjuster",
});
const oob = [];
// TODO introduce workers
for (const file of process.argv.slice(2)) {
console.error('\nProcessing:', file);
const ds = gdal.open(file);
const band = ds.bands.get(1);
const geotransform = ds.geoTransform;
outer: for (const element of elements) {
const center = coord_transform.transformPoint({
x: element.lat,
y: element.lon,
});
const cx = Math.round(center.x);
const cy = Math.round(center.y);
let maxEle = -Infinity;
let px;
let py;
let pd;
try {
for (let x = cx - 100; x < cx + 100; x++) {
for (let y = cy - 100; y < cy + 100; y++) {
const d = Math.sqrt((x - cx) ** 2 + (y - cy) ** 2);
if (d < 100) {
const ele = band.pixels.get(
x - geotransform[0],
geotransform[3] - y
);
if (ele === band.noDataValue) {
process.stderr.write("·");
continue outer;
}
if (ele > maxEle) {
maxEle = ele;
px = x;
py = y;
pd = d;
}
}
}
}
// if (pd < 3) {
// process.stderr.write("✓");
// continue;
// }
if (pd > 95) {
process.stderr.write("✗");
oob.push(element.id);
continue;
}
const latlon = coord_transform1.transformPoint({ x: px, y: py });
process.stderr.write("+");
const node = root.ele("node", {
id: element.id,
version: element.version,
lat: latlon.x,
lon: latlon.y,
user: element.user,
timestamp: element.timestamp,
visible: "true",
action: "modify",
});
for (const [k, v] of Object.entries(element.tags)) {
node.ele("tag", { k, v });
}
node.ele("tag", { k: 'ele:bpv', v: maxEle.toFixed(2) });
node.ele("tag", { k: 'source:ele:bpv', v: 'ÚGKK SR DMR5.0' });
node.ele("tag", { k: 'source:position', v: 'ÚGKK SR DMR5.0' });
} catch (err) {
if (!err.message.includes("out of")) {
throw err;
}
process.stderr.write("·");
}
}
}
console.error("\nOut of bounds peaks: " + oob.join(" "));
console.log(root.end({ prettyPrint: true }));
}