-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
125 lines (107 loc) · 3.16 KB
/
model.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
/*
model.js
This file is required. It must export a class with at least one public function called `getData`
Documentation: http://koopjs.github.io/docs/usage/provider
*/
const request = require('request').defaults({gzip: true, json: true})
const config = require('config')
function Model (koop) {}
// Public function to return data from the
// Return: GeoJSON FeatureCollection
//
// Config parameters (config/default.json)
// req.
//
// URL path parameters:
// req.params.host (if index.js:hosts true)
// req.params.id (if index.js:disableIdParam false)
// req.params.layer
// req.params.method
var atob = require('atob');
var b64 = "SGVsbG8sIFdvcmxkIQ==";
var bin = atob(b64);
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
Model.prototype.getData = function (req, callback) {
let url = atob(req.params.id);
url = url.replaceAll('_', '/');
// Call the remote API with our developer key
request(`${atob(req.params.id)}`, (err, res, body) => {
if (err) return callback(err)
// translate the response into geojson
//const geojson = translate(body)
const geojson = body
// Optional: cache data for 10 seconds at a time by setting the ttl or "Time to Live"
// geojson.ttl = 10
// Optional: Service metadata and geometry type
// geojson.metadata = {
// title: 'Koop Sample Provider',
// description: `Generated from ${url}`,
// geometryType: 'Polygon' // Default is automatic detection in Koop
// }
// hand off the data to Koop
callback(null, geojson)
})
}
function translate (input) {
return {
type: 'FeatureCollection',
features: input.resultSet.vehicle.map(formatFeature)
}
}
function formatFeature (inputFeature) {
// Most of what we need to do here is extract the longitude and latitude
const feature = {
type: 'Feature',
properties: inputFeature,
geometry: {
type: 'Point',
coordinates: [inputFeature.longitude, inputFeature.latitude]
}
}
// But we also want to translate a few of the date fields so they are easier to use downstream
const dateFields = ['expires', 'serviceDate', 'time']
dateFields.forEach(field => {
feature.properties[field] = new Date(feature.properties[field]).toISOString()
})
return feature
}
module.exports = Model
/* Example provider API:
- needs to be converted to GeoJSON Feature Collection
{
"resultSet": {
"queryTime": 1488465776220,
"vehicle": [
{
"tripID": "7144393",
"signMessage": "Red Line to Beaverton",
"expires": 1488466246000,
"serviceDate": 1488441600000,
"time": 1488465767051,
"latitude": 45.5873117,
"longitude": -122.5927705,
}
]
}
Converted to GeoJSON:
{
"type": "FeatureCollection",
"features": [
"type": "Feature",
"properties": {
"tripID": "7144393",
"signMessage": "Red Line to Beaverton",
"expires": "2017-03-02T14:50:46.000Z",
"serviceDate": "2017-03-02T08:00:00.000Z",
"time": "2017-03-02T14:42:47.051Z",
},
"geometry": {
"type": "Point",
"coordinates": [-122.5927705, 45.5873117]
}
]
}
*/