From 19ca467527d2ef143fb4654fac94342e58139c7a Mon Sep 17 00:00:00 2001 From: Kevin Date: Wed, 17 Jan 2024 18:28:36 +0100 Subject: [PATCH] fix(Features): Copy, Split and Stratification now use amounts instead of area --- .../copy/copy-query.service.ts | 25 ++++++++--- .../split/split-query.service.ts | 43 +++++++------------ .../stratification-query.service.ts | 19 +++++++- 3 files changed, 52 insertions(+), 35 deletions(-) diff --git a/api/apps/api/src/modules/scenarios-features/copy/copy-query.service.ts b/api/apps/api/src/modules/scenarios-features/copy/copy-query.service.ts index b576d8e905..42dd7a37d1 100644 --- a/api/apps/api/src/modules/scenarios-features/copy/copy-query.service.ts +++ b/api/apps/api/src/modules/scenarios-features/copy/copy-query.service.ts @@ -58,6 +58,15 @@ export class CopyQuery { const isDerivedFeature = isDefined(fields.baseFeatureId); const query = ` with inserted_sfp as ( + with total_amounts as ( + select feature_id, SUM(amount) as total_amount from feature_amounts_per_planning_unit group by feature_id + ), + protected_amounts as ( + select spd.scenario_id, fappu.feature_id, SUM(fappu.amount) as protected_amount + from scenarios_pu_data spd inner join feature_amounts_per_planning_unit fappu on fappu.project_pu_id = spd.project_pu_id + where spd.lockin_status = 1 + group by spd.scenario_id, fappu.feature_id + ) insert into scenario_features_preparation as sfp (feature_class_id, api_feature_id, scenario_id, @@ -75,18 +84,22 @@ export class CopyQuery { ${fields.fpf}, ${fields.target}, ${fields.prop}, - coalesce(areas_cache.total_area, ${fields.totalArea}), - coalesce(areas_cache.current_pa, ${fields.protectedArea}), + (select total_amount from total_amounts ta where ta.feature_id = ${ + fields.featureId + }), + (select protected_amount from protected_amounts pa where pa.feature_id = ${ + fields.featureId + } and pa.scenario_id = ${fields.scenarioId}) , md5hash from features_data as fd ${joins.featurePropertiesKvJoin} ${joins.planningAreaJoin} ${joins.md5HashJoin} - left join areas_cache on areas_cache.hash = md5hash where feature_id = ${ isDerivedFeature ? fields.baseFeatureId : fields.featureId } - and st_intersects(st_makeenvelope( + --many data will be repeated + and st_intersects(st_makeenvelope(--can be removed? Alicia? ${fields.bbox[0]}, ${fields.bbox[2]}, ${fields.bbox[1]}, @@ -250,8 +263,8 @@ export class CopyQuery { get featurePropertiesKvJoin() { return (usedJoins.featurePropertiesKvJoin ??= isDefined(featureGeoOps) && baseFeatureAndKeyAreDefined - ? `inner join ( - SELECT DISTINCT feature_data_id + ? `inner join ( + SELECT DISTINCT feature_data_id FROM feature_properties_kv fpkv WHERE feature_id = ${fields.baseFeatureId} and fpkv.key = ${fields.key} diff --git a/api/apps/api/src/modules/scenarios-features/split/split-query.service.ts b/api/apps/api/src/modules/scenarios-features/split/split-query.service.ts index 23e5c29bdb..9a896ee6fb 100644 --- a/api/apps/api/src/modules/scenarios-features/split/split-query.service.ts +++ b/api/apps/api/src/modules/scenarios-features/split/split-query.service.ts @@ -35,14 +35,6 @@ export class SplitQuery { planningAreaId: isDefined(planningAreaLocation) ? `$${parameters.push(planningAreaLocation.id)}` : `NULL`, - protectedAreaIds: - protectedAreaFilterByIds.length > 0 - ? protectedAreaFilterByIds - .map((id) => `$${parameters.push(id)}`) - .join(', ') - : undefined, - protectedArea: - protectedAreaFilterByIds.length > 0 ? 'protected.area' : 'NULL', baseFeatureId: `$${parameters.push(singleSplitFeature.baseFeatureId)}`, apiFeatureId: `$${parameters.push(apiFeatureId)}`, westBbox: [ @@ -57,26 +49,10 @@ export class SplitQuery { `$${parameters.push(eastBbox[2])}`, `$${parameters.push(eastBbox[3])}`, ], - totalArea: isDefined(planningAreaLocation) - ? `st_area(st_transform(st_intersection(pa.the_geom, fd.the_geom), 3410))` - : `NULL`, }; const planningAreaJoin = isDefined(planningAreaLocation) ? `left join ${planningAreaLocation.tableName} as pa on pa.id = ${fields.planningAreaId}` : ``; - const protectedArea = fields.protectedAreaIds - ? `st_area( - st_transform( - st_intersection( - st_intersection(pa.the_geom, fd.the_geom), - ( - select st_union(wdpa.the_geom) as area - from wdpa where wdpa.id in (${fields.protectedAreaIds}) - ) - ) - ,3410) - )` - : 'NULL'; const query = ` insert into scenario_features_preparation as sfp (feature_class_id, api_feature_id, @@ -99,6 +75,15 @@ export class SplitQuery { ? `and trim('"' FROM fpkv.value::text) = trim('"' FROM ${fields.filterByValue}::text)` : `` } + ), + total_amounts as ( + select feature_id, SUM(amount) as total_amount from feature_amounts_per_planning_unit group by feature_id + ), + protected_amounts as ( + select spd.scenario_id, fappu.feature_id, SUM(fappu.amount) as protected_amount + from scenarios_pu_data spd inner join feature_amounts_per_planning_unit fappu on fappu.project_pu_id = spd.project_pu_id + where spd.lockin_status = 1 + group by spd.scenario_id, fappu.feature_id ) select fd.id, ${fields.apiFeatureId}, @@ -107,12 +92,16 @@ export class SplitQuery { ${fields.fpf}, ${fields.target}, ${fields.prop}, - ${fields.totalArea}, - ${protectedArea} + (select total_amount from total_amounts ta where ta.feature_id = ${ + fields.baseFeatureId + }), + (select protected_amount from protected_amounts pa where pa.feature_id = ${ + fields.baseFeatureId + } and pa.scenario_id = ${fields.scenarioId}) from split join features_data as fd on (split.feature_data_id = fd.id) - ${planningAreaJoin} + ${planningAreaJoin} where st_intersects(ST_MakeEnvelope(${fields.westBbox .map((coordinate) => coordinate) .join(',')}, 4326), fd.the_geom) diff --git a/api/apps/api/src/modules/scenarios-features/stratification/stratification-query.service.ts b/api/apps/api/src/modules/scenarios-features/stratification/stratification-query.service.ts index 01e0c893d5..bc448fb2d9 100644 --- a/api/apps/api/src/modules/scenarios-features/stratification/stratification-query.service.ts +++ b/api/apps/api/src/modules/scenarios-features/stratification/stratification-query.service.ts @@ -59,6 +59,8 @@ export class StratificationQuery { const hasSubSetFilter = (input.selectSubSets ?? []).length > 0; const hasSplit = isDefined(input.splitByProperty); + // @TODO Because the feature stratification was abandoned for the time being, the changes for the switch from + // area to amounts related to issue 562 could not be properly tested, but are left donehere for consistency with the other operations const query = ` with inserted_features_data as ( insert into features_data (the_geom, properties, source, feature_id) @@ -80,6 +82,15 @@ export class StratificationQuery { )}') as x( value varchar, target float8, fpf float8, prop float8 ) + ), + total_amounts as ( + select feature_id, SUM(amount) as total_amount from feature_amounts_per_planning_unit group by feature_id + ), + protected_amounts as ( + select spd.scenario_id, fappu.feature_id, SUM(fappu.amount) as protected_amount + from scenarios_pu_data spd inner join feature_amounts_per_planning_unit fappu on fappu.project_pu_id = spd.project_pu_id + where spd.lockin_status = 1 + group by spd.scenario_id, fappu.feature_id ) insert into scenario_features_preparation as sfp (feature_class_id, scenario_id, @@ -95,8 +106,12 @@ export class StratificationQuery { subsets.fpf, subsets.target, subsets.prop, - ${fields.totalArea}, - ${fields.protectedArea} + (select total_amount from total_amounts ta where ta.feature_id = ${ + fields.baseFeatureId + }), + (select protected_amount from protected_amounts pa where pa.feature_id = ${ + fields.baseFeatureId + } and pa.scenario_id = ${fields.scenarioId}) from inserted_features_data ifd ${planningAreaJoin} ${