-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
110 lines (103 loc) · 4.5 KB
/
lib.js
File metadata and controls
110 lines (103 loc) · 4.5 KB
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
// Function library
// optionally use WKT module
try {
var wkt = require("wkt");
} catch (e) {
var wkt = null;
}
// Config settings
process.env.NODE_ENV = "local_default"; // uses any settings in "local_default" to override "default"
const config = require('config');
var corsAllow = null;
if (config.has('corsAllow')) {
corsAllow = config.get('corsAllow') || '*';
if (Array.isArray(corsAllow)) {
for (var ca of corsAllow) {
ca = new RegExp(ca);
}
}
}
var geoFields = null;
if (config.has('geoFields')) {
geoFields = config.get('geoFields');
}
function corsHeaders (req, res) {
//if (!corsAllow || !req.headers['origin']) return; // no CORS processing
if (!corsAllow) return;
res.setHeader('access-control-allow-credentials', 'false');
if (req.headers['access-control-request-method']) {
res.setHeader('access-control-allow-methods', 'GET, POST, OPTIONS, HEAD'); // only allow read access
}
if (req.headers['access-control-request-headers']) {
res.setHeader('access-control-allow-headers', req.headers['access-control-request-headers']); // agree to any request header
// or "Origin,Authorization,X-Requested-With,Content-Type,Accept"
}
if (Array.isArray(corsAllow)) {
if (!req.headers['origin']) return;
for (const origin_match of corsAllow) {
if (req.headers['origin'].match(origin_match)) { // does the pattern match this origin
res.setHeader('access-control-allow-origin', req.headers['origin']);
res.setHeader('vary', 'Origin');
break;
}
}
} else {
res.setHeader('access-control-allow-origin', corsAllow);
}
};
function _extractRowGeometry (row, geoFields) {
for (const gf of geoFields) {
if (gf.geojson != null && gf.types != null && row[gf.geojson] != null) {
if (gf.types.indexOf(row[gf.geojson].type) > -1) { // do the contents match an acceptable type?
var geometry = row[gf.geojson];
delete row[gf.geojson]; // redundant, so removed from properties
return geometry;
}
} else if (gf.point_pair != null && row[gf.point_pair[0]] != null && row[gf.point_pair[1]] != null) {
return { type: 'Point', coordinates:
[ row[gf.point_pair[0]], row[gf.point_pair[1]] ] }; // new GeoJson Point entry
break;
} else if (gf.coordinates != null && gf.type != null && row[gf.coordinates] != null) {
return { type: gf.type, coordinates: row[gf.coordinates] }; // new GeoJson entry
} else if (wkt != null && gf.wkt != null && gf.types != null && row[gf.wkt] != null) {
var geoj = wkt.parse(row[gf.wkt]);
if (geoj && gf.types.indexOf(geoj.type) > -1) { // do the contents match an acceptable type?
return geoj;
}
}
}
return null;
}
function jsonToGeoJSON (body) {
if (!geoFields) return body; // return data as is if no geo related results are specified
var feature; var newbody;
if (Array.isArray(body)) { // rows of data
newbody = { type: "FeatureCollection", features: [] }; // always returns a collection
for (var row of body) {
feature = { type: "Feature" };
feature.geometry = _extractRowGeometry(row, geoFields); // note can alter the row, also result can be null
feature.properties = row;
newbody.features.push(feature); // always add a feature even if geometry is null = no location
}
} else {
feature = { type: "Feature" };
feature.geometry = _extractRowGeometry(body, geoFields); // note can alter the body, also result can be null
feature.properties = body;
//newbody.features.push(feature); // always add a feature even if geometry is null = no location
newbody = feature;
}
return newbody;
}
function openAPIJSON (body) {
const read_methods = [ 'get', 'post' ];
for (var p in body.paths) { // path keys
for (var m in body.paths[p]) { // method keys
if (read_methods.indexOf(m) < 0) {
delete body.paths[p][m];
}
}
}
return body;
}
// export library functions
module.exports = { jsonToGeoJSON, corsHeaders, openAPIJSON };