Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Feature/592 implement heat map on the frontend #804

Open
wants to merge 182 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
182 commits
Select commit Hold shift + click to select a range
2a5724a
#638 initial shadings endpoints
kitzbergerg Jul 22, 2023
140f98f
refactor SQL calculate_heatmap to instead call function calculate_score
kitzbergerg Jul 22, 2023
8bbc9c0
use array to store layer ids at dedicated positions
kitzbergerg Jul 22, 2023
1317e34
#638 use shadings in heatmap calculation
kitzbergerg Jul 22, 2023
e97b77a
Merge branch 'master' of github.com:ElektraInitiative/PermaplanT into…
kitzbergerg Jul 22, 2023
ec75d26
add shadings to changelog
kitzbergerg Jul 22, 2023
e8297d6
fix down.sql
kitzbergerg Jul 22, 2023
1c822f1
move shade_layer to current and add gabriel as backend implementor
kitzbergerg Jul 22, 2023
cf2d503
update SQL doc according to suggestions
kitzbergerg Jul 23, 2023
ee50d96
update SQL doc and uppercase integers
kitzbergerg Jul 23, 2023
71d1e3b
add manual test for shading with heatmap to show that it works
kitzbergerg Jul 23, 2023
99f580a
add alpha channel to PNG
kitzbergerg Jul 23, 2023
c4500cb
rename fields of score
kitzbergerg Jul 23, 2023
a570bb6
change heatmap gradient to red-green
kitzbergerg Jul 23, 2023
b8fe47a
change heatmap logging to trace
kitzbergerg Jul 23, 2023
01fbc25
change default map size back to 1ha
kitzbergerg Jul 23, 2023
07a1a05
fix down.sql
kitzbergerg Jul 23, 2023
ee23b84
Merge branch 'master' of github.com:ElektraInitiative/PermaplanT into…
kitzbergerg Jul 25, 2023
5d16ad0
set SQL type formatting to uppercase
kitzbergerg Jul 25, 2023
e6dc289
use typeshare.toml to map Polygon to ts object
kitzbergerg Jul 25, 2023
d2f8aec
changes according to suggestions, include light requirement in shade …
kitzbergerg Jul 25, 2023
20cab0e
add test for light requirement
kitzbergerg Jul 25, 2023
9435306
add heatmap test for plant relations
kitzbergerg Jul 25, 2023
d64613c
fix light requirement calculation and add test case
kitzbergerg Jul 25, 2023
d05be15
reduce influence of shading on preference
kitzbergerg Jul 25, 2023
da4b884
heatmap: add checks that layers are of correct type
kitzbergerg Jul 25, 2023
c05804c
heatmap: use sigmoid as scaling function
kitzbergerg Jul 25, 2023
6276850
Merge branch 'master' of github.com:ElektraInitiative/PermaplanT into…
kitzbergerg Jul 25, 2023
22a6855
heatmap: performance test scripts initial commit
kitzbergerg Jul 25, 2023
792edac
Merge branch 'master' into #638_shade_layer_endpoint
kitzbergerg Jul 31, 2023
6d8d969
update backend performance benchmark doc
kitzbergerg Jul 31, 2023
82dd3dd
update backend performance benchmark scripts and doc
kitzbergerg Jul 31, 2023
2578a84
use date in heatmap API
kitzbergerg Jul 31, 2023
2cd8cdd
write SQL logic for date, write tests for heatmap date query
kitzbergerg Jul 31, 2023
df86c22
Merge branch 'master' of github.com:ElektraInitiative/PermaplanT into…
kitzbergerg Jul 31, 2023
dfbcfa1
move frontend benchmarks to own folder
kitzbergerg Jul 31, 2023
a694345
fine tune shading score calculation
kitzbergerg Jul 31, 2023
a8b0079
Merge branch 'master' of github.com:ElektraInitiative/PermaplanT into…
kitzbergerg Jul 31, 2023
a80db45
execute manual test with newest commit
kitzbergerg Jul 31, 2023
aab1a33
add more data for larger map benchmark
kitzbergerg Jul 31, 2023
25656af
add benchmark scripts for relations endpoint
kitzbergerg Jul 31, 2023
ef57c22
update benchmark doc
kitzbergerg Jul 31, 2023
765263f
Merge branch 'master' of github.com:ElektraInitiative/PermaplanT into…
kitzbergerg Jul 31, 2023
cee1ebf
fix large map data insertion
kitzbergerg Aug 1, 2023
62d7ed5
calculate granularity dynamically depending on map size
kitzbergerg Aug 1, 2023
458d560
remove debug symbols for release mode
kitzbergerg Aug 1, 2023
bb1a7c8
update benchmark doc
kitzbergerg Aug 1, 2023
7e9b805
map: start implementing heatmap frontend.
Aug 3, 2023
e2b7bee
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
Aug 3, 2023
4ad98c1
Merge remote-tracking branch 'origin/#638_shade_layer_endpoint' into …
Aug 3, 2023
6cd40ad
update doc according to suggestions
kitzbergerg Aug 3, 2023
1b2690d
Merge branch 'master' of github.com:ElektraInitiative/PermaplanT into…
kitzbergerg Aug 3, 2023
bf56bbd
heatmap: fix broken image.
Aug 6, 2023
3057e6c
heatmap: set scale according to map geometry.
Aug 6, 2023
2467660
heatmap: remove unused image property.
Aug 7, 2023
23faa98
heat map: refactor component.
Aug 7, 2023
dd55dcd
heat map: refactor into different files & add tests.
Aug 7, 2023
352acf8
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
Aug 7, 2023
6d56edd
chore: add changelog.
Aug 7, 2023
92627d3
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Aug 8, 2023
dbd0162
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Aug 8, 2023
2fed5d9
Merge remote-tracking branch 'origin/#638_shade_layer_endpoint' into …
Aug 8, 2023
32e5e98
heat map: make non relevent areas transparent.
Aug 8, 2023
e575763
migrations: add shade layers for old maps
Aug 8, 2023
a22e955
migration: enable rollback of new shade layers.
Aug 9, 2023
d80614b
migration: add new line at end of file.
Aug 9, 2023
7efd82f
test: fix failing tests due to modified heat map endpoint.
Aug 9, 2023
3b26916
test: fix test failing after heat map api change.
Aug 10, 2023
16eb44c
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Aug 10, 2023
08caa84
test: fix another test failing after heat map api change.
Aug 10, 2023
0c22297
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Aug 10, 2023
43f3410
sql: include feedback by felix.
Aug 13, 2023
af76c3a
doc: add change requests by temmey.
Aug 14, 2023
1b10b33
doc: add db changes by temmey.
Aug 14, 2023
3e18f6f
db: reverse changes that might have caused tests to fail.
Aug 14, 2023
642b057
heat map: make undefined check more specific.
badnames Aug 26, 2023
0d4b292
migrations: fix typo.
badnames Aug 26, 2023
3a3b58a
Merge with current master.
badnames Dec 17, 2023
020fdd9
Fix compile errors and remove dead code in frontend.
badnames Dec 17, 2023
ad82045
Add missing files that got lost in the merge process.
badnames Dec 17, 2023
cd6494e
Display Heatmap Layer on the map.
badnames Dec 17, 2023
7e35e65
Display Heatmap Layer on the map.
badnames Dec 17, 2023
0ac815f
Remove shade layer from layer list.
badnames Dec 17, 2023
11fe830
Prevent timeout of heatmap request.
Dec 18, 2023
568db8d
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Dec 18, 2023
119eb31
Add inital components for editing shadings.
Dec 18, 2023
4da2e1d
Add additional UI components to left shade layer toolbar.
Dec 20, 2023
e9cc402
Add shading right toolbar.
Dec 20, 2023
f89ac57
Use smaller geadline in geometry tool form.
Dec 20, 2023
1bee8f8
Add map store types for shade layer.
badnames Dec 22, 2023
707cce7
Add selected shade for new shading to UntrackedShadeLayerState.
badnames Dec 22, 2023
e2006fb
Implement component for rendering a Shading on the map.
badnames Dec 22, 2023
8229323
Implement create and delete shading actions.
badnames Dec 22, 2023
fbd6bd7
Load shade layer when map is opened.
badnames Dec 22, 2023
75c9f63
Add additional facilities for adding new shadings to the map.
badnames Dec 22, 2023
cb6442d
Missing translation.
badnames Dec 22, 2023
8cac4ed
Extract duplicated status panel content into its own component.
badnames Dec 26, 2023
87c1cd0
Fix wrong mapId being used for loading the shade layer.
badnames Dec 26, 2023
f51d1e0
Fix wrong URL causing a 404 error while loading shadings.
badnames Dec 26, 2023
517b785
Add buttons for shading selection.
badnames Dec 26, 2023
3bc8a2a
Enable creation of new shades on the map.
badnames Dec 27, 2023
489e77d
Reset untracked shade layer state when another layer is selected.
badnames Dec 27, 2023
f273bae
Make individual shadings selectable.
badnames Dec 28, 2023
44abd9f
Disable object manipulation on shade layer.
badnames Dec 28, 2023
d426846
Display left shading toolbar if one or more shadings have been selected.
badnames Dec 28, 2023
0ee8afb
Enable modification of shading type.
badnames Dec 29, 2023
45f1d64
Enable adding dates to shadings.
badnames Dec 30, 2023
3901181
Add functionality for delete shading button.
badnames Dec 30, 2023
c798fa5
Only display shadings that apply to the current map date.
badnames Dec 30, 2023
936bf4e
Use react-hook-form for SelectMenu.
badnames Dec 30, 2023
72a303c
Fix shade layer left toolbar not disappearing when no shading is sele…
badnames Dec 30, 2023
77f25c3
Add missing disabled props.
badnames Dec 30, 2023
cdcfd16
Activate polygon manipulation for shadings.
badnames Jan 4, 2024
d18fc65
Prevent shadings being deselected while editing their polygon.
badnames Jan 4, 2024
accce9e
Enable move and delete operations for shading polygon points.
badnames Jan 4, 2024
0f6d0a5
Shadings can now receive additional points.
badnames Jan 4, 2024
0018e54
Make shading colors less transparent.
badnames Jan 5, 2024
8f4f3b9
Fix multiple edge cases during state transition.
badnames Jan 5, 2024
fd1ed2d
Implement left toolbar for multiple selected shadings.
badnames Jan 5, 2024
47d34e8
Fix styling issues.
badnames Jan 5, 2024
2ec4633
Update failing tests.
badnames Jan 5, 2024
746038d
Update frontend/src/config/i18n/de/shadeLayer.json
badnames Jan 8, 2024
389bdaa
Update frontend/src/config/i18n/de/shadeLayer.json
badnames Jan 8, 2024
061f3b1
Update frontend/src/config/i18n/de/shadeLayer.json
badnames Jan 8, 2024
86244d0
Update frontend/src/components/Form/SelectMenu.tsx
badnames Jan 16, 2024
ed660b7
Merge with current master.
badnames Jan 18, 2024
5478d78
Merge remote-tracking branch 'origin/1134-add-shade-layer-to-frontend…
badnames Jan 18, 2024
b12f4f7
Fix heatmap distortion after map polygon has been edited.
badnames Jan 18, 2024
7856fa1
Refactor duplicate polygon types.
badnames Jan 18, 2024
7b96a80
minor formatting changes by prettier.
badnames Jan 18, 2024
a81fcd5
Update snapshot to fix test.
badnames Jan 18, 2024
0871ac0
Rename variable referring to plantings in Shading.tsx
badnames Jan 18, 2024
d3ddd77
Use useCallback instead of the old ListenerRegister.
badnames Jan 19, 2024
81960af
Fix a bug where the same point is added to a polygon twice.
badnames Jan 19, 2024
8b4a48e
Add missing dependency in useEffect.
badnames Jan 19, 2024
c903148
Remove ListenerRegister from BaseStage.
badnames Jan 19, 2024
61b63d7
Prevent inconsistent states after deleting a shading.
badnames Jan 19, 2024
88cd06f
Add legend explaining the purpose and function of each shading type.
badnames Jan 20, 2024
6712eb8
Use status messages for different shading types.
badnames Jan 20, 2024
9710794
Deactivate all shade layer manipulations after escape was pressed.
badnames Jan 20, 2024
7fc5b81
Display an error message if the applied from date is after the applie…
badnames Jan 20, 2024
5d1d656
Prevent ghost transformers after deleting a single shading.
badnames Jan 21, 2024
4995116
Ensure that darker shadings are always on top of lighter shadings.
badnames Jan 21, 2024
9891b5f
Make new shadings hexagons octagons instead of squares.
badnames Jan 21, 2024
8f49051
Fix shading selection.
badnames Jan 29, 2024
479a47c
Remove shading from line element.
badnames Jan 29, 2024
282ad4b
Fix shading being deselected after shade value is changed.
badnames Feb 1, 2024
408c23a
Extract isShadingElementEdited into its own function.
badnames Feb 7, 2024
ba75f06
Fix shading points being inserted twice.
badnames Feb 8, 2024
c011f89
Fix various update bugs.
badnames Mar 14, 2024
e91443f
Set shading select menu to null if multiple shadings do not have a co…
badnames Mar 14, 2024
1803022
Add missing undefined type.
badnames Mar 14, 2024
43a2bcc
Fix left toolbar not being updated for shadings.
badnames Mar 14, 2024
c4e306f
Add additional refactoring hints.
badnames Mar 14, 2024
91fbf67
Add refactor hint in map store tests.
badnames Mar 15, 2024
6d5ae50
Fix transparent polygon lines.
badnames Mar 20, 2024
7f49fe8
Add first batch of shade layer actions tests.
badnames Mar 20, 2024
0a1b397
Add shade layer test verifying that objects are removed if their addD…
badnames Mar 20, 2024
aeede12
Add isReadOnlyMode condition to update functions.
badnames Mar 20, 2024
d37fcd7
Assert that loadedObjects has correct size.
badnames Mar 20, 2024
f9a8d72
Add manual shade layer tests.
badnames Mar 20, 2024
8933bde
Use correct shading legend.
Mar 27, 2024
75d52d0
Change layer order such that the plant layer is on top of the shade a…
Mar 27, 2024
4131293
Remove redundant comment.
badnames Apr 2, 2024
a74237c
Add shade layer to frontend (#1135)
markus2330 Apr 4, 2024
ed76165
Merge with current master.
Apr 12, 2024
d398f3f
Remove unused action id and user id from shading action structs.
Apr 15, 2024
bbdb987
Fix incorrect routing.
Apr 15, 2024
daaeeaa
Reintegrate shade layer with new APis.
Apr 15, 2024
009aebe
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Apr 16, 2024
2e67c68
Fix remaining integration issues with the new API.
Apr 16, 2024
9d43b6d
Fix clippy error.
Apr 16, 2024
fab392f
Fix RemoteActions.
Apr 16, 2024
267bc9f
Prevent errors when ShadeLayerActions are applied.
Apr 16, 2024
6dcdb73
Check if any value has changed before submitting an actions.
Apr 16, 2024
18a1c74
Fix shading add dates not being updated if the shade select box is em…
Apr 17, 2024
056f6d4
Fix selection rectangle vanishing after the left mouse button is rele…
Apr 17, 2024
71caabb
Fix clippy errors.
Apr 17, 2024
62bb3a4
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Apr 19, 2024
72d7a9d
Merge branch 'master' into feature/592-implement-heat-map-on-the-fron…
badnames Apr 21, 2024
7c8bedf
Fix wrong import.
Apr 22, 2024
2d44adc
Fix test compile errors.
Apr 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/migrations/2023-07-03-165000_heatmap/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- This file should undo anything in `up.sql`
DROP FUNCTION get_plant_relations;
DROP FUNCTION calculate_score_from_relations;
DROP FUNCTION calculate_score;
DROP FUNCTION scale_score;
DROP FUNCTION calculate_heatmap;
DROP TYPE SCORE;
DROP FUNCTION calculate_bbox;
238 changes: 238 additions & 0 deletions backend/migrations/2023-07-03-165000_heatmap/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
-- Your SQL goes here

-- Calculate the bounding box of the map geometry.
CREATE OR REPLACE FUNCTION calculate_bbox(map_id INTEGER)
RETURNS TABLE (x_min INTEGER, y_min INTEGER, x_max INTEGER, y_max INTEGER) AS $$
BEGIN
RETURN QUERY
SELECT
CAST(floor(ST_XMin(bbox)) AS INTEGER) AS x_min,
CAST(floor(ST_YMin(bbox)) AS INTEGER) AS y_min,
CAST(ceil(ST_XMax(bbox)) AS INTEGER) AS x_max,
CAST(ceil(ST_YMax(bbox)) AS INTEGER) AS y_max
FROM (
SELECT
ST_Envelope(geometry) AS bbox
FROM maps
WHERE id = map_id
) AS subquery;
END;
$$ LANGUAGE plpgsql;

-- The score is defined as the preference and the relevance.
CREATE TYPE score AS (
preference REAL,
relevance REAL
);

-- Returns preference from 0-1 and relevance from 0-1 for each pixel of the map.
--
-- Positions where the plant should not be placed have preference close to 0.
-- Positions where the plant should be placed have preference close to 1.
--
-- Positions where there is no relevant data have relevance close to 0.
-- Positions where there is relevant data have relevance close to 1.
--
-- The resulting matrix does not contain valid (x,y) map coordinates,
-- instead (x,y) are simply the indices in the matrix.
-- The (x,y) coordinate of the computed heatmap always starts at
-- (0,0) no matter the boundaries of the map.
-- To get valid coordinates the user would therefore need to move and scale the
-- calculated heatmap by taking into account the boundaries of the map.
--
-- View the API documentation via Swagger for additional information.
--
-- p_map_id ... map id
-- p_layer_ids ... ids of the layers
-- p_plant_id ... id of the plant for which to consider relations
-- date ... date at which to generate the heatmap
-- granularity ... resolution of the map (must be greater than 0)
-- x_min,y_min,x_max,y_max ... boundaries of the map
CREATE OR REPLACE FUNCTION calculate_heatmap(
p_map_id INTEGER,
p_layer_ids INTEGER [],
p_plant_id INTEGER,
date DATE,
granularity INTEGER,
x_min INTEGER,
y_min INTEGER,
x_max INTEGER,
y_max INTEGER
)
RETURNS TABLE (preference REAL, relevance REAL, x INTEGER, y INTEGER) AS $$
DECLARE
score SCORE;
map_geometry GEOMETRY(POLYGON, 4326);
point GEOMETRY;
bbox GEOMETRY;
num_cols INTEGER;
num_rows INTEGER;
x_pos INTEGER;
y_pos INTEGER;
plant_relation RECORD;
BEGIN
-- Makes sure the layers exists and fits to the map
FOR i IN 1..array_length(p_layer_ids, 1) LOOP
IF NOT EXISTS (SELECT 1 FROM layers WHERE id = p_layer_ids[i] AND map_id = p_map_id) THEN
RAISE EXCEPTION 'Layer with id % not found on map with id %', p_layer_ids[i], p_map_id;
END IF;
END LOOP;
-- Makes sure the plant exists
IF NOT EXISTS (SELECT 1 FROM plants WHERE id = p_plant_id) THEN
RAISE EXCEPTION 'Plant with id % not found', p_plant_id;
END IF;

-- INTO STRICT makes sure the map exists. Does have to be explicitly checked as bounding box calculation would error anyways.
SELECT geometry FROM maps WHERE id = p_map_id INTO STRICT map_geometry;

-- Calculate the number of rows and columns based on the map's size and granularity
num_cols := FLOOR((x_max - x_min) / granularity); -- Adjusted for granularity
num_rows := FLOOR((y_max - y_min) / granularity); -- Adjusted for granularity

-- Calculate the score for each point on the heatmap
FOR i IN 0..num_cols-1 LOOP
-- i and j do not represent coordinates. We need to adjust them to actual coordinates.
x_pos := x_min + (i * granularity) + (granularity / 2);

FOR j IN 0..num_rows-1 LOOP
y_pos := y_min + (j * granularity) + (granularity / 2);

-- Create a point from x_pos and y_pos
point := ST_SetSRID(ST_MakePoint(x_pos, y_pos), 4326);

-- If the point is on the map calculate a score; otherwise set score to 0.
IF ST_Intersects(point, map_geometry) THEN
score := calculate_score(p_map_id, p_layer_ids, p_plant_id, date, x_pos, y_pos);
score := scale_score(score); -- scale to be between 0 and 1
preference := score.preference;
relevance := score.relevance;
ELSE
preference := 0.0;
relevance := 0.0;
END IF;

x := i;
y := j;

RETURN NEXT;
END LOOP;
END LOOP;
END;
$$ LANGUAGE plpgsql;

-- Scales to values between 0 and 1.
--
-- Preference input space: Any value.
-- Relevance input space: >=0
CREATE OR REPLACE FUNCTION scale_score(input SCORE)
RETURNS SCORE AS $$
DECLARE
score SCORE;
BEGIN
score.preference := 1 / (1 + exp(-input.preference)); -- standard sigmoid, so that f(0)=0.5
score.relevance := (2 / (1 + exp(-input.relevance))) - 1; -- modified sigmoid, so that f(0)=0
RETURN score;
END;
$$ LANGUAGE plpgsql;

-- Calculate score for a certain position.
--
-- p_map_id ... map id
-- p_layer_ids[1] ... plant layer (only the first array-element is used by the function)
-- p_plant_id ... id of the plant for which to consider relations
-- date ... date at which to generate the heatmap
-- x_pos,y_pos ... coordinates on the map where to calculate the score
CREATE OR REPLACE FUNCTION calculate_score(
p_map_id INTEGER,
p_layer_ids INTEGER [],
p_plant_id INTEGER,
date DATE,
x_pos INTEGER,
y_pos INTEGER
)
RETURNS SCORE AS $$
DECLARE
plants SCORE;
BEGIN
plants := calculate_score_from_relations(p_layer_ids[1], p_plant_id, date, x_pos, y_pos);

RETURN plants;
END;
$$ LANGUAGE plpgsql;

-- Calculate score using the plants relations and their distances.
CREATE OR REPLACE FUNCTION calculate_score_from_relations(
p_layer_id INTEGER,
p_plant_id INTEGER,
date DATE,
x_pos INTEGER,
y_pos INTEGER
)
RETURNS SCORE AS $$
DECLARE
plant_relation RECORD;
distance REAL;
weight REAL;
score SCORE;
BEGIN
IF NOT EXISTS (SELECT 1 FROM layers WHERE id = p_layer_id AND type = 'plants') THEN
RAISE EXCEPTION 'Plant layer with id % not found', p_layer_id;
END IF;

score.preference := 0.0;
score.relevance := 0.0;

FOR plant_relation IN (SELECT * FROM get_plant_relations(p_layer_id, p_plant_id, date)) LOOP
-- calculate euclidean distance
distance := sqrt((plant_relation.x - x_pos)^2 + (plant_relation.y - y_pos)^2);

-- calculate weight based on distance
-- weight decreases between 1 and 0 based on distance
-- distance is squared so it decreases faster the further away
-- weight is halved at 50 cm away
weight := 1 / (1 + (distance / 50)^2);

-- update score based on relation
IF plant_relation.relation = 'companion' THEN
score.preference := score.preference + 0.5 * weight;
ELSE
score.preference := score.preference - 0.5 * weight;
END IF;

score.relevance := score.relevance + 0.5 * weight;
END LOOP;

RETURN score;
END;
$$ LANGUAGE plpgsql;

-- Get all relations for the plant on the specified layer.
CREATE OR REPLACE FUNCTION get_plant_relations(
p_layer_id INTEGER,
p_plant_id INTEGER,
date DATE
)
RETURNS TABLE (x INTEGER, y INTEGER, relation RELATION_TYPE) AS $$
BEGIN
RETURN QUERY
-- We only need x,y and type of relation to calculate a score.
SELECT plantings.x, plantings.y, relations.relation
FROM plantings
JOIN plants ON plantings.plant_id = plants.id
JOIN (
-- We need UNION as the relation is bidirectional.
SELECT plant1 AS plant, r1.relation
FROM relations r1
WHERE plant2 = p_plant_id
AND r1.relation != 'neutral'
UNION
SELECT plant2 AS plant, r2.relation
FROM relations r2
WHERE plant1 = p_plant_id
AND r2.relation != 'neutral'
) relations ON plants.id = relations.plant
WHERE plantings.layer_id = p_layer_id
AND (plantings.add_date IS NULL OR plantings.add_date <= date)
AND (plantings.remove_date IS NULL OR plantings.remove_date > date);
END;
$$ LANGUAGE plpgsql;
24 changes: 24 additions & 0 deletions backend/migrations/2023-07-22-110000_shadings/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- This file should undo anything in `up.sql`
DROP FUNCTION calculate_score_from_shadings;

CREATE OR REPLACE FUNCTION calculate_score(
p_map_id INTEGER,
p_layer_ids INTEGER [],
p_plant_id INTEGER,
date DATE,
x_pos INTEGER,
y_pos INTEGER
)
RETURNS SCORE AS $$
DECLARE
plants SCORE;
BEGIN
plants := calculate_score_from_relations(p_layer_ids[1], p_plant_id, date, x_pos, y_pos);

RETURN plants;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER check_shade_layer_type_before_insert_or_update ON shadings;
DROP FUNCTION check_shade_layer_type;
DROP TABLE shadings;
Loading