Browse Source

Initial commit

Add files from Pridepack.
master
TheoryOfNekomata 1 year ago
commit
6ba29aec61
11 changed files with 3898 additions and 0 deletions
  1. +13
    -0
      .eslintrc
  2. +107
    -0
      .gitignore
  3. +7
    -0
      LICENSE
  4. +95
    -0
      package.json
  5. +3
    -0
      pridepack.json
  6. +7
    -0
      src/global.d.ts
  7. +81
    -0
      src/index.ts
  8. +63
    -0
      test/index.test.ts
  9. +21
    -0
      tsconfig.eslint.json
  10. +21
    -0
      tsconfig.json
  11. +3480
    -0
      yarn.lock

+ 13
- 0
.eslintrc View File

@@ -0,0 +1,13 @@
{
"root": true,
"extends": [
"lxsmnsyc/typescript"
],
"rules": {
"import/prefer-default-export": "off",
"@typescript-eslint/unbound-method": "off"
},
"parserOptions": {
"project": "./tsconfig.eslint.json"
}
}

+ 107
- 0
.gitignore View File

@@ -0,0 +1,107 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.production
.env.development

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

.npmrc

+ 7
- 0
LICENSE View File

@@ -0,0 +1,7 @@
MIT License Copyright (c) 2022 TheoryOfNekomata <allan.crisostomo@outlook.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 95
- 0
package.json View File

@@ -0,0 +1,95 @@
{
"name": "@theoryofnekomata/orbis-core",
"version": "0.0.0",
"types": "dist/types/index.d.ts",
"main": "dist/cjs/production/index.js",
"module": "dist/esm/production/index.js",
"exports": {
".": {
"development": {
"require": "./dist/cjs/development/index.js",
"import": "./dist/esm/development/index.js"
},
"require": "./dist/cjs/production/index.js",
"import": "./dist/esm/production/index.js",
"types": "./dist/types/index.d.ts"
},
"./dev": {
"production": {
"require": "./dist/cjs/production/index.js",
"import": "./dist/esm/production/index.js"
},
"require": "./dist/cjs/development/index.js",
"import": "./dist/esm/development/index.js",
"types": "./dist/types/index.d.ts"
},
"./esm": {
"development": "./dist/esm/development/index.js",
"production": "./dist/esm/production/index.js",
"default": "./dist/esm/production/index.js",
"types": "./dist/types/index.d.ts"
},
"./cjs": {
"development": "./dist/cjs/development/index.js",
"production": "./dist/cjs/production/index.js",
"default": "./dist/cjs/production/index.js",
"types": "./dist/types/index.d.ts"
}
},
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
},
"license": "MIT",
"keywords": [
"pridepack",
"map",
"projection"
],
"devDependencies": {
"@types/d3": "^7.1.0",
"@types/d3-geo": "^3.0.2",
"@types/node": "^17.0.13",
"eslint": "^8.8.0",
"eslint-config-lxsmnsyc": "^0.4.0",
"fast-check": "^2.24.0",
"pridepack": "1.1.0",
"tslib": "^2.3.1",
"typescript": "^4.5.4",
"vitest": "^0.2.5"
},
"scripts": {
"prepublishOnly": "pridepack clean && pridepack build",
"build": "pridepack build",
"type-check": "pridepack check",
"lint": "pridepack lint",
"clean": "pridepack clean",
"watch": "pridepack watch",
"start": "pridepack start",
"dev": "pridepack dev",
"test": "vitest"
},
"private": false,
"description": "Utility for map projections.",
"repository": {
"url": "https://code.modal.sh/modal-soft/orbis-core",
"type": "git"
},
"homepage": "",
"bugs": {
"url": "https://code.modal.sh/modal-soft/orbis-core/issues"
},
"author": "TheoryOfNekomata <allan.crisostomo@outlook.com>",
"publishConfig": {
"access": "public"
},
"dependencies": {
"d3": "^7.4.3",
"d3-geo": "^3.0.1",
"d3-geo-polygon": "^1.12.1",
"d3-geo-projection": "^4.0.0"
}
}

+ 3
- 0
pridepack.json View File

@@ -0,0 +1,3 @@
{
"target": "es2017"
}

+ 7
- 0
src/global.d.ts View File

@@ -0,0 +1,7 @@
declare module 'd3-geo-polygon';
declare module 'd3-geo-projection' {
import * as d3geo from 'd3-geo';

export * from 'd3-geo';
export function geoSinusoidal(): d3geo.GeoProjection;
}

+ 81
- 0
src/index.ts View File

@@ -0,0 +1,81 @@
import * as d3geo from 'd3-geo';
import * as d3geoProjection from 'd3-geo-projection';
import * as d3geoPolygon from 'd3-geo-polygon';

type Coords = [number, number]
type Bounds = [Coords, Coords]

type ProjectionFunction = (...args: unknown[]) => (
d3geo.GeoProjection | d3geoProjection.GeoProjection
)

