-
-
Save jometho/1621a556633385da45b2 to your computer and use it in GitHub Desktop.
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