All Mapvember OSGEO QGIS Scotland

Scotland Azimuth Orthographic Projection

Thanks to the excellent tutorial by Hamish Campbell at:

Quick Scotland centric view of the world.

QGIS Azimuth Orthographic Projections

All Mapvember OSGEO PostGIS QGIS

Centroid Within Selection in QGIS

Edit – This can now be done using a plugin: QGIS Select Within Plugin

While we have some options for spatial selection in QGIS through the Spatial Query plugin. One option that is glaringly missing is centroid within. This is extremely useful for easily selecting polygons that mainly fall within other polygons. This tutorial will run through how to do a polygon centroid within another polygon selection in QGIS.

Our initial setup is a postcode dataset, where we want to extract all of the ones that are mainly within Glasgow City Council. The boundaries are not the same but are roughly the same. However an intersect query would bring ones that simply touched the area, and a within query would exclude the ones that fall just outside. A centroid within should work great.


In this image the red lines are our postcodes, and the yellow area is the highlighted Glasgow City polygon.

We are going to cheat slightly by using SpatiaLite, which is a stand alone, single file, spatial database. It is however very tightly integrated into QGIS and we do not have to leave the program so I feel this counts as a QGIS solution.

First using one of the browser panels create a new database:


Transfer your data into the database. This can be done by dragging and dropping a .shp file into the newly created database using two browser panels.

I created a subset of the postcode dataset, with a simple polygon selection of roughly the Glasgow area (postcode_glasgow_nrs_rough). My other dataset is the UK unitary authorities dataset (district_borough_unitary_region).

Then open up the DB Manager. Database>DB Manager>DB Manager.

Once in the database we can do the query using simple SQL:

SELECT  postcode_glasgow_nrs_rough.*

FROM postcode_glasgow_nrs_rough

JOIN district_borough_unitary_region

ON ST_intersects(ST_centroid(postcode_glasgow_nrs_rough.geom),district_borough_unitary_region.geom)

WHERE LIKE "%lasgow%"

We also have a WHERE statement so only the ones that within Glasgow are selected. “%lasgow%” used to avoid capitalization mismatches.


We can also directly add this query in as a layer in QGIS using the “Load as new layer” feature. An excellent feature, and only requires you to select the primary key and geometry column. This allows us to visually check our results.

The query has worked as intended, but we have some strangely shaped polygons so the results are not what I had hoped.


We can see that one of the postcode polygons is missing from the selection because its centroid actually falls outside of itself.

Not to worry we have a better option than centroid for this query, which is ST_PointOnSurface. Details can be found on the Boundless PostGIS pages.

So lets try this.

SELECT  postcode_glasgow_nrs_rough.*

FROM postcode_glasgow_nrs_rough

JOIN district_borough_unitary_region

ON ST_intersects(ST_PointOnSurface(postcode_glasgow_nrs_rough.geom),district_borough_unitary_region.geom)

WHERE LIKE "%lasgow%"


Adding it in we see the results as expected.


So great we now have our data selected, but how do I get it out of SpatiaLite? We could wait for the “Load as new layer” to load in all the features, then save it as a shapefile, but for my query, while great for quick look, the “Load as new layer” was running quite slow and thus not an option.

So instead, we can simply create a new table in the database from our selection.

CREATE TABLE glasgow_postcode_nrs AS

SELECT  postcode_glasgow_nrs_rough.*

FROM postcode_glasgow_nrs_rough

JOIN district_borough_unitary_region

ON ST_intersects(ST_PointOnSurface(postcode_glasgow_nrs_rough.geom),district_borough_unitary_region.geom)

WHERE LIKE "%lasgow%";

Note the ; at the end. This creates a new table pretty quickly. And to get it to appear as a spatial table we simply register its geometry in the geometry column:

INSERT INTO geometry_columns VALUES ('glasgow_postcode_nrs', 'geom',6, 2, 27700, 0);

With the options being: Table name, Geometry column, Geometry (type 6 for polygon), dimensions, SRID, Spatial index boolean.

The table the appears in our browser.


And our final result.


I am loving the database integration in QGIS. It makes some workflows much easier and adds a wealth of new opportunities. Also the “Load as new layer” views are amazing, lots of possibilities.

All Mapvember OSGEO QGIS Scotland

