[GIS] QGIS merge multiple layers and keep highest valued polygon / grid cell

mergemmqgisqgisspatial-joinspatio-temporal-data

I'm still at a beginner level at QGIS. I'm aware of MMQGIS toolbox but I still can't figure out how to solve this problem.:

I got multiple layers indicating the number of events in a grid cells within a certain time. Figure 1 (orange) shows the number events in grid cells between 1AM and 4AM and figure 2 shows events between 4AM and 9AM.

Now I want to merge these layers so that the new layer will consist out of orange and blue grid cells depending on the higher number of events in the overlaying grid cell. If the number of events in a orange grid cell is higher than in a blue grid cell then the grid cell should be orange and vice versa.

This problem sounds simple quite simple to me though. Maybe you got some hints how to approach this problem.

Figure 1:point in polygon: total number of events in a cellFigure 2:
point in polygon: total number of events in a cell

EDIT (my solution):

I did a quite long workaround with Python before where I merged all my data (which I saved in QGIS from .shp to .csv) with all timeframes (1AM-5AM, 5AM-9AM, 9AM- 1PM, 1PM-5PM etc.). I added a column with the appropriate timeframe. Then I grouped my data according to the field IDs. Finally, I calculated the maximum value within the group.

The output: I had the field ID with the highest value and the appropriate timeframe. I saved the data to CSV.

Afterwards, I merged my shapefiles with MMQGIS > Combine > Attributes Join from CSV file to add the timeframe attribute to the Field IDs. I saved to output layer and selected Categorized style with the timeframe column to set and display my values accordinly.

Best Answer

Nice answer by HowToInQGIS!

Assuming both your layers contain grid cells which align perfectly when they overlap, you could do a join using one of the following processing algorithms to merge both layers into one:

  • Join attributes table

    This will only work properly if both your grid layers contain some sort of ID field (i.e. a field containing a unique identification value for each grid cell).


  • Join attributes by location

    If the grid cells do not have a unique value to match against then you can use this algorithm provided that, as mentioned above, your grid cells align perfectly.


The output layer should hopefully contain all fields from both layers. You can then use the Field Calculator to create a new field to find which grid cell contains the higher number. The following expression adds a 0 if the number of events in the "orange_count" field (or whatever field you named it) is less than the number of events in the "blue_count" field, else it adds a 1:

if("orange_count" < "blue_count", 0, 1)

Save the edits then right-click the output layer and go to Properties > Style, select a style such as Categorised or Rule-based and set the values accordingly:

Categorised style

Related Question