Query buildings by location or reference

This commit is contained in:
Tom Russell 2018-09-29 19:09:48 +01:00
parent 2a1902f6ce
commit 3e4beb5423
2 changed files with 107 additions and 69 deletions

View File

@ -1,6 +1,6 @@
import { query } from './db';
function queryBuildingAtPoint(lng, lat) {
function queryBuildingsAtPoint(lng, lat) {
return query(
`SELECT
b.building_id as id,
@ -17,26 +17,52 @@ function queryBuildingAtPoint(lng, lat) {
),
g.geometry_geom
)
LIMIT 1
`,
[lng, lat]
).then(function(data){
const rows = data.rows
if (rows.length){
const id = rows[0].id
const doc = rows[0].doc
const geometry_id = rows[0].geometry_id
doc.id = id
doc.geometry_id = geometry_id
return doc
}
return undefined;
}).catch(function(error){
).then(buildingRowsToDocs).catch(function(error){
console.error(error);
return undefined;
});
}
function queryBuildingsByReference(key, id) {
if (key === 'toid'){
return query(
`SELECT
b.building_id as id,
b.building_doc as doc,
g.geometry_id as geometry_id
FROM buildings as b, geometries as g
WHERE
b.geometry_id = g.geometry_id
AND
b.ref_toid = $1
`,
[id]
).then(buildingRowsToDocs).catch(function(error){
console.error(error);
return undefined;
});
}
if (key === 'uprn') {
return query(
`SELECT
b.building_id as id,
b.building_doc as doc,
g.geometry_id as geometry_id
FROM buildings as b, geometries as g
WHERE
b.geometry_id = g.geometry_id
AND
b.ref_uprn = $1
`,
[id]
).then(buildingRowsToDocs).catch(function(error){
console.error(error);
return undefined;
});
}
return {error: 'Key must be UPRN or TOID'};
}
function getBuildingById(id) {
return query(
@ -50,22 +76,24 @@ function getBuildingById(id) {
building_id = $1
`,
[ id ]
).then(function(data){
).then(buildingRowsToDocs).catch(function(error){
console.error(error);
return undefined;
});
}
function buildingRowsToDocs(data){
const rows = data.rows
if (rows.length){
const id = rows[0].id
const doc = rows[0].doc
const geometry_id = rows[0].geometry_id
const data = rows.map(function(row){
const id = row.id
const doc = row.doc
const geometry_id = row.geometry_id
doc.id = id
doc.geometry_id = geometry_id
return doc
}
return undefined;
}).catch(function(error){
console.error(error);
return undefined;
});
return data;
}
function saveBuilding(id, building_doc) {
@ -88,4 +116,4 @@ function saveBuilding(id, building_doc) {
});
}
export { queryBuildingAtPoint, getBuildingById, saveBuilding };
export { queryBuildingsAtPoint, queryBuildingsByReference, getBuildingById, saveBuilding };

View File

@ -11,7 +11,8 @@ import pgConnect from 'connect-pg-simple';
import App from './frontend/app';
import { pool } from './db';
import { authUser, createUser, getUserById } from './user';
import { queryBuildingAtPoint, getBuildingById, saveBuilding } from './building';
import { queryBuildingsAtPoint, queryBuildingsByReference, getBuildingById,
saveBuilding } from './building';
import tileserver from './tileserver';
import { parseBuildingURL } from './parse';
@ -93,7 +94,7 @@ function renderHTML(context, data, req, res){
res.redirect(context.url);
} else {
res.status(context.status).send(
`<!doctype html>
`<!doctype html>
<html lang="">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
@ -134,27 +135,37 @@ function renderHTML(context, data, req, res){
// GET tiles
server.use('/tiles', tileserver);
// GET buildings
// not implemented - may be useful to GET all buildings, paginated
// GET building at point
server.get('/buildings.json', function(req, res){
const { lng, lat } = req.query
queryBuildingAtPoint(lng, lat).then(function(result){
if (result) {
res.send(result)
} else {
res.status(404).send({error:'Not Found'})
}
// GET buildings at point
server.get('/buildings/locate', function(req, res){
const { lng, lat } = req.query;
queryBuildingsAtPoint(lng, lat).then(function(result){
res.send(result);
}).catch(function(error){
console.error(error);
res.send({error:'Database error'})
})
})
});
// Building routes
// GET buildings by reference (UPRN/TOID or other identifier)
server.get('/buildings/reference', function(req, res){
const { key, id } = req.query;
queryBuildingsByReference(key, id).then(function(result){
res.send(result);
}).catch(function(error){
console.error(error);
res.send({error:'Database error'})
})
});
// GET individual building, POST building updates
server.route('/building/:building_id.json')
.get(function (req, res) {
const { building_id } = req.params;
getBuildingById(building_id).then(function(result){
if (result) {
if (result && result.length) {
res.send(result)
} else {
res.status(404).send({error:'Not Found'})
@ -173,7 +184,6 @@ server.route('/building/:building_id.json')
)
})
// POST new user
server.post('/users', function(req, res){
const user = req.body;
@ -221,6 +231,7 @@ server.post('/login', function(req, res){
// POST user logout
server.post('/logout', function(req, res){
req.session.user_id = undefined;
req.session.destroy(function(err){
if (err) {
console.error(err);
@ -244,5 +255,4 @@ server.get('/users/me', function(req, res){
});
});
export default server;