-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
util.ts
193 lines (171 loc) · 5.4 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import booleanClockwise from '@turf/boolean-clockwise';
import {
Longitude,
Latitude,
GeofenceId,
GeofenceInput,
GeofencePolygon,
LinearRing,
} from './types';
export function validateCoordinates(lng: Longitude, lat: Latitude): void {
if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
throw new Error(`Invalid coordinates: [${lng},${lat}]`);
}
if (lat < -90 || 90 < lat) {
throw new Error('Latitude must be between -90 and 90 degrees inclusive.');
} else if (lng < -180 || 180 < lng) {
throw new Error(
'Longitude must be between -180 and 180 degrees inclusive.'
);
}
}
export function validateGeofenceId(geofenceId: GeofenceId): void {
const geofenceIdRegex = /^[-._\p{L}\p{N}]+$/iu;
// Check if geofenceId is valid
if (!geofenceIdRegex.test(geofenceId)) {
throw new Error(
`Invalid geofenceId: '${geofenceId}' - IDs can only contain alphanumeric characters, hyphens, underscores and periods.`
);
}
}
export function validateLinearRing(
linearRing: LinearRing,
geofenceId?: GeofenceId
): void {
const errorPrefix = geofenceId ? `${geofenceId}: ` : '';
// Validate LinearRing size, must be at least 4 points
if (linearRing.length < 4) {
throw new Error(
`${errorPrefix}LinearRing must contain 4 or more coordinates.`
);
}
// Validate all coordinates are valid, error with which ones are bad
const badCoordinates = [];
linearRing.forEach(coordinates => {
try {
validateCoordinates(coordinates[0], coordinates[1]);
} catch (error) {
badCoordinates.push({ coordinates, error: error.message });
}
});
if (badCoordinates.length > 0) {
throw new Error(
`${errorPrefix}One or more of the coordinates in the Polygon LinearRing are not valid: ${JSON.stringify(
badCoordinates
)}`
);
}
// Validate first and last coordinates are the same
const [lngA, latA] = linearRing[0];
const [lngB, latB] = linearRing[linearRing.length - 1];
if (lngA !== lngB || latA !== latB) {
throw new Error(
`${errorPrefix}LinearRing's first and last coordinates are not the same`
);
}
if (booleanClockwise(linearRing)) {
throw new Error(
`${errorPrefix}LinearRing coordinates must be wound counterclockwise`
);
}
}
export function validatePolygon(
polygon: GeofencePolygon,
geofenceId?: GeofenceId
): void {
const errorPrefix = geofenceId ? `${geofenceId}: ` : '';
if (!Array.isArray(polygon)) {
throw new Error(
`${errorPrefix}Polygon is of incorrect structure. It should be an array of LinearRings`
);
}
if (polygon.length < 1) {
throw new Error(
`${errorPrefix}Polygon must have a single LinearRing array.`
);
}
if (polygon.length > 1) {
throw new Error(
`${errorPrefix}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.`
);
}
const verticesCount = polygon.reduce(
(prev, linearRing) => prev + linearRing.length,
0
);
if (verticesCount > 1000) {
throw new Error(
`${errorPrefix}Polygon has more than the maximum 1000 vertices.`
);
}
polygon.forEach(linearRing => {
validateLinearRing(linearRing, geofenceId);
});
}
export function validateGeofencesInput(geofences: GeofenceInput[]) {
const geofenceIds = {};
geofences.forEach((geofence: GeofenceInput) => {
// verify all required properties are present
// Validate geofenceId exists
if (!geofence.geofenceId) {
throw new Error(`Geofence '${geofence}' is missing geofenceId`);
}
const { geofenceId } = geofence;
validateGeofenceId(geofenceId);
// Validate geofenceId is unique
if (geofenceIds[geofenceId]) {
throw new Error(`Duplicate geofenceId: ${geofenceId}`);
} else {
geofenceIds[geofenceId] = true;
}
// Validate geometry exists
if (!geofence.geometry) {
throw new Error(`Geofence '${geofenceId}' is missing geometry`);
}
const { geometry } = geofence;
// Validate polygon exists
if (!geometry.polygon) {
throw new Error(`Geofence '${geofenceId}' is missing geometry.polygon`);
}
const { polygon } = geometry;
// Validate polygon length and structure
try {
validatePolygon(polygon, geofenceId);
} catch (error) {
if (
error.message.includes(
'Polygon has more than the maximum 1000 vertices.'
)
) {
throw new Error(
`Geofence '${geofenceId}' has more than the maximum of 1000 vertices`
);
}
}
// Validate LinearRing length, structure, and coordinates
const [linearRing] = polygon;
validateLinearRing(linearRing, geofenceId);
});
}
export function mapSearchOptions(options, locationServiceInput) {
const locationServiceModifiedInput = { ...locationServiceInput };
locationServiceModifiedInput.FilterCountries = options.countries;
locationServiceModifiedInput.MaxResults = options.maxResults;
if (options.searchIndexName) {
locationServiceModifiedInput.IndexName = options.searchIndexName;
}
if (options['biasPosition'] && options['searchAreaConstraints']) {
throw new Error(
'BiasPosition and SearchAreaConstraints are mutually exclusive, please remove one or the other from the options object'
);
}
if (options['biasPosition']) {
locationServiceModifiedInput.BiasPosition = options['biasPosition'];
}
if (options['searchAreaConstraints']) {
locationServiceModifiedInput.FilterBBox = options['searchAreaConstraints'];
}
return locationServiceModifiedInput;
}