-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathprocess_geodata.mjs
268 lines (233 loc) · 10.7 KB
/
process_geodata.mjs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import simplify from '@turf/simplify';
import { geoIdentity, geoPath } from 'd3-geo';
import fs from 'fs';
import mapshaper from 'mapshaper';
import path from 'path';
import config, { getNEFilename } from './config.mjs';
const { filters, inputDir, layers, resolutions, scopes, unFilename, vectors } = config;
// Create output directories
const outputDirGeojson = path.resolve(config.outputDirGeojson);
if (!fs.existsSync(outputDirGeojson)) fs.mkdirSync(outputDirGeojson, { recursive: true });
const outputDirTopojson = path.resolve(config.outputDirTopojson);
if (!fs.existsSync(outputDirTopojson)) fs.mkdirSync(outputDirTopojson, { recursive: true });
async function convertShpToGeo(filename) {
const inputFilePath = `${inputDir}/${filename}.shp`;
const outputFilePath = `${outputDirGeojson}/${filename}.geojson`;
const commands = [inputFilePath, `-proj wgs84`, `-o format=geojson ${outputFilePath}`].join(' ');
await mapshaper.runCommands(commands);
}
function getJsonFile(filename) {
try {
return JSON.parse(fs.readFileSync(filename, 'utf8'));
} catch (err) {
console.error(`❌ Failed to load JSON input file '${filename}':`, err.message);
process.exit(1);
}
}
async function createCountriesLayer({ bounds, filter, name, resolution, source }) {
const inputFilePath = `${outputDirGeojson}/${unFilename}_${resolution}m/${source}.geojson`;
const outputFilePath = `${outputDirGeojson}/${name}_${resolution}m/countries.geojson`;
const commands = [
inputFilePath,
bounds.length ? `-clip bbox=${bounds.join(',')}` : '',
filter ? `-filter '${filter}'` : '',
`-o ${outputFilePath}`
].join(' ');
await mapshaper.runCommands(commands);
addCentroidsToGeojson(outputFilePath);
}
function addCentroidsToGeojson(geojsonPath) {
const geojson = getJsonFile(geojsonPath);
if (!geojson.features) return;
const features = geojson.features.map((feature) => {
const centroid = getCentroid(feature);
feature.properties.ct = centroid;
return feature;
});
fs.writeFileSync(geojsonPath, JSON.stringify({ ...geojson, features }));
}
async function createLandLayer({ bounds, name, resolution, source }) {
// TODO: Figure out way to only include North and Central America via filter, dissolve
const inputFilePath = `${outputDirGeojson}/${unFilename}_${resolution}m/${source}.geojson`;
const outputFilePath = `${outputDirGeojson}/${name}_${resolution}m/land.geojson`;
const commands = [
inputFilePath,
'-dissolve',
bounds.length ? `-clip bbox=${bounds.join(',')}` : '',
`-o ${outputFilePath}`
].join(' ');
await mapshaper.runCommands(commands);
}
async function createCoastlinesLayer({ bounds, name, resolution, source }) {
// TODO: Update source to be a path?
const inputFilePath = `${outputDirGeojson}/${unFilename}_${resolution}m/${source}.geojson`;
const outputFilePath = `${outputDirGeojson}/${name}_${resolution}m/coastlines.geojson`;
const commands = [
inputFilePath,
'-dissolve',
'-lines',
bounds.length ? `-clip bbox=${bounds.join(',')}` : '',
`-o ${outputFilePath}`
].join(' ');
await mapshaper.runCommands(commands);
}
async function createOceanLayer({ bounds, name, resolution, source }) {
const inputFilePath = `./tasks/topojson/world_rectangle.geojson`;
const outputFilePath = `${outputDirGeojson}/${name}_${resolution}m/ocean.geojson`;
const eraseFilePath = `${outputDirGeojson}/${unFilename}_${resolution}m/${source}.geojson`;
const commands = [
inputFilePath,
bounds.length ? `-clip bbox=${bounds.join(',')}` : '',
`-erase ${eraseFilePath}`,
`-o ${outputFilePath}`
].join(' ');
await mapshaper.runCommands(commands);
}
async function createRiversLayer({ name, resolution, source }) {
const inputFilePath = `${outputDirGeojson}/${getNEFilename({ resolution, source })}.geojson`;
const outputFilePath = `${outputDirGeojson}/${name}_${resolution}m/rivers.geojson`;
const commands = [
inputFilePath,
`-clip ${outputDirGeojson}/${name}_${resolution}m/countries.geojson`, // Clip to the continent
`-o ${outputFilePath}`
].join(' ');
await mapshaper.runCommands(commands);
}
async function createLakesLayer({ name, resolution, source }) {
const inputFilePath = `${outputDirGeojson}/${getNEFilename({ resolution, source })}.geojson`;
const outputFilePath = `${outputDirGeojson}/${name}_${resolution}m/lakes.geojson`;
const commands = [
inputFilePath,
`-clip ${outputDirGeojson}/${name}_${resolution}m/countries.geojson`, // Clip to the continent
`-o ${outputFilePath}`
].join(' ');
await mapshaper.runCommands(commands);
}
async function createSubunitsLayer({ name, resolution, source }) {
const filter = ['AUS', 'BRA', 'CAN', 'USA'].map((id) => `adm0_a3 === "${id}"`).join(' || ');
const inputFilePath = `${outputDirGeojson}/${getNEFilename({ resolution, source })}.geojson`;
const outputFilePath = `${outputDirGeojson}/${name}_${resolution}m/subunits.geojson`;
const commands = [
inputFilePath,
`-filter "${filter}"`,
`-clip ${outputDirGeojson}/${name}_${resolution}m/countries.geojson`, // Clip to the continent
`-o ${outputFilePath}`
].join(' ');
await mapshaper.runCommands(commands);
addCentroidsToGeojson(outputFilePath);
}
function pruneProperties(topojson) {
for (const layer in topojson.objects) {
switch (layer) {
case 'countries':
topojson.objects[layer].geometries = topojson.objects[layer].geometries.map((geometry) => {
const { properties } = geometry;
if (properties) {
geometry.id = properties.iso3cd;
geometry.properties = {
ct: properties.ct
};
}
return geometry;
});
break;
case 'subunits':
topojson.objects[layer].geometries = topojson.objects[layer].geometries.map((geometry) => {
const { properties } = geometry;
if (properties) {
geometry.id = properties.postal;
geometry.properties = {
ct: properties.ct,
gu: properties.gu_a3
};
}
return geometry;
});
break;
default:
topojson.objects[layer].geometries = topojson.objects[layer].geometries.map((geometry) => {
delete geometry.id;
delete geometry.properties;
return geometry;
});
break;
}
}
return topojson;
}
function getCentroid(feature) {
const { type } = feature.geometry;
const projection = geoIdentity();
const path = geoPath(projection);
if (type === 'MultiPolygon') {
let maxArea = -Infinity;
for (const coordinates of feature.geometry.coordinates) {
const polygon = { type: 'Polygon', coordinates };
const area = path.area(polygon);
if (area > maxArea) {
maxArea = area;
feature = polygon;
}
}
}
return path.centroid(feature).map((coord) => +coord.toFixed(2));
}
async function convertLayersToTopojson({ name, resolution }) {
const regionDir = path.join(outputDirGeojson, `${name}_${resolution}m`);
if (!fs.existsSync(regionDir)) return;
const outputFile = `${outputDirTopojson}/${name}_${resolution}m.json`;
// Layer names default to file names
const commands = [`${regionDir}/*.geojson combine-files`, `-o format=topojson ${outputFile}`].join(' ');
await mapshaper.runCommands(commands);
// Remove extra information from features
const topojson = getJsonFile(outputFile);
const prunedTopojson = pruneProperties(topojson);
fs.writeFileSync(outputFile, JSON.stringify(prunedTopojson));
}
// Get polygon features from UN GeoJSON
const inputFilePath = `${inputDir}/${unFilename}.geojson`;
const outputFilePath50m = `${outputDirGeojson}/${unFilename}_50m/all_features.geojson`;
const outputPath110m = `${outputDirGeojson}/${unFilename}_110m`;
const commandsAllFeatures = [inputFilePath, `-o target=1 ${outputFilePath50m}`].join(' ');
await mapshaper.runCommands(commandsAllFeatures);
const geojson = getJsonFile(outputFilePath50m);
const simplifiedGeojson = {
...geojson,
features: geojson.features.map((f) => simplify(f, { tolerance: 0.01, highQuality: true }))
};
if (!fs.existsSync(outputPath110m)) fs.mkdirSync(outputPath110m, { recursive: true });
fs.writeFileSync(`${outputPath110m}/all_features.geojson`, JSON.stringify(simplifiedGeojson));
for (const resolution of resolutions) {
for (const { source } of Object.values(vectors)) {
await convertShpToGeo(getNEFilename({ resolution, source }));
}
// Get countries from all polygon features
const inputFilePathCountries = `${outputDirGeojson}/${unFilename}_${resolution}m/all_features.geojson`;
const outputFilePathCountries = `${outputDirGeojson}/${unFilename}_${resolution}m/countries.geojson`;
const commandsCountries = [
inputFilePathCountries,
`-filter '${filters.countries}'`,
`-o ${outputFilePathCountries}`
].join(' ');
await mapshaper.runCommands(commandsCountries);
// Get land from all polygon features
const inputFilePathLand = `${outputDirGeojson}/${unFilename}_${resolution}m/all_features.geojson`;
const outputFilePathLand = `${outputDirGeojson}/${unFilename}_${resolution}m/land.geojson`;
const commandsLand = [inputFilePathLand, `-filter '${filters.land}'`, `-clean -o ${outputFilePathLand}`].join(' ');
await mapshaper.runCommands(commandsLand);
}
for (const resolution of resolutions) {
for (const {
name,
specs: { bounds, filter }
} of scopes) {
await createCountriesLayer({ bounds, filter, name, resolution, source: layers.countries });
await createLandLayer({ bounds, name, resolution, source: layers.land });
await createCoastlinesLayer({ bounds, name, resolution, source: layers.coastlines });
await createOceanLayer({ bounds, name, resolution, source: layers.ocean });
await createRiversLayer({ bounds, name, resolution, source: layers.rivers });
await createLakesLayer({ bounds, name, resolution, source: layers.lakes });
await createSubunitsLayer({ bounds, name, resolution, source: layers.subunits });
await convertLayersToTopojson({ name, resolution });
}
}