8

I have a layer of polygons that represent two specific zones within a city. I want to get a count of the number of parcel polygons in another layer that are within the two specific zones I am interested in. Some of the boundaries of the two layers touch each other. Is there a way to get a count of parcels in each zone in QGIS?

I've tried doing the "Join attributes by location" using the zones as the target layer and the parcels as the join layer. I used contains with a precision of 2 to try an account for the boundaries touching. I have the attribute summary as a sum and Keep all records. The outcome is a shapefile but there is no sum of the number of parcels in each zone even though I have a count column in the parcel attribute table.

Is this the best way of going about this? Is there a way I could select using an expression from either attribute table?

Here are the images of the polygons and the Join dialogue box for the options I used:

Options for Join by Location

6
  • 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
    Commented Feb 14, 2017 at 20:23
  • Thanks, I added some more detail about what I have tried or ideas.
    – Aurust
    Commented Feb 14, 2017 at 21:21
  • The tool you used seems to be adapt for your case. Did you set the "Take summary of intersecting features" option in the "Attribute summary" parameter?
    – mgri
    Commented Feb 14, 2017 at 21:23
  • Yes I did, I though this would give me the sum I specified in the summary
    – Aurust
    Commented Feb 14, 2017 at 21:40
  • Could you post an image of the polygons? And also a screenshot of the tool's dialog?
    – mgri
    Commented Feb 14, 2017 at 23:32

4 Answers 4

4

To find which parcel is located in which zone use the Intersection Tool under Vector-> Geoprocessing Tools with both layers as input layers.

The result is a layer, which contains every parcel (or part of it) which is inside the zone layer and the attribute table contains all attributes from both input layers.

Now you can either directly work with the intersection layer or split it with Vector-> Data Management Tools-> Split vector layer using the field which specifies the outer zone. Each of the new file now has directly the number of features which are located inside the corresponding zone.

1

A simple solution (or workaround if you wish) I tried is to:

  1. Create a point in each centroid of the polygons you wish to count. Processing Toolbox > Vector Geometry > Centroids
  2. Use the "count points in polygon" tool to count the total points in each polygon. Vector > Analysis Tools > Count Points in Polygon
1

An option using native QGIS expressions, for example to use within field calculator:

array_length(overlay_contains('polygon_layername_to_count',$id))

Depending on your usecase or depending on which layer you want to apply this, you can of course also use overlay_intersects(), overlay_within(), overlay_disjoint() and all the other overlay expressions.

1

I added some algorithms to the ProcessX Plugin in the past for this usecase. You can use one of the following processing tools. Both accept points, lines and polygons for both inputs. You can also choose between different geometric predicates such as contains, within, intersects, etc.

  • Count Features in Features with Condition (you do not need to use an attribute condition)

This Algorithm counts features in features with a given optional attribute condition. To count singlepoints in polygons use "Count Points in Polygons with Condition" algorithm instead - it is a lot faster for this case

  • Count Features in Features by Category

This Algorithm counts features in features (both can be Points, Lines or Polygons of any type) by a given category, evaluated either via an expression or a field. If it does not find a match, it adds a 0 as count. You can also choose one or multiple geometric predicate(s) on which features you want to count and set whether only at least one predicate must match or all of them. Additionally you may choose whether a feature should be counted only once or multiple times. You can set up the structure of the output layer yourself, the options are: Create a feature for each category: This creates a new feature for every source feature and every possible category. You will get n_source_features * n_categories = n_output_features with two fields; One indicating the name of the category and one having the count of this category. Create a field for each category: This creates one single feature for every source feature with one field for each category containing the count of this category for this feature. Warning: If the number of different categories exceed the limit of maximum fields possible, this option can crash QGIS (especially when opening the attribute table). Create a dictionary/map for all categories: This creates one single feature for every source feature with one field containing the results for this feature. The result will be a string in form of a Python dictionary with the category names as keys and the category counts as values, like {'cat_a':0,'cat_b':13,'cat_c':7}. Warning: If the length of the sum of all different category names exceeds the maximum string length, this option can crash QGIS (especially when opening the attribute table). With both options you are save if you do not have weird characters in the category names, a lot of different categories and the category names are quite short. If you are unsure, just use Create a feature for each category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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