-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (93 loc) · 3.88 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
const gp = require('geojson-precision');
const turf = {
point: require('@turf/helpers').point,
polygon: require('@turf/helpers').polygon,
geometryCollection: require('@turf/helpers').geometryCollection,
featureCollection: require('@turf/helpers').featureCollection
};
//const moment = require('moment-timezone');
const rewind = require('geojson-rewind');
const extend = require('xtend');
module.exports = {
/**
* Converts the ACT ESA Incidents Upstream Feed to GeoJSON with features:
*
* - Only include ACT incidents
* - Unpack overloaded description field
* - Limit coordinate precision
* - Enforce GeoJSON winding order
*
* @param {Object} json ACT ESA Incidents Upstream Feed as a JSON Object
* @returns {Object} The GeoJSON result
*/
toGeoJSON: function(json) {
var self = this;
var geoJSONFeatures = json.filter(function (item) {
return item.state === "ACT";
}).map(function (item) {
var properties = {
title: item.title,
icon: item.icon
};
var descriptionItems = item.description.split(/<br *\/?>/).map(function (line) {
var split = line.split(/:/);
if (split.length > 1) {
var key = split.shift();
var value = split.join(':');
return {
key: key.trim().toLowerCase().replace(/ /g, '-'),
value: value.trim()
};
}
}).reduce(function (acc, cur) {
acc[cur.key] = cur.value;
return acc;
}, {});
properties = extend(descriptionItems, properties); // use this order to avoid description properties overriding 1st level properties
// remove redundant incident field
if (properties.title === properties.incident) {
delete properties.incident;
}
// clean up location trailing whitespace
properties.location = properties.location.replace(/[, ]*$/, '');
// icon is just type and status
delete properties.icon;
var id = item.guid;
var pointCoordinates;
var polygonCoordinates;
if (item.point && item.point.type === "Point" && item.point.coordinates && item.point.coordinates.length > 1) {
pointCoordinates = [Number(item.point.coordinates[1]), Number(item.point.coordinates[0])];
}
if (item.polygon && item.polygon.type === "Polygon" && item.polygon.coordinates && item.polygon.coordinates.length > 1) {
var outer = [];
for (var i = 0; i < item.polygon.coordinates.length; i += 2) {
var lat = Number(item.polygon.coordinates[i]);
var lng = Number(item.polygon.coordinates[i + 1]);
outer.push([lng, lat]);
}
polygonCoordinates = [outer];
}
if (pointCoordinates && polygonCoordinates) {
return turf.geometryCollection([{
type: 'Point',
coordinates: pointCoordinates
}, {
type: 'Polygon',
coordinates: polygonCoordinates
}], properties, {id: id});
}
if (pointCoordinates) {
return turf.point(pointCoordinates, properties, {id: id});
}
if (polygonCoordinates) {
return turf.polygon(polygonCoordinates, properties, {id: id});
}
});
var geoJSON = turf.featureCollection(geoJSONFeatures);
// Limit Coordinate Precision
geoJSON = gp.parse(geoJSON, 4);
// Enforce Winding Order
geoJSON = rewind(geoJSON);
return geoJSON;
}
};