colouring-montreal/app/src/tiles/dataDefinition.ts

235 lines
6.4 KiB
TypeScript
Raw Normal View History

2019-09-17 13:03:20 -04:00
import { strictParseInt } from "../parse";
2019-11-07 02:48:51 -05:00
import { DataConfig } from "./types";
2019-09-17 13:03:20 -04:00
const LAYER_QUERIES = {
base_light: `
2019-09-17 13:03:20 -04:00
SELECT
geometry_id
2019-09-17 13:03:20 -04:00
FROM
buildings`,
base_night: `
2019-09-17 13:03:20 -04:00
SELECT
geometry_id
2019-09-17 13:03:20 -04:00
FROM
buildings`,
number_labels:`
2019-09-17 13:03:20 -04:00
SELECT
geometry_id,
location_number
2019-09-17 13:03:20 -04:00
FROM
buildings`,
highlight: `
SELECT
geometry_id
FROM
buildings
WHERE building_id = !@highlight!`,
date_year: `
2019-09-17 13:03:20 -04:00
SELECT
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
(
coalesce(size_storeys_attic, 0) +
coalesce(size_storeys_core, 0)
) AS size_storeys
2019-09-17 13:03:20 -04:00
FROM
buildings
2019-09-17 13:03:20 -04:00
WHERE
size_storeys_attic IS NOT NULL OR size_storeys_core IS NOT NULL`,
size_height: `
2020-01-16 09:52:10 -05:00
SELECT
geometry_id,
size_height_apex AS size_height
2020-01-16 09:52:10 -05:00
FROM
buildings
WHERE
size_height_apex IS NOT NULL`,
construction_core_material: `
SELECT
geometry_id,
construction_core_material::text AS construction_core_material
FROM
buildings
WHERE
construction_core_material IS NOT NULL`,
location: `
2019-09-17 13:03:20 -04:00
SELECT
geometry_id,
2019-09-17 13:03:20 -04: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
buildings`,
likes: `
2019-09-17 13:03:20 -04:00
SELECT
geometry_id,
likes_total AS likes
2019-09-17 13:03:20 -04:00
FROM
buildings
2019-09-17 13:03:20 -04:00
WHERE
likes_total > 0`,
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
`,
planning_combined: `
SELECT
geometry_id,
(
CASE
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'
ELSE 'None'
END
) AS listing_type,
planning_in_conservation_area
FROM buildings
WHERE
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
geometry_id
2019-09-17 13:03:20 -04:00
FROM
buildings
2019-09-17 13:03:20 -04:00
WHERE
planning_in_conservation_area = true`,
sust_dec: `
SELECT
geometry_id,
sust_dec::text AS sust_dec
FROM
buildings
WHERE
sust_dec IS NOT NULL`,
building_attachment_form: `
SELECT
geometry_id,
building_attachment_form::text AS building_attachment_form
FROM
buildings
WHERE
building_attachment_form IS NOT NULL`,
landuse: `
2019-12-02 10:10:45 -05:00
SELECT
geometry_id,
current_landuse_order,
current_landuse_group[1] as current_landuse_group
FROM
buildings
WHERE
current_landuse_order IS NOT NULL`,
2021-03-11 14:16:37 -05:00
dynamics_demolished_count: `
SELECT
geometry_id,
jsonb_array_length(demolished_buildings) as demolished_buildings_count,
dynamics_has_demolished_buildings
2021-03-11 14:16:37 -05:00
FROM
buildings
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';
function getBuildingLayerNames() {
return Object.keys(LAYER_QUERIES);
}
function getAllLayerNames() {
return ['highlight', ...getBuildingLayerNames()];
}
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: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
2022-04-21 11:48:35 -04:00
JOIN
buildings AS b
ON d.geometry_id = b.geometry_id
WHERE
b.latest_demolish_date IS NULL
2022-04-21 11:29:34 -04:00
) AS data
`;
2019-09-17 13:03:20 -04:00
return {
geometry_field: GEOMETRY_FIELD,
table: query
2019-09-17 13:03:20 -04: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
if(isNaN(highlight) || base.match(/^\w+$/) == undefined) {
throw new Error('Bad parameters for highlight layer');
}
2019-09-17 13:03:20 -04:00
return {
highlight,
base_data_layer: base
};
2019-09-17 13:03:20 -04:00
}
return {};
2019-09-17 13:03:20 -04:00
}
export {
getBuildingLayerNames,
getAllLayerNames,
getDataConfig,
getLayerVariables
2019-09-17 13:03:20 -04:00
};