3

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.

2
  • What have you already tried?
    – MaryBeth
    Dec 30, 2016 at 16:12
  • Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq.
    – underdark
    Dec 30, 2016 at 21:11

2 Answers 2

3

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

1
  • @MichaelKollek - Most welcome, glad it was helpful :)
    – Joseph
    Jan 5, 2017 at 9:56
3

The fastest solution would be a PyQGIS script. However, I propose a working (not too short, but simple) approach for solving your issue in QGIS. I assume to start from two vectors storing the number of events in the field "attr" (below, the "orange" and "blue" sample vectors that I have created):

The following step will be taken using the Processing framework.

1) Convert the vectors to raster layers

Convert your input layers to rasters using the Rasterize tool from the Processing Toolbox (adapt the values of the parameters to your situation):

enter image description here

Please, be sure to set null as Nodata value in the Advanced parameters dialog, in order to avoid the creation of raster cells where there isn't any data:

enter image description here

For example, this will be the result for the "orange" vector:

enter image description here

2) Aggregate your data

Repeat the first step also for the "blue" vector and then run the r.series Grass algorithm from the Processing Toolbox:

enter image description here

You will obtain a raster containing the highest value in the case of overlapping cells:

enter image description here

3) Convert the resulting raster to a vector layer

Convert the resulting raster to a vector using the Polygonize tool from the Processing Toolbox. In this way, you will have a vector which merges your input layers and stores the maximum value if overlapping occurs:

enter image description here

2
  • Thank you very much for your detailed answer! I'm sure that this would be a suitable solution for me but somehow I get an error message when trying to rasterize the layers. ERROR: "Attempt to create 0x0 dataset is illegal, sizes must be larger than zero" I did some research and I think the problem is that the raster and vector were not in the same projection. So, I had to reproject my shapefile to fit the raster CRS.
    – m2thek
    Jan 4, 2017 at 18:22
  • I can't do some research by now, but I ask you if you ran the algorithm from Processing.
    – mgri
    Jan 4, 2017 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.