@allmaps/stdlib
Advanced tools
Comparing version 1.0.0-beta.17 to 1.0.0-beta.18
@@ -1,2 +0,2 @@ | ||
import type { Position } from '@allmaps/types'; | ||
import type { Point } from '@allmaps/types'; | ||
type Color = [number, number, number]; | ||
@@ -10,3 +10,3 @@ type ColorCount = { | ||
}; | ||
export declare function getImageData(imageElement: HTMLImageElement, mask?: Position[]): ImageData; | ||
export declare function getImageData(imageElement: HTMLImageElement, mask?: Point[]): ImageData; | ||
export declare function getColorsArray(imageData: ImageData, resolution?: number): Color[]; | ||
@@ -13,0 +13,0 @@ export declare function getColorHistogram(colors: Color[], binSize?: number): Histogram; |
@@ -1,6 +0,14 @@ | ||
import type { Polygon, LineString, Bbox, Extent } from '@allmaps/types'; | ||
export declare function computeExtent(values: number[]): Extent; | ||
export declare function computeBbox(points: LineString): Bbox; | ||
export declare function computeBbox(points: Polygon): Bbox; | ||
export declare function combineBBoxes(bbox1: Bbox, bbox2: Bbox): Bbox; | ||
import type { Point, Polygon, Geometry, Line, Rectangle, Bbox, Size, GeojsonGeometry } from '@allmaps/types'; | ||
export declare function computeMinMax(values: number[]): [number, number]; | ||
export declare function computeBbox(points: Geometry | GeojsonGeometry): Bbox; | ||
export declare function combineBboxes(bbox0: Bbox, bbox1: Bbox): Bbox; | ||
export declare function isOverlapping(bbox0: Bbox, bbox1: Bbox): boolean; | ||
export declare function bboxToRectangle(bbox: Bbox): Rectangle; | ||
export declare function bboxToPolygon(bbox: Bbox): Polygon; | ||
export declare function bboxToLine(bbox: Bbox): Line; | ||
export declare function bboxToDiameter(bbox: Bbox): number; | ||
export declare function geometryToDiameter(geometry: Geometry | GeojsonGeometry): number; | ||
export declare function bboxToCenter(bbox: Bbox): Point; | ||
export declare function bboxToSize(bbox: Bbox): Size; | ||
export declare function sizesToScale(size0: Size, size1: Size): number; | ||
export declare function bboxesToScale(bbox0: Bbox, bbox1: Bbox): number; |
@@ -1,3 +0,5 @@ | ||
import { isPolygon } from './geometry.js'; | ||
export function computeExtent(values) { | ||
import { isGeojsonGeometry, convertGeojsonGeometryToGeometry } from './geojson.js'; | ||
import { isPoint, isPolygon, distance } from './geometry.js'; | ||
// Compute | ||
export function computeMinMax(values) { | ||
let min = Number.POSITIVE_INFINITY; | ||
@@ -19,6 +21,13 @@ let max = Number.NEGATIVE_INFINITY; | ||
} | ||
// Note: bbox order is minX, minY, maxX, maxY | ||
export function computeBbox(points) { | ||
if (isPoint(points)) { | ||
points = [points]; | ||
} | ||
if (isPolygon(points)) { | ||
points = points.flat(); | ||
} | ||
if (isGeojsonGeometry(points)) { | ||
return computeBbox(convertGeojsonGeometryToGeometry(points)); | ||
} | ||
const xs = []; | ||
@@ -30,23 +39,58 @@ const ys = []; | ||
} | ||
const [minX, maxX] = computeExtent(xs); | ||
const [minY, maxY] = computeExtent(ys); | ||
const [minX, maxX] = computeMinMax(xs); | ||
const [minY, maxY] = computeMinMax(ys); | ||
return [minX, minY, maxX, maxY]; | ||
} | ||
export function combineBBoxes(bbox1, bbox2) { | ||
export function combineBboxes(bbox0, bbox1) { | ||
return [ | ||
Math.min(bbox1[0], bbox2[0]), | ||
Math.min(bbox1[1], bbox2[1]), | ||
Math.max(bbox1[2], bbox2[2]), | ||
Math.max(bbox1[3], bbox2[3]) | ||
Math.min(bbox0[0], bbox1[0]), | ||
Math.min(bbox0[1], bbox1[1]), | ||
Math.max(bbox0[2], bbox1[2]), | ||
Math.max(bbox0[3], bbox1[3]) | ||
]; | ||
} | ||
//[xMin, yMin, xMax, yMax] | ||
export function isOverlapping(bbox0, bbox1) { | ||
const isOverlappingInX = bbox0[2] >= bbox1[0] && bbox1[2] >= bbox0[0]; | ||
const isOverlappingInY = bbox0[3] >= bbox1[1] && bbox1[3] >= bbox0[1]; | ||
return isOverlappingInX && isOverlappingInY; | ||
} | ||
// Transform | ||
export function bboxToRectangle(bbox) { | ||
return [ | ||
[bbox[0], bbox[1]], | ||
[bbox[2], bbox[1]], | ||
[bbox[2], bbox[3]], | ||
[bbox[0], bbox[3]] | ||
]; | ||
} | ||
export function bboxToPolygon(bbox) { | ||
return [bboxToRectangle(bbox)]; | ||
} | ||
export function bboxToLine(bbox) { | ||
return [ | ||
[ | ||
[bbox[0], bbox[1]], | ||
[bbox[2], bbox[1]], | ||
[bbox[2], bbox[3]], | ||
[bbox[0], bbox[3]] | ||
] | ||
[bbox[0], bbox[1]], | ||
[bbox[2], bbox[3]] | ||
]; | ||
} | ||
export function bboxToDiameter(bbox) { | ||
return distance(bboxToLine(bbox)); | ||
} | ||
export function geometryToDiameter(geometry) { | ||
return distance(bboxToLine(computeBbox(geometry))); | ||
} | ||
export function bboxToCenter(bbox) { | ||
return [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2]; | ||
} | ||
export function bboxToSize(bbox) { | ||
return [bbox[2] - bbox[0], bbox[3] - bbox[1]]; | ||
} | ||
// Scales | ||
export function sizesToScale(size0, size1) { | ||
const scaleX = size0[0] / size1[0]; | ||
const scaleY = size0[1] / size1[1]; | ||
return Math.min(scaleX, scaleY); | ||
} | ||
export function bboxesToScale(bbox0, bbox1) { | ||
return sizesToScale(bboxToSize(bbox0), bboxToSize(bbox1)); | ||
} |
@@ -1,2 +0,2 @@ | ||
import type { Position, LineString, Ring, Polygon, GeojsonPoint, GeojsonLineString, GeojsonPolygon, GeojsonGeometry, GeojsonFeature, GeojsonFeatureCollection, SvgGeometry } from '@allmaps/types'; | ||
import type { Point, LineString, Ring, Polygon, Geometry, GeojsonPoint, GeojsonLineString, GeojsonPolygon, GeojsonGeometry, GeojsonFeature, GeojsonFeatureCollection, SvgGeometry } from '@allmaps/types'; | ||
export declare function isGeojsonPoint(input: any): input is GeojsonPoint; | ||
@@ -6,6 +6,7 @@ export declare function isGeojsonLineString(input: any): input is GeojsonLineString; | ||
export declare function isGeojsonGeometry(obj: unknown): obj is GeojsonGeometry; | ||
export declare function convertGeojsonPointToPosition(geometry: GeojsonPoint): Position; | ||
export declare function convertGeojsonPointToPoint(geometry: GeojsonPoint): Point; | ||
export declare function convertGeojsonLineStringToLineString(geometry: GeojsonLineString): LineString; | ||
export declare function convertGeojsonPolygonToRing(geometry: GeojsonPolygon, close?: boolean): Ring; | ||
export declare function convertGeojsonPolygonToPolygon(geometry: GeojsonPolygon, close?: boolean): Polygon; | ||
export declare function convertGeojsonGeometryToGeometry(geometry: GeojsonGeometry): Geometry; | ||
export declare function convertGeojsonToSvg(geometry: GeojsonGeometry): SvgGeometry; | ||
@@ -12,0 +13,0 @@ export declare function geometryToFeature(geometry: GeojsonGeometry, properties?: unknown): GeojsonFeature; |
@@ -1,2 +0,2 @@ | ||
import { isLineString, isPolygon, isPosition, conformRing, conformPolygon } from './geometry.js'; | ||
import { isLineString, isPolygon, isPoint as isPoint, conformRing, conformPolygon } from './geometry.js'; | ||
// Assert | ||
@@ -7,3 +7,3 @@ export function isGeojsonPoint(input) { | ||
input.type === 'Point' && | ||
isPosition(input.coordinates)); | ||
isPoint(input.coordinates)); | ||
} | ||
@@ -34,3 +34,3 @@ export function isGeojsonLineString(input) { | ||
// Convert to Geometry | ||
export function convertGeojsonPointToPosition(geometry) { | ||
export function convertGeojsonPointToPoint(geometry) { | ||
return geometry.coordinates; | ||
@@ -51,2 +51,16 @@ } | ||
} | ||
export function convertGeojsonGeometryToGeometry(geometry) { | ||
if (isGeojsonPoint(geometry)) { | ||
return convertGeojsonPointToPoint(geometry); | ||
} | ||
if (isGeojsonLineString(geometry)) { | ||
return convertGeojsonLineStringToLineString(geometry); | ||
} | ||
if (isGeojsonPolygon(geometry)) { | ||
return convertGeojsonPolygonToPolygon(geometry); | ||
} | ||
else { | ||
throw new Error('Geometry type not supported'); | ||
} | ||
} | ||
// Convert to SVG | ||
@@ -53,0 +67,0 @@ export function convertGeojsonToSvg(geometry) { |
@@ -1,17 +0,22 @@ | ||
import type { Position, LineString, Ring, Polygon, GeojsonPoint, GeojsonLineString, GeojsonPolygon } from '@allmaps/types'; | ||
export declare function isPosition(input: any): input is Position; | ||
import type { Point, LineString, Line, Ring, Polygon, Geometry, GeojsonPoint, GeojsonLineString, GeojsonPolygon, GeojsonGeometry } from '@allmaps/types'; | ||
export declare function isPoint(input: any): input is Point; | ||
export declare function isLineString(input: any): input is LineString; | ||
export declare function isPolygon(input: any): input is Polygon; | ||
export declare function isGeometry(input: any): input is Geometry; | ||
export declare function conformLineString(lineString: LineString): LineString; | ||
export declare function conformRing(ring: Ring): Ring; | ||
export declare function conformPolygon(polygon: Polygon): Polygon; | ||
export declare function convertPositionToGeojsonPoint(position: Position): GeojsonPoint; | ||
export declare function convertPointToGeojsonPoint(point: Point): GeojsonPoint; | ||
export declare function convertLineStringToGeojsonLineString(lineString: LineString): GeojsonLineString; | ||
export declare function convertRingToGeojsonPolygon(ring: Ring, close?: boolean): GeojsonPolygon; | ||
export declare function convertPolygonToGeojsonPolygon(polygon: Polygon, close?: boolean): GeojsonPolygon; | ||
export declare function isClosed(input: Position[]): boolean; | ||
export declare function isEqualPosition(position1: Position, position2: Position): boolean; | ||
export declare function isEqualPositionArray(positionArray1: Position[], positionArray2: Position[]): boolean; | ||
export declare function isEqualPositionArrayArray(positionArrayArray1: Position[][], positionArrayArray2: Position[][]): boolean; | ||
export declare function getMidPosition(position1: Position, position2: Position): Position; | ||
export declare function getDistance(from: Position, to: Position): number; | ||
export declare function convertGeometryToGeojsonGeometry(geometry: Geometry): GeojsonGeometry; | ||
export declare function isClosed(input: Point[]): boolean; | ||
export declare function isEqualPoint(point1: Point, point: Point): boolean; | ||
export declare function isEqualPointArray(pointArray1: Point[], pointArray2: Point[]): boolean; | ||
export declare function isEqualPointArrayArray(pointArrayArray1: Point[][], pointArrayArray2: Point[][]): boolean; | ||
export declare function midPoint(point0: Point, point1: Point): Point; | ||
export declare function mixPoints(point0: Point, point1: Point, t: number): Point; | ||
export declare function distance(line: Line): number; | ||
export declare function distance(from: Point, to: Point): number; | ||
export declare function degreesToRadians(degrees: number): number; |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { rewindGeometry } from '@placemarkio/geojson-rewind'; // TODO: consider implementing these functions in this module instead of using dependencies | ||
// Assert | ||
export function isPosition(input) { | ||
export function isPoint(input) { | ||
return (Array.isArray(input) && | ||
@@ -11,3 +11,3 @@ input.length === 2 && | ||
export function isLineString(input) { | ||
return Array.isArray(input) && input.every(isPosition); | ||
return Array.isArray(input) && input.every(isPoint); | ||
// && !isClosed(input) // Possible addition if we want to check for closedness | ||
@@ -18,3 +18,3 @@ } | ||
function isRing(input) { | ||
return (Array.isArray(input) && input.every(isPosition) | ||
return (Array.isArray(input) && input.every(isPoint) | ||
// && isClosed(input) == closed // Possible addition if we want to check for closedness, with closed an input parameter with default false | ||
@@ -26,10 +26,13 @@ ); | ||
} | ||
export function isGeometry(input) { | ||
return isPoint(input) || isLineString(input) || isPolygon(input); | ||
} | ||
// Conform | ||
export function conformLineString(lineString) { | ||
// Filter out repeated positions | ||
lineString = lineString.filter(function (position, i, originalLineString) { | ||
return i === 0 || !isEqualPosition(position, originalLineString[i - 1]); | ||
// Filter out repeated points | ||
lineString = lineString.filter(function (point, i, originalLineString) { | ||
return i === 0 || !isEqualPoint(point, originalLineString[i - 1]); | ||
}); | ||
if (lineString.length < 2) { | ||
throw new Error('LineString should contain at least 2 positions'); | ||
throw new Error('LineString should contain at least 2 points'); | ||
} | ||
@@ -39,7 +42,7 @@ return lineString; | ||
export function conformRing(ring) { | ||
// Filter out repeated positions | ||
ring = ring.filter(function (position, i, originalRing) { | ||
return i === 0 || !isEqualPosition(position, originalRing[i - 1]); | ||
// Filter out repeated points | ||
ring = ring.filter(function (point, i, originalRing) { | ||
return i === 0 || !isEqualPoint(point, originalRing[i - 1]); | ||
}); | ||
// Remove last position if input is closed ring | ||
// Remove last point if input is closed ring | ||
if (isClosed(ring)) { | ||
@@ -49,3 +52,3 @@ ring.splice(-1); | ||
if (ring.length < 3) { | ||
throw new Error('Ring should contain at least 3 positions'); | ||
throw new Error('Ring should contain at least 3 points'); | ||
} | ||
@@ -60,6 +63,6 @@ return ring; | ||
// Convert to GeoJSON | ||
export function convertPositionToGeojsonPoint(position) { | ||
export function convertPointToGeojsonPoint(point) { | ||
return { | ||
type: 'Point', | ||
coordinates: position | ||
coordinates: point | ||
}; | ||
@@ -91,2 +94,16 @@ } | ||
} | ||
export function convertGeometryToGeojsonGeometry(geometry) { | ||
if (isPoint(geometry)) { | ||
return convertPointToGeojsonPoint(geometry); | ||
} | ||
if (isLineString(geometry)) { | ||
return convertLineStringToGeojsonLineString(geometry); | ||
} | ||
if (isPolygon(geometry)) { | ||
return convertPolygonToGeojsonPolygon(geometry); | ||
} | ||
else { | ||
throw new Error('Geometry type not supported'); | ||
} | ||
} | ||
// Check | ||
@@ -96,20 +113,20 @@ export function isClosed(input) { | ||
input.length >= 2 && | ||
isEqualPosition(input[0], input[input.length - 1])); | ||
isEqualPoint(input[0], input[input.length - 1])); | ||
} | ||
export function isEqualPosition(position1, position2) { | ||
if (position1 === position2) | ||
export function isEqualPoint(point1, point) { | ||
if (point1 === point) | ||
return true; | ||
if (position1 == null || position2 == null) | ||
if (point1 == null || point == null) | ||
return false; | ||
return position1[0] == position2[0] && position1[1] == position2[1]; | ||
return point1[0] == point[0] && point1[1] == point[1]; | ||
} | ||
export function isEqualPositionArray(positionArray1, positionArray2) { | ||
if (positionArray1 === positionArray2) | ||
export function isEqualPointArray(pointArray1, pointArray2) { | ||
if (pointArray1 === pointArray2) | ||
return true; | ||
if (positionArray1 == null || positionArray2 == null) | ||
if (pointArray1 == null || pointArray2 == null) | ||
return false; | ||
if (positionArray1.length !== positionArray2.length) | ||
if (pointArray1.length !== pointArray2.length) | ||
return false; | ||
for (let i = 0; i < positionArray1.length; ++i) { | ||
if (isEqualPosition(positionArray1[i], positionArray2[i])) | ||
for (let i = 0; i < pointArray1.length; ++i) { | ||
if (isEqualPoint(pointArray1[i], pointArray2[i])) | ||
return false; | ||
@@ -119,11 +136,11 @@ } | ||
} | ||
export function isEqualPositionArrayArray(positionArrayArray1, positionArrayArray2) { | ||
if (positionArrayArray1 === positionArrayArray2) | ||
export function isEqualPointArrayArray(pointArrayArray1, pointArrayArray2) { | ||
if (pointArrayArray1 === pointArrayArray2) | ||
return true; | ||
if (positionArrayArray1 == null || positionArrayArray2 == null) | ||
if (pointArrayArray1 == null || pointArrayArray2 == null) | ||
return false; | ||
if (positionArrayArray1.length !== positionArrayArray2.length) | ||
if (pointArrayArray1.length !== pointArrayArray2.length) | ||
return false; | ||
for (let i = 0; i < positionArrayArray1.length; ++i) { | ||
if (isEqualPositionArray(positionArrayArray1[i], positionArrayArray2[i])) | ||
for (let i = 0; i < pointArrayArray1.length; ++i) { | ||
if (isEqualPointArray(pointArrayArray1[i], pointArrayArray2[i])) | ||
return false; | ||
@@ -134,10 +151,27 @@ } | ||
// Compute | ||
export function getMidPosition(position1, position2) { | ||
export function midPoint(point0, point1) { | ||
return [ | ||
(position2[0] - position1[0]) / 2 + position1[0], | ||
(position2[1] - position1[1]) / 2 + position1[1] | ||
(point1[0] - point0[0]) / 2 + point0[0], | ||
(point1[1] - point0[1]) / 2 + point0[1] | ||
]; | ||
} | ||
export function getDistance(from, to) { | ||
return Math.sqrt((to[0] - from[0]) ** 2 + (to[1] - from[1]) ** 2); | ||
export function mixPoints(point0, point1, t) { | ||
return [ | ||
point0[0] * t + point1[0] * (1 - t), | ||
point0[1] * t + point1[1] * (1 - t) | ||
]; | ||
} | ||
export function distance(from, to) { | ||
if (isLineString(from) && from.length == 2) { | ||
return distance(from[0], from[1]); | ||
} | ||
else if (isPoint(from) && isPoint(to)) { | ||
return Math.sqrt((to[0] - from[0]) ** 2 + (to[1] - from[1]) ** 2); | ||
} | ||
else { | ||
throw new Error('Input type not supported'); | ||
} | ||
} | ||
export function degreesToRadians(degrees) { | ||
return degrees * (Math.PI / 180); | ||
} |
@@ -6,6 +6,7 @@ export * from './api.js'; | ||
export * from './fetch.js'; | ||
export * from './geojson.js'; | ||
export * from './geometry.js'; | ||
export * from './main.js'; | ||
export * from './masks.js'; | ||
export * from './geometry.js'; | ||
export * from './svg.js'; | ||
export * from './geojson.js'; | ||
export * from './projection.js'; |
@@ -6,6 +6,7 @@ export * from './api.js'; | ||
export * from './fetch.js'; | ||
export * from './geojson.js'; | ||
export * from './geometry.js'; | ||
export * from './main.js'; | ||
export * from './masks.js'; | ||
export * from './geometry.js'; | ||
export * from './svg.js'; | ||
export * from './geojson.js'; | ||
export * from './projection.js'; |
export declare function equalArray<T>(arr1: Array<T> | null, arr2: Array<T> | null): boolean; | ||
export declare function equalSet<T>(set1: Set<T> | null, set2: Set<T> | null): boolean; | ||
export declare function maxOfNumberOrUndefined(number1: number | undefined, number2: number | undefined): number | undefined; | ||
export declare function isValidHttpUrl(string: string): boolean; |
@@ -15,2 +15,22 @@ export function equalArray(arr1, arr2) { | ||
} | ||
export function equalSet(set1, set2) { | ||
if (!set1 || !set2) { | ||
return false; | ||
} | ||
if (set1.size !== set2.size) { | ||
return false; | ||
} | ||
return [...set1].every((x) => set2.has(x)); | ||
} | ||
export function maxOfNumberOrUndefined(number1, number2) { | ||
if (number1 !== undefined && number2 !== undefined) { | ||
return Math.max(number1, number2); | ||
} | ||
else if (number1 !== undefined) { | ||
return number1; | ||
} | ||
else if (number2 !== undefined) { | ||
return number2; | ||
} | ||
} | ||
export function isValidHttpUrl(string) { | ||
@@ -17,0 +37,0 @@ let url; |
@@ -0,1 +1,3 @@ | ||
// TODO: the fullResourceMask is available directly in WarpedMap class! | ||
// This also means this function can be removed from stdlib. | ||
export function getFullResourceMask(imageWidth, imageHeight) { | ||
@@ -2,0 +4,0 @@ return [ |
@@ -51,3 +51,3 @@ import { parse } from 'svg-parser'; | ||
type: 'polyline', | ||
coordinates: getNodePositions(node) | ||
coordinates: getNodePoints(node) | ||
}; | ||
@@ -58,3 +58,3 @@ } | ||
type: 'polygon', | ||
coordinates: getNodePositions(node) | ||
coordinates: getNodePoints(node) | ||
}; | ||
@@ -95,3 +95,3 @@ } | ||
} | ||
function getNodePositions(node) { | ||
function getNodePoints(node) { | ||
const points = node?.properties?.points; | ||
@@ -109,3 +109,3 @@ if (points) { | ||
} | ||
function positionsToString(coordinates) { | ||
function pointsToString(coordinates) { | ||
return coordinates.map((coordinate) => coordinate.join(',')).join(' '); | ||
@@ -139,3 +139,3 @@ } | ||
...geometry.attributes, | ||
points: positionsToString(geometry.coordinates) | ||
points: pointsToString(geometry.coordinates) | ||
}); | ||
@@ -146,3 +146,3 @@ } | ||
...geometry.attributes, | ||
points: positionsToString(geometry.coordinates) | ||
points: pointsToString(geometry.coordinates) | ||
}); | ||
@@ -149,0 +149,0 @@ } |
{ | ||
"name": "@allmaps/stdlib", | ||
"version": "1.0.0-beta.17", | ||
"author": { | ||
"name": "Bert Spaan", | ||
"email": "hello@bertspaan.nl", | ||
"url": "https://bertspaan.nl" | ||
}, | ||
"version": "1.0.0-beta.18", | ||
"contributors": [ | ||
{ | ||
"name": "Bert Spaan", | ||
"email": "hello@bertspaan.nl", | ||
"url": "https://bertspaan.nl" | ||
}, | ||
{ | ||
"name": "Manuel Claeys Bouuaert", | ||
"email": "manuel.claeys.b@gmail.com", | ||
"url": "https://manuelclaeysbouuaert.be" | ||
} | ||
], | ||
"description": "Allmaps Standard Library", | ||
@@ -47,5 +54,5 @@ "type": "module", | ||
"dependencies": { | ||
"@allmaps/annotation": "^1.0.0-beta.19", | ||
"@allmaps/iiif-parser": "^1.0.0-beta.28", | ||
"@allmaps/types": "^1.0.0-beta.6", | ||
"@allmaps/annotation": "^1.0.0-beta.20", | ||
"@allmaps/iiif-parser": "^1.0.0-beta.29", | ||
"@allmaps/types": "^1.0.0-beta.7", | ||
"@placemarkio/geojson-rewind": "^1.0.2", | ||
@@ -65,3 +72,3 @@ "svg-parser": "^2.0.4" | ||
}, | ||
"gitHead": "e63909896795bd5576eceb1f5789c1ce8d386a9e" | ||
"gitHead": "f2c698aa696286f4ad7b6a842178ce5fa8c4e5c2" | ||
} |
38229
34
1038