This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pg = require('pg'); | |
var conString = "postgres://username:password@hostname.rds.amazonaws.com:5432/database"; //TODO: point to RDS instance | |
exports.bbox = function(req, res) { | |
var client = new pg.Client(conString); | |
client.connect(); | |
var crsobj = {"type": "name","properties": {"name": "urn:ogc:def:crs:EPSG:6.3:4326"}}; | |
var idformat = "'" + req.params.id + "'"; | |
idformat = idformat.toUpperCase(); | |
var query = client.query("select st_asgeojson(st_envelope(shape)) as geojson from ne_countries where iso_a3 = " + idformat + ";"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "feature", | |
"crs": { | |
"type": "name", | |
"properties": { | |
"name": "urn:ogc:def:crs:EPSG:6.3:4326" | |
} | |
}, | |
"geometry": { | |
"type": "Polygon", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dynamically include routes (Controller) | |
fs.readdirSync('./controllers').forEach(function (file) { | |
if(file.substr(-3) == '.js') { | |
route = require('./controllers/' + file); | |
route.controller(app); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var meta = client.query("select * from geometry_columns where f_table_name = '" + tablename + "' and f_table_schema = '" + schemaname + "';"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
meta.on('row', function (row) { | |
var query; | |
var coll; | |
spatialcol = row.f_geometry_column; | |
if (geom == "features") { | |
query = client.query("select st_asgeojson(st_transform(" + spatialcol + ",4326)) as geojson, * from " + fullname + ";"); | |
coll = { | |
type : "FeatureCollection", | |
features : [] | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/roll up the results | |
query.on('row', function (result) { | |
if (!result) { | |
return res.send('No data found'); | |
} else { | |
if (geom == "features") { | |
coll.features.push(geojson.getFeatureResult(result, spatialcol)); //use helper function | |
} else if (geom == "geometry") { | |
var shape = JSON.parse(result.geojson); | |
coll.geometries.push(shape); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.getFeatureResult = function(result, spatialcol) { | |
var props = new Object; | |
var crsobj = { | |
"type" : "name", | |
"properties" : { | |
"name" : "urn:ogc:def:crs:EPSG:6.3:4326" | |
} | |
}; | |
//builds feature properties from database columns | |
for (var k in result) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -X POST -d "{ \"type\": \"Point\", \"coordinates\": [-98.35, 39.7] }" -H "Content-Type: application/json" http://localhost:3000/vector/public/states_gen/features/intersect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* retrieve all features that intersect the input GeoJSON geometry | |
*/ | |
app.post('/vector/:schema/:table/:geom/intersect', function (req, res, next) { | |
//console.log(JSON.stringify(req.body)); | |
var queryshape = JSON.stringify(req.body); | |
//continue with the rest of app.post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SQL to retrieve schema | |
//var sql = "SELECT n.nspname as schemaname,c.relname as table_name,a.attname as column_name,format_type(a.atttypid, a.atttypmod) AS //type,col_description(a.attrelid, a.attnum) as comments"; | |
//sql = sql + " FROM pg_class c INNER JOIN pg_namespace n ON c.relnamespace = n.oid LEFT JOIN pg_attribute a ON a.attrelid = c.oid"; | |
//sql = sql + " WHERE a.attnum > 0 and c.relname = '" + tablename + "' and n.nspname = '" + schemaname + "';"; | |
//SQL to retrieve layer list | |
//sql = "SELECT 'geometry' AS geotype, * FROM geometry_columns UNION SELECT 'geography' as geotype, * FROM geography_columns;"; |