-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (94 loc) · 3.02 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
#!/usr/bin/env node
// read through all the files in the in folder
const fs = require('fs');
// const csv = require('csv');
const parse = require('csv-parse/lib/sync');
const stringify = require('csv-stringify/lib/sync');
// const data = [];
/** @typedef {{
PointID: string,
PointCode: string,
SUID: string,
FID: string,
PPID: string,
xPos: string,
yPos: string,
zHeight: string,
PrismType: string,
PrismHeight: string,
Date: string,
Time: string,
}} Row */
fs.readdirSync('in', 'utf8').forEach((file) => {
if (file === '.gitkeep') return;
const fileContent = fs.readFileSync('in/' + file, 'utf8');
const date = file.match(/(\d{8})\.txt/)?.[1];
if (!date) {
console.warn(
`No date found in filename "in/${file}" (attempt to find last eight numbers before file extension failed)! Skipped file.`
);
return;
}
// for each file, parse the content as tab separated (possibly also remove extra spaces?)
// [
// {
// PointID: '2003484',
// PointCode: 'PP',
// SUID: '065_243',
// FID: '2',
// PPID: 'BS_IF',
// xPos: '17437.8715724798',
// yPos: '389942.9379840849',
// zHeight: '297.9981262636',
// PrismType: 'Ref',
// PrismHeight: '1.299',
// Date: '1999-3-1',
// Time: '18:59:12.3'
// },
// ]
/** @type Row[] */
const rows = parse(fileContent, {
columns: true,
ltrim: true,
skip_empty_lines: true,
delimiter: '\t',
});
// filter the rows to only include rows with PAP in the PointCode field
const passPointsRows = rows.filter((row) => {
if (row.PointCode === 'PAP') {
return true;
} else {
return false;
}
});
// group the rows by those having identical SE numbers
const passPointsRowsGroups = passPointsRows.reduce((groups, row) => {
const groupKey = 'SE' + row.SUID + '-' + row.PPID;
if (!groups[groupKey]) {
groups[groupKey] = [];
}
// for each group, create a separated object as follows:
// .1 Att2 is added to the first column, with "target " prepended
// .2 Easting is added to the second column
// .3 Northing is added to the third column
// .4 OrthoHeight is added to the fourth column
groups[groupKey].push({
Att2: 'target ' + row.FID,
Easting: row.xPos,
Northing: row.yPos,
OrthoHeight: row.zHeight,
});
return groups;
}, /** @type {{ [key: string]: {Att2: string, Easting: string, Northing: string, OrthoHeight: string}[] }} */ ({}));
// the file name is generated as follows, with "-" separating the parts:
// .1 the last 8 numbers from the "in" file are added as the first part
// .2 PointCode is added as the second part
// .3 SE-Nr is added with "SE" prefix
// .4 Att3 is added as the fourth part
Object.entries(passPointsRowsGroups).forEach(([suIdAndFid, group]) => {
const filePath = 'out/' + date + '_PP_' + suIdAndFid + '.txt';
fs.writeFileSync(filePath, stringify(group));
console.log(`Successfully created ${filePath}`);
});
// data.push(passPointsRowsGroups);
});