-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS2CSWrapper.js
63 lines (53 loc) · 1.59 KB
/
CS2CSWrapper.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
const { spawn } = require("child_process");
class CS2CSWrapper {
constructor(fromCrs, toCrs, precision = 8) {
this.fromCrs = fromCrs;
this.toCrs = toCrs;
this.precision = precision;
}
transformPoint(x, y, z = 0) {
return new Promise((resolve, reject) => {
try {
const command = "cs2cs";
const args = [
"--3d",
`${this.fromCrs}`,
`${this.toCrs}`,
"-f",
`%.${this.precision}f`,
];
const subprocess = spawn(command, args);
// Prepare the input point string
const pointString = `${x} ${y} ${z}`;
subprocess.stdin.write(pointString);
subprocess.stdin.end();
// Event handlers for subprocess output
subprocess.stdout.on("data", (data) => {
console.log(`Subprocess output: ${data}`);
// Extract the transformed coordinates
const transformedX = parseFloat(
data.toString().split(" ")[0].split("\t")[0]
);
const transformedY = parseFloat(
data.toString().split(" ")[0].split("\t")[1]
);
const transformedZ = parseFloat(
data.toString().split("\n")[0].split(" ")[1]
);
resolve({
x: transformedX,
y: transformedY,
z: transformedZ,
});
});
subprocess.stderr.on("data", (data) => {
console.error(`Subprocess error: ${data}`);
reject(new Error(data));
});
} catch (error) {
reject(error);
}
});
}
}
module.exports = CS2CSWrapper;