Skip to content

Commit 20354ad

Browse files
layer-update-one-to-n-relations (#274)
* layer-update-one-to-n-relations Co-authored-by: Peter Petrik <[email protected]>
1 parent 80048db commit 20354ad

19 files changed

+40
-20
lines changed

scripts/wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ uncheck
217217
undoable
218218
url
219219
uuid
220+
UUIDs
220221
xcf
221222
xyz
222223
yml

src/layer/one-to-n-relations/index.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,72 @@ You can clone these projects to take a closer look on 1-N relations:
77
- Adding multiple photos to a single feature: <MerginMapsProject id="documentation/forms_multiple_photos" />
88
:::
99

10-
It is often the case that you have a set of spatial features and you want to record some parameters every now and then. For example, there is a GIS layer representing the manholes and the surveyors carry out regular inspections of the manholes. Instead of duplicating the manhole layer and recording each inspection, you can create a non-spatial table and store each inspection as a new row.
10+
It is often the case that you have a set of spatial features and you want to record their status every now and then. For example, there is a GIS layer representing the manholes and the surveyors carry out regular inspections of the manholes using <MainPlatformNameLink />. Instead of duplicating the manhole layer and recording each inspection as a new feature, inspections can be recorded in a non-spatial table that is linked to the spatial layer. This way, multiple records can be linked to one feature.
1111

12-
The image below shows the manholes locations:
12+
The image below shows the manhole locations and a form with listed inspections in <MobileAppName />.
1313

14-
![Manhole locations](./input_forms_many-relations_concept1.png)
14+
![Multiple inspections linked to one point in Mergin Maps Input](./input-1-n.jpg "Multiple inspections linked to one point in Mergin Maps Input")
1515

16-
The manhole point layer has the following attribute table:
16+
The manhole point layer has the following attribute table:
1717

18-
![Manhole locations](./input_forms_many-relations_concept2.png)
18+
| fid | Manhole | Manhole UUID |
19+
|:---:|:---:|:---:|
20+
| 1 | 1 | `{70c59616-492e-4757-aa9a-ee61b207ce94}` |
21+
| 2 | 2 | `{be01b98f-3585-49d4-be74-4cf3530a2989}` |
22+
| 3 | 3 | `{03178264-0070-45c8-a981-b2474627d7e0}` |
1923

20-
In a separate [non-spatial table](../working_with_nonspatial_data/), we can record the inspections:
24+
This layer contains only information about the manholes. `Manhole UUID` values are generated using [`uuid()` function as a default value](../attach-multiple-photos-to-features/) when a feature is created. This ensures that these values are **unique** even when multiple surveyors capture new features at the same time. This field will be used to link inspections and manholes.
2125

22-
![Manhole locations](./input_forms_many-relations_concept3.png)
26+
:::danger
27+
**Why UUID?** FID can be changed during [synchronisation](../../manage/synchronisation/#synchronisation). As a result, records can end up being linked to wrong features.
2328

24-
In QGIS, we are using the **Manhole ID** from the point layer and the inspection table to create a 1-N relation between the two tables.
29+
On the other hand, <QGISHelp ver="latest" link="user_manual/expressions/functions_list.html#uuid" text="UUID" /> (Universally Unique Identifier) is generated to be unique and will not be changed when synced. Therefore, we recommend always using UUID to link layers.
30+
:::
31+
32+
Inspections are recorded in a separate [non-spatial table](../working_with_nonspatial_data/) with attribute table such as:
33+
34+
| Inspection Date | Blocked? | Flooded? | Inspector | Manhole UUID |
35+
|:---:|:---:|:---:|:---:|:---:|
36+
| 10/05/2022 | | | <NoSpellcheck id="Joe Schmoe" /> | `{70c59616-492e-4757-aa9a-ee61b207ce94}` |
37+
| 10/05/2022 | | :heavy_check_mark: | <NoSpellcheck id="John Doe" /> | `{03178264-0070-45c8-a981-b2474627d7e0}` |
38+
| 12/05/2022 | :heavy_check_mark: | | <NoSpellcheck id="Fred Bloggs" /> | `{70c59616-492e-4757-aa9a-ee61b207ce94}` |
39+
| 14/05/2022 | :heavy_check_mark: | :heavy_check_mark: | <NoSpellcheck id="Joe Schmoe" /> | `{be01b98f-3585-49d4-be74-4cf3530a2989}` |
40+
41+
In this table, all information about the inspections are recorded. `Manhole UUID` is filled in automatically based on a 1-N relation that we will set up in QGIS.
2542

2643
The same principle can be used when you want to capture [multiple photos for a single feature](../attach-multiple-photos-to-features/).
2744

2845

29-
## Project setup in QGIS
46+
## How to set up 1-N relations in QGIS project
3047
You can follow this example by cloning <MerginMapsProject id="documentation/forms_one-to-many-relations" />.
3148

49+
:::tip
50+
Make sure that your survey layer has a **unique UUID** field to create the link correctly. You will find detailed steps how to set it up in [How to Attach Multiple Photos to Features](../attach-multiple-photos-to-features/)
51+
:::
52+
3253
To configure 1-N relations in QGIS:
3354
1. From the main menu, select **Projects** > **Properties ...**
3455
2. In the **Relations** tab, select **Add Relation**
35-
![relations](../attach-multiple-photos-to-features/qgis_relation_tab.png)
56+
![Layer properties in QGIS with highlighted Relations tab and Add Relation button](../attach-multiple-photos-to-features/qgis_relation_tab.png "Layer properties in QGIS with highlighted Relations tab and Add Relation button")
3657
3. A new window will appear, where we can define the parent and child layers and the fields to link the two layers:
3758
- **Name** is the name of the relation, e.g. `Inspection`
3859
- **Referenced (parent)** is the spatial layer `manhole_locations`
39-
- **Field 1** of the **Referenced (parent)** is the field `Manhole` that contains the ID
60+
- **Field 1** of the **Referenced (parent)** is the field `Manhole UUID` that contains the **unique** UUID
4061
- **Referencing (child)** is the non-spatial layer `inspections`
41-
- **Field 1** of the **Referencing (child)** layer is the `Manhole ID`, which acts as a foreign key to link inspections to spatial features.
62+
- **Field 1** of the **Referencing (child)** layer is the `Manhole UUID`, which acts as a foreign key to link inspections to spatial features
4263

43-
![1-N relations in QGIS](./input_forms_many-relations1.png)
64+
![Add Relation form in QGIS with defined 1-N relation between the parent and the child layer](./qgis-add-relation.jpg "Add Relation form in QGIS with defined 1-N relation between the parent and the child layer")
4465

4566
4. Right-click on the survey layer, select **Properties** and go to the **Attributes** form tab.
4667
5. Drag and drop the **Inspections** relation to the **Form Layout**.
68+
![Relation added to the Drag and Drop Form Layout in QGIS](./qgis-forms-relations-setup.jpg "Relation added to the Drag and Drop Form Layout in QGIS")
4769

48-
![form](./one-to-n-dnd.png)
49-
50-
This now allows you to add multiple inspections for each manhole location (**manhole_locations** point layer). The inspections records will be stored in the **inspections** table.
51-
52-
When you open the form for an existing record in the **manhole_locations** point layer, you will be able to see the existing inspection records and optionally add, delete or edit the records:
70+
Now you can add multiple inspections for each manhole location. The inspections records will be stored in the `inspections` table.
5371

54-
![1-N relations in QGIS - form view](./input_forms_many-relations2.png)
72+
When you open the form for an existing record in the `manhole_locations` point layer, it will display existing inspection records and you can also add, delete or edit the records:
73+
![Form view of a feature with 1-N relation in QGIS](./qgis-1-N-form.jpg "Form view of a feature with 1-N relation in QGIS")
5574

5675
In <MobileAppName />, the form will look like this:
5776

58-
![Many photos to a single feature](./input_forms_one-to-many.png)
77+
![Form view of a feature with 1-N relation in Mergin Maps Input](./input_forms_one-to-n.jpg "Form view of a feature with 1-N relation in Mergin Maps Input")
5978

167 KB
Loading
3.32 MB
Binary file not shown.
-50.4 KB
Binary file not shown.
-27.6 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-36.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)