@konfirm/geojson 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +16 -0
- package/.github/workflows/release.yml +21 -0
- package/.github/workflows/tests.yml +56 -0
- package/CHANGELOG.md +21 -0
- package/LICENSE +21 -0
- package/README.md +260 -0
- package/dist/Domain/Constants.d.ts +5 -0
- package/dist/Domain/GeoJSON/Concept/Altitude.d.ts +3 -0
- package/dist/Domain/GeoJSON/Concept/BoundingBox.d.ts +6 -0
- package/dist/Domain/GeoJSON/Concept/ExteriorRing.d.ts +4 -0
- package/dist/Domain/GeoJSON/Concept/GeoJSONObject.d.ts +11 -0
- package/dist/Domain/GeoJSON/Concept/InteriorRing.d.ts +4 -0
- package/dist/Domain/GeoJSON/Concept/Latitude.d.ts +3 -0
- package/dist/Domain/GeoJSON/Concept/LinearRing.d.ts +4 -0
- package/dist/Domain/GeoJSON/Concept/Longitude.d.ts +3 -0
- package/dist/Domain/GeoJSON/Concept/Position.d.ts +7 -0
- package/dist/Domain/GeoJSON/Feature.d.ts +12 -0
- package/dist/Domain/GeoJSON/FeatureCollection.d.ts +8 -0
- package/dist/Domain/GeoJSON/GeoJSON.d.ts +7 -0
- package/dist/Domain/GeoJSON/Geometry/LineString.d.ts +11 -0
- package/dist/Domain/GeoJSON/Geometry/MultiLineString.d.ts +7 -0
- package/dist/Domain/GeoJSON/Geometry/MultiPoint.d.ts +8 -0
- package/dist/Domain/GeoJSON/Geometry/MultiPolygon.d.ts +7 -0
- package/dist/Domain/GeoJSON/Geometry/Point.d.ts +11 -0
- package/dist/Domain/GeoJSON/Geometry/Polygon.d.ts +11 -0
- package/dist/Domain/GeoJSON/Geometry.d.ts +9 -0
- package/dist/Domain/GeoJSON/GeometryCollection.d.ts +8 -0
- package/dist/Domain/GeoJSON/GeometryObject.d.ts +12 -0
- package/dist/Domain/Guards/Number.d.ts +3 -0
- package/dist/Domain/Guards/Tuple.d.ts +3 -0
- package/dist/Domain/Iterator/IterablePair.d.ts +32 -0
- package/dist/Domain/Iterator/SimpleGeometry.d.ts +90 -0
- package/dist/Domain/Utility/Calculate.d.ts +9 -0
- package/dist/Domain/Utility/Distance.d.ts +3 -0
- package/dist/Domain/Utility/Intersect.d.ts +2 -0
- package/dist/Domain/Utility/Segments.d.ts +2 -0
- package/dist/Domain/Utility/Winding.d.ts +3 -0
- package/dist/geojson.cjs.js +655 -0
- package/dist/geojson.cjs.min.js +1 -0
- package/dist/geojson.d.ts +198 -0
- package/dist/geojson.es.js +627 -0
- package/dist/geojson.es.min.js +1 -0
- package/dist/geojson.js +660 -0
- package/dist/geojson.min.js +1 -0
- package/dist/main.d.ts +15 -0
- package/package.json +63 -0
- package/rollup.config.mjs +43 -0
- package/source/Domain/Constants.ts +5 -0
- package/source/Domain/GeoJSON/Concept/Altitude.ts +9 -0
- package/source/Domain/GeoJSON/Concept/BoundingBox.ts +31 -0
- package/source/Domain/GeoJSON/Concept/ExteriorRing.ts +12 -0
- package/source/Domain/GeoJSON/Concept/GeoJSONObject.ts +23 -0
- package/source/Domain/GeoJSON/Concept/InteriorRing.ts +12 -0
- package/source/Domain/GeoJSON/Concept/Latitude.ts +7 -0
- package/source/Domain/GeoJSON/Concept/LinearRing.ts +14 -0
- package/source/Domain/GeoJSON/Concept/Longitude.ts +7 -0
- package/source/Domain/GeoJSON/Concept/Position.ts +18 -0
- package/source/Domain/GeoJSON/Feature.ts +24 -0
- package/source/Domain/GeoJSON/FeatureCollection.ts +16 -0
- package/source/Domain/GeoJSON/GeoJSON.ts +9 -0
- package/source/Domain/GeoJSON/Geometry/LineString.ts +12 -0
- package/source/Domain/GeoJSON/Geometry/MultiLineString.ts +9 -0
- package/source/Domain/GeoJSON/Geometry/MultiPoint.ts +10 -0
- package/source/Domain/GeoJSON/Geometry/MultiPolygon.ts +9 -0
- package/source/Domain/GeoJSON/Geometry/Point.ts +12 -0
- package/source/Domain/GeoJSON/Geometry/Polygon.ts +20 -0
- package/source/Domain/GeoJSON/Geometry.ts +11 -0
- package/source/Domain/GeoJSON/GeometryCollection.ts +23 -0
- package/source/Domain/GeoJSON/GeometryObject.ts +20 -0
- package/source/Domain/Guards/Number.ts +13 -0
- package/source/Domain/Guards/Tuple.ts +6 -0
- package/source/Domain/Guards/Utility.ts +1 -0
- package/source/Domain/Iterator/IterablePair.ts +47 -0
- package/source/Domain/Iterator/SimpleGeometry.ts +137 -0
- package/source/Domain/Utility/Calculate.ts +143 -0
- package/source/Domain/Utility/Distance.ts +56 -0
- package/source/Domain/Utility/Intersect.ts +42 -0
- package/source/Domain/Utility/Numeric.ts +8 -0
- package/source/Domain/Utility/Segments.ts +5 -0
- package/source/Domain/Utility/Winding.ts +19 -0
- package/source/main.ts +20 -0
- package/test/Domain/GeoJSON/Concept/Altitude.ts +58 -0
- package/test/Domain/GeoJSON/Concept/BoundingBox.ts +77 -0
- package/test/Domain/GeoJSON/Concept/ExteriorRing.ts +65 -0
- package/test/Domain/GeoJSON/Concept/GeoJSONObject.ts +56 -0
- package/test/Domain/GeoJSON/Concept/InteriorRing.ts +65 -0
- package/test/Domain/GeoJSON/Concept/Latitude.ts +56 -0
- package/test/Domain/GeoJSON/Concept/LinearRing.ts +63 -0
- package/test/Domain/GeoJSON/Concept/Longitude.ts +56 -0
- package/test/Domain/GeoJSON/Concept/Position.ts +56 -0
- package/test/Domain/GeoJSON/Feature.ts +9 -0
- package/test/Domain/GeoJSON/FeatureCollection.ts +9 -0
- package/test/Domain/GeoJSON/GeoJSON.ts +21 -0
- package/test/Domain/GeoJSON/Geometry/LineString.ts +11 -0
- package/test/Domain/GeoJSON/Geometry/MultiLineString.ts +11 -0
- package/test/Domain/GeoJSON/Geometry/MultiPoint.ts +11 -0
- package/test/Domain/GeoJSON/Geometry/MultiPolygon.ts +11 -0
- package/test/Domain/GeoJSON/Geometry/Point.ts +11 -0
- package/test/Domain/GeoJSON/Geometry/Polygon.ts +11 -0
- package/test/Domain/GeoJSON/Geometry.ts +9 -0
- package/test/Domain/GeoJSON/GeometryCollection.ts +9 -0
- package/test/Domain/GeoJSON/GeometryObject.ts +4 -0
- package/test/Domain/Guards/Number.ts +49 -0
- package/test/Domain/Guards/Tuple.ts +27 -0
- package/test/Domain/Iterator/IterablePair.ts +203 -0
- package/test/Domain/Iterator/SimpleGeometry.ts +195 -0
- package/test/Domain/Utility/Calculate.ts +178 -0
- package/test/Domain/Utility/Distance.ts +19 -0
- package/test/Domain/Utility/Intersect.ts +54 -0
- package/test/Domain/Utility/Numeric.ts +30 -0
- package/test/Domain/Utility/Winding.ts +52 -0
- package/test/README.ts +123 -0
- package/test/data/Distance.ts +51 -0
- package/test/data/HolySee.ts +74 -0
- package/test/data/Intersect.ts +300 -0
- package/test/data/Italy.ts +2883 -0
- package/test/data/SanMarino.ts +83 -0
- package/test/data/Shapes.ts +107 -0
- package/test/helper/geometry.ts +76 -0
- package/test/helper/malform.ts +82 -0
- package/test/main.ts +4 -0
- package/tsconfig.json +12 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# EditorConfig is awesome: https://EditorConfig.org
|
|
2
|
+
|
|
3
|
+
# top-most EditorConfig file
|
|
4
|
+
root = true
|
|
5
|
+
|
|
6
|
+
[*]
|
|
7
|
+
indent_style = tab
|
|
8
|
+
indent_size = 4
|
|
9
|
+
end_of_line = crlf
|
|
10
|
+
charset = utf-8
|
|
11
|
+
trim_trailing_whitespace = true
|
|
12
|
+
insert_final_newline = true
|
|
13
|
+
|
|
14
|
+
[*.yml]
|
|
15
|
+
indent_style = space
|
|
16
|
+
indent_size = 2
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "**"
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
package:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v3
|
|
13
|
+
- uses: actions/setup-node@v3
|
|
14
|
+
with:
|
|
15
|
+
node-version: 16
|
|
16
|
+
registry-url: https://registry.npmjs.org/
|
|
17
|
+
- run: npm install
|
|
18
|
+
- run: npm run test:pretty
|
|
19
|
+
- run: npm publish --access public
|
|
20
|
+
env:
|
|
21
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
|
2
|
+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
|
3
|
+
|
|
4
|
+
name: Tests
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches:
|
|
9
|
+
- "main"
|
|
10
|
+
- "fix/*"
|
|
11
|
+
- "feature/*"
|
|
12
|
+
paths-ignore:
|
|
13
|
+
- "**.md"
|
|
14
|
+
pull_request:
|
|
15
|
+
types: [opened, synchronize, closed]
|
|
16
|
+
paths-ignore:
|
|
17
|
+
- "**.md"
|
|
18
|
+
|
|
19
|
+
jobs:
|
|
20
|
+
test-typescript:
|
|
21
|
+
runs-on: ${{ matrix.os }}
|
|
22
|
+
|
|
23
|
+
strategy:
|
|
24
|
+
matrix:
|
|
25
|
+
os:
|
|
26
|
+
- ubuntu-latest
|
|
27
|
+
- windows-latest
|
|
28
|
+
- macos-latest
|
|
29
|
+
node-version:
|
|
30
|
+
- 14.x
|
|
31
|
+
- 16.x
|
|
32
|
+
- 18.x
|
|
33
|
+
|
|
34
|
+
steps:
|
|
35
|
+
- uses: actions/checkout@v3
|
|
36
|
+
- name: Use Node ${{ matrix.node-version }}
|
|
37
|
+
uses: actions/setup-node@v3
|
|
38
|
+
with:
|
|
39
|
+
node-version: ${{ matrix.node-version }}
|
|
40
|
+
- run: npm install
|
|
41
|
+
- run: npm run test:pretty
|
|
42
|
+
|
|
43
|
+
build:
|
|
44
|
+
needs: test-typescript
|
|
45
|
+
runs-on: ubuntu-latest
|
|
46
|
+
steps:
|
|
47
|
+
- uses: actions/checkout@v3
|
|
48
|
+
- uses: actions/setup-node@v3
|
|
49
|
+
with:
|
|
50
|
+
node-version: 16
|
|
51
|
+
- run: npm install
|
|
52
|
+
- run: npm run build
|
|
53
|
+
- uses: actions/upload-artifact@v3
|
|
54
|
+
with:
|
|
55
|
+
name: bundle
|
|
56
|
+
path: dist/
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
All notable changes to this project will be documented in this file.
|
|
3
|
+
|
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased] -
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
### Changed
|
|
11
|
+
### Deprecated
|
|
12
|
+
### Removed
|
|
13
|
+
### Fixed
|
|
14
|
+
### Security
|
|
15
|
+
|
|
16
|
+
## [1.0.0] - 2023-02-05
|
|
17
|
+
|
|
18
|
+
_Initial release_
|
|
19
|
+
|
|
20
|
+
[unreleased]: https://github.com/konfirm/geojson/compare/v1.0.0...HEAD
|
|
21
|
+
[1.0.0]: https://github.com/konfirm/geojson/releases/tag/v1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
MIT License Copyright (c) 2021-2023 Rogier Spieker (Konfirm)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
[](https://github.com/konfirm/geojson/actions/workflows/release.yml)
|
|
2
|
+
[](https://github.com/konfirm/geojson/actions/workflows/tests.yml)
|
|
3
|
+
|
|
4
|
+
# @konfirm/geojson
|
|
5
|
+
|
|
6
|
+
GeoJSON validation, iteration, intersection and distance calculation.
|
|
7
|
+
|
|
8
|
+
## API
|
|
9
|
+
|
|
10
|
+
### Types
|
|
11
|
+
|
|
12
|
+
All [GeoJSON types](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1) are available as export
|
|
13
|
+
|
|
14
|
+
| type | descriptions | note |
|
|
15
|
+
| ------------------ | ------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
16
|
+
| Position | A [GeoJSON Position](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.1) | `[Longitude, Latitude, Altitude?]` |
|
|
17
|
+
| Point | A [GeoJSON Point](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.2) | The `coordinates` property is a `Position` |
|
|
18
|
+
| MultiPoint | A [GeoJSON MultiPoint](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.3) | The `coordinates` property is an array of `Position` |
|
|
19
|
+
| LineString | A [GeoJSON LineString](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.4) | The `coordinates` property is an array of two or more `Position` |
|
|
20
|
+
| MultiLineString | A [GeoJSON MultiLineString](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.5) | The `coordinates` property is an array of`LineString` coordinates |
|
|
21
|
+
| Polygon | A [GeoJSON Polygon](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.6) | The `coordinates` property is an array of "LinearRings" (closed `LineString` coordinates, where the first and last `Position` are identical) |
|
|
22
|
+
| MultiPolygon | A [GeoJSON MultiPolygon](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.7) | The `coordinates` property is an array of `Polygon` coordinate arrays |
|
|
23
|
+
| GeometryCollection | A [GeoJSON GeometryCollection](https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.8) | geometries is an array of Geometries (`Point`, `MultiPoint`, `LineString`, `MultiLineString`, `Polygon`, `MultiPolygon`) |
|
|
24
|
+
| Feature | A [GeoJSON Feature](https://datatracker.ietf.org/doc/html/rfc7946#section-3.2) | A spatially bounded 'Thing', consisting of a `geometry` property (`Point`, `MultiPoint`, `LineString`, `MultiLineString`, `Polygon`, `MultiPolygon`, `GeometryCollection`) with additional `properties` |
|
|
25
|
+
| FeatureCollection | A [GeoJSON Collection](https://datatracker.ietf.org/doc/html/rfc7946#section-3.3) | The `features` property is an array of `Feature` objects |
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
### Type Guards
|
|
29
|
+
|
|
30
|
+
Most of the exported functionality is based on validation of GeoJSON objects, validating required and optional (if provided) properties.
|
|
31
|
+
The `isStrict*` variants of the type guards also validate the following:
|
|
32
|
+
- `Longitude` is a number in the range (inclusive) `-180..180`
|
|
33
|
+
- `Latitude` is a number in the range (inclusive) `-90..90`
|
|
34
|
+
- `Altitude` is a number in the range (inclusive) `-6371008.7714..20180000` (Earth center(-ish) up to the GPS satelite distance)
|
|
35
|
+
- `Polygon` "LinearRing" are closed (first and last `Position` are identical)
|
|
36
|
+
- `Polygon` "LinearRing" have the correct winding (counterclockwise for exterior rings (outline), clockwise for interior rings (holes))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
| type | guard | strict guard | description |
|
|
40
|
+
| ------------------ | ---------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
|
41
|
+
| Position | `isPosition` | `isStrictPosition` | validate whether the input is valid GeoJSON Position |
|
|
42
|
+
| Point | `isPoint` | `isStrictPoint` | validate whether the input is valid GeoJSON Point |
|
|
43
|
+
| MultiPoint | `isMultiPoint` | `isStrictMultiPoint` | validate whether the input is valid GeoJSON MultiPoint |
|
|
44
|
+
| LineString | `isLineString` | `isStrictLineString` | validate whether the input is valid GeoJSON LineString |
|
|
45
|
+
| MultiLineString | `isMultiLineString` | `isStrictMultiLineString` | validate whether the input is valid GeoJSON MultiLineString |
|
|
46
|
+
| Polygon | `isPolygon` | `isStrictPolygon` | validate whether the input is valid GeoJSON Polygon |
|
|
47
|
+
| MultiPolygon | `isMultiPolygon` | `isStrictMultiPolygon` | validate whether the input is valid GeoJSON MultiPolygon |
|
|
48
|
+
| GeometryCollection | `isGeometryCollection` | `isStrictGeometryCollection` | validate whether the input is valid GeoJSON GeometryCollection |
|
|
49
|
+
| Geometry | `isGeometry` | `isStrictGeometry` | validate whether the input is valid GeoJSON Geometry (`Point`, `LineString`, `Polygon` or their `Multi*` variants) |
|
|
50
|
+
| Feature | `isFeature` | `isStrictFeature` | validate whether the input is valid GeoJSON Feature |
|
|
51
|
+
| FeatureCollection | `isFeatureCollection` | `isStrictFeatureCollection` | validate whether the input is valid GeoJSON FeatureCollection |
|
|
52
|
+
| GeoJSON | `isGeoJSON` | `isStrictGeoJSON` | validate the input to be valid GeoJSON |
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { isPoint, isStrictPoint } from '@konfirm/geojson';
|
|
56
|
+
|
|
57
|
+
const point: Point = {
|
|
58
|
+
type: 'Point',
|
|
59
|
+
coordinates: [181, 91],
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
console.log('the point is a GeoJSON Point', isPoint(point)); // true, as the structure is up to specification
|
|
63
|
+
console.log('the point is a strict GeoJSON Point', isStrictPoint(point)); // false, as the coordinates are not within the specified ranges
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### intersect
|
|
67
|
+
|
|
68
|
+
Verify whether the provided GeoJSON objects intersect.
|
|
69
|
+
|
|
70
|
+
Usage: `intersect(<Geometry(Collection)|Feature(Collection)>, <Geometry(Collection)|Feature(Collection)>): boolean`
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { intersect, Point, Feature } from '@konfirm/geojson';
|
|
74
|
+
|
|
75
|
+
const point: Point = {
|
|
76
|
+
type: 'Point',
|
|
77
|
+
coordinates: [1, 1],
|
|
78
|
+
};
|
|
79
|
+
const feature: Feature = {
|
|
80
|
+
type: 'Feature',
|
|
81
|
+
properties: {
|
|
82
|
+
name: 'triangle'
|
|
83
|
+
},
|
|
84
|
+
geometry: {
|
|
85
|
+
type: 'Polygon',
|
|
86
|
+
coordinates: [[[0, 0], [0, 2], [2, 1], [0, 0]]],
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
console.log('point intersects feature', intersect(point, feature)); // true
|
|
91
|
+
console.log('feature intersects point', intersect(feature, point)); // true
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### distance
|
|
95
|
+
|
|
96
|
+
Obtain the (shortest) distance in meters between two GeoJSON objects. There are three formulas which can be used:
|
|
97
|
+
|
|
98
|
+
- `cartesian` (default), calculates the distance between coordinates using the Pythagorean equation, this is the fastest formula at the cost of (huge amount of) accuracy
|
|
99
|
+
- `haversine`, calculates the distance between coordinates using the [haversine formula](https://en.wikipedia.org/wiki/Haversine_formula), improves the accuracy to a level which is probably suitable for most needs with a decent performance
|
|
100
|
+
- `vincenty`, calculates the distance between coordinates using the [Vincenty's formula](https://en.wikipedia.org/wiki/Vincenty%27s_formulae), the most accurate but the least performant algorithm.
|
|
101
|
+
|
|
102
|
+
Usage: `distance(<Geometry(Collection)|Feature(Collection)>, <Geometry(Collection)|Feature(Collection)> [, <'cartesian'|'haversine'|'vincenty'>]): number`
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { distance, Feature } from '@konfirm/geojson';
|
|
106
|
+
|
|
107
|
+
const a: Feature = {
|
|
108
|
+
type: 'Feature',
|
|
109
|
+
properties: {
|
|
110
|
+
name: 'Schiphol Airpoirt, Amsterdam',
|
|
111
|
+
},
|
|
112
|
+
geometry: {
|
|
113
|
+
type: 'Point',
|
|
114
|
+
coordinates: [4.763889, 52.308333],
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
const b: Feature = {
|
|
118
|
+
type: 'Feature',
|
|
119
|
+
properties: {
|
|
120
|
+
name: 'John F. Kennedy International Airport, New York',
|
|
121
|
+
},
|
|
122
|
+
geometry: {
|
|
123
|
+
type: 'Point',
|
|
124
|
+
coordinates: [-73.778889, 40.639722],
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
console.log(distance(a, b)); // 8829424.604594177
|
|
129
|
+
console.log(distance(a, b, 'direct'); // 8829424.604594177 ('direct' is the default)
|
|
130
|
+
console.log(distance(a, b, 'haversine'); // 5847546.425707642
|
|
131
|
+
console.log(distance(a, b, 'vincenty'); // 5863355.371234315
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### SimpleGeometryIterator
|
|
135
|
+
|
|
136
|
+
The SimpleGeometryIterator class is a convenience helper utility which yeilds all simple Geometric shapes (`Point`, `LineString`, `Polygon`) from any GeoJSON object. Using a SimpleGeometryIterator allows you to focus on just implementing logic for the simple Geometric shapes whilst supporting any compbination of GeoJSON objects as input.
|
|
137
|
+
|
|
138
|
+
| input type | yields type(s) | description |
|
|
139
|
+
| -------------------- | ---------------------------------- | ---------------------------------------------------------------------------------------- |
|
|
140
|
+
| `Point` | `Point` | yields the `Point` geometry as is |
|
|
141
|
+
| `MultiPoint` | `Point` | yields every `Point` of the `MultiPoint` |
|
|
142
|
+
| `LineString` | `LineString` | yields the `LineString` geometry as is |
|
|
143
|
+
| `MultiLineString` | `LineString` | yields every `LineString` of the `MultiLineString` |
|
|
144
|
+
| `Polygon` | `Polygon` | yields the `Polygon` geometry as is |
|
|
145
|
+
| `MultiPolygon` | `Polygon` | yields every `Polygon` of the `MultiPolygon` |
|
|
146
|
+
| `GeometryCollection` | `Point`, `LineString` or `Polygon` | yields every geometry contained within the collection (see the corresponing types above) |
|
|
147
|
+
| `Feature` | `Point`, `LineString` or `Polygon` | yields the geometry of a feature (see the corresponding types above) |
|
|
148
|
+
| `FeatureCollection` | `Point`, `LineString` or `Polygon` | yields every `Feature` (see `Feature`) |
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import type { Point, MultiPoint, LineString, MultiLineString, GeometryCollection, Feature, FeatureCollection } from '@konfirm/geojson';
|
|
152
|
+
import { SimpleGeometryIterator } from '@konfirm/geojson';
|
|
153
|
+
|
|
154
|
+
const point: Point = {
|
|
155
|
+
type: 'Point',
|
|
156
|
+
coordinates: [5.903949737548828, 51.991936460056515],
|
|
157
|
+
};
|
|
158
|
+
const multipoint: MultiPoint = {
|
|
159
|
+
type: 'MultiPoint',
|
|
160
|
+
coordinates: [
|
|
161
|
+
[5.896482467651367, 52.00039200820837],
|
|
162
|
+
[5.888843536376953, 51.99912377779024],
|
|
163
|
+
],
|
|
164
|
+
};
|
|
165
|
+
const linestring: LineString = {
|
|
166
|
+
type: 'LineString',
|
|
167
|
+
coordinates: [
|
|
168
|
+
[5.9077370166778564, 51.9944435645134],
|
|
169
|
+
[5.90764045715332, 51.994209045542206],
|
|
170
|
+
[5.907415151596069, 51.99408683094357],
|
|
171
|
+
],
|
|
172
|
+
};
|
|
173
|
+
const multilinestring: MultiLineString = {
|
|
174
|
+
type: 'MultiLineString',
|
|
175
|
+
coordinates: [
|
|
176
|
+
[
|
|
177
|
+
[5.905205011367797, 51.99430813821511],
|
|
178
|
+
[5.905086994171143, 51.994261894995034],
|
|
179
|
+
[5.905033349990845, 51.99414958983319],
|
|
180
|
+
[5.9051138162612915, 51.994116558849626]
|
|
181
|
+
],
|
|
182
|
+
[
|
|
183
|
+
[5.904818773269653, 51.993825885143515],
|
|
184
|
+
[5.905521512031555, 51.993551725259614],
|
|
185
|
+
[5.905789732933044, 51.99358805979856],
|
|
186
|
+
],
|
|
187
|
+
],
|
|
188
|
+
};
|
|
189
|
+
const geometrycollection: GeometryCollection = {
|
|
190
|
+
type: 'GeometryCollection',
|
|
191
|
+
geometries: [point],
|
|
192
|
+
};
|
|
193
|
+
const featurecollection: FeatureCollection = {
|
|
194
|
+
type: 'FeatureCollection',
|
|
195
|
+
features: [
|
|
196
|
+
{
|
|
197
|
+
type: 'Feature',
|
|
198
|
+
properties: {},
|
|
199
|
+
geometry: linestring,
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
type: 'Feature',
|
|
203
|
+
properties: {},
|
|
204
|
+
geometry: multilinestring,
|
|
205
|
+
},
|
|
206
|
+
]
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const simplified = [...new SimpleGeometryIterator(multipoint, geometrycollection, featurecollection)];
|
|
210
|
+
/*
|
|
211
|
+
[
|
|
212
|
+
{
|
|
213
|
+
type: 'Point',
|
|
214
|
+
coordinates: ...multipoint[0]
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
type: 'Point',
|
|
218
|
+
coordinates: ...multipoint[1]
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
type: 'Point',
|
|
222
|
+
coordinates: ...point
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
type: 'LineString',
|
|
226
|
+
coordinates: ...linestring
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
type: 'LineString',
|
|
230
|
+
coordinates: ...multilinestring[0]
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
type: 'LineString',
|
|
234
|
+
coordinates: ...multilinestring[1]
|
|
235
|
+
},
|
|
236
|
+
]
|
|
237
|
+
*/
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## License
|
|
241
|
+
|
|
242
|
+
MIT License Copyright (c) 2021-2023 Rogier Spieker (Konfirm)
|
|
243
|
+
|
|
244
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
245
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
246
|
+
in the Software without restriction, including without limitation the rights
|
|
247
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
248
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
249
|
+
furnished to do so, subject to the following conditions:
|
|
250
|
+
|
|
251
|
+
The above copyright notice and this permission notice shall be included in all
|
|
252
|
+
copies or substantial portions of the Software.
|
|
253
|
+
|
|
254
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
255
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
256
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
257
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
258
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
259
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
260
|
+
SOFTWARE.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const EARTH_RADIUS = 6371008.7714;
|
|
2
|
+
export declare const EARTH_RADIUS_MAJOR = 6378137;
|
|
3
|
+
export declare const EARTH_RADIUS_MINOR = 6356752.314245;
|
|
4
|
+
export declare const EARTH_FLATTENING = 298.257223563;
|
|
5
|
+
export declare const GPS_SATELLITE_ORBIT = 20180000;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Longitude } from "./Longitude";
|
|
2
|
+
import { Latitude } from "./Latitude";
|
|
3
|
+
import { Altitude } from "./Altitude";
|
|
4
|
+
export type BoundingBox = [Longitude, Latitude, Altitude, Longitude, Latitude, Altitude] | [Longitude, Latitude, Longitude, Latitude];
|
|
5
|
+
export declare const isBoundingBox: import("@konfirm/guard").Guard<BoundingBox>;
|
|
6
|
+
export declare const isStrictBoundingBox: import("@konfirm/guard").Guard<BoundingBox>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Guard } from "@konfirm/guard";
|
|
2
|
+
import { BoundingBox } from "./BoundingBox";
|
|
3
|
+
type GeoJSONBase = {
|
|
4
|
+
type: string;
|
|
5
|
+
bbox?: BoundingBox;
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
};
|
|
8
|
+
export type GeoJSONObject<T extends GeoJSONBase = GeoJSONBase> = GeoJSONBase & T;
|
|
9
|
+
export declare function isGeoJSONObject<T extends GeoJSONBase>(type: string): Guard<T>;
|
|
10
|
+
export declare function isStrictGeoJSONObject<T extends GeoJSONBase>(type: string): Guard<T>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Altitude } from "./Altitude";
|
|
2
|
+
import { Latitude } from "./Latitude";
|
|
3
|
+
import { Longitude } from "./Longitude";
|
|
4
|
+
export type Position = [Longitude, Latitude, Altitude?];
|
|
5
|
+
export declare const isPosition: import("@konfirm/guard").Guard<Position>;
|
|
6
|
+
export declare const isStrictPosition: import("@konfirm/guard").Guard<Position>;
|
|
7
|
+
export declare function isEquivalentPosition(one: any, two: any): boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GeoJSONObject } from "./Concept/GeoJSONObject";
|
|
2
|
+
import { Geometry } from "./Geometry";
|
|
3
|
+
import { GeometryCollection } from "./GeometryCollection";
|
|
4
|
+
export type Feature = GeoJSONObject<{
|
|
5
|
+
type: 'Feature';
|
|
6
|
+
geometry: Geometry | GeometryCollection;
|
|
7
|
+
properties: {
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
} | null;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const isFeature: import("@konfirm/guard").Guard<Feature>;
|
|
12
|
+
export declare const isStrictFeature: import("@konfirm/guard").Guard<Feature>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Feature } from "./Feature";
|
|
2
|
+
import { GeoJSONObject } from "./Concept/GeoJSONObject";
|
|
3
|
+
export type FeatureCollection = GeoJSONObject<{
|
|
4
|
+
type: 'FeatureCollection';
|
|
5
|
+
features: Array<Feature>;
|
|
6
|
+
}>;
|
|
7
|
+
export declare const isFeatureCollection: import("@konfirm/guard").Guard<FeatureCollection>;
|
|
8
|
+
export declare const isStrictFeatureCollection: import("@konfirm/guard").Guard<FeatureCollection>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Feature } from "./Feature";
|
|
2
|
+
import { FeatureCollection } from "./FeatureCollection";
|
|
3
|
+
import { GeometryCollection } from "./GeometryCollection";
|
|
4
|
+
import { Geometry } from "./Geometry";
|
|
5
|
+
export type GeoJSON = Geometry | GeometryCollection | Feature | FeatureCollection;
|
|
6
|
+
export declare const isGeoJSON: import("@konfirm/guard").Guard<GeoJSON>;
|
|
7
|
+
export declare const isStrictGeoJSON: import("@konfirm/guard").Guard<GeoJSON>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Guard } from "../../Guards/Utility";
|
|
2
|
+
import { GeometryObject } from "../GeometryObject";
|
|
3
|
+
import { MultiPoint } from "./MultiPoint";
|
|
4
|
+
export type LineString = GeometryObject<{
|
|
5
|
+
type: 'LineString';
|
|
6
|
+
coordinates: MultiPoint['coordinates'];
|
|
7
|
+
}>;
|
|
8
|
+
export declare const isLineStringCoordinates: Guard<import("../Concept/Position").Position[]>;
|
|
9
|
+
export declare const isLineString: Guard<LineString>;
|
|
10
|
+
export declare const isStrictLineStringCoordinates: Guard<import("../Concept/Position").Position[]>;
|
|
11
|
+
export declare const isStrictLineString: Guard<LineString>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { MultiGeometryObject } from "../GeometryObject";
|
|
2
|
+
import { LineString } from "./LineString";
|
|
3
|
+
export type MultiLineString = MultiGeometryObject<LineString>;
|
|
4
|
+
export declare const isMultiLineStringCoordinates: import("@konfirm/guard").Guard<unknown>;
|
|
5
|
+
export declare const isMultiLineString: import("@konfirm/guard").Guard<MultiLineString>;
|
|
6
|
+
export declare const isStrictMultiLineStringCoordinates: import("@konfirm/guard").Guard<unknown>;
|
|
7
|
+
export declare const isStrictMultiLineString: import("@konfirm/guard").Guard<MultiLineString>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Guard } from "../../Guards/Utility";
|
|
2
|
+
import { MultiGeometryObject } from "../GeometryObject";
|
|
3
|
+
import { Point } from "./Point";
|
|
4
|
+
export type MultiPoint = MultiGeometryObject<Point>;
|
|
5
|
+
export declare const isMultiPointCoordinates: Guard<MultiPoint['coordinates']>;
|
|
6
|
+
export declare const isMultiPoint: import("@konfirm/guard").Guard<MultiPoint>;
|
|
7
|
+
export declare const isStrictMultiPointCoordinates: Guard<MultiPoint['coordinates']>;
|
|
8
|
+
export declare const isStrictMultiPoint: import("@konfirm/guard").Guard<MultiPoint>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { MultiGeometryObject } from "../GeometryObject";
|
|
2
|
+
import { Polygon } from "./Polygon";
|
|
3
|
+
export type MultiPolygon = MultiGeometryObject<Polygon>;
|
|
4
|
+
export declare const isMultiPolygonCoordinates: import("@konfirm/guard").Guard<unknown>;
|
|
5
|
+
export declare const isMultiPolygon: import("@konfirm/guard").Guard<MultiPolygon>;
|
|
6
|
+
export declare const isStrictMultiPolygonCoordinates: import("@konfirm/guard").Guard<unknown>;
|
|
7
|
+
export declare const isStrictMultiPolygon: import("@konfirm/guard").Guard<MultiPolygon>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { GeometryObject } from "../GeometryObject";
|
|
2
|
+
import { Position } from "../Concept/Position";
|
|
3
|
+
import { Guard } from "../../Guards/Utility";
|
|
4
|
+
export type Point = GeometryObject<{
|
|
5
|
+
type: 'Point';
|
|
6
|
+
coordinates: Position;
|
|
7
|
+
}>;
|
|
8
|
+
export declare const isPointCoordinates: Guard<Point['coordinates']>;
|
|
9
|
+
export declare const isPoint: Guard<Point>;
|
|
10
|
+
export declare const isStrictPointCoordinates: Guard<Point['coordinates']>;
|
|
11
|
+
export declare const isStrictPoint: Guard<Point>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { GeometryObject } from "../GeometryObject";
|
|
2
|
+
import { ExteriorRing } from "../Concept/ExteriorRing";
|
|
3
|
+
import { InteriorRing } from "../Concept/InteriorRing";
|
|
4
|
+
export type Polygon = GeometryObject<{
|
|
5
|
+
type: 'Polygon';
|
|
6
|
+
coordinates: [ExteriorRing, ...Array<InteriorRing>];
|
|
7
|
+
}>;
|
|
8
|
+
export declare const isPolygonCoordinates: import("@konfirm/guard").Guard<unknown>;
|
|
9
|
+
export declare const isPolygon: import("@konfirm/guard").Guard<Polygon>;
|
|
10
|
+
export declare const isStrictPolygonCoordinates: import("@konfirm/guard").Guard<unknown>;
|
|
11
|
+
export declare const isStrictPolygon: import("@konfirm/guard").Guard<Polygon>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LineString } from "./Geometry/LineString";
|
|
2
|
+
import { MultiLineString } from "./Geometry/MultiLineString";
|
|
3
|
+
import { MultiPoint } from "./Geometry/MultiPoint";
|
|
4
|
+
import { MultiPolygon } from "./Geometry/MultiPolygon";
|
|
5
|
+
import { Point } from "./Geometry/Point";
|
|
6
|
+
import { Polygon } from "./Geometry/Polygon";
|
|
7
|
+
export type Geometry = Point | MultiPoint | LineString | MultiLineString | Polygon | MultiPolygon;
|
|
8
|
+
export declare const isGeometry: import("@konfirm/guard").Guard<Geometry>;
|
|
9
|
+
export declare const isStrictGeometry: import("@konfirm/guard").Guard<Geometry>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { GeoJSONObject } from "./Concept/GeoJSONObject";
|
|
2
|
+
import { Geometry } from "./Geometry";
|
|
3
|
+
export type GeometryCollection = GeoJSONObject<{
|
|
4
|
+
type: 'GeometryCollection';
|
|
5
|
+
geometries: Array<Geometry | GeometryCollection>;
|
|
6
|
+
}>;
|
|
7
|
+
export declare function isGeometryCollection(value: any): value is GeometryCollection;
|
|
8
|
+
export declare function isStrictGeometryCollection(value: any): value is GeometryCollection;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Guard, Validator } from "@konfirm/guard";
|
|
2
|
+
import { GeoJSONObject } from "./Concept/GeoJSONObject";
|
|
3
|
+
type GeometryBase = GeoJSONObject & {
|
|
4
|
+
coordinates: Array<unknown>;
|
|
5
|
+
};
|
|
6
|
+
export type GeometryObject<T extends GeometryBase> = Omit<GeometryBase, 'coordinates'> & T;
|
|
7
|
+
export type MultiGeometryObject<T extends GeometryBase> = GeometryObject<{
|
|
8
|
+
type: `Multi${T['type']}`;
|
|
9
|
+
coordinates: Array<T['coordinates']>;
|
|
10
|
+
}>;
|
|
11
|
+
export declare function isGeometryObject<T extends GeometryBase>(type: string, isCoordinates: Validator): Guard<T>;
|
|
12
|
+
export {};
|