@aws-amplify/geo 1.2.4 → 1.2.5-geo.7
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/dist/aws-amplify-geo.js +2145 -136
- package/dist/aws-amplify-geo.js.map +1 -1
- package/dist/aws-amplify-geo.min.js +6 -6
- package/dist/aws-amplify-geo.min.js.map +1 -1
- package/lib/Geo.d.ts +37 -4
- package/lib/Geo.js +153 -5
- package/lib/Geo.js.map +1 -1
- package/lib/Providers/AmazonLocationServiceProvider.d.ts +37 -1
- package/lib/Providers/AmazonLocationServiceProvider.js +417 -8
- package/lib/Providers/AmazonLocationServiceProvider.js.map +1 -1
- package/lib/types/AmazonLocationServiceProvider.d.ts +21 -1
- package/lib/types/Geo.d.ts +48 -1
- package/lib/types/Provider.d.ts +5 -1
- package/lib/types/Provider.js +0 -12
- package/lib/types/Provider.js.map +1 -1
- package/lib/util.d.ts +6 -0
- package/lib/util.js +142 -0
- package/lib/util.js.map +1 -0
- package/lib-esm/Geo.d.ts +37 -4
- package/lib-esm/Geo.js +153 -5
- package/lib-esm/Geo.js.map +1 -1
- package/lib-esm/Providers/AmazonLocationServiceProvider.d.ts +37 -1
- package/lib-esm/Providers/AmazonLocationServiceProvider.js +418 -9
- package/lib-esm/Providers/AmazonLocationServiceProvider.js.map +1 -1
- package/lib-esm/types/AmazonLocationServiceProvider.d.ts +21 -1
- package/lib-esm/types/Geo.d.ts +48 -1
- package/lib-esm/types/Provider.d.ts +5 -1
- package/lib-esm/types/Provider.js +0 -12
- package/lib-esm/types/Provider.js.map +1 -1
- package/lib-esm/util.d.ts +6 -0
- package/lib-esm/util.js +132 -0
- package/lib-esm/util.js.map +1 -0
- package/package.json +6 -4
- package/src/Geo.ts +122 -3
- package/src/Providers/AmazonLocationServiceProvider.ts +423 -6
- package/src/types/AmazonLocationServiceProvider.ts +56 -1
- package/src/types/Geo.ts +72 -2
- package/src/types/Provider.ts +31 -1
- package/src/util.ts +171 -0
package/src/util.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
5
|
+
* the License. A copy of the License is located at
|
|
6
|
+
*
|
|
7
|
+
* http://aws.amazon.com/apache2.0/
|
|
8
|
+
*
|
|
9
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
10
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
11
|
+
* and limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
import booleanClockwise from '@turf/boolean-clockwise';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
Longitude,
|
|
17
|
+
Latitude,
|
|
18
|
+
GeofenceInput,
|
|
19
|
+
GeofencePolygon,
|
|
20
|
+
LinearRing,
|
|
21
|
+
} from './types';
|
|
22
|
+
|
|
23
|
+
export function validateCoordinates(lng: Longitude, lat: Latitude): void {
|
|
24
|
+
if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
|
|
25
|
+
throw new Error(`Invalid coordinates: [${lng},${lat}]`);
|
|
26
|
+
}
|
|
27
|
+
if (lat < -90 || lat > 90) {
|
|
28
|
+
throw new Error('Latitude must be between -90 and 90 degrees inclusive.');
|
|
29
|
+
} else if (lng < -180 || lng > 180) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
'Longitude must be between -180 and 180 degrees inclusive.'
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function validateGeofenceId(geofenceId: string): void {
|
|
37
|
+
const geofenceIdRegex = /^[-._\p{L}\p{N}]+$/iu;
|
|
38
|
+
|
|
39
|
+
// Check if geofenceId is valid
|
|
40
|
+
if (!geofenceIdRegex.test(geofenceId)) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Invalid geofenceId: '${geofenceId}' - IDs can only contain alphanumeric characters, hyphens, underscores and periods.`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function validateLinearRing(
|
|
48
|
+
linearRing: LinearRing,
|
|
49
|
+
geofenceId?: string
|
|
50
|
+
): void {
|
|
51
|
+
// Validate LinearRing size, must be at least 4 points
|
|
52
|
+
if (linearRing.length < 4) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`${geofenceId}: LinearRing must contain 4 or more coordinates.`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Validate all coordinates are valid, error with which ones are bad
|
|
59
|
+
const badCoordinates = [];
|
|
60
|
+
linearRing.forEach(coordinates => {
|
|
61
|
+
try {
|
|
62
|
+
validateCoordinates(coordinates[0], coordinates[1]);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
badCoordinates.push({ coordinates, error: error.message });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
if (badCoordinates.length > 0) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`${geofenceId}: One or more of the coordinates in the Polygon LinearRing are not valid: ${JSON.stringify(
|
|
70
|
+
badCoordinates
|
|
71
|
+
)}`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Validate first and last coordinates are the same
|
|
76
|
+
const [lngA, latA] = linearRing[0];
|
|
77
|
+
const [lngB, latB] = linearRing[linearRing.length - 1];
|
|
78
|
+
|
|
79
|
+
if (lngA !== lngB || latA !== latB) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
`${geofenceId}: LinearRing's first and last coordinates are not the same`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (booleanClockwise(linearRing)) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`${geofenceId}: LinearRing coordinates must be wound counterclockwise`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function validatePolygon(
|
|
93
|
+
polygon: GeofencePolygon,
|
|
94
|
+
geofenceId?: string
|
|
95
|
+
): void {
|
|
96
|
+
if (!Array.isArray(polygon)) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`${geofenceId}: Polygon is of incorrect structure. It should be an array of LinearRings`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (polygon.length < 1) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
`${geofenceId}: Polygon must have a single LinearRing array.`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (polygon.length > 1) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`${geofenceId}: Polygon must have a single LinearRing array. Note: We do not currently support polygons with holes, multipolygons, polygons that are wound clockwise, or that cross the antimeridian.`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
const verticesCount = polygon.reduce(
|
|
112
|
+
(prev, linearRing) => prev + linearRing.length,
|
|
113
|
+
0
|
|
114
|
+
);
|
|
115
|
+
if (verticesCount > 1000) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`${geofenceId}: Polygon has more than the maximum 1000 vertices.`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function validateGeofencesInput(geofences: GeofenceInput[]) {
|
|
123
|
+
const geofenceIds = {};
|
|
124
|
+
|
|
125
|
+
geofences.forEach((geofence: GeofenceInput) => {
|
|
126
|
+
// verify all required properties are present
|
|
127
|
+
|
|
128
|
+
// Validate geofenceId exists
|
|
129
|
+
if (!geofence.geofenceId) {
|
|
130
|
+
throw new Error(`Geofence '${geofence}' is missing geofenceId`);
|
|
131
|
+
}
|
|
132
|
+
const { geofenceId } = geofence;
|
|
133
|
+
validateGeofenceId(geofenceId);
|
|
134
|
+
|
|
135
|
+
// Validate geofenceId is unique
|
|
136
|
+
if (geofenceIds[geofenceId]) {
|
|
137
|
+
throw new Error(`Duplicate geofenceId: ${geofenceId}`);
|
|
138
|
+
} else {
|
|
139
|
+
geofenceIds[geofenceId] = true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Validate geometry exists
|
|
143
|
+
if (!geofence.geometry) {
|
|
144
|
+
throw new Error(`Geofence '${geofenceId}' is missing geometry`);
|
|
145
|
+
}
|
|
146
|
+
const { geometry } = geofence;
|
|
147
|
+
|
|
148
|
+
// Validate polygon exists
|
|
149
|
+
if (!geometry.polygon) {
|
|
150
|
+
throw new Error(`Geofence '${geofenceId}' is missing geometry.polygon`);
|
|
151
|
+
}
|
|
152
|
+
const { polygon } = geometry;
|
|
153
|
+
|
|
154
|
+
// Validate polygon length and structure
|
|
155
|
+
try {
|
|
156
|
+
validatePolygon(polygon, geofenceId);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
if (
|
|
159
|
+
error.message === `Polygon has more than the maximum 1000 vertices.`
|
|
160
|
+
) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`Geofence '${geofenceId}' has more than the maximum of 1000 vertices`
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Validate LinearRing length, structure, and coordinates
|
|
168
|
+
const [linearRing] = polygon;
|
|
169
|
+
validateLinearRing(linearRing, geofenceId);
|
|
170
|
+
});
|
|
171
|
+
}
|