Step-By-Step Guide To Creating Custom Polygon Layers With Qgis Geometry Expressions

What is a Geometry Expression?

A geometry expression in QGIS is a textual definition of a geometric shape using functions and operators. They provide a flexible and powerful way to construct custom polygon vector layers without needing to create or edit the underlying geometry manually.

Geometry expressions allow users to define polygons by specifying vertices, bounding boxes, buffers, mathematical intersections, and other programmatic constructions. The output is a virtual layer that behaves like any other polygon vector data, enabling styling, analysis, and mapping automation through expressions.

Constructing a Geometry Expression

Geometry expressions use a variety of functions and syntax rules to construct polygons from points, lines, existing features, and parameters:

  • Built-in geometry functions like buffer() to create buffers, interpolate() for interpolation, intersection() to calculate intersections
  • Mathematical operators for union, difference, combining geometries
  • Geoemtry constructors like make_point(), make_line(), make_polygon()
  • References to existing map layers using attributes(), $geometry, etc
  • Parameter variables defined by the user to control geometries

For example, the expression below constructs a polygon using make_polygon() by defining points as vertices and connecting them into a bounding box:

  make_polygon( make_line( make_point(0,0), make_point(0,10),
   make_point(10,10), make_point(10,0), make_point(0,0) ) )

The intersect() function can clip polygons using the bounding box of other layers:

  intersect( $geometry, 
            buffer( geometry( get_feature( 'cities', 'name', "Chicago") ), 1) )

And buffer() is used to create buffers around input geometries:

  buffer( $geometry, 1.5)

Drawing Polygons with Expressions

To draw a custom polygon layer from expressions in QGIS:

  1. Add a new vector layer, choosing the Geometry Generator symbology type
  2. In Layer Properties > Geometry Generator, write an expression like the examples above
  3. Set the geometry type to Polygon / MultiPolygon
  4. Optionally set Layer CRS if needed, else use project CRS

The output is a polygon layer rendered according to the expression, with features available for querying and analysis.

Styling and Symbolizing Custom Polygons

Expression-based polygons can be styled using the full range of vector symbolization options in QGIS:

  • Fill colors, border lines, transparency levels through the Layer Styling panel
  • Define multiple symbol layers with offsets, gradients, shadows
  • Use markers, font symbols to enhance polygons
  • Create rule-based styling from attributes, map units, parameters
  • Build custom polygon symbols with Symbol Editor

For example, a categorized or graduated renderer can symbolize polygons based on a variable parameter controlling buffer distances or intersection areas. This enables informative styling choices.

Using Custom Polygons in Analysis

Expression polygons unlock advanced spatial analysis automation in QGIS:

  • Overlay with other layers to enrich with vector intersection statistics
  • Aggregate raster values using zonal statistics on demand
  • Model flows, least cost paths with on-the-fly buffer corridors
  • Build live viewsheds, viewshed obstruction mapping

Analysis can be embedded into expressions through geoprocessing functions. For example, clipping all visible raster pixels within a viewshed computed from a given observation point:

  intersection(
    viewshed(
      make_point( inputs.observer_x, inputs.observer_y, inputs.observer_z ), 
      raster_layer, 
      intervisibility, 
      observer_elevation
    ),
    raster_layer
  )

Example Use Cases and Code Snippets

Recreation Area Service Polygons

buffer(
  make_polygon( 
    make_line(
      map_get( recreation_points, 'vertex1' ),
      map_get( recreation_points, 'vertex2' ), 
      map_get( recreation_points, 'vertex3' ),
      map_get( recreation_points, 'vertex4' ),
      map_get( recreation_points, 'vertex1')
    )
  ),
  20 * 1000 * ( ($area/200000) ^ 0.54) 
)

Constructs recreation area service polygons by buffering known vertices proportional to point coverage density.

Border Crossing Catchments

aggregate(
  layer:='border_crossings',
  aggregate:='concatenate',
  expression:=(
    buffer(
      $geometry,
      if( 
        "highway" in (railway, motorway), 15000,
        if("highway"='primary', 7500, 5000)
      )
    )
  ), 
  filter:=intersects(geometry(@parent), $geometry) 
)

Builds variable buffers around border crossings to map catchment zones, merging overlapping zones.

Projected Sales Territories

convex_hull(
  union( 
    buffer(
      centroid( $geometry ),
      4000 + 3000 * "sales_potential"
    ),
    geometry( @parent )
  )
)  

Creates sales regions by buffering state centroid weighted by sales potential, merging and convex hull.

Limitations and Considerations

While geometry expressions unlock tremendous vector automation potential, be aware of key limitations:

  • Complex expressions with intersections, large unions can tax resources
  • Result geometry accuracy reduced for large output vertex counts
  • Outputs not directly editable without conversion to standard layer
  • Expressions only recalculated on layer refresh, not live views

Performance tuning methods:

  • Simplify initial geometries referenced in expressions
  • Use convex hulls instead of detailed unions
  • Add spatial indexes on referenced geometry attributes
  • Avoid multiple overlays in same expression

Despite caveats, geometry expressions provide a versatile vector construction kit for custom spatial analysis.

Leave a Reply

Your email address will not be published. Required fields are marked *