Beyond Expressions: What To Do When QGIS Expressions Go Too Far And Get Too Complex?

I got to writing this after having tried grouping points in polygons with expressions for too long, even with aggregate.
so what do you do when expressions fail you? 

SQL to the rescue.

 

TL;DR Version: You can use SQL and Spatial SQL (In this case SQLite and not PostGIS functions, although most are very similar) directly on layers in QGIS.

Just long enough version: I recently re-discovered this fact (apparently, I already upvoted the answer that led me here) when trying to group and count point within polygon for an atlas table.

    What you can do for something like that, or any other workflow that feels easier for you to perform in spatial SQL, is simply open the DB Manager and scroll down to Virtual layers, what you'll find there is your project layers.

You can then open a new SQL Window where you can just write whatever query you want.

 

     A short example using Natural Earth's Countries and Airports layers. What I did was count the number of airports in each country.

SELECT c.NAME ,COUNT(a.name) n_airports

FROM Airports a, Countries_50m c

WHERE ST_Within(a.geometry, c.geometry)

GROUP BY c.name

    The same way I used ST_Within, You could use any other spatial SQL function, like transforming your data, getting its coordinates or checking relationships. I would recommend checking the superb Introduction to PostGIS free workshop which is a terrific introduction to everything SQL and spatial SQL.

    The original query I wrote gives us a table with our grouped values, we can load this table as a new virtual layer (without geometry, jsut as a table).


    We can also add the geometry for the countries to get this new layer as a new countries layer with a minimal table (only the countries name and how many airports does it have). All you have to do to load this layer (or just the table) is check the Load as new layer checkbox and click on Load.

Bonus: We can later edit our Query Layer's SQL statement by right clicking the layers name in the Layers panel and click on Edit Virtual Layer..., you can then edit the query, for example check only the countries in Africa.

 

Some helpful links:


Since it seems the virtual layers use Spatialite SQL function here are their reference:

SpatiaLite latest SQL functions reference list

The Introduction to PostGIS Workshop.

Anita Graser's Answer that led me to explore this option.


About using expressions to solve some of your spatial problems, check out 


Exploring The QGIS Expression Engine, Part 1: Getting Values From JSON & HSTORE.

Exploring The QGIS Expression Engine, Part 2: What's Missing From Select By Location

Exploring The QGIS Expression Engine, Part 3: Writing Custom Expression Functions 


Comments