1
- import rewind from '@mapbox/geojson-rewind'
1
+ import rewind from '@mapbox/geojson-rewind' ;
2
2
import { geoIdentity , geoPath } from 'd3-geo' ;
3
- import { geoStitch } from " d3-geo-projection"
3
+ import { geoStitch } from ' d3-geo-projection' ;
4
4
import fs from 'fs' ;
5
5
import mapshaper from 'mapshaper' ;
6
6
import path from 'path' ;
7
- import { topology } from 'topojson-server'
7
+ import { topology } from 'topojson-server' ;
8
8
import config , { getNEFilename } from './config.mjs' ;
9
9
10
10
const { filters, inputDir, layers, resolutions, scopes, unFilename, vectors } = config ;
@@ -46,15 +46,13 @@ function addCentroidsToGeojson(geojsonPath) {
46
46
}
47
47
48
48
// Wind the polygon rings in the correct direction to indicate what is solid and what is whole
49
- const rewindGeojson = ( geojson , clockwise = true ) => rewind ( geojson , clockwise )
49
+ const rewindGeojson = ( geojson , clockwise = true ) => rewind ( geojson , clockwise ) ;
50
50
51
51
// Clamp x-coordinates to the antimeridian
52
52
function clampToAntimeridian ( inputFilepath , outputFilepath ) {
53
- outputFilepath ||= inputFilepath
54
- const jsonString = fs . readFileSync ( inputFilepath , 'utf8' )
55
- const updatedString = jsonString
56
- . replaceAll ( / 1 7 9 \. 9 9 9 9 \d + , / g, '180,' )
57
- . replaceAll ( / 1 8 0 \. 0 0 0 0 \d + , / g, '180,' )
53
+ outputFilepath ||= inputFilepath ;
54
+ const jsonString = fs . readFileSync ( inputFilepath , 'utf8' ) ;
55
+ const updatedString = jsonString . replaceAll ( / 1 7 9 \. 9 9 9 9 \d + , / g, '180,' ) . replaceAll ( / 1 8 0 \. 0 0 0 0 \d + , / g, '180,' ) ;
58
56
59
57
fs . writeFileSync ( outputFilepath , updatedString ) ;
60
58
}
@@ -165,7 +163,7 @@ async function createCoastlinesLayer({ bounds, name, resolution, source }) {
165
163
`-o ${ outputFilePath } `
166
164
] . join ( ' ' ) ;
167
165
await mapshaper . runCommands ( commands ) ;
168
- clampToAntimeridian ( outputFilePath )
166
+ clampToAntimeridian ( outputFilePath ) ;
169
167
}
170
168
171
169
async function createOceanLayer ( { bounds, name, resolution, source } ) {
@@ -222,14 +220,14 @@ async function convertLayersToTopojson({ name, resolution }) {
222
220
223
221
const outputFile = `${ outputDirTopojson } /${ name } _${ resolution } m.json` ;
224
222
// Scopes with polygons that cross the antimeridian need to be stitched
225
- if ( [ " antarctica" , " world" ] . includes ( name ) ) {
226
- const geojsonObjects = { }
223
+ if ( [ ' antarctica' , ' world' ] . includes ( name ) ) {
224
+ const geojsonObjects = { } ;
227
225
for ( const layer of Object . keys ( config . layers ) ) {
228
- const filePath = path . join ( regionDir , `${ layer } .geojson` )
229
- geojsonObjects [ layer ] = geoStitch ( rewindGeojson ( getJsonFile ( filePath ) ) )
226
+ const filePath = path . join ( regionDir , `${ layer } .geojson` ) ;
227
+ geojsonObjects [ layer ] = geoStitch ( rewindGeojson ( getJsonFile ( filePath ) ) ) ;
230
228
}
231
229
// Convert geojson to topojson
232
- const topojsonTopology = topology ( geojsonObjects , 1000000 )
230
+ const topojsonTopology = topology ( geojsonObjects , 1000000 ) ;
233
231
fs . writeFileSync ( outputFile , JSON . stringify ( topojsonTopology ) ) ;
234
232
} else {
235
233
// In Mapshaper, layer names default to file names
@@ -251,13 +249,16 @@ const outputFilePathFiji50m = `${outputDirGeojson}/${unFilename}_50m/fiji.geojso
251
249
const outputFilePathFijiAntimeridian50m = `${ outputDirGeojson } /${ unFilename } _50m/fiji_antimeridian.geojson` ;
252
250
const outputFilePathRussia50m = `${ outputDirGeojson } /${ unFilename } _50m/russia.geojson` ;
253
251
const outputFilePathRussiaAntimeridian50m = `${ outputDirGeojson } /${ unFilename } _50m/russia_antimeridian.geojson` ;
254
- const copyFieldsList = "objectid,iso3cd,m49_cd,nam_en,lbl_en,georeg,geo_cd,sub_cd,int_cd,subreg,intreg,iso2cd,lbl_fr,name_fr,globalid,stscod,isoclr,ct,FID"
252
+ const copyFieldsList =
253
+ 'objectid,iso3cd,m49_cd,nam_en,lbl_en,georeg,geo_cd,sub_cd,int_cd,subreg,intreg,iso2cd,lbl_fr,name_fr,globalid,stscod,isoclr,ct,FID' ;
255
254
// The following fix up code is necessary to isolate/join/cut the polygons that cross the antimeridian.
256
255
// This is necessary for two reasons: the UN geojson is poor around the antimeridian and Mapshaper
257
256
// doesn't handle antimeridian cutting.
258
257
259
258
// Fix up Antarctica polygons
260
- await mapshaper . runCommands ( `${ inputFilePathUNGeojson } -filter 'iso3cd === "ATA"' target=1 -o ${ outputFilePathAntarctica50m } ` )
259
+ await mapshaper . runCommands (
260
+ `${ inputFilePathUNGeojson } -filter 'iso3cd === "ATA"' target=1 -o ${ outputFilePathAntarctica50m } `
261
+ ) ;
261
262
const commandsAntarctica = [
262
263
outputFilePathAntarctica50m ,
263
264
// Use 'snap-interval' to patch gap in Antarctica
@@ -267,19 +268,21 @@ const commandsAntarctica = [
267
268
'-merge-layers target=antarctica,antarctica_rectangle force' ,
268
269
`-dissolve2 target=antarctica copy-fields=${ copyFieldsList } ` ,
269
270
`-o force target=antarctica ${ outputFilePathAntarctica50m } `
270
- ] . join ( " " )
271
- await mapshaper . runCommands ( commandsAntarctica )
271
+ ] . join ( ' ' ) ;
272
+ await mapshaper . runCommands ( commandsAntarctica ) ;
272
273
273
274
// Fix up Fiji polygons
274
- await mapshaper . runCommands ( `${ inputFilePathUNGeojson } -filter 'iso3cd === "FJI"' target=1 -o ${ outputFilePathFiji50m } ` )
275
+ await mapshaper . runCommands (
276
+ `${ inputFilePathUNGeojson } -filter 'iso3cd === "FJI"' target=1 -o ${ outputFilePathFiji50m } `
277
+ ) ;
275
278
const commandsIsolateFijiAntimeridian = [
276
279
outputFilePathFiji50m ,
277
280
'-explode' ,
278
281
`-each 'id = this.id'` ,
279
282
`-filter '[31, 36, 39, 40].includes(id)' target=fiji + name=fiji_antimeridian` ,
280
283
`-o target=fiji_antimeridian ${ outputFilePathFijiAntimeridian50m } `
281
- ] . join ( " " )
282
- await mapshaper . runCommands ( commandsIsolateFijiAntimeridian )
284
+ ] . join ( ' ' ) ;
285
+ await mapshaper . runCommands ( commandsIsolateFijiAntimeridian ) ;
283
286
284
287
const commandsFixFijiAntimeridian = [
285
288
outputFilePathFijiAntimeridian50m ,
@@ -293,8 +296,8 @@ const commandsFixFijiAntimeridian = [
293
296
`-dissolve2 target=complete copy-fields=${ copyFieldsList } ` ,
294
297
'-proj wgs84' ,
295
298
`-o force target=complete ${ outputFilePathFijiAntimeridian50m } `
296
- ] . join ( " " )
297
- await mapshaper . runCommands ( commandsFixFijiAntimeridian )
299
+ ] . join ( ' ' ) ;
300
+ await mapshaper . runCommands ( commandsFixFijiAntimeridian ) ;
298
301
299
302
const commandsFiji = [
300
303
`-i combine-files ${ outputFilePathFiji50m } ${ outputFilePathFijiAntimeridian50m } ` ,
@@ -304,19 +307,21 @@ const commandsFiji = [
304
307
'-merge-layers target=fiji,fiji_antimeridian force name=fiji' ,
305
308
`-dissolve2 target=fiji copy-fields=${ copyFieldsList } ` ,
306
309
`-o force target=fiji ${ outputFilePathFiji50m } `
307
- ] . join ( " " )
308
- await mapshaper . runCommands ( commandsFiji )
310
+ ] . join ( ' ' ) ;
311
+ await mapshaper . runCommands ( commandsFiji ) ;
309
312
310
313
// Fix up Russia polygons
311
- await mapshaper . runCommands ( `${ inputFilePathUNGeojson } -filter 'iso3cd === "RUS"' target=1 -o ${ outputFilePathRussia50m } ` )
314
+ await mapshaper . runCommands (
315
+ `${ inputFilePathUNGeojson } -filter 'iso3cd === "RUS"' target=1 -o ${ outputFilePathRussia50m } `
316
+ ) ;
312
317
const commandsIsolateRussiaAntimeridian = [
313
318
outputFilePathRussia50m ,
314
319
'-explode' ,
315
320
`-each 'id = this.id'` ,
316
321
`-filter '[13, 15].includes(id)' target=russia + name=russia_antimeridian` ,
317
322
`-o target=russia_antimeridian ${ outputFilePathRussiaAntimeridian50m } `
318
- ] . join ( " " )
319
- await mapshaper . runCommands ( commandsIsolateRussiaAntimeridian )
323
+ ] . join ( ' ' ) ;
324
+ await mapshaper . runCommands ( commandsIsolateRussiaAntimeridian ) ;
320
325
321
326
const commandsFixRussiaAntimeridian = [
322
327
outputFilePathRussiaAntimeridian50m ,
@@ -330,8 +335,8 @@ const commandsFixRussiaAntimeridian = [
330
335
`-dissolve2 target=complete copy-fields=${ copyFieldsList } ` ,
331
336
'-proj wgs84' ,
332
337
`-o force target=complete ${ outputFilePathRussiaAntimeridian50m } `
333
- ] . join ( " " )
334
- await mapshaper . runCommands ( commandsFixRussiaAntimeridian )
338
+ ] . join ( ' ' ) ;
339
+ await mapshaper . runCommands ( commandsFixRussiaAntimeridian ) ;
335
340
336
341
const commandsRussia = [
337
342
`-i combine-files ${ outputFilePathRussia50m } ${ outputFilePathRussiaAntimeridian50m } ` ,
@@ -341,8 +346,8 @@ const commandsRussia = [
341
346
'-merge-layers target=russia,russia_antimeridian force name=russia' ,
342
347
`-dissolve2 target=russia copy-fields=${ copyFieldsList } ` ,
343
348
`-o force target=russia ${ outputFilePathRussia50m } `
344
- ] . join ( " " )
345
- await mapshaper . runCommands ( commandsRussia )
349
+ ] . join ( ' ' ) ;
350
+ await mapshaper . runCommands ( commandsRussia ) ;
346
351
347
352
// Process 50m UN geodata
348
353
@@ -366,16 +371,12 @@ const commandsCountries50m = [
366
371
`-o ${ outputFilePathCountries50m } `
367
372
] . join ( ' ' ) ;
368
373
await mapshaper . runCommands ( commandsCountries50m ) ;
369
- clampToAntimeridian ( outputFilePathCountries50m )
374
+ clampToAntimeridian ( outputFilePathCountries50m ) ;
370
375
371
376
// Get land from all polygon features
372
377
const inputFilePathLand50m = outputFilePathCountries50m ;
373
378
const outputFilePathLand50m = `${ outputDirGeojson } /${ unFilename } _50m/land.geojson` ;
374
- const commandsLand50m = [
375
- inputFilePathLand50m ,
376
- '-dissolve2' ,
377
- `-o ${ outputFilePathLand50m } `
378
- ] . join ( ' ' ) ;
379
+ const commandsLand50m = [ inputFilePathLand50m , '-dissolve2' , `-o ${ outputFilePathLand50m } ` ] . join ( ' ' ) ;
379
380
await mapshaper . runCommands ( commandsLand50m ) ;
380
381
381
382
// Process 110m UN geodata
@@ -395,11 +396,7 @@ await mapshaper.runCommands(commandsCountries110m);
395
396
// Get land from all polygon features
396
397
const inputFilePathLand110m = outputFilePathCountries110m ;
397
398
const outputFilePathLand110m = `${ outputDirGeojson } /${ unFilename } _110m/land.geojson` ;
398
- const commandsLand110m = [
399
- inputFilePathLand110m ,
400
- '-dissolve2' ,
401
- `-o ${ outputFilePathLand110m } `
402
- ] . join ( ' ' ) ;
399
+ const commandsLand110m = [ inputFilePathLand110m , '-dissolve2' , `-o ${ outputFilePathLand110m } ` ] . join ( ' ' ) ;
403
400
await mapshaper . runCommands ( commandsLand110m ) ;
404
401
405
402
for ( const resolution of resolutions ) {
0 commit comments