-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-geojson.js
113 lines (77 loc) · 2.98 KB
/
create-geojson.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
const fs = require('fs');
const csv = require('csv');
const GeoJSON = require('geojson');
const progress = require('cli-progress');
async function loadTreeRegistry(filepath) {
return new Promise((resolve, reject) => {
const registry = [];
fs
.createReadStream(filepath)
.pipe(csv.parse({ cast: true, columns: true }))
.on('data', row => registry.push(row))
.on('end', () => resolve(registry))
.on('error', reject);
});
}
async function loadTrees(inputFilepath, treeRegistry) {
console.log(`Loading and preparing tree information`);
return new Promise((resolve, reject) => {
const rows = [];
fs
.createReadStream(inputFilepath)
.pipe(csv.parse({ cast: true, columns: true }))
.on('data', row => rows.push(row))
.on('end', () => {
const trees = [];
const progressBar = new progress.SingleBar({}, progress.Presets.shades_classic);
progressBar.start(rows.length, 0);
for (const row of rows) {
progressBar.increment();
trees.push(...createTreesFromCsvRow(row, treeRegistry));
}
resolve(trees);
progressBar.stop();
})
.on('error', reject);
});
}
function createTreesFromCsvRow(row, treeRegistry) {
const treeIds = row.tree_ids.split('|');
return treeIds
.map(treeId => {
const tree = treeRegistry.find(t => t.Baumnr === treeId)
if (!tree) {
return false;
}
const genus = tree.Gattung.indexOf(',') > -1 ? tree.Gattung.split(',')[0].trim() : tree.Gattung;
const common = tree.Gattung.indexOf(',') > -1 ? tree.Gattung.split(',')[1].trim() : '';
return {
name: treeId + ' - ' + common,
lat: tree ? tree.latitude : null,
lng: tree ? tree.longitude : null,
reportedDate: row.reported_date,
filename: row.filename,
filesize: row.filesize,
ref: treeId,
genus,
common,
height: tree.Baumhoehe,
crown: tree.Kronendurc,
dbh: tree.Stammumfan,
address: tree.Gebiet
};
})
.filter(tree => !!tree);
}
function saveAsGeoJson(trees, outputFilepath) {
const geoJson = GeoJSON.parse(trees, { Point: ['lat', 'lng'] });
fs.writeFileSync(outputFilepath, JSON.stringify(geoJson));
}
async function createGeoJson(args) {
const { inputFilepath, outputFilepath } = args;
const treeRegistry = await loadTreeRegistry('./data/Baumkataster-Magdeburg-2021.csv');
const trees = await loadTrees(inputFilepath, treeRegistry);
saveAsGeoJson(trees, outputFilepath);
console.log(`Results saved to ${outputFilepath}`);
}
module.exports = createGeoJson;