forked from NYCPlanning/labs-postgis-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
103 lines (90 loc) · 3.04 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
//postgis-preview
//A super simple node app + leaflet frontend for quickly viewing PostGIS query results
//dependencies
var express = require('express'),
pgp = require('pg-promise')(),
dbgeo = require('dbgeo'),
jsonexport = require('jsonexport');
//create express app and prepare db connection
var app = express(),
port = process.env.PORT || 4000,
config = require('./config.js'),
db = pgp(config);
//use express static to serve up the frontend
app.use(express.static(__dirname + '/public'));
//expose sql endpoint, grab query as URL parameter and send it to the database
app.get('/sql', function (req, res) {
var sql = req.query.q;
var format = req.query.format || 'topojson';
console.log('Executing SQL: ' + sql, format);
//query using pg-promise
db.any(sql)
.then(function (data) { //use dbgeo to convert WKB from PostGIS into topojson
switch (format) {
case 'csv':
return jsonExport(data).then(function (data) {
res.setHeader('Content-disposition', 'attachment; filename=query.csv');
res.setHeader('Content-Type', 'text/csv');
return data;
});
case 'geojson':
return dbGeoParse(data, format).then(function (data) {
res.setHeader('Content-disposition', 'attachment; filename=query.geojson');
res.setHeader('Content-Type', 'application/json');
return data;
});
default:
return dbGeoParse(data, format);
}
})
.then(function (data) {
res.send(data);
})
.catch(function (err) { //send the error message if the query didn't work
var msg = err.message || err;
console.log("ERROR:", msg);
res.send({
error: msg
});
});
});
function dbGeoParse(data, format) {
//check the raw data for geom, inject nulls if there is none
if(!data[0].geom) {
data.forEach(function(row) {
row.geom='010100000000000000000000000000000000000000';
})
}
return new Promise(function (resolve, reject) {
dbgeo.parse({
data: data,
outputFormat: format,
geometryColumn: 'geom',
geometryType: 'wkb'
}, function (err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
function jsonExport(data) {
//remove geom
data.forEach(function (row) {
delete row.geom;
});
return new Promise(function (resolve, reject) {
jsonexport(data, function (err, csv) {
if (err) {
reject(err);
} else {
resolve(csv);
}
});
});
}
//start the server
app.listen(port);
console.log('postgis-preview is listening on port ' + port + '...');