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

Backend heatmap #566

Merged
merged 44 commits into from
Jul 16, 2023
Merged

Backend heatmap #566

merged 44 commits into from
Jul 16, 2023

Conversation

kitzbergerg
Copy link
Contributor

@kitzbergerg kitzbergerg commented Jul 3, 2023

Implementation of the backends heatmap.
Resolves #457, resolves #635, resolves #494.

Basics

  • I added a line to /doc/CHANGELOG.md
  • The PR is rebased with current master.
  • Details of what you changed are in commit messages.
  • References to issues, e.g. close #X, are in the commit messages.
  • The buildserver is happy.

Checklist

  • I have installed and I am using pre-commit hooks
  • I fully described what my PR does in the documentation
    (not in the PR description)
  • I fixed all affected documentation
  • I fixed all affected decisions
  • I added unit tests for my code
  • I added code comments, logging, and assertions as appropriate
  • I translated all strings visible to the user
  • I mentioned every code or binary not directly written or done by me in reuse syntax
  • I created left-over issues for things that are still to be done
  • Code is conforming to our Architecture
  • Code is conforming to our Guidelines
    (exceptions are documented)
  • Code is consistent to our Design Decisions

Review

  • I've tested the code
  • I've read through the whole code
  • Examples are well chosen and understandable

@markus2330
Copy link
Contributor

Great to see this! 🚀

@kitzbergerg
Copy link
Contributor Author

@Bushuo There is an error in storybook. It seems to also scan the generated definitions file which contains an example of the geometry in json. Would you mind fixing that? Probably the best solution would be to exclude the definitions.ts from storybook somehow, if that is not possible I don't really know how to handle that.

@Bushuo
Copy link
Contributor

Bushuo commented Jul 7, 2023

@Bushuo There is an error in storybook. It seems to also scan the generated definitions file which contains an example of the geometry in json. Would you mind fixing that? Probably the best solution would be to exclude the definitions.ts from storybook somehow, if that is not possible I don't really know how to handle that.

@kitzbergerg Hi, I looked at it.
I don't want to fix it, as it is quite complicated.
It can be avoided though, by putting the json code inside of backticks like {"rings":[[{"x":0.0,"y":0.0},{"x":5.0,"y":0.0},{"x":5.0,"y":5.0},{"x":0.0,"y":5.0},{"x":0.0,"y":0.0}]],"srid":4326}
This will tell mdx to treat it as a code block and thus ignore any reserved characters.

@kitzbergerg kitzbergerg marked this pull request as ready for review July 8, 2023 18:50
@kitzbergerg
Copy link
Contributor Author

kitzbergerg commented Jul 8, 2023

@markus2330 This is now finished I would say. It is a bit difficult to test as many things are not yet in place, but I will provide some scripts that can be used.

@markus2330 @Bushuo Here are a few issues/implementation details that appeared related to this:

  • I believe the (x,y) coordinates of plantings are set wrong in the frontend. Coordinates in PostGIS are (0,0) at the bottom left, so this needs to be fixed. (Done by changing the coordinate system in the backend to be (0,0) at top left.)
  • Add plant relations to scraper #606 Plant relations cannot yet be inserted into the database with the scraper (unless I missed something).
  • The polygon for the map still has to be created in the frontend. Currently it is just a hardcoded dummy implementation.
  • Implement Heat-Map on the Frontend #592 The generated map needs to be moved and scaled in the frontend.

Steps

Here are the steps I used to test the heatmap:

  1. Create the database.
  2. Insert the plants using the scraper (cd scraper && npm run insert)
  3. Start the backend.
  4. Create a map (can be done in the frontend as well).
curl --location 'http://localhost:8080/api/maps' \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
    "name": "Test1",
    "creation_date": "2023-07-06",
    "is_inactive": false,
    "zoom_factor": 100,
    "honors": 0,
    "visits": 0,
    "harvested": 0,
    "privacy": "public",
    "description": "",
    "geometry": {
        "rings": [
            [
                {
                    "x": 0.0,
                    "y": 0.0
                },
                {
                    "x": 100.0,
                    "y": 0.0
                },
                {
                    "x": 100.0,
                    "y": 100.0
                },
                {
                    "x": 50.0,
                    "y": 100.0
                },
                {
                    "x": 50.0,
                    "y": 50.0
                },
                {
                    "x": 0.0,
                    "y": 50.0
                },
                {
                    "x": 0.0,
                    "y": 0.0
                }
            ]
        ],
        "srid": 4326
    }
}'
  1. Insert some dummy relations and plantings
INSERT INTO relations
VALUES (1, 2, 'companion'),
       (1, 3, 'neutral'),
       (1, 4, 'antagonist');

