-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
46 lines (43 loc) · 1.46 KB
/
transform.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
import _ from "lodash";
export default {
/*
* Convert objects returned from the API in to pure GeoJSON objects ready to be consumed by the map service.
*/
transformToGeoJSON: function (data) {
let featureCollectionTemplate = `{
"type": "FeatureCollection",
"features": []
}`;
let featureTemplate = `{
"type": "Feature",
"properties": {},
"geometry": {}
}`;
let pointTemplate = `{
"type": "Point",
"coordinates": []
}`;
// Iterate over the array, converting to a new object type
let results = _.compact(
data.map((element) => {
// If it has geometry let that take precedence
let newEl = JSON.parse(featureTemplate);
if (element.hasGeometry) {
newEl.geometry = JSON.parse(element.hasGeometry.asJSON._value);
newEl.properties = element; // Copy the raw object on to properties
return newEl;
// Else use latitude and longitude
} else if (element.lat && element.long) {
newEl.geometry = JSON.parse(pointTemplate);
newEl.geometry.coordinates = [element.long, element.lat];
newEl.properties = element; // Copy the raw object on to properties
return newEl;
}
}),
);
// Wrap the whole object up in GeoJSON wrapper object
let result = JSON.parse(featureCollectionTemplate);
result.features = results;
return result;
},
};