Skip to content
← Back to QGIS from first project to capstone
QGIS · Part 11 of 20Intermediate· 8 min readSources

QGIS Vector Geoprocessing: Overlay, Proximity, Points in Polygon and the Graphical Modeler

Combine layers with union, intersection and difference, count points and measure lines inside polygons, run proximity and distance matrix analysis, and chain tools into a reusable model.

Data used

Download these with QuickOSM (Part 5) for Bengaluru and save them in one GeoPackage called osm_bengaluru.gpkg.

LayerKey and valueGeometry
parksleisure = parkPolygons
lakesnatural = waterPolygons
roadshighway = primaryLines
hospitalsamenity = hospitalPoints
study_areaDraw a rectangle around central BengaluruPolygon

Reproject every layer to EPSG:32643 first (Reproject Layer, Part 4). Distances and areas below assume metres.

The OSM natural = water layer holds streams and small ponds as well as lakes. Keep only the large water bodies with Extract by Expression: area($geometry) > 50000, which keeps polygons larger than 5 hectares. In the tutorial window of 26 km by 17 km around the city centre this leaves 48 lakes.

Run Vector > Geometry Tools > Check Validity on the polygon layers and Processing > Fix Geometries if errors appear. Invalid polygons are the main reason overlay tools fail.

1. The overlay tools

Overlay tools compare two layers. Find them under Vector > Geoprocessing Tools.

ToolOutput
ClipInput features cut to the shape of the overlay. Attributes come from the input only
IntersectionThe parts where both layers overlap, with attributes from both
UnionEverything from both layers, split where they overlap, with attributes from both
DifferenceThe parts of the input that are not covered by the overlay
Symmetric DifferenceThe parts covered by only one of the two layers
DissolveMerges features that share a value (single layer)

Practice: parks that are not near lakes

  1. Buffer lakes by 500 m with Dissolve result ticked, and name it lake_zone.
  2. Run Difference with parks as the input and lake_zone as the overlay. The result is the park area farther than 500 m from a lake.
  3. Run Intersection of parks and lake_zone. The result is park area within 500 m of a lake.
  4. Add a field area_ha with the Field Calculator, $area / 10000, to both outputs and compare the totals with Statistics Panel.
Parks within and beyond 500 m of lakes in two colours
Figure 11.1. Parks within and beyond 500 m of lakes in two colours

In the 26 km by 17 km window, the OSM parks have 431 ha within 500 m of a lake and 625 ha beyond it. Your numbers will differ with the window and with the OSM extract date.

Union and overlap

Union keeps all areas from both layers, so a polygon from layer A and a polygon from layer B that overlap become three pieces: A only, B only, and both. Features that came from only one layer have empty fields for the other layer, so look for NULL values when you read the table.

2. Points in polygon

Point in polygon analysis counts or summarises points inside each polygon.

  1. Create a polygon grid: Processing > Create Grid. Type: Rectangle, Extent from study_area, Horizontal and Vertical spacing 2000 m, CRS EPSG:32643.
  2. Open Processing > Count Points in Polygon. Polygons: the grid. Points: hospitals. Output field name: hospital_count.
  3. Style the grid with Graduated symbology on hospital_count.
Grid cells coloured by hospital count
Figure 11.2. Grid cells coloured by hospital count

To summarise an attribute instead of counting, use Join Attributes by Location (Summary), choose the point layer, a numeric field, and the statistics you want (sum, mean, max).

Extract by location

Vector > Research Tools > Select by Location (or Processing > Extract by Location) picks features by their relationship to another layer. Predicates include intersects, contains, within, touches and crosses. Pick the one that matches your question. "Hospitals within parks" uses within, and "parks that contain a hospital" reverses the layers.

3. Lines in polygon

  1. Open Processing > Sum Line Lengths. Lines: roads. Polygons: the grid.
  2. The output has LENGTH (the total length of road in each cell) and COUNT (how many line pieces). Divide LENGTH by the cell area to get road density.
  3. To split roads at the grid edges, run Intersection (lines first, grid second). Each piece keeps the grid cell ID.

Polygon on polygon

Run Intersection between parks and the grid to get the area of park in each cell, then Dissolve by cell ID and add $area to see park area per cell.

4. Proximity analysis

Buffers

Buffer draws a zone at a set distance. Use Multi-ring Buffer (constant distance) to draw several zones at once, for example hospitals at 500 m, 1000 m and 2000 m. Each ring gets a distance field you can use for styling.

Multi-ring buffer around hospitals
Figure 11.3. Multi-ring buffer around hospitals

Nearest feature

Processing > Join Attributes by Nearest finds the closest feature in another layer.

  1. Input layer: places_utm (from Part 4). Input layer 2: hospitals.
  2. Maximum nearest neighbors: 1.
  3. The output has the hospital's attributes and a distance field, in the layer's CRS units (metres here).

This gives straight-line distance. For travel distance along roads, use the network tools in Q16.

Distance matrix

A distance matrix lists the distance between every pair of points.

  1. Processing > Distance Matrix.
  2. Input point layer and Target point layer can be the same or different.
  3. Matrix type: choose Linear (N * k x 3) for a long table with one row per pair, or Standard (N x T) for a grid.
  4. Number of nearest points to use: 0 for all.
  5. Run and open the output table.

Distances are straight lines, measured in the layer CRS. For a geographic CRS the result is in degrees, so reproject first.

Nearest neighbour analysis

Vector > Analysis Tools > Nearest Neighbour Analysis reports the observed mean distance, the expected mean distance for a random pattern, and the ratio between them. A ratio below 1 suggests clustering and above 1 suggests dispersion. It gives a first look at the pattern, and it does not replace a proper statistical test (see Q18).

5. Build a model

The Graphical Modeler saves a series of tools as one tool you can run again.

  1. Processing > Graphical Modeler. Name it hospital_access, group Practice.
  2. Under Inputs, drag in a Vector Layer (name it Hospitals, point type) and a Number (name it Distance in metres, default 1000).
  3. Under Algorithms, drag in Buffer. Set Input layer to the Hospitals model input, and Distance to the Distance in metres input. Tick Dissolve result.
  4. Drag in Extract by Location. Features to extract from: your places layer. Where the features: intersect. By comparing to: the Buffer output. Name the output Places near hospitals.
  5. Click the save icon, then run the model (the play button).
Graphical Modeler canvas with inputs, Buffer and Extract by Location
Figure 11.4. Graphical Modeler canvas with inputs, Buffer and Extract by Location

The saved model appears in the Processing Toolbox under Models. Right-click it and choose Execute as Batch Process to run it on many layers at once.

Model tips

  • Give every input and output a clear name, because those names become the labels in the dialog.
  • Mark only the final results as outputs. Intermediate layers can be temporary.
  • Test each tool once by hand before putting it in the model.
  • Keep the CRS consistent. A model has no way of knowing that your two layers disagree.

Common mistakes

  • Running overlay tools on invalid geometry, which fails or silently drops features.
  • Measuring distance in degrees.
  • Using Union when only Intersection is needed, which multiplies features.
  • Forgetting to dissolve buffers, which double counts overlapping areas.
  • Mixing up the input and overlay layers in Difference. The result is the reverse.
  • Treating straight-line proximity as travel time.

Key takeaways