INSERT INTO plantings
VALUES ('00000000-0000-0000-0000-000000000000', 2, 1, 10, 10, 0, 0, 0, 0, 0),
       ('00000000-0000-0000-0000-000000000001', 2, 2, 11, 10, 0, 0, 0, 0, 0),
       ('00000000-0000-0000-0000-000000000002', 2, 2, 23, 10, 0, 0, 0, 0, 0),
       ('00000000-0000-0000-0000-000000000003', 2, 2, 40, 10, 0, 0, 0, 0, 0),
       ('00000000-0000-0000-0000-000000000004', 2, 2, 10, 20, 0, 0, 0, 0, 0),
       ('00000000-0000-0000-0000-000000000005', 2, 3, 10, 20, 0, 0, 0, 0, 0);
  1. Execute the request (Probably better to use swagger or Postman otherwise you can't see the image).
curl --location 'http://localhost:8080/api/maps/1/layers/plants/heatmap?plant_id=1&layer_id=2' \
--header 'Authorization: Bearer <token>'

@kitzbergerg kitzbergerg requested review from markus2330 and Bushuo July 8, 2023 19:06
@Bushuo
Copy link
Contributor

Bushuo commented Jul 8, 2023

@kitzbergerg

I believe the (x,y) coordinates of plantings are set wrong in the frontend. Coordinates in PostGIS are (0,0) at the bottom left, so this needs to be fixed.

Yes, placement is indeed currently wrong. We changed the plant from a square to a circle which broke it.
It was intended though, that (x,y) is the plantings center point.

Should this be the bottom left of the planting?
I don't understand, please explain.

Copy link
Contributor

@markus2330 markus2330 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is certainly a big step forwards, great to have the endpoint. I would prefer integer used more. Documentation etc. is a bit weak. Thanks for adding the test protocol, it now worked for me but I couldn't reproduce it with self-planted plantings. Probably a bigger map is needed for that (it should be 1ha per default anyways).

About the (not previously discussed) implementation in plpgsql (we actually said it should be in Rust or directly in a SQL query using PostGIS) I am not sure. The main concern is the performance: please check it as part of your thesis.

doc/tests/protocols/MTP_2023-07-8_Heatmap_V1.md Outdated Show resolved Hide resolved
doc/tests/protocols/MTP_2023-07-8_Heatmap_V1.md Outdated Show resolved Hide resolved
doc/tests/protocols/MTP_2023-07-8_Heatmap_V1.md Outdated Show resolved Hide resolved
backend/src/test/plant_layer_heatmap.rs Outdated Show resolved Hide resolved
backend/src/test/plant_layer_heatmap.rs Show resolved Hide resolved
@@ -0,0 +1,104 @@
# Test Protocol Heatmap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link test protocol from use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what this is supposed to mean. There is no test protocol for the use case in plant layer.
Should I link to the usecase? Something like ../usecases/current/plants_layer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the use case (plants layer), there should be a link to this test protocol doc/tests/protocols/MTP_2023-07-8_Heatmap_V1.md.

@@ -127,6 +127,20 @@ export default function MapCreateForm() {
privacy: mapInput.privacy,
description: mapInput.description,
location: !Number.isNaN(mapInput.location.latitude) ? mapInput.location : undefined,
// TODO: implement selector
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please describe better what you mean here. Do you mean #494, then please link to the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this refers to #349 (#494 is a duplciate of that?).
Simply said when a map is created the user needs to specify the boundaries of the map. This boundary is then used by the heatmap (and maybe other layers like the base layer?).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a distinction between these two issues (one for backend, other for frontend).

Simply said when a map is created the user needs to specify the boundaries of the map. This boundary is then used by the heatmap (and maybe other layers like the base layer?).

Please read the use cases, the map needs to be created without the boundaries. So it must be possible to set a boundary afterwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the map boundary be part of the base layer or part of the map? As far as I understand the following means it is part of map: these boarders are stored in the map and not subjective to "alternatives" (for the base_layer use case).
If so that would mean I simply need to set the field to nullable and add an option to update the boundary?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the map boundary must be part of the map.

backend/src/model/entity/plant_layer.rs Outdated Show resolved Hide resolved
@markus2330
Copy link
Contributor

Usually the left-over issues should be created by the implementer of a PR.
I created them for this PR:

  • I believe the (x,y) coordinates of plantings are set wrong in the frontend. Coordinates in PostGIS are (0,0) at the bottom left, so this needs to be fixed. (Done by changing the coordinate system in the backend to be (0,0) at top left.)

#635

  • Plant relations cannot yet be inserted into the database with the scraper (unless I missed something).

#636

  • The polygon for the map still has to be created in the frontend. Currently it is just a hardcoded dummy implementation.

#349

  • The generated map needs to be moved and scaled in the frontend.

#637

@kitzbergerg kitzbergerg requested a review from markus2330 July 15, 2023 09:28
@markus2330
Copy link
Contributor

jenkins build please

Copy link
Member

@badnames badnames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very well done.

@markus2330 Just one question:
How often is this heatmap supposed to be updated?
As it is now, it seems to be quite resource intensive to generate.

entity::plant_layer,
},
};

/// Generates a heatmap signaling ideal locations for planting the plant.
/// The return values are raw bytes of an PNG image.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The return values are raw bytes of an PNG image.
/// The return values are raw bytes of a PNG image.

@markus2330
Copy link
Contributor

How often is this heatmap supposed to be updated?

It always gets generated freshly, as it is different for every plant and after basically every change in the map.

As it is now, it seems to be quite resource intensive to generate.

Is this just your general feeling or do have specific concerns?

@markus2330 markus2330 merged commit 7fa730d into master Jul 16, 2023
@markus2330 markus2330 deleted the backend_heatmap branch July 16, 2023 16:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backend changes in Rust code please merge please review Review by unspecified person requested
Projects
None yet
5 participants