2019-09-17 13:03:20 -04:00
|
|
|
import { strictParseInt } from "../parse";
|
2019-11-07 02:48:51 -05:00
|
|
|
|
2019-10-07 08:34:22 -04:00
|
|
|
import { DataConfig } from "./types";
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
const LAYER_QUERIES = {
|
|
|
|
base_light: `
|
2019-09-17 13:03:20 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id
|
2019-09-17 13:03:20 -04:00
|
|
|
FROM
|
2022-04-21 11:12:45 -04:00
|
|
|
buildings`,
|
2021-01-28 21:40:11 -05:00
|
|
|
base_night: `
|
2019-09-17 13:03:20 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id
|
2019-09-17 13:03:20 -04:00
|
|
|
FROM
|
2022-04-21 11:12:45 -04:00
|
|
|
buildings`,
|
2021-01-28 21:40:11 -05:00
|
|
|
number_labels:`
|
2019-09-17 13:03:20 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
|
|
|
location_number
|
2019-09-17 13:03:20 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings`,
|
|
|
|
highlight: `
|
|
|
|
SELECT
|
|
|
|
geometry_id
|
|
|
|
FROM
|
|
|
|
buildings
|
|
|
|
WHERE building_id = !@highlight!`,
|
|
|
|
date_year: `
|
2019-09-17 13:03:20 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
|
|
|
date_year
|
|
|
|
FROM
|
|
|
|
buildings
|
|
|
|
WHERE date_year IS NOT NULL`,
|
|
|
|
size_storeys: `
|
|
|
|
SELECT
|
|
|
|
geometry_id,
|
2019-09-17 13:03:20 -04:00
|
|
|
(
|
2021-01-28 21:40:11 -05:00
|
|
|
coalesce(size_storeys_attic, 0) +
|
|
|
|
coalesce(size_storeys_core, 0)
|
|
|
|
) AS size_storeys
|
2019-09-17 13:03:20 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings
|
2019-09-17 13:03:20 -04:00
|
|
|
WHERE
|
2021-01-28 21:40:11 -05:00
|
|
|
size_storeys_attic IS NOT NULL OR size_storeys_core IS NOT NULL`,
|
|
|
|
size_height: `
|
2020-01-16 09:52:10 -05:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
|
|
|
size_height_apex AS size_height
|
2020-01-16 09:52:10 -05:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings
|
|
|
|
WHERE
|
|
|
|
size_height_apex IS NOT NULL`,
|
|
|
|
construction_core_material: `
|
2020-04-01 07:18:44 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
|
|
|
construction_core_material::text AS construction_core_material
|
2020-04-01 07:18:44 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings
|
|
|
|
WHERE
|
|
|
|
construction_core_material IS NOT NULL`,
|
|
|
|
location: `
|
2019-09-17 13:03:20 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
2019-09-17 13:03:20 -04:00
|
|
|
(
|
2021-01-28 21:40:11 -05:00
|
|
|
case when location_name IS NULL then 0 else 1 end +
|
|
|
|
case when location_number IS NULL then 0 else 1 end +
|
|
|
|
case when location_street IS NULL then 0 else 1 end +
|
|
|
|
case when location_line_two IS NULL then 0 else 1 end +
|
|
|
|
case when location_town IS NULL then 0 else 1 end +
|
|
|
|
case when location_postcode IS NULL then 0 else 1 end +
|
|
|
|
case when location_latitude IS NULL then 0 else 1 end +
|
|
|
|
case when location_longitude IS NULL then 0 else 1 end +
|
|
|
|
case when ref_toid IS NULL then 0 else 1 end +
|
|
|
|
case when ref_osm_id IS NULL then 0 else 1 end
|
|
|
|
) AS location_info_count
|
2019-09-17 13:03:20 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings`,
|
|
|
|
likes: `
|
2019-09-17 13:03:20 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
|
|
|
likes_total AS likes
|
2019-09-17 13:03:20 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings
|
2019-09-17 13:03:20 -04:00
|
|
|
WHERE
|
2021-01-28 21:40:11 -05:00
|
|
|
likes_total > 0`,
|
2021-10-01 08:30:03 -04:00
|
|
|
community_local_significance_total: `
|
|
|
|
SELECT
|
|
|
|
geometry_id,
|
|
|
|
community_local_significance_total
|
|
|
|
FROM
|
|
|
|
buildings
|
|
|
|
WHERE
|
|
|
|
community_local_significance_total > 0
|
|
|
|
`,
|
|
|
|
community_in_public_ownership: `
|
|
|
|
SELECT
|
|
|
|
geometry_id,
|
|
|
|
CASE
|
|
|
|
WHEN community_public_ownership = 'Not in public/community ownership' THEN false
|
|
|
|
ELSE true
|
|
|
|
END AS in_public_ownership
|
|
|
|
FROM
|
|
|
|
buildings
|
|
|
|
WHERE
|
|
|
|
community_public_ownership IS NOT NULL
|
|
|
|
`,
|
2021-01-28 21:40:11 -05:00
|
|
|
planning_combined: `
|
2020-02-09 17:51:03 -05:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
2020-02-09 17:51:03 -05:00
|
|
|
(
|
|
|
|
CASE
|
2021-01-28 21:40:11 -05:00
|
|
|
WHEN planning_list_cat = 'Listed Building' and planning_list_grade = 'I' THEN 'Grade I Listed'
|
|
|
|
WHEN planning_list_cat = 'Listed Building' and planning_list_grade = 'II*' THEN 'Grade II* Listed'
|
|
|
|
WHEN planning_list_cat = 'Listed Building' and planning_list_grade = 'II' THEN 'Grade II Listed'
|
|
|
|
WHEN planning_in_local_list THEN 'Locally Listed'
|
2020-02-09 17:51:03 -05:00
|
|
|
ELSE 'None'
|
|
|
|
END
|
2021-01-28 21:40:11 -05:00
|
|
|
) AS listing_type,
|
|
|
|
planning_in_conservation_area
|
|
|
|
FROM buildings
|
2020-02-09 17:51:03 -05:00
|
|
|
WHERE
|
2021-01-28 21:40:11 -05:00
|
|
|
planning_in_conservation_area
|
|
|
|
OR planning_in_local_list
|
|
|
|
OR planning_list_cat IS NOT NULL`,
|
|
|
|
conservation_area: `
|
2019-09-17 13:03:20 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id
|
2019-09-17 13:03:20 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings
|
2019-09-17 13:03:20 -04:00
|
|
|
WHERE
|
2021-01-28 21:40:11 -05:00
|
|
|
planning_in_conservation_area = true`,
|
|
|
|
sust_dec: `
|
2019-10-02 08:32:00 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
|
|
|
sust_dec::text AS sust_dec
|
2019-10-02 08:32:00 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings
|
2019-10-02 08:32:00 -04:00
|
|
|
WHERE
|
2021-01-28 21:40:11 -05:00
|
|
|
sust_dec IS NOT NULL`,
|
|
|
|
building_attachment_form: `
|
2019-10-02 09:34:44 -04:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
|
|
|
building_attachment_form::text AS building_attachment_form
|
2019-10-02 09:34:44 -04:00
|
|
|
FROM
|
2021-01-28 21:40:11 -05:00
|
|
|
buildings
|
2019-10-02 09:34:44 -04:00
|
|
|
WHERE
|
2021-01-28 21:40:11 -05:00
|
|
|
building_attachment_form IS NOT NULL`,
|
|
|
|
landuse: `
|
2019-12-02 10:10:45 -05:00
|
|
|
SELECT
|
2021-01-28 21:40:11 -05:00
|
|
|
geometry_id,
|
2022-02-15 13:56:43 -05:00
|
|
|
current_landuse_order,
|
|
|
|
current_landuse_group[1] as current_landuse_group
|
2021-01-28 21:40:11 -05:00
|
|
|
FROM
|
|
|
|
buildings
|
|
|
|
WHERE
|
|
|
|
current_landuse_order IS NOT NULL`,
|
2021-03-11 14:16:37 -05:00
|
|
|
dynamics_demolished_count: `
|
|
|
|
SELECT
|
|
|
|
geometry_id,
|
2021-03-18 15:41:28 -04:00
|
|
|
jsonb_array_length(demolished_buildings) as demolished_buildings_count,
|
|
|
|
dynamics_has_demolished_buildings
|
2021-03-11 14:16:37 -05:00
|
|
|
FROM
|
|
|
|
buildings
|
2021-03-18 15:41:28 -04:00
|
|
|
WHERE jsonb_array_length(demolished_buildings) > 0 OR dynamics_has_demolished_buildings = FALSE`,
|
2019-09-17 13:03:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const GEOMETRY_FIELD = 'geometry_geom';
|
|
|
|
|
2019-10-07 12:27:22 -04:00
|
|
|
function getBuildingLayerNames() {
|
2021-01-28 21:40:11 -05:00
|
|
|
return Object.keys(LAYER_QUERIES);
|
2019-10-07 12:27:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getAllLayerNames() {
|
|
|
|
return ['highlight', ...getBuildingLayerNames()];
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
function getDataConfig(tileset: string): DataConfig {
|
|
|
|
const table = LAYER_QUERIES[tileset];
|
2019-09-17 13:03:20 -04:00
|
|
|
|
|
|
|
if(table == undefined) {
|
|
|
|
throw new Error('Invalid tileset requested');
|
|
|
|
}
|
2022-04-21 11:22:25 -04:00
|
|
|
|
2022-04-21 11:29:34 -04:00
|
|
|
const query = `(
|
|
|
|
SELECT
|
|
|
|
d.*,
|
|
|
|
g.geometry_geom
|
|
|
|
FROM (
|
|
|
|
${table}
|
|
|
|
) AS d
|
|
|
|
JOIN
|
|
|
|
geometries AS g
|
|
|
|
ON d.geometry_id = g.geometry_id
|
|
|
|
) AS data
|
|
|
|
`;
|
|
|
|
|
2022-04-21 11:22:25 -04:00
|
|
|
if(table == 'buildings') {
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2022-04-21 11:22:25 -04:00
|
|
|
const query = `(
|
|
|
|
SELECT
|
|
|
|
d.*,
|
|
|
|
g.geometry_geom
|
|
|
|
FROM (
|
|
|
|
${table}
|
|
|
|
) AS d
|
|
|
|
JOIN
|
|
|
|
geometries AS g
|
|
|
|
ON d.geometry_id = g.geometry_id
|
|
|
|
WHERE
|
|
|
|
d.latest_demolish_date IS NULL
|
|
|
|
) AS data
|
|
|
|
`;
|
|
|
|
|
|
|
|
}
|
2021-01-28 21:40:11 -05:00
|
|
|
|
2019-09-17 13:03:20 -04:00
|
|
|
return {
|
|
|
|
geometry_field: GEOMETRY_FIELD,
|
2021-01-28 21:40:11 -05:00
|
|
|
table: query
|
2019-09-17 13:03:20 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
function getLayerVariables(tileset: string, dataParams: any): object {
|
|
|
|
if(tileset == 'highlight') {
|
|
|
|
let { highlight, base } = dataParams;
|
|
|
|
|
|
|
|
highlight = strictParseInt(highlight);
|
|
|
|
base = base || 'default';
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
if(isNaN(highlight) || base.match(/^\w+$/) == undefined) {
|
|
|
|
throw new Error('Bad parameters for highlight layer');
|
|
|
|
}
|
2019-09-17 13:03:20 -04:00
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
return {
|
|
|
|
highlight,
|
|
|
|
base_data_layer: base
|
|
|
|
};
|
2019-09-17 13:03:20 -04:00
|
|
|
}
|
|
|
|
|
2021-01-28 21:40:11 -05:00
|
|
|
return {};
|
2019-09-17 13:03:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2019-10-07 12:27:22 -04:00
|
|
|
getBuildingLayerNames,
|
|
|
|
getAllLayerNames,
|
2021-01-28 21:40:11 -05:00
|
|
|
getDataConfig,
|
|
|
|
getLayerVariables
|
2019-09-17 13:03:20 -04:00
|
|
|
};
|