I'm using QGIS 2.12.3-Lyon.

I have a polygon layer (~130k square polygons with some small overlaps) and I have imported a road network from OpenStreetMap (via Geofabrik, ~141k road segments) for my study area. I used the field calculator to assign a numeric category to each road type. Here is the code I used:

 CASE WHEN "type"  = 'motorway' OR  "type"  = 'motorway_link' OR  "type"  =  'trunk' THEN 5
 WHEN "type"  = 'trunk_link' OR  "type"  = 'primary' OR  "type"  =  'primary_link' OR  "type" = 'secondary' THEN 4
 WHEN "type"  = 'secondary_link' OR  "type"  = 'tertiary' OR  "type"  =  'unclassified' THEN 3
 WHEN "type"  = 'tertiary_link' OR  "type"  = 'residential' OR  "type"  =  'yes' THEN 2
 WHEN "type"  = 'track' OR  "type"  = 'service' OR  "type"  =  'living_street' THEN 1

This resulted in an additional column specifying the road category. Now my goal is to check in each of my polygons (sort-of grid-cells) what is the maximum road category number from that list. I removed all columns except the road category from the roads layer. Then I converted both layers to the same CRS.

Next I went to Vector -> Data Management Tools -> Join Attributes by Location... and selected the polygon layer as Target and polygon layer as Join. Chose "take summary of intersecting features" and marked "Max". Also picked "Keep all records".

The resulting layer is a polygon layer identical to the polygon layer I had before, only with added columns of MAX and COUNT, both of which are filled entirely with NULL values.

What am I doing wrong?

  • Hi Shimi, can you check if the new 'numeric category' field type is an integer or a real (e.g. by checking if the characters are aligned to the right)? If not, I know you did it but be sure that the two layers are in the same CRS (e.g. with ogrinfo or the QGIS browser).
    – pzijd
    Feb 10, 2016 at 13:58
  • It is aligned to the left. I now made a second column using toint() of the first column, and the new column is still aligned to the left in the attibutes table. However, the Basic Statistics analysis tool does not produce mean/std/etc for the first column and does produce them for the new column. Sadly, still I'm not getting the desired result. The max column is now actually added but it is filled with NULL.
    – Shimi
    Feb 10, 2016 at 14:22
  • Regarding the CRS: I checked with QGIS browser and they are exactly the same.
    – Shimi
    Feb 10, 2016 at 14:29
  • My first comment seems to be wrong. I don't know what happened but I can no longer re-produce what I did there. With the validated numeric category (produces basic statistics on the whole set) I'm getting just a NULL "COUNT" column, as described in the OP.
    – Shimi
    Feb 10, 2016 at 15:24
  • If I was you, I would start all the steps again, beginning by the field calculator and making sure that the 'Output field type' is set to 'Whole number (integer)'.
    – pzijd
    Feb 10, 2016 at 15:46

1 Answer 1


You could try doing it a different way using the refFunctions plugin, which operates in the field calculator (can be slow for large layers though). The syntax would be something like max(geomintersects('roads_layer_name', 'road_code_field')).

  • I'm getting "error: too many features to compare"
    – Shimi
    Feb 11, 2016 at 11:03

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.