Created
November 22, 2014 13:23
-
-
Save geobabbler/d867f562e8f8b1f0a4f6 to your computer and use it in GitHub Desktop.
Code snippet #2 for http://blog.geomusings.com/2013/12/11/building-a-simple-geodata-service-with-node-and-amazon-rds/
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:[email protected]: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 + ";"); | |
var retval = "no data"; | |
query.on('row', function(result) { | |
client.end(); | |
if (!result) { | |
return res.send('No data found'); | |
} else { | |
res.setHeader('Content-Type', 'application/json'); | |
//build a GeoJSON feature and return it | |
res.send({type: "feature",crs: crsobj, geometry: JSON.parse(result.geojson), properties:{"iso": req.params.id, "representation": "extent"}}); | |
} | |
}); | |
}; | |
exports.bboxSrid = function(req, res) { | |
var client = new pg.Client(conString); | |
client.connect(); | |
var crsobj = {"type": "name","properties": {"name": "urn:ogc:def:crs:EPSG:6.3:" + req.params.srid}}; | |
var idformat = "'" + req.params.id + "'"; | |
idformat = idformat.toUpperCase(); | |
var query = client.query("select st_asgeojson(st_envelope(st_transform(shape, " + req.params.srid + "))) as geojson from ne_countries where iso_a3 = " + idformat + ";"); | |
var retval = "no data"; | |
query.on('row', function(result) { | |
client.end(); | |
if (!result) { | |
return res.send('No data found'); | |
} else { | |
res.setHeader('Content-Type', 'application/json'); | |
res.send({type: "feature",crs: crsobj, geometry: JSON.parse(result.geojson), properties:{"iso": req.params.id, "representation": "extent"}}); | |
} | |
}); | |
}; | |
exports.polygon = function(req, res) { | |
//TODO: Flesh this out. Logic will be similar to bounding box. | |
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(shape) as geojson from ne_countries where iso_a3 = " + idformat + ";"); | |
var retval = "no data"; | |
query.on('row', function(result) { | |
client.end(); | |
if (!result) { | |
return res.send('No data found'); | |
} else { | |
res.setHeader('Content-Type', 'application/json'); | |
res.send({type: "feature", crs: crsobj, geometry: JSON.parse(result.geojson), properties:{"iso": req.params.id, "representation": "boundary"}}); | |
} | |
}); }; | |
exports.polygonSrid = function(req, res) { | |
var client = new pg.Client(conString); | |
client.connect(); | |
var crsobj = {"type": "name","properties": {"name": "urn:ogc:def:crs:EPSG:6.3:" + req.params.srid}}; | |
var idformat = "'" + req.params.id + "'"; | |
idformat = idformat.toUpperCase(); | |
var query = client.query("select st_asgeojson(st_transform(shape, " + req.params.srid + ")) as geojson from ne_countries where iso_a3 = " + idformat + ";"); | |
var retval = "no data"; | |
query.on('row', function(result) { | |
client.end(); | |
if (!result) { | |
return res.send('No data found'); | |
} else { | |
res.setHeader('Content-Type', 'application/json'); | |
res.send({type: "feature",crs: crsobj, geometry: JSON.parse(result.geojson), properties:{"iso": req.params.id, "representation": "boundary"}}); | |
} | |
}); }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment