2018-08-01 13:41:50 -04:00
|
|
|
/**
|
|
|
|
* Serve API
|
|
|
|
*/
|
|
|
|
const express = require('express')
|
|
|
|
|
|
|
|
// db connection pool
|
|
|
|
const db = require('./db')
|
|
|
|
|
|
|
|
// set up app
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
// building geometry by lat/lon - TODO join geometry against buildings
|
|
|
|
app.get('/buildings', function(req, res){
|
|
|
|
const { lng, lat } = req.query
|
|
|
|
db.query(
|
2018-08-02 11:56:11 -04:00
|
|
|
`SELECT
|
|
|
|
b.building_id as id,
|
|
|
|
b.building_doc as doc,
|
|
|
|
g.geometry_id as geometry_id
|
|
|
|
FROM buildings as b, geometries as g
|
2018-08-01 13:41:50 -04:00
|
|
|
WHERE
|
2018-08-02 11:56:11 -04:00
|
|
|
b.geometry_id = g.geometry_id
|
|
|
|
AND
|
2018-08-01 13:41:50 -04:00
|
|
|
ST_Intersects(
|
|
|
|
ST_Transform(
|
|
|
|
ST_SetSRID(ST_Point($1, $2), 4326),
|
|
|
|
3857
|
|
|
|
),
|
2018-08-02 11:56:11 -04:00
|
|
|
g.geometry_geom
|
2018-08-01 13:41:50 -04:00
|
|
|
)
|
2018-08-02 11:56:11 -04:00
|
|
|
LIMIT 1
|
2018-08-01 13:41:50 -04:00
|
|
|
`,
|
|
|
|
[lng, lat]
|
|
|
|
).then(function(data){
|
|
|
|
const rows = data.rows
|
|
|
|
if (rows.length){
|
2018-08-02 11:56:11 -04:00
|
|
|
const id = rows[0].id
|
|
|
|
const doc = rows[0].doc
|
2018-08-01 13:41:50 -04:00
|
|
|
const geometry_id = rows[0].geometry_id
|
2018-08-02 11:56:11 -04:00
|
|
|
console.log(lng, lat, id)
|
|
|
|
doc.id = id
|
|
|
|
doc.geometry_id = geometry_id
|
|
|
|
res.send(doc)
|
2018-08-01 13:41:50 -04:00
|
|
|
} else {
|
|
|
|
console.log(lng, lat, undefined)
|
|
|
|
res.status(404).send({error:'Not Found'})
|
|
|
|
}
|
|
|
|
}).catch(function(error){
|
|
|
|
console.error('Error:', error)
|
|
|
|
res.status(500).send({error:'Database error'})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
app.listen(8081, () => console.log('API server listening on port 8081'))
|