Add land use classification API processing logic

This commit is contained in:
Maciej Ziarkowski 2019-12-03 13:50:24 +00:00
parent b375c34ee3
commit 3ea505359a
2 changed files with 83 additions and 3 deletions

View File

@ -92,12 +92,16 @@ async function queryBuildingsByReference(key: string, ref: string) {
} }
} }
async function getBuildingById(id: number) { async function getCurrentBuildingDataById(id: number) {
try { return db.one(
const building = await db.one(
'SELECT * FROM buildings WHERE building_id = $1', 'SELECT * FROM buildings WHERE building_id = $1',
[id] [id]
); );
}
async function getBuildingById(id: number) {
try {
const building = await getCurrentBuildingDataById(id);
building.edit_history = await getBuildingEditHistory(id); building.edit_history = await getBuildingEditHistory(id);
@ -422,6 +426,7 @@ function compare(oldObj: object, newObj: object): [object, object] {
export { export {
queryBuildingsAtPoint, queryBuildingsAtPoint,
queryBuildingsByReference, queryBuildingsByReference,
getCurrentBuildingDataById,
getBuildingById, getBuildingById,
getBuildingLikeById, getBuildingLikeById,
getBuildingEditHistory, getBuildingEditHistory,

View File

@ -0,0 +1,75 @@
import db from '../../../db';
import { getBuildingById, getCurrentBuildingDataById } from '../building';
export async function processCurrentLandUseClassifications(building: any): Promise<any> {
const updateFrom = getUpdateStartingStage(building);
const currentData = await getCurrentBuildingDataById(building.building_id);
if(updateFrom === 'class') {
building.current_landuse_group = await deriveGroupFromClass(building.current_landuse_class);
building.current_landuse_order = await deriveOrderFromGroup(building.current_landuse_group);
} else if (updateFrom === 'group') {
if(currentData.current_landuse_class == undefined) {
building.current_landuse_order = await deriveOrderFromGroup(building.current_landuse_group);
} else {
delete building.current_landuse_group;
delete building.current_landuse_order;
}
} else if (updateFrom === 'order') {
if(currentData.current_landuse_class == undefined && currentData.current_landuse_group == undefined) {
// do nothing, building.current_landuse_order is already correctly set and can be used for the update
} else {
delete building.current_landuse_order;
}
}
return building;
}
/**
* Choose which level of the land use classification hierarchy the update should start from.
* @param building
*/
function getUpdateStartingStage(building) {
if(building.hasOwnProperty('current_landuse_class')) {
return 'class';
} else if(building.hasOwnProperty('current_landuse_group')) {
return 'group';
} else if(building.hasOwnProperty('current_landuse_order')) {
return 'order';
} else return 'none';
}
function deriveGroupFromClass(classes: string[]): Promise<string[]> {
return db.many(
`
SELECT DISTINCT parent.description
FROM reference_tables.buildings_landuse_group AS parent
JOIN reference_tables.buildings_landuse_class AS child
ON child.parent_group_id = parent.landuse_id
WHERE child.description IN ($1:csv)
ORDER BY parent.description`,
[classes]
);
}
async function deriveOrderFromGroup(groups: string[]): Promise<string> {
const orders = await db.many(
`
SELECT DISTINCT parent.description
FROM reference_tables.buildings_landuse_order AS parent
JOIN reference_tables.buildings_landuse_group AS child
ON child.parent_order_id = parent.landuse_id
WHERE child.description IN ($1:csv)
ORDER BY parent.description
`,
[groups]
);
if(orders.length === 1) {
return orders[0];
} else if (orders.length > 1) {
return 'Mixed use';
} else return null;
}