Browse Source

Update bounds computation

Use D3's bounds computation per projection.
master
TheoryOfNekomata 2 years ago
parent
commit
848a817436
12 changed files with 322 additions and 16 deletions
  1. +1
    -0
      .gitignore
  2. BIN
      __fixtures__/gb.png
  3. BIN
      __fixtures__/jp.png
  4. BIN
      __fixtures__/mercator.png
  5. BIN
      __fixtures__/ph.png
  6. BIN
      __fixtures__/robinson.png
  7. BIN
      __fixtures__/ru.png
  8. BIN
      __fixtures__/sinusoidal.png
  9. BIN
      __fixtures__/us.png
  10. BIN
      __fixtures__/world.png
  11. +260
    -0
      assets/ne_10m_admin_0_countries.json
  12. +61
    -16
      src/index.ts

+ 1
- 0
.gitignore View File

@@ -105,3 +105,4 @@ dist
.tern-port

.npmrc
assets/

BIN
__fixtures__/gb.png View File

Before After
Width: 800  |  Height: 406  |  Size: 112 KiB

BIN
__fixtures__/jp.png View File

Before After
Width: 800  |  Height: 406  |  Size: 108 KiB

BIN
__fixtures__/mercator.png View File

Before After
Width: 461  |  Height: 461  |  Size: 54 KiB

BIN
__fixtures__/ph.png View File

Before After
Width: 200  |  Height: 320  |  Size: 24 KiB Width: 800  |  Height: 400  |  Size: 98 KiB

BIN
__fixtures__/robinson.png View File

Before After
Width: 461  |  Height: 234  |  Size: 54 KiB

BIN
__fixtures__/ru.png View File

Before After
Width: 800  |  Height: 406  |  Size: 112 KiB

BIN
__fixtures__/sinusoidal.png View File

Before After
Width: 461  |  Height: 231  |  Size: 46 KiB

BIN
__fixtures__/us.png View File

Before After
Width: 800  |  Height: 406  |  Size: 112 KiB

BIN
__fixtures__/world.png View File

Before After
Width: 4608  |  Height: 4608  |  Size: 7.1 MiB

+ 260
- 0
assets/ne_10m_admin_0_countries.json
File diff suppressed because it is too large
View File


+ 61
- 16
src/index.ts View File

@@ -1,6 +1,6 @@
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { project, Bounds } from '@theoryofnekomata/orbis-core';
import { project, ProjectBounds } from '@theoryofnekomata/orbis-core';
import { writeFile } from 'fs/promises';
import { basename, dirname, resolve } from 'path';

@@ -8,10 +8,11 @@ type ProjectArgs = {
input: string,
projection: string,
output?: string,
bounds: Bounds,
bounds: ProjectBounds,
width?: number,
height?: number,
padding: [number, number],
country?: string,
};

const coerceNumber = (n?: string) => {
@@ -36,21 +37,62 @@ const coercePadding = (n?: string) => {
];
};

const coerceBounds = (n?: string): Bounds => {
const coerceBounds = (n?: string): ProjectBounds => {
if (typeof n !== 'string') {
return [
[-180, 90],
[180, -90],
] as Bounds;
return {
type: 'geojson',
value: {
type: 'Sphere',
},
};
}

return n
.split(';')
.map((p) => (
p
.split(',')
.map((c: string) => Number(c))
)) as Bounds;
const [boundsType, etcBounds] = n.split(':');

if (boundsType === 'geojson') {
const [geometryType, etcArgs] = etcBounds.split(';');

if (geometryType === 'Sphere') {
return {
type: boundsType,
value: {
type: geometryType,
},
};
}

if (geometryType === 'Polygon' || geometryType === 'MultiPolygon') {
return {
type: boundsType,
value: {
type: geometryType,
coordinates: JSON.parse(etcArgs),
},
};
}

return {
type: 'geojson',
value: {
type: 'Sphere',
},
};
}

if (boundsType === 'country') {
const [country] = etcBounds.split(';');
return {
type: boundsType,
value: country,
};
}

return {
type: 'geojson',
value: {
type: 'Sphere',
},
};
};

const main = async (argv: string | readonly string[]) => {
@@ -85,12 +127,15 @@ const main = async (argv: string | readonly string[]) => {
.coerce('height', coerceNumber)
.option('padding', {
alias: 'p',
default: [0, 0],
default: '0;0',
})
.coerce('padding', coercePadding)
.option('bounds', {
alias: 'b',
default: [[-180, 90], [180, -90]],
default: 'geojson:Sphere',
})
.option('country', {
alias: 'c',
})
.coerce('bounds', coerceBounds),
handler: async (projectArgvRaw) => {


Loading…
Cancel
Save