Site menu:

Cyclocross For Roadies: How and Why to Get Started - Buy Now!

Site search


Label Only Features Intersecting with Current Atlas Feature in QGIS

If you’ve ever used the excellent Atlas functionality feature in QGIS, you know that you can make some very nice map books (‘atlases’ in QGIS parlance) with feature-driven layouts, titles, styling, etc.. This functionality is becoming more and more advanced in recent releases. Yesterday, I was working on an atlas, and wanted to label only roads and lakes that were within the currently ‘active’ atlas feature, rather than labeling all roads and lakes. With a bit of searching, I was able to find a solution that worked beautifully. I present it below in the hope that others find it useful!

In my example, I had an atlast with a ‘fade out’ so that features outside of my current atlas feature got ‘blurred’ until they became white. However, when I labelled my roads, all labels were showing, even in the otherwise white areas. This was not desirable, as you can see below:

Labels On White Background

Notice how the road labels show on the otherwise whited-out background

To fix this issue, I set a rule for the labels that looks as follows, where LABEL_EXPRESSION_FIELD should be replaced with the appropriate field name or expression that you want to use for your labels:

CASE WHEN intersects($atlasgeometry, $geometry ) THEN “LABEL_EXPRESSION_FIELD”  END

All you need to do is go into the label setting, and use the above expression in the “Label this layer with” expression field:

Label Expression

Label Settings Window showing how I set up the expression.

QGIS will then only label features that intersect (i.e. touch at some point) the current atlas feature. In my case, this gives the much more desirable results shown below. Note how the white areas no longer have labelled roads shown:

A much nicer looking map, with only roads labelled that touch the current map feature.

A much nicer looking map, with roads labelled only where they touch the current map feature.


Of course you can have a lot of fun with this doing other neat things as well. Hopefully this inspires you to create your own awesome atlases!

Create Midpoints Along Lines with OGR

Today I found myself trying to create a ‘midpoint’ on a large shapefile line layer. QGIS doesn’t have a tool to do this, and because the layer was large, I really didn’t want to mess around converting file formats to stick it into a database. However, similar to my last post about Creating Points On A Surface Using OGR, I found a quick trick (via this question on that also uses the awesome ‘-dialect’ switch in OGR. It works like this:

ogr2ogr OutputPoints.shp InputLines.shp -dialect sqlite -sql “SELECT AsText(Line_Interpolate_Point(InputLines.geometry, 0.5)),OTHER,FIELDS,CAN,GO,HERE from InputLines”

Simply replace “OutputPoints” with the desired output filename, and “InputLines” with the name of the line file you wish to create the midpoints from. You can change the “0.5″ to any number you wish – it represents how far along the line the point will fall, so if you want a midpoint, leave it at 0.5, but if you want the point 1/4 of the way along, change 0.5 to 0.25.  If don’t specify fields where I have “OTHER,FIELDS,CAN,GO,HERE” you will get only a point file with no attributes. If you include other field names from InputLines.shp (comma separated) after the closing bracket, OGR will also return those attributes from InputLines.shp.

Here’s a quick visual:

Lines With Midpoints

Lines With Midpoints

Bonus assignment: rather than making an intermediate point layer, let’s just write the lat/lon of the midpoint directly to the attribute table:

ogr2ogr OutputLines.shp InputLines.shp -dialect sqlite -sql “SELECT *, X(Line_Interpolate_Point(InputLines.geometry, 0.5)) as X, Y(Line_Interpolate_Point(InputLines.geometry, 0.5)) as Y from InputLines”


Search and Filter in Golden Cheetah v3

I put together a quick screencast showing the Search and Filter capabilities of Golden Cheetah v3. Check it out!

Golden Cheetah has a multi-function Search and Filter box in the top right-hand corner. By default, Golden Cheetah starts with the Search function enabled. This is indicated by the word Search… in the query box, and the presence of a magnifying glass icon. To switch to Filter mode, simply click the icon, and it will change to a funnel icon, and the query box will change to read Filter…. Naturally, clicking the Filter Icon will then toggle you back to Search mode.

The Search tool allows you to do a ‘free text’ search. It will search for the term(s) that you enter in any text (but not number or date) field. Use Search to find a word or words that you have entered somewhere in an activity.


  • To search for the word mountain anywhere, enter mountain
  • To search for the exact phrase “massive hill”, enter “massive hill” (notice the surrounding double quotes)
  • To search for either the word massive OR the word hill, enter massive hill (notice the lack of quotes), or, alternatively, enter massive OR hill
  • To search for both of the words massive AND hill, but not necessarily as a phrase (e.g. the phrase massive hill climb would be a valid result), enter massive AND climb
  • To find the words cyclocross and cyclocosym (or anything else starting with cyclo,) use an asterisk as a wildcard by entering cyclo*
  • A single letter wildcard is also possible, using a question mark. For example, to search for Brazil or Brasil, enter Bra?il
  • Searches are not case sensitive.
  • To escape special characters, use a \

Creating Points On a Surface using OGR

If you’ve ever had to create a point on a polygon surface, you fairly quickly realize that there’s a lack of easy tools that will perform this task. Almost every desktop GIS package will create a ‘centroid,’ but that doesn’t work when you need to find a point that’s guaranteed to fall on the surface of your polygon. Below is an example of two polygons that will work fine with a ‘centroid’ tool, and two that will cause the resulting point to fall off the surface of the polygon:

Example Polygons

Example Polygons

Running the QGIS “Polygon Centroids” tool on these polygons gives the following results:

Centroids - Notice how the top right and bottom results are incorrect.

Centroids – Notice how the top right and bottom results are incorrect.

Of course, many of the more advanced GIS packages (PostGIS, SpatiaLite, etc.) have a tool for this exact purpose. However, the problem is that often those tools are overkill, or require you to convert your data format in order to use their nifty spatial functions. What a pain!

However, there’s an easy solution, and once again, it involves the heavy-hitter OGR , which never ceases to amaze me with its functionality. One simple command will create points forced onto the polygon surface, using the smarts of SpatiaLite, but not requiring you to convert formats or load anything into a database. Here’s all you need to run, from the command line (I suggest installing OGR via OSGeo4W if you’re on Windows):

ogr2ogr OutputPoints.shp InputPolygons.shp -dialect sqlite -sql “SELECT ST_PointOnSurface(geometry) from InputPolygons”

That’s it! The result is a point for each polygon, with it ‘forced’ onto the surface.

The resulting points, forced onto the polygon surface.

The resulting points, forced onto the polygon surface.

Cool eh? Observant readers will notice that all the OGR command is doing is actually changing it’s ‘dialect’ to sqlite, which allows you to pass in any SpatiaLite query. This of course opens up many of the other awesome tools available to you, right from the command line, with any file type. Perhaps I’ll post some other neat examples soon; stay tuned!

Moving Polygons in QGIS 1.9 While Respecting Snapping Settings

Have you ever tried using the “Move Feature(s)” tool in QGIS, only to get frustrated that you cannot use it and also snap to existing features at the same time? I sure have. With a bit of research, I found a workaround. It’s simple really; select all nodes with the Node Tool, and then drag the feature. It will respect your snapping settings! Check out the video below for a demonstration!

Bookshelf 2.0 developed by