-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
210 lines (172 loc) · 6.8 KB
/
server.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
'use strict';
const compression = require(`compression`);
const { SparqlService, PostgresService, directQueries } = require(`osm-regions/src`);
const app = require(`express`)();
const secrets = require(`./secrets`);
const topojson = require(`topojson`);
const port = 9978;
// const rdfServerUrl = `https://sophox.org/bigdata/sparql`;
const rdfService = `https://sophox.org/bigdata/namespace/wdq/sparql`;
// app.use(function (req, resp, next) {
// resp.header(`Access-Control-Allow-Origin`, `*`);
// resp.header(`Access-Control-Allow-Methods`, `GET`);
// resp.header(`Access-Control-Allow-Headers`, `Content-Type, Content-Length`);
// next();
// });
app.options(`/*`, function (req, resp) {
resp.sendStatus(200);
});
app.use(compression());
app.get(`/regions/:format`, handleRequest);
const sparqlService = new SparqlService({
url: rdfService,
userAgent: `osm-regions`,
Accept: `application/sparql-results+json`
});
const postgresService = new PostgresService({
queries: directQueries,
host: secrets.host,
port: secrets.port,
database: secrets.database,
user: secrets.user,
password: secrets.password,
});
app.listen(port, err => {
if (err) {
console.error(err);
} else {
console.log(`server is listening on ${port}`);
}
});
class MyError extends Error {
constructor(code, msg) {
super();
this.code = code;
this.msg = msg;
}
}
async function handleRequest(req, resp) {
try {
await processQueryRequest(req, resp);
} catch (err) {
if (err instanceof MyError) {
resp.status(err.code).send(`\n\n${err.msg}\n\n`);
} else {
resp.status(500).send(`\n\nboom\n\n`);
}
try {
if (err instanceof MyError) {
console.error(err.msg, JSON.stringify(req.params), JSON.stringify(req.query));
} else {
console.error(err, JSON.stringify(req.params), JSON.stringify(req.query));
}
} catch (e2) {
console.error(err);
}
}
}
// Allowed params (per topojson code)
const NUMERIC_PARAMS = {
// 'planarArea': { desc: 'minimum planar triangle area (absolute)' },
// 'planarQuantile': { desc: 'minimum planar triangle area (quantile)', max: 1 },
sphericalArea: { desc: `minimum spherical excess (absolute)` },
sphericalQuantile: { desc: `minimum spherical excess (quantile)`, max: 1 },
};
const parseNumber = function (params, name, info) {
const value = parseFloat(params[name]);
if (!(value >= 0 && value <= (info.max || Number.MAX_VALUE))) {
throw new Error(`${name} parameter, ${info.desc}, must be a non-negative number${
info.max ? ` not larger than ${info.max}` : ``}`);
}
return value;
};
function parseParams(req) {
const params = { ...req.query, format: req.params.format };
if ((params.ids === undefined) === (params.sparql === undefined)) {
throw new MyError(400, `Either "ids" or "query" parameter must be given, but not both`);
}
let ids = params.ids;
if (ids !== undefined) {
ids = ids.split(`,`).filter(id => id !== ``);
if (ids.length > 1000) throw new MyErorr(400, `No more than 1000 IDs is allowed`);
ids.forEach(val => {
if (!/^Q[1-9][0-9]{0,15}$/.test(val)) throw new MyError(400, `Invalid Wikidata ID`);
});
}
const format = params.format;
if (format !== `geojson.json` && format !== `topojson.json`) {
throw new MyError(400, `bad format parameter. Allows "geojson.json" and "topojson.json"`);
}
let simplifyCmd, value;
for (const name of Object.keys(NUMERIC_PARAMS)) {
if (params.hasOwnProperty(name)) {
if (simplifyCmd) {
throw new Error(`${name} parameter cannot be used together with ${simplifyCmd}`);
}
simplifyCmd = name;
value = parseNumber(params, name, NUMERIC_PARAMS[name]);
}
}
// By default, without any params, optimize the result to a fraction of the original.
// To preserve the original geometry, set sphericalQuantile=1
if (!simplifyCmd) {
simplifyCmd = `sphericalQuantile`;
value = 0.07;
} else if (simplifyCmd === `sphericalQuantile` && value === 1) {
simplifyCmd = undefined;
value = undefined;
}
let filter = params.filter;
if (filter === undefined) {
filter = simplifyCmd ? `all` : `none`;
} else if (filter !== `none` && filter !== `all` && filter !== `detached`) {
throw new MyError(400, `bad filter parameter. Allows "none", "all" and "detached"`);
}
let quantize = simplifyCmd ? 4 : 0;
if (params.hasOwnProperty(`quantize`)) {
quantize = parseNumber(params, `quantize`, { desc: `Exponent to use for quantizing`, max: 8 });
}
quantize = quantize ? Math.pow(10, quantize) : 0;
const postgresOpts = {}; // { waterTable: secrets.waterTable };
return { ids, sparql: params.sparql, simplifyCmd, value, quantize, format, filter, postgresOpts };
}
async function processQueryRequest(req, resp) {
let { sparql, ids, simplifyCmd, value, quantize, format, filter, postgresOpts } = parseParams(req);
let newValue = value;
let equivLog = ``;
let qres;
if (sparql) {
qres = await sparqlService.query(sparql, `id`);
ids = Object.keys(qres);
}
const pres = await postgresService.query(secrets.table, ids, postgresOpts);
let result = PostgresService.toGeoJSON(pres, qres);
const originalSize = result.length;
if (simplifyCmd || format === `topojson.json`) {
result = topojson.topology({ data: JSON.parse(result) }, quantize);
if (simplifyCmd) {
const system = (simplifyCmd === `sphericalArea` || simplifyCmd === `sphericalQuantile`) ? `spherical` : `planar`;
result = topojson.presimplify(result, topojson[`${system }TriangleArea`]);
if (simplifyCmd === `planarQuantile` || simplifyCmd === `sphericalQuantile`) {
newValue = topojson.quantile(result, newValue);
resp.header(`X-Equivalent-${system}Area`, newValue.toString());
equivLog = `X-Equivalent-${system}Area=${newValue.toString()}`;
}
result = topojson.simplify(result, newValue);
if (filter !== `none`) {
const filterFunc = filter === `all` ? topojson.filterWeight : topojson.filterAttachedWeight;
result = topojson.filter(result, filterFunc(result, newValue, topojson[`${system }RingArea`]));
}
// if (quantize) {
// result = topojson.quantize(result, quantize);
// }
if (format === `geojson.json`) {
result = topojson.feature(result, result.objects.data);
}
}
}
const contentType = format === `geojson.json` ? `application/geo+json` : `application/topo+json`;
resp.setHeader(`Cache-Control`, `public, max-age=43200`);
resp.status(200).type(contentType).send(result);
console.log(`\n*************${new Date().toISOString()} ${format} ${simplifyCmd || 'noSimpl'}=${value || 0} filter=${filter} ${equivLog} quantize=${quantize} size=${originalSize}=>${(typeof result === 'string' ? result : JSON.stringify(result)).length} ip=${req.headers['x-real-ip']}\n${sparql}`);
}