-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMap.js
More file actions
214 lines (189 loc) · 4.97 KB
/
Map.js
File metadata and controls
214 lines (189 loc) · 4.97 KB
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
// @flow
import { WarpedMapLayer } from '@allmaps/maplibre';
import {
bbox,
bboxPolygon,
buffer,
feature,
featureCollection
} from '@turf/turf';
import _ from 'underscore';
import circle from '@turf/circle';
const MIN_LATITUDE = -90;
const MAX_LATITUDE = 90;
const MIN_LONGITUDE = -180;
const MAX_LONGITUDE = 180;
/**
* Returns a GeoJSON circle feature with the given center point and radius.
* @param point - The center point of the circle.
* @param radius - The radius of the circle in kilometers.
* @returns {Feature<Geometry, Properties>} - The GeoJSON circle feature.
*/
const buildCircle = (point, radius) => (
circle(point.coordinates, radius, { units: 'kilometers', steps: 32 })
);
/**
* Returns a GeoJSON feature collection containing circles for each item in the given array.
*/
const getCertaintyCircles = (
items,
getCertaintyRadius: (item: any) => number | undefined
) => {
const features = [];
for (const item of items) {
if (getCertaintyRadius(item)) {
if (item.geometry?.type === 'FeatureCollection') {
for (const childFeature of item.geometry.features) {
if (childFeature.geometry?.type === 'Point') {
features.push(buildCircle(childFeature.geometry, getCertaintyRadius(item)));
}
}
} else if (item.geometry.type === 'GeometryCollection') {
for (const geometry of item.geometry.geometries) {
if (geometry.type === 'Point') {
features.push(buildCircle(geometry, getCertaintyRadius(item)));
}
}
} else if (item.geometry?.type === 'Point') {
features.push(buildCircle(item.geometry, getCertaintyRadius(item)));
}
}
}
return featureCollection(features);
};
/**
* Adds the geo-referenced image layer to the passed map.
*
* @param map
* @param layerId
* @param options
*/
const addGeoreferenceLayer = (map, layerId, options = {}) => {
const warpedMapLayer = new WarpedMapLayer(layerId);
map.addLayer(warpedMapLayer);
if (options.url) {
warpedMapLayer.addGeoreferenceAnnotationByUrl(options.url);
} else if (options.manifest) {
warpedMapLayer.addGeoreferenceAnnotation(options.manifest);
}
if (options.opacity) {
warpedMapLayer.setOpacity(options.opacity);
}
};
/**
* Returns a bounding box for the passed geometry (with optional buffer).
*
* @param data
* @param bufferDistance
*
* @returns {BBox}
*/
const getBoundingBox = (data, bufferDistance = null) => {
// Convert the GeoJSON into a bounding box
const box = bbox(data);
if (!validateBoundingBox(box)) {
return null;
}
// Convert the bounding box to a polygon
const polygon = bboxPolygon(box);
// Create a buffer around the polygon (if a distance is provided)
let polygonBuffer;
if (bufferDistance) {
polygonBuffer = buffer(polygon, bufferDistance, { units: 'miles' });
} else {
polygonBuffer = polygon;
}
// Convert the buffer to a bounding box
return bbox(polygonBuffer);
};
/**
* Removes a layer from the passed map.
*
* @param map
* @param layerId
*
* @returns {*}
*/
const removeLayer = (map, layerId) => map && map.removeLayer(layerId);
/**
* Wraps the passed record in a feature.
*
* @param record
* @param item
* @param geometry
* @param originalProperties
*
* @returns {Feature<Geometry, {
* id: *,
* ccode: [],
* title: *,
* uuid: *,
* record_id: *,
* name: *,
* names: *,
* type: *,
* items: [*],
* url: *
* }>}
*/
const toFeature = (record: any, item: any, geometry: any, originalProperties?: any) => {
const properties = {
id: record.record_id,
ccode: [],
title: record.name,
uuid: record.uuid,
record_id: record.record_id,
name: record.name,
names: record.names?.map((toponym: string) => ({ toponym })),
type: record.type,
items: [item],
url: record.url,
originalProperties: originalProperties || {}
};
const id = parseInt(record.record_id, 10);
return feature(geometry, properties, { id });
};
/**
* Returns a feature collection for the passed set of features.
*
* @param features
*
* @returns {FeatureCollection<Geometry, GeoJsonProperties>}
*/
const toFeatureCollection = (features: Array<any>) => featureCollection(features);
/**
* Validates that the passed bounding box contains finite coordinates.
*
* @param boundingBox
*
* @returns {*}
*/
const validateBoundingBox = (boundingBox: Array<number>) => _.every(boundingBox, _.isFinite);
/**
* Returns true if the passed coordinates are valid.
*
* @param coordinates
*
* @returns {boolean}
*/
const validateCoordinates = (coordinates) => {
let valid = false;
if (coordinates) {
const [latitude, longitude] = coordinates;
valid = latitude >= MIN_LATITUDE
&& latitude <= MAX_LATITUDE
&& longitude >= MIN_LONGITUDE
&& longitude <= MAX_LONGITUDE;
}
return valid;
};
export default {
addGeoreferenceLayer,
getCertaintyCircles,
getBoundingBox,
removeLayer,
toFeature,
toFeatureCollection,
validateBoundingBox,
validateCoordinates
};