Skip to content

Instantly share code, notes, and snippets.

View geobabbler's full-sized avatar
💭
Working

William Dollins geobabbler

💭
Working
View GitHub Profile
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 + ";");
{
"type": "feature",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG:6.3:4326"
}
},
"geometry": {
"type": "Polygon",
// dynamically include routes (Controller)
fs.readdirSync('./controllers').forEach(function (file) {
if(file.substr(-3) == '.js') {
route = require('./controllers/' + file);
route.controller(app);
}
});
var meta = client.query("select * from geometry_columns where f_table_name = '" + tablename + "' and f_table_schema = '" + schemaname + "';");
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 : []
};
/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);
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) {
@geobabbler
geobabbler / snippet6.bat
Created November 23, 2014 14:33
Code snippet 6 for blog post
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
/**
* 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
//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;";