Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Added conversion for custom field names and coordinate systems #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions config.sample.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module.exports = {
host: 'localhost',
user: 'yourusername',
password: 'yourpassword',
port: 5432,
database: 'yourdatabase'
}
database: {
host: 'localhost',
user: 'yourusername',
password: 'yourpassword',
port: 5432,
database: 'yourdatabase'
},
dataCoordinateSystem: 26915,
geomFieldNames: ['pt_geom', 'the_geom']
}
32 changes: 31 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,44 @@ var express = require('express'),
var app = express(),
port = process.env.PORT || 3000,
config = require('./config.js'),
db = pgp(config);
db = pgp(config['database']);

//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;

// Check for custom fields or ST_Transform in SELECT [...] FROM
sqlSplit=sql.split('FROM');
sql=sqlSplit[0]

// Apply ST_Transform and replace custom geom field names as necessary
if(config['dataCoordinateSystem'] != 4326){
if(config['geomFieldNames'].length > 0){
config['geomFieldNames'].forEach(function(v){
sql=sql.replace(v, 'ST_Transform('+v+', 4326) as $geom'); //Custom geom name and coordinate system
});
}
else{
sql=sql.replace('geom', 'ST_Transform(geom, 4326)'); //Custom coordinate system only
}
}
else{
// Custom geom field name only
if(config['geomFieldNames'].length > 0){
config['geomFieldNames'].forEach(function(v){
sql=sql.replace(v, v+' as $geom');
});
}
}

// Merge sql into one string
sqlSplit.shift(); //Remove first part of query
sql=sql+' FROM '+sqlSplit.join(); //Append remaining from original sql
sql=sql.replace('$geom', 'geom'); //Replace $geom - needed due to potential SELECT (ST_Transform(geom, 4326) as geom) issues

console.log('Executing SQL: ' + sql);

//query using pg-promise
Expand Down