-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPrepareGeoJSON.ts
131 lines (119 loc) · 3.86 KB
/
PrepareGeoJSON.ts
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
import { createWriteStream } from "fs";
import merge from "merge2";
import { FeatureType } from "openskidata-format";
import { Readable } from "stream";
import StreamToPromise from "stream-to-promise";
import { Config } from "./Config";
import clusterSkiAreas from "./clustering/ClusterSkiAreas";
import { DataPaths, getPath } from "./io/GeoJSONFiles";
import { readGeoJSONFeatures } from "./io/GeoJSONReader";
import addElevation from "./transforms/Elevation";
import toFeatureCollection from "./transforms/FeatureCollection";
import { formatLift } from "./transforms/LiftFormatter";
import * as MapboxGLFormatter from "./transforms/MapboxGLFormatter";
import { formatRun } from "./transforms/RunFormatter";
import { InputSkiAreaType, formatSkiArea } from "./transforms/SkiAreaFormatter";
import {
SkiAreaSiteProvider,
addSkiAreaSites,
} from "./transforms/SkiAreaSiteProvider";
import {
accumulate,
flatMap,
map,
mapAsync,
} from "./transforms/StreamTransforms";
import { RunNormalizerAccumulator } from "./transforms/accumulator/RunNormalizerAccumulator";
export default async function prepare(paths: DataPaths, config: Config) {
const siteProvider = new SkiAreaSiteProvider();
siteProvider.loadSites(paths.input.osmJSON.skiAreaSites);
console.log("Processing ski areas...");
await StreamToPromise(
merge([
readGeoJSONFeatures(paths.input.geoJSON.skiAreas).pipe(
flatMap(formatSkiArea(InputSkiAreaType.OPENSTREETMAP_LANDUSE)),
),
Readable.from(siteProvider.getGeoJSONSites()),
readGeoJSONFeatures(paths.input.geoJSON.skiMapSkiAreas).pipe(
flatMap(formatSkiArea(InputSkiAreaType.SKIMAP_ORG)),
),
])
.pipe(toFeatureCollection())
.pipe(
createWriteStream(
config.arangoDBURLForClustering
? paths.intermediate.skiAreas
: paths.output.skiAreas,
),
),
);
console.log("Processing runs...");
await StreamToPromise(
readGeoJSONFeatures(paths.input.geoJSON.runs)
.pipe(flatMap(formatRun))
.pipe(map(addSkiAreaSites(siteProvider)))
// write stream here
// do topo conversion in a separate command
// then open the topojson separately and normalize
.pipe(accumulate(new RunNormalizerAccumulator()))
.pipe(
mapAsync(
config.elevationServerURL
? addElevation(config.elevationServerURL)
: null,
10,
),
)
.pipe(toFeatureCollection())
.pipe(
createWriteStream(
config.arangoDBURLForClustering
? paths.intermediate.runs
: paths.output.runs,
),
),
);
console.log("Processing lifts...");
await StreamToPromise(
readGeoJSONFeatures(paths.input.geoJSON.lifts)
.pipe(flatMap(formatLift))
.pipe(map(addSkiAreaSites(siteProvider)))
.pipe(
mapAsync(
config.elevationServerURL
? addElevation(config.elevationServerURL)
: null,
10,
),
)
.pipe(toFeatureCollection())
.pipe(
createWriteStream(
config.arangoDBURLForClustering
? paths.intermediate.lifts
: paths.output.lifts,
),
),
);
if (config.arangoDBURLForClustering) {
console.log("Clustering ski areas...");
await clusterSkiAreas(
paths.intermediate,
paths.output,
config.arangoDBURLForClustering,
config.geocodingServer,
);
}
console.log("Formatting for maps...");
await Promise.all(
[FeatureType.SkiArea, FeatureType.Lift, FeatureType.Run].map((type) => {
return StreamToPromise(
readGeoJSONFeatures(getPath(paths.output, type))
.pipe(flatMap(MapboxGLFormatter.formatter(type)))
.pipe(toFeatureCollection())
.pipe(createWriteStream(getPath(paths.output.mapboxGL, type))),
);
}),
);
console.log("Done preparing");
}