-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
243 lines (214 loc) · 6.13 KB
/
index.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import circle from '@turf/circle';
import PropTypes from 'prop-types';
import { isFunction } from 'Utils';
import mapboxgl from 'Lib';
/**
* Props describing a coordinates
* @type {Object}
*/
export const LngLatLike = PropTypes.oneOfType([
PropTypes.shape({
lat: PropTypes.number.isRequired,
lng: PropTypes.number.isRequired,
}),
PropTypes.shape({
lat: PropTypes.number.isRequired,
lon: PropTypes.number.isRequired,
}),
PropTypes.arrayOf(PropTypes.number.isRequired),
]);
/**
* Number of points to draw a circle.
* @type {Number}
*/
const CIRCLE_POINTS_CONFIG = 64;
/**
* Array of possible units
* @type {Array}
*/
export const UNITS = ['kilometers', 'meters', 'miles', 'feet'];
/**
* Convert the radius on given unit to a compatible unit for turf/circle
* @param {Number} radius of given unit
* @param {String} unit of given radius
*/
export function convertRadiusUnit(radius, unit = 'kilometers') {
let convertedRadius = Number(radius);
let convertedUnit = unit;
if (!Number.isFinite(radius)) {
global.console.error('The radius given is not a number');
}
if (UNITS.indexOf(unit) === -1) {
// eslint-disable-next-line prefer-destructuring
convertedUnit = UNITS[0];
global.console.warn(
`The unit "${unit}" is not supported, the fallback "${convertedUnit}" is used`,
);
}
if (unit === 'meters') {
convertedRadius = radius / 1000;
convertedUnit = 'kilometers';
} else if (unit === 'feet') {
convertedRadius = radius / 5280;
convertedUnit = 'miles';
}
return { radius: convertedRadius, unit: convertedUnit };
}
/**
* Parse given argument as a coordinates
* @param {Any} coord Value representing a LngLatLike
* @return {Object} A comparable coordinates
*/
function parseCoordinates(coord) {
if (Array.isArray(coord) && coord.length === 2) {
return {
lng: coord[0],
lat: coord[1],
};
}
if (coord instanceof Object && coord !== null) {
return {
lng: coord.lng || coord.lon,
lat: coord.lat,
};
}
return {};
}
/**
* Check if coordinates are equal.
* @param {Object} a First coordinates
* @param {Object} b Second coordinates
* @return {Boolean} True if they are equal, false otherwise
*/
export function coordinatesAreEqual(a, b) {
const aCoord = parseCoordinates(a);
const bCoord = parseCoordinates(b);
return aCoord.lat === bCoord.lat && aCoord.lng === bCoord.lng;
}
/**
* Return new empty bounds according to underlying map library.
* @param {Object} sw South West Bound.
* @param {Object} ne North East Bound.
* @return {Object} Bounds
*/
export function newBounds(sw, ne) {
return new mapboxgl.LngLatBounds(sw, ne);
}
/**
* Create new bound according to the underlying library.
* @param {Object} coordinates Coordinates of bound.
* @return {BoundObject} Bound object matching the underlying map library
*/
export function newBound(coordinates) {
const safeCoordinates = parseCoordinates(coordinates);
return [safeCoordinates.lng, safeCoordinates.lat];
}
/**
* Get circle points.
* @param {Object} coordinates Center coordinates
* @param {Number} radius Radius
* @param {String} unit Unit of the radius
* @return {Object} GeoJSON data
*/
export function getCircleData(coordinates, radius, unit) {
const { radius: usedRadius, unit: usedUnit } = convertRadiusUnit(radius, unit);
return circle(newBound(coordinates), usedRadius, {
steps: CIRCLE_POINTS_CONFIG,
units: usedUnit,
});
}
/**
* Get layer identifier from source identifier.
* @param {String} id Source's identifier
* @return {String} Layer's identifier
*/
export function getLayerId(id) {
return `${id}_layer`;
}
/**
* Draw GeoJSON on map.
* @param {Object} map MapBox map
* @param {String} id Identifier of layer
* @param {Object} data Layer data
* @param {Object} paint Paint option of polygon
* @param {Function} onClick Add on click to the layer
*/
export function drawGeoJSON(map, id, data, paint = {}, onClick, type = 'fill') {
if (!id || !map || !data) {
return;
}
const layerId = getLayerId(id);
const source = map.getSource(id);
if (source) {
source.setData(data);
Object.keys(paint).forEach(property =>
map.setPaintProperty(layerId, property, paint[property]),
);
} else {
map.addSource(id, {
type: 'geojson',
data,
});
map.addLayer({
id: layerId,
type,
source: id,
layout: {},
paint,
});
if (isFunction(onClick)) {
map.on('click', layerId, onClick);
/* eslint-disable no-param-reassign */
map.on('mouseenter', layerId, () => {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', layerId, () => {
map.getCanvas().style.cursor = '';
});
/* eslint-enable no-param-reassign */
}
}
}
/**
* Remove GeoJSON from map.
* @param {Object} map MapBox map
* @param {String} id Identifier of layer
*/
export function removeGeoJSON(map, id) {
if (!id || !map) {
return;
}
const layerId = getLayerId(id);
try {
const layer = map.getLayer(layerId);
if (layer) {
map.removeLayer(layerId);
}
} catch (e) {
global.console.warn(
`Error while removing GeoJSON layer with id ${layerId}. It's sometimes due to an already removed map`,
e,
);
}
try {
const source = map.getSource(id);
if (source) {
map.removeSource(id);
}
} catch (e) {
global.console.warn(
`Error while removing GeoJSON soruce with id ${id}. It's sometimes due to an already removed map`,
e,
);
}
}
export default {
convertRadiusUnit,
coordinatesAreEqual,
drawGeoJSON,
getCircleData,
getLayerId,
newBound,
newBounds,
removeGeoJSON,
};