const referenceWidth = 960 as const;
const referenceHeight = 480 as const;

type ProjectOptions = {
inputDimensions?: { width: number, height: number },
bounds?: Bounds,
}

type Projection = [string, ...unknown[]]

const isValidLongitude = (lng: number) => lng >= -180 && lng <= 180;
const isValidLatitude = (lat: number) => lat >= -90 && lat <= 90;
const getCenter = (bounds: Bounds): Coords => {
const [nw, se] = bounds;
const [nwLng, nwLat] = nw;
const [seLng, seLat] = se;
return [
// TODO: better center checking that wraps longitudes
(nwLng + seLng) / 2,
(nwLat + seLat) / 2,
];
};

export const project = (
point: [number, number],
projection: Projection,
options: ProjectOptions = {},
): [number, number] | null => {
const [lng, lat] = point;
if (!(isValidLongitude(lng) && isValidLatitude(lat))) {
throw new RangeError('Invalid value for point. Only values from [-180, -90] to [180, 90] are allowed');
}

const [projectionName, ...projectionArgs] = projection;
if (projectionName === 'Equirectangular') {
return point;
}

const projectionFunctionName = `geo${projectionName}`;
let geoProjectionSource: Record<string, unknown> | undefined;
if (projectionFunctionName in d3geoPolygon) {
geoProjectionSource = d3geoPolygon as Record<string, unknown>;
} else if (projectionFunctionName in d3geoProjection) {
geoProjectionSource = d3geoProjection;
} else if (projectionFunctionName in d3geo) {
geoProjectionSource = d3geo;
}

if (!geoProjectionSource) {
throw new Error(`Unknown projection: ${projectionName}`);
}

const projectionFn = geoProjectionSource[projectionFunctionName] as ProjectionFunction;
const baseProjection = projectionFn(...projectionArgs);
const {
bounds = [[-180, 90], [180, -90]],
inputDimensions = { width: referenceWidth, height: referenceHeight },
} = options;

const { invert } = baseProjection.center(getCenter(bounds));
if (!invert) {
return null;
}

const { width: sx, height: sy } = inputDimensions;
return invert([
(lng / sx) * referenceWidth,
(lat / sy) * referenceHeight,
]) as [number, number];
};

+ 63
- 0
test/index.test.ts View File

@@ -0,0 +1,63 @@
// import * as fc from 'fast-check';
import { describe, it, expect } from 'vitest';
import * as orbisCore from '../src';

describe('orbis-core', () => {
describe('project', () => {
it('throws error for invalid coordinates', () => {
expect(() => orbisCore.project([-181, 90], ['Equirectangular'])).toThrowError(RangeError);
expect(() => orbisCore.project([181, 90], ['Equirectangular'])).toThrowError(RangeError);
expect(() => orbisCore.project([-180, 91], ['Equirectangular'])).toThrowError(RangeError);
expect(() => orbisCore.project([-180, -91], ['Equirectangular'])).toThrowError(RangeError);
});

it('returns identity for equirectangular', () => {
// fc.assert(
// fc.property(
// fc.tuple(
// fc.float().filter((c) => -180 <= c && c <= 180),
// fc.float().filter((c) => -180 <= c && c <= 180),
// ),
// (coords) => {
// expect(orbisCore.project(coords, 'Equirectangular')).toEqual(coords);
// },
// ),
// );

[
[0, 0] as [number, number],
[45, 45] as [number, number],
[90, 90] as [number, number],
]
.forEach((coords) => {
expect(orbisCore.project(coords, ['Equirectangular'])).toEqual(coords);
});
});

it('returns coords for other projections', () => {
// fc.assert(
// fc.property(
// fc.tuple(
// fc.float().filter((c) => -180 <= c && c <= 180),
// fc.float().filter((c) => -180 <= c && c <= 180),
// ),
// (coords) => {
// expect(orbisCore.project(coords, 'Equirectangular')).toEqual(coords);
// },
// ),
// );

[
[0, 0] as [number, number],
[45, 45] as [number, number],
[90, 90] as [number, number],
]
.forEach((coords) => {
expect(orbisCore.project(coords, ['Mercator'])).toEqual([
expect.any(Number),
expect.any(Number),
]);
});
});
});
});

+ 21
- 0
tsconfig.eslint.json View File

@@ -0,0 +1,21 @@
{
"exclude": ["node_modules"],
"include": ["src", "types", "test"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"jsx": "react",
"esModuleInterop": true,
"target": "ES2017"
}
}

+ 21
- 0
tsconfig.json View File

@@ -0,0 +1,21 @@
{
"exclude": ["node_modules"],
"include": ["src", "types"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./src",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"jsx": "react",
"esModuleInterop": true,
"target": "ES2017"
}
}

+ 3480
- 0
yarn.lock
File diff suppressed because it is too large
View File


Loading…
Cancel
Save