Today, I updated my QGIS Time Manager plugin to version 0.8. It now works with the QGIS 2.0 API and that means that we can take advantage of all the cool new features in our animations. The following quick example uses the “multiply” blend mode with the tweet sample data which is provided by default when you install the plugin:
(The video here is a little small. Watch it on Youtube to see the details.)
You will meet well known Keynote Speakers (random order): Jeff McKenna, Paul C. Smits, Jáchym Čepický, Schuyler Erle, Maria Antonia Brovelli, Dirk Frigne, Markus Neteler, Alyssa Wright, and Radu Puchiu.
No words for this one, just pictures! Above: What we are trying to achieve… Above: Our symbol layers Above: The roof colour is data defined A shadow Layer And a highlight layer One other hint – don’t forget to enable symbol levels!
Sextante is quickly becoming the goto geoprocessing toolbox for me. I’ve been working with Sextante 1.0.8 on QGIS 1.8 and lately I’ve started looking into Sextante 1.1 for QGIS 2. This post highlights some of the main differences between the two versions. I’m sure there are many more hidden gems I have not discovered so far.
One thing you will notice if you have used previous versions of Sextante is that the new version comes with a simplified interface which groups tools into three categories: geoalgorithms, models, and scripts. If you prefer the old style grouping by algorithm source such as GDAL, GRASS, etc. you can switch to the Advanced interface.
Let’s start with the bad news: Models created in 1.0.8 are not compatible with 1.1 since many of the algorithms have been rearranged in new categories and Sextante cannot find them by their old names anymore, e.g.
QGIS Cloud and Sourcepole are proud to be official sponsors of the team Öcher-Safari, attending the Allgäu-Orient-Rallye. One of the last adventures in the world of cars. Sourcepole serves the team with know how, infrastructure and more. Information about the team and the charity ideas of this event you can find on Öcher-Safari and the official web site of the Allgäu-Orient-Rallye.
Advanced Python field calculator is one of the numerous tools in Sextante for QGIS. It’s extremely powerful but it doesn’t use the syntax of QGIS’ default field calculator (the one you can access via attribute table). Therefore, here comes a short introduction:
The upper half of the Advanced Python field calculator is rather self-explanatory but the lower half is where it gets interesting: Code in the global expression section will be executed only once before the calculator starts iterating through all the features of the input layer. Therefore, this is the correct place to import necessary modules or to calculate variables that will be used in subsequent calculations. Code in the formula section will be evaluated for each feature in the input layer. As shown in the following example, this is where we can calculate new values, e.g. the area of the polygons in km²:
As you can see, the feature geometry can be accessed using $geom.
If you want to access an existing attribute, that’s possible using <attribute_name>.
Anyway, this is the resulting layer’s attribute table including the new areaKM2 field:
Rulers are a well-known feature in graphics programs such as Gimp and Photoshop. Now you can also find them in QGIS Print Composer. Click onto the ruler, hold the mouse key down and move the cursor to position guide lines for map feature alignment.
Of course, there’s also the handy “Snap to grid” functionality.
Today’s spotlight is on a feature which you’ll really love if you have to arrange a little more text on a print layout:
Regular labels are limited to one font, size and color. With the new “Render as HTML” option, you gain flexibility to use HTML tags to style your text by adding headers, lists and even images (note the QGIS logo I added by pointing to the image online):
After guide lines and multi-column legends, today’s focus is on
Small overview maps are used to help the reader get an idea of where the region displayed on the main map is located. In the new Print Composer, it’s simple to add such overviews: Add the main map as usual. Then add another map object to the composition an go to the “Overview” section. There, you can specify that the second map object should be an overview map for the first one: Just specify “Map 0″ in the dropdown list:
To run a script, you must add it to Script Runner using the Add Script tool on
the toolbar. Select the script from the file dialog to add it to a list in the left panel. The list of scripts
is persisted between uses of QGIS.
Running a Script
To run a script, select it from your list of scripts and click the Run tool. Output from the script will be displayed in the Script Runner console
Remove a Script
You can remove a script by selecting it from the list and clicking the Remove Script tool. This just removes it from the list; it does nothing to the script
file on disk.
Clicking the Info tool will populate
the Info and Source tabs in the panel on the right. The Info tab contains the
docstring from your module and then a list of the classes, methods, and
functions found in the script. Having a proper docstring at the head of your
script will help you determine the purpose of script.
At version 0.6 the Info tool is only needed if you have disabled automatic display of info/source (see Preferences).
Viewing the Source
You can view the source of the script on the Source tab. This allows you to
quickly confirm that you are using the right script and it does what you think
Version 0.6 implements a number of new features.
Passing Arguments to a Script
You can pass arguments to your script using keywords. Your run_script function must have two arguments:
Running your script is done using the Run script with arguments tool. This prompts you to enter your argument(s) in a key=value format:
All strings must be quoted and multiple arguments should be separated by a comma.
When your script is run, the arguments are contained in args, which is a Python dict. In the example above, you could access them like this:
All print statements in your script will be directed to the Script Runner console. In addition, any tracebacks from exceptions will be displayed there, as well as in a popup dialog.
Printing messages to the console can be useful for both development and status updates in long running scripts. You can clear the console using the Clear Console tool. There is also an option in Preferences to clear the console each time a script is run.
Logging to Disk
You can choose to log everything that goes to the output console to disk. Use the Preferences dialog to setup the directory where the scriptrunner.log will be written.
Editing a Script
You can open the selected script in an external editor by right-clicking on it and choosing Edit script in external editor from the popup menu. The default system editor for .py files will be used to open the script. In Preferences, you can specify a different editor by entering the full path to the executable.
The Preferences dialog allows you to set the following options:
Here are three script examples: a simple script that has only a
run_script function, one that uses a Python class, and one that passes keyword arguments.
This simple script contains only a run_script function:
""" Load a layer and change the fill color to red. """fromPyQt4.QtCoreimport*fromPyQt4.QtGuiimport*fromqgis.coreimport*fromqgis.guiimport*defrun_script(iface):mapreg=QgsMapLayerRegistry.instance()mapreg.removeAllMapLayers()wb=QgsVectorLayer('/data/world_borders.shp','world_borders','ogr')mapreg.instance().addMapLayer(wb)renderer=wb.rendererV2()symb=renderer.symbol()symb.setColor(QColor(Qt.red))wb.setCacheImage(None)wb.triggerRepaint()iface.refreshLegend(wb)
When executed by Script Runner, the script removes any layers currently
on the map, then loads the world_borders shapefile, sets its fill color to
red, and updates the map canvas and the legend. The
run_script function does all the work. You could expand this script by
adding additional functions that are called from run_script.
A Script with a Class
This script uses a class that is initialized from the run_script function to load some layers:
"""Load all shapefiles in a given directory. """fromglobimportglobfromosimportpathfromqgis.coreimport*fromqgis.guiimport*importqgis.utilsclassLoader:def__init__(self,iface):"""Initialize using the qgis.utils.iface object passed from the console. """self.iface=qgis.utils.ifacedefload_shapefiles(self,shp_path):"""Load all shapefiles found in shp_path"""print"Loading shapes from %s"%path.join(shp_path,"*.shp")shps=glob(path.join(shp_path,"*.shp"))forshpinshps:(shpdir,shpfile)=path.split(shp)print"Loading %s"%shpfilelyr=QgsVectorLayer(shp,shpfile,'ogr')QgsMapLayerRegistry.instance().addMapLayer(lyr)defrun_script(iface):ldr=Loader(iface)print"Loading all shapefiles in /qgis_sample_data/vmap0_shapefiles"ldr.load_shapefiles('/qgis_sample_data/vmap0_shapefiles')
In this example, the run_script function creates an instance (ldr) of a
class named Loader that is defined in the same source file. It then calls a
method in the Loader class named load_shapefiles to do something
useful—in this case, load all the shapefiles in a specified directory.
This script illustrates passing an argument (a path) to load all shapefiles in a directory:
"""Load all shapefiles in a given directory."""fromglobimportglobfromosimportpathfromqgis.coreimport*fromqgis.guiimport*importqgis.utilsclassLoader:def__init__(self,iface):"""Initialize using the qgis.utils.iface object passed from the console. """self.iface=qgis.utils.ifacedefload_shapefiles(self,shp_path):"""Load all shapefiles found in shp_path"""print"Loading shapes from %s"%path.join(shp_path,"*.shp")shps=glob(path.join(shp_path,"*.shp"))forshpinshps:(shpdir,shpfile)=path.split(shp)print"Loading %s"%shpfilelyr=QgsVectorLayer(shp,shpfile,'ogr')QgsMapLayerRegistry.instance().addMapLayer(lyr)defrun_script(iface,**args):ldr=Loader(iface)print"Loading all shapefiles in %s"%args['path']ldr.load_shapefiles(args['path'])
Lines 29 and 30 illustrate using the passed argument to print a message to the console and call the method to add the shapefiles to the map.
The script examples are minimalistic—they don’t do error checking in a number of places, such as checking to see if a QgsVectorLayer is valid or if needed arguments were actually passed to the script. Don’t take the scripts as examples of complete, solid code.
Comments on this post are welcome, as are bug reports. The link to the bug tracker for Script Runner can be found in the About box, along with the link to the code repository and my email address.
In the panel on the right, there is a new section called “Columns”. Here we can create multi-column legend layouts by specifying the desired number of columns. Add some spacing too. It will make the result look more balanced.
By default, Print Composer tries to keep all classes of one layer in one column. You can override this behavior – as I did in this example – by ticking “Split Layers”:
Another useful trick is to use text wrapping for long class labels. This option can be found in the legend’s “Main properties” right at the top of the side panel. In this example, I specified wrap on the pipe “|” symbol and inserted this symbol into the longer class names to force a line break:
This is the first post in a series dedicated solely to Print Composer in QGIS 2.0 which you can already admire in recent nightly builds.
Guide lines & snapping for user-friendly map element arrangement
Arranging map elements has never been easier: Elements can be moved as freely as before but now they will automatically try to align with other elements on the page or the page borders. Additional red guide lines help interpret the snapping behavior.
The simplicity is a little bit too much, when it comes to serve multiple WFS-T on the same server. There is only one configuration file, but we want a configuration for each service. There are solutions for that, but I never saw a documentation of the most elegant solution we found. It uses Apache rewrite capabilities to set the TINYOWS_CONFIG_FILE environment variable according to the called URL:
# URL rewriting
# Forbid direct access
RewriteRule ^/cgi-bin/.*$ - [F]
# Rewrite /xxx to /cgi-bin/tinyows with TINYOWS_CONFIG_FILE=/etc/tinyows/xxx.xml
RewriteRule ^/(.+)$ /cgi-bin/tinyows [QSA,PT,L,E=TINYOWS_CONFIG_FILE:/etc/tinyows/$1.xml]
This configuration included in a virtual host declaration (wfs.example.com) serves your WFS-T on wfs.example.com/servicename.