Glasgow CAG High Density Areas

Example of the new multiple overview feature in QGIS 2.6. Point displacement, but not amazing with the zoom levels and number of points used.

Primarily looking at spacing and composition in QGIS.

Glasgow CAG

All Mapvember OSGEO QGIS

Polygon Outlines in QGIS

I had an effect of an outline around the grid in the Glasgow Commercial Property Density heatmap. I really like the effect, but to create it I had to save the grid itself as a new file, dissolve it and then symbolise it. There should be a way to do it simply through symbology. When working on some categorised symbology I though symbol levels would be the way to do it, and QGIS 2.6 actually made it possible.

I am not a huge fan of how QGIS deals with polygon boundaries, shared boundaries usually become thicker than non-shared boundaries. But we can still achieve some really good outputs.

We will start out with a simple categorised symbology.


I would like to remove the borders, but that results in very small gaps. We can tell by turning the project background black.


So I will set a narrow white border, which is very unintrusive.


Now for the QGIS 2.6 touch. We can now turn a graduated symbology into a rule based one, simply set up a graduated symbology and then select rule based and your rules will be automatically generated.


For the border we create a catch-all rule that has our desired border.


Resulting in.


But then setting up our symbol levels.


We get the desired result.


Final output.

QGIS Polygon Borders

Desired result with one dataset, excellent.

All Mapvember OSGEO QGIS

London Bus Route Density

London bus routes overlaid. Simple heatmap generated with yellow lines and a multiply blending mode in QGIS.

London Bus Heatmap

All Mapvember OSGEO QGIS Scotland

Glasgow 3D Residential Property Density QGIS2threejs

Made using the excellent QGIS2Three.js plugin.

Legend actually refers to property count rather than density.


For more information see Anita Graser’s post:

All Mapvember OSGEO QGIS

London Bus Route Maps

There are some great features in QGIS 2.6 atlas generation. You can for example filter visible polygons using an attribute from the feature controlling the atlas. So I have a full bus routes extent layer, and one with each direction. The two maps are thus filtered by the full extent layer. Data from Transport for London.

My rules look like:
regexp_substr( group,'(\\w$)’) = 1 AND regexp_substr( group,'(\\w+)’) = attribute($atlasfeature, ‘route’) AND $map = ‘map1’
regexp_substr( group,'(\\w$)’) = 2 AND regexp_substr( group,'(\\w+)’) = attribute($atlasfeature, ‘route’) AND $map = ‘map2’

Thus we can split the features into the two different maps in the composer. We can filter going and return using the “regexp_substr( group,'(\\w$)’) = 1”, since our routes are originally names 101-1 for example for the outgoing and 101-2 for the return.

The Vector Map District background map styling is thanks to Ross :
With slight modifications.

The jpgs are not as good, but Imgur provides hosting:
All London Bus Routes

All Mapvember OSGEO QGIS Scotland

Glasgow Subcrawl Map

Created using the QGIS atlas generation feature. Does not contain all pubs, only the ones that were in Open Street Map at the time of creation.

Glasgow’s (in)famous sub-crawl, a mightily booze-sodden tour of the city via its subway. Participants buy an all-day Discovery ticket (£3.80) for the world’s third-oldest underground system (London is oldest, then Budapest) and get off for a drink at the nearest pub to all 15 stations on the six-and-a-half-mile circuit. By anyone’s standards, it is a stern test of constitution, and often used as a coming-of-age ceremony for graduating students.

The Guardian

Click on the image to see all stations in the flickr album.

1 of 15 - Hillhead

All Mapvember OSGEO QGIS

QGIS Inverse Shapeburst Fills

Starting with QGIS 2.4 we have seen the introduction of both shapeburst fills and inverse fills. This allows for some great visualizations.

QGIS Inverse Shapeburst Fill

Inverse Shapeburst Fill Settings

QGIS Layer Style File (save it as a “.qml” file and load a style):






































All Leaflet Mapvember OSGEO

Trans Siberian Leaflet

This was a great learning experience. Same as the GitHub example, but now with Leaflet.


Leaflet code:

In addition we have the following in the header:

So the route is being read directly from a .geojson file. Although it has been renamed to route.js, and “var route = [” has been added to the beginning of the file and “]” to the end.