Skip to content

Commit

Permalink
fix(Features): Copy, Split and Stratification now use amounts instead…
Browse files Browse the repository at this point in the history
… of area
  • Loading branch information
KevSanchez committed Jan 17, 2024
1 parent 067e2ea commit 19ca467
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]},
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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,
Expand All @@ -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},
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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}
${
Expand Down

0 comments on commit 19ca467

Please sign in to comment.