This repository has been archived by the owner on Dec 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwhere.js
233 lines (191 loc) · 5.77 KB
/
where.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
// config
var pjson = require('./package.json'),
config = require('./config.json'),
// dependencies
restify = require('restify'),
fs = require('fs'),
request = require('request'),
schedule = require('node-schedule'),
turf = require('turf'),
// scripts
inquire = require('./scripts/inquire.js'),
h = require('./scripts/helper.js'),
docs = require('./scripts/docs.js'),
TimSort = require('timsort'),
// variables
files = {}
init()
// import files
function init() {
var dataInitPromises = []
Object.keys(config.files).forEach(function(key) {
dataInitPromises.push(new Promise(function(resolve) {
update(resolve, key)
}))
if (config.files[key].schedule) {
schedule.scheduleJob(config.files[key].schedule, function() {
update(null, key)
})
}
})
Promise.all(dataInitPromises).then(initServer)
}
function update(resolve, key) {
if (config.files[key].path) {
fs.readFile(config.data_dir + config.files[key].path, 'utf8', function(err, data) {
if (err) throw err
files[key] = JSON.parse(data)
if (resolve)
resolve()
})
} else {
request(config.files[key].url, function(err, resp, body) {
if (err) throw err
files[key] = JSON.parse(body)
if (resolve)
resolve()
})
}
}
function initServer() {
var server = restify.createServer({
name: 'WHERE',
version: pjson.version
})
// server.pre(restify.pre.sanitizePath());
// Unsure if this is needed at some point
// server.use(restify.acceptParser(server.acceptable))
// server.use(restify.queryParser())
// server.use(restify.bodyParser())
server.use(restify.CORS());
// define routes
server.get('/q/:geojson', function(req, res) {
res.send(respond(req.params.geojson))
})
server.get('/q/:geojson/:geometry', function(req, res) {
res.send(respond(req.params.geojson, req.params.geometry))
})
server.get('/q/:geojson/:geometry/:props/', function(req, res) {
res.send(respond(req.params.geojson, req.params.geometry, req.params.props))
})
server.get('/q/:geojson/:geometry/:props/:options', function(req, res) {
res.send(respond(req.params.geojson, req.params.geometry, req.params.props, parseOptions(req.params.options)))
})
// send documentation (assuming it's not disabled in config.json)
if (config.docs) {
server.get('/', function(req, res) {
var body = docs.docs(config, files);
res.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/html'
});
res.write(body);
res.end();
})
}
// init server
server.listen((process.env.PORT || config.port), function() {
console.log('%s listening at port %s', server.name, (process.env.PORT || config.port))
})
}
function respond(file, geometry, props, opts) {
file = file == " " ? false : file
geometry = geometry == " " ? false : geometry
props = props == " " ? false : props
opts = opts == " " ? false : opts
if (!file || !config.files[file]) {
return {
"code": "ResourceNotFound",
"message": "invalid file name"
}
}
if (!geometry && !props && !opts) {
return files[file]
}
var features = h.getDeepObj(files, file + "." + (config.files[file].level || config.level))
var filtered = []
// this will be changed to allow more complex spatial queries
features.forEach(function(feature) {
delete(feature.dist)
if (geometry && props) {
if (inquire.inquireGeometry(geometry, feature) && inquire.inquireProperties(props, feature.properties))
filtered.push(feature)
} else if (geometry) {
if (inquire.inquireGeometry(geometry, feature))
filtered.push(feature)
} else if (props) {
if (inquire.inquireProperties(props, feature.properties))
filtered.push(feature)
} else {
filtered.push(feature)
}
})
// apply options
var userOptions = {},
options = {}
h.parseUserOptions(userOptions, (opts || ""))
h.parseOptions(options, userOptions, config, file)
if (options.dist) {
var user = turf.point(options.dist.split(","))
filtered.forEach(function(feature) {
if (feature.geometry.type == "Point") {
// feature.properties.dist = getDistanceFromLatLonInM(feature.geometry.coordinates[0], feature.geometry.coordinates[1], user.geometry.coordinates[0], user.geometry.coordinates[1])
feature.properties.dist = turf.distance(feature, user)
}
})
}
if (options.sortby) {
// filtered.sort(h.propComparator(options));
TimSort.sort(filtered, h.propComparator(options))
//
// filtered = sort(filtered,options.sortby)
}
var total = filtered.length
if (options.limit) {
filtered = filtered.slice(options.page * options.limit, options.page * options.limit + parseInt(options.limit))
}
if (options.properties != -1) {
if (!options.properties) {
filtered.forEach(function(feature) {
feature.properties = {}
})
} else {
filtered.forEach(function(feature) {
var newProps = {}
options.properties.split("|").forEach(function(property) {
newProps[property] = feature.properties[property]
})
feature.properties = newProps
})
}
}
var response = {
"type": "FeatureCollection",
"properties": {
"results": total,
"limit": options.limit,
"page": options.page
},
"features": filtered
}
return response
}
function parseOptions(options) {
return options
}
function getDistanceFromLatLonInM(lon1, lat1, lon2, lat2) {
//based on http://stackoverflow.com/questions/18883601/function-to-calculate-distance-between-two-coordinates-shows-wrong
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c * 1000; // Distance in m
return Math.round(d);
}
function deg2rad(deg) {
return deg * (Math.PI / 180)
}