Related Plugins and Tags

QGIS Planet

Using a QGIS spatial index to speed up your code

If you need to do any kind of spatial operations in QGIS using Python or C++ you really want them to be as fast a possible in order reduce the amount of time you make the user wait. Lets take the simple scenario of a recent question that was asked on gis.stackexchange; Summing up values of neighboring polygons?.

I went for the SQL approach as I like how quick SQL can express what you need to do, however SQL is not the only way to skin a cat as spatialthoughts has shown in his blog post. Here Ujaval has used Python to find the neighboring polygons of each feature. Running the script on a small dataset yields results in reasonable time however running it on a larger dataset can take a long time.

In order to check if a feature touches another you need to have two features to compare against each other. The simple way to do this is to create two loops where you check each feature against every other feature. Here is a quick code example of just that.

layer = qgis.utils.iface.activeLayer()
# Select all features along with their attributes
allAttrs = layer.pendingAllAttributesList()
layer.select(allAttrs)
# Get all the features to start
allfeatures = {feature.id(): feature for (feature) in layer}

def noindex():
        for feature in allfeatures.values():
                for f in allfeatures.values():
                        touches = f.geometry().touches(feature.geometry())
                        # It doesn't matter if we don't return anything it's just an example

import timeit
print "Without Index: %s seconds " % timeit.timeit(noindex,number=1)

So the above code is pretty simple, just loop each feature and check against every other feature. No worries. No worries at least until you run this on a large dataset then I think you can see the issue here. Running the above code on a layer with around 28000 features takes 1912.41 seconds – that’s 31 minutes. Holy crap!

Note: We put all the features of the layer into a dictionary as it will make lookup quicker in the later index example.

How can we speed up the above code? Lets take a gander at QgsSpatialIndex

QgsSpatialIndex to rule them all

QgsSpatialIndex is a wrapper around the open source SpatailIndex lib and uses a RTree for an index method. If you don’t know what an index is you can think of it like the index in a book – a pointer to a location in the book rather then having to scan every page to find a word.

There isn’t much to using QgsSpatialIndex just insert each QgsFeature and it handles the rest, when we need something out we just use the intersects method to return any features inside an area.

layer = qgis.utils.iface.activeLayer()
# Select all features along with their attributes
allAttrs = layer.pendingAllAttributesList()
layer.select(allAttrs)
# Get all the features to start
allfeatures = {feature.id(): feature for (feature) in layer}

def withindex():
        # Build the spatial index for faster lookup.
        index = QgsSpatialIndex()
        for f in allfeatures.values():
                index.insertFeature(f)

        # Loop each feature in the layer again and get only the features that are going to touch.
        for feature in allfeatures.values():
          # Get the ids of all the features in the index that are within
          # the bounding box of the current feature because these are the ones
          # that will be touching.
          ids = index.intersects(feature.geometry().boundingBox())
          for id in ids:
            f = allfeatures[id]
            if f == feature: continue
            touches = f.geometry().touches(feature.geometry())
            # It doesn't matter if we don't return anything it's just an example

import timeit
print "With Index: %s seconds " % timeit.timeit(withindex,number=1)

Running this code on our 28000 feature layer returns the results in 10 seconds. 31 minutes down to 10 seconds by just using a spatial index. Nice!

So the next time you need to do some spatial operations remember to use the handy QgsSpatialIndex in order to speed up your code. If you don’t want to use QgsSpatialIndex, or need some more flexiblity, you could even use the Python RTree module.

Full code

layer = qgis.utils.iface.activeLayer()

# Select all features along with their attributes
allAttrs = layer.pendingAllAttributesList()
layer.select(allAttrs)
# Get all the features to start
allfeatures = {feature.id(): feature for (feature) in layer}

def noindex():
	for feature in allfeatures.values():
		for f in allfeatures.values():
			touches = f.geometry().touches(feature.geometry())
			# It doesn't matter if we don't return anything it's just an example

def withindex():
	# Build the spatial index for faster lookup.
	index = QgsSpatialIndex()
	map(index.insertFeature, allfeatures.values())

	# Loop each feature in the layer again and get only the features that are going to touch.
	for feature in allfeatures.values():
	  ids = index.intersects(feature.geometry().boundingBox())
	  for id in ids:
	    f = allfeatures[id]
	    touches = f.geometry().touches(feature.geometry())
	    # It doesn't matter if we don't return anything it's just an example

import timeit
print "With Index: %s seconds " % timeit.timeit(withindex,number=1)
print "Without Index: %s seconds " % timeit.timeit(noindex,number=1)


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, qgis, Quantum GIS, spatial operations

Installing Python setuptools into OSGeo4W Python

The easiest way install Python libraries is to use easy_install and pip.  easy_install and pip are package managers for Python. From the easy_install page:

Easy Install is a python module (easy_install) bundled with setuptools that lets you automatically download, build, install, and manage Python packages.

and from the pip page:

pip is a tool for installing and managing Python packages, such as those found in the Python Package Index. It’s a replacement for easy_install.

To get easy_install you need to install Python setuptools and you are good to go. Sounds easy!  However the setuptools installer assumes that you have the normal standalone Python installed which writes it’s install location to the registry, and when you run the installer it will say that it can’t find Python on the system.  What the!?

If you have installed QGIS, or any other tool from the OSGeo4W install, you will see that OSGeo4W bundles its own version of Python in: C:\OSGeo4W\apps\python27. This is the Python that is used when calling python in the OSGeo4W shell.  It seems someone on the OSGeo wiki has made a bootstrapped installer for setuptools that will install setuptools and easy_install into the  C:\OSGeo4W\apps\python27 folder for you.

Steps to take

  • Download ez_setup.py
  • Run python ez_setup.py in your OSGeo4W shell
  • Done!

To install a package with easy_install just use:

easy_install {packagename}

I wanted to have bottle and flask installed:

easy_install bottle

which gives you something like:

Searching for bottle
Reading http://pypi.python.org/simple/bottle/
Reading http://bottle.paws.de/
Reading http://github.com/defnull/bottle
Reading http://bottlepy.org/
Best match: bottle 0.11.4
Downloading http://pypi.python.org/packages/source/b/bottle/bottle-0.11.4.tar.gz#md5=f767c340de0b7c9581917c48e609479b
Processing bottle-0.11.4.tar.gz
Running bottle-0.11.4\setup.py -q bdist_egg –dist-dir c:\users\woo\appdata\local\temp\easy_install-5b4qq6\bottle-0.11.4\egg-dist-tmp-q2yd68
zip_safe flag not set; analyzing archive contents…
bottle: module references __file__
bottle: module references __path__
Adding bottle 0.11.4 to easy-install.pth file
Installing bottle.py script to C:\OSGeo4W\apps\Python27\Scripts
Installed c:\osgeo4w\apps\python27\lib\site-packages\bottle-0.11.4-py2.7.egg
Processing dependencies for bottle
Finished processing dependencies for bottle

Install pip

Note

Most of the time any Python packages that are needed by your OSGeo4W tools are bundled in the installer and can be downloaded using the OSGeo4W installer, however there have been cases when I wanted to install a non OSGeo4W package into my setup by using easy_install or pip. Like bottle and flask in the example above.


Filed under: Open Source Tagged: FOSSGIS, osgeo, python, qgis, Quantum GIS

Five new awesomely awesome QGIS features – Round 2

As QGIS is such a fast moving project I have decided to make this a regular blog post in order to highlight some new features added to QGIS. If you haven’t already, don’t forget to check out round one.

Remember that some of these features may still only be new which might change between now and the next official released version. With that out of the way lets get listing.

Atlas integration

If you are a regular user of QGIS Python plugins, and who isn’t, then you would have used the awesome Atlas plugin developed by Vincent Picavet. This great tool can be used to generate mapbooks, or an atlas as some people like to say, using a coverage layer and a print composer. What makes this even more awesome is that it is now built into the print composer.

Atlas composer intergration

The builtin atlas function also gives you the ability to use an expression to do runtime text replacement, including access to all the fields on the coverage layer.  The coverage layer doesn’t even have to be a region layer, it can be a simple point layer, or even a line layer.   You can see the result of me running the atlas generation from the above example here

Big thanks to Oslandia for integrating this great feature, and the companies sponsoring the work.

New Python console

This new addition comes from the great work that Salvatore Larosa has been doing to add a better Python console to QGIS.

The new Python console includes attribute auto complete, syntax highlighting, better copy and paste, uploading to codepad, the ability to run code from a file, etc.  You don’t realise how much difference there is until you go back to using the old one in version 1.8.

New Python console

Tabbed and groups in builtin forms

One of the things I really loved about QGIS, coming from MapInfo, was the builtin forms.  Just having the ability to enter data using controls like combo boxes, calendar widgets, etc makes you one step closer to having better data.   This feature is the exact reason I setup a 67 year old for kerb data collection.

As good as they are the builtin forms have an issue of ending up with as a big scrolling list with lots of fields; also the lack of  the ability to group or put fields on tabs in the UI meant you had to create a custom form.  Well not any more.

There is now a combo box on the Fields tab that allows you to build a generated form but also add tabs and group boxes.  You can even have the same field shown more then once on the form, handy for something like an ID field that you would like to show on each tab.

With this new ability the builtin forms can get me 95% of the way for data entry jobs, the other 5% I just make a custom form – but that is very rare.

Sextante

Sextante is a great and powerful analytical framework that has been added to the core of QGIS thanks to Victor Olaya.  This is not a feature that I use a lot but this is only due to most of my work being in development and not analysis, however that doesn’t mean that it’s not a really cool feature.

One of the greatest things about the Sextante toolbox is that it allows you to integrate other great open source tools like GRASS, SAGA, R, OTB, etc, right into your QGIS workflow and view the results in the canvas. It even includes a modeller so that you can build a connected diagram of all the bits of your process, even if it crosses between programs.

The toolbox

For me what is even better is that you can use Sextante in your plugins or custom Python code.  Sextante has a Python interface – well the whole thing is written in Python – that you can use to run a Sextante supported algorithm.

import sextante
outputs_0=sextante.runalg("grass:v.crossbones", /file, 0, ,, 1, 2, 3, 4, 1=3.0,2=8.0,5=6.0,8=6.0,11=6.0,14=6.0, None)
outputs_1=sextante.runalg("grass:v.delaunay", outputs_0['output'], True, True, None)
outputs_2=sextante.runalg("grass:v.dissolve", outputs_0['output'], , None)

Victor has created a blog to cover some Sextante recipes at QGIS-SEXTANTE cookbook/. There are also some really cool example of Sextante in use at:

Massive amount of composer additions

This last feature, or rather feature set,  comes from the sponsorship and support of the World BankAustralia-Indonesia Facility for Disaster Reduction, Geoscience Australia and the GFDRR.  Most of this work was done to aid in the development of a new QGIS plugin called inaSAFE, which has also received some great praise

“(InaSAFE) is very beneficial for all of us. It’s a good example of our partnership.”
Dr Susilo Bambang Yudhoyono – President of Indonesia

Some of the improvements include:

  • A new loadFromTemplate method, in the API, that can do text replacement in the QGIS composer template.
  • Better support for EPSG:4326 scale bars
  • Multipage in one composer
  • Automatic overview frame in map frame
  • HTML frame
  • Zebra style grid frame border
  • More control of grid labels. Inside, outside, disabled
  • etc, etc

These are great additions to the QGIS composer and I have already used the overview frame feature along with the new atlas integration  to make some quick nice looking map books. \

A huge thanks to the World BankAustralia-Indonesia Facility for Disaster ReductionGeoscience Australia and the GFDRR, and all the developers included.

You can see some of the output that InaSAFE generates using some of these new features at http://quake.linfiniti.com/

P.S The World Bank also sponsored the new raster Save As.. ability just like we have for vector layers.

 


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, osgeo, qgis, Quantum GIS

FOSS4G-AU in summary

Last Thursday and Friday was our first local Australian FOSS4G event which was hosted at the CSRIO building in Brisbane.  Very big thanks to CSRIO for hosting the event.  The venue was setup perfectly for  hosting an event like this, including dual projectors for presenting, video calls over to Perth, etc.

The first day was done using a un-conference style of event. This is the first time I’ve been to a un-conference and I liked the format a lot.   Once everyone was there on the first morning we collected ideas from people and everyone voted on which ones they would like to see.  After we had picked enough topics Shaun and I made a program for the day and we started.

Topics included:

Me presenting QMap

The second day was a code sprint.  I worked on converting a MapBasic scripts from one of the guys to QGIS, and Jody enlisted the others to help check the headers of the GeoServer project so that it can finally pass OSGeo incubation.

Overall I think it was a very successful event.  I would like to make these a yearly event if we can, provided that we have people to talk, or projects to work on.

More information about up coming OSGeo events in Australia and New Zealand can be found at http://www.meetup.com/osgeo-aust-nz/


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, open source gis, osgeo, qgis, Quantum GIS

Australian FOSS4G is here

So that came around quick.  Seems it is November already.

Tomorrow is the FOSS4G-Au a locally organized un-conference.

How do I register?

If you would still like to come – which you should – you can register here (it’s free!) http://www.meetup.com/osgeo-aust-nz/events/83965312/ but you must RSVP on the meetup page in order to get building access and internet access.

Where is it at? (Directions)

The un-conference is held at The Queensland Centre for Advanced Technologies in Brisbane. Kudos to CSIRO for allowing us to use their facilities.

More details on un-conference?

Jody Garnett has also done a post on the un-conference and what this kind of conference style is like.  You can find more details here: http://how2map.blogspot.com.au/2012/11/foss4g-au-tomorrow.html 

The venue

 


Filed under: Open Source, qgis Tagged: foss4g, FOSSGIS, Open Source, OSS, qgis

User defined expression functions for QGIS

Ever since I added expression based labels, including the new expression builder UI, something that I always wanted to add is the ability to define custom user defined functions in Python (or C++) and use them in an expression. The expression engine is used for labels, rule based rendering, layer actions, field calculator, and atlas composer tags.  Thanks to the all the awesome work on the expression engine by Martin all this cool stuff is now possible.

Today I pushed a commit into master that adds the ability to define a function in Python (or C++), register it in the expression engine, then use it anywhere expressions are used.

The good stuff

Lets take a use case from Ujaval Gandhi and his example of counting vertices for each feature.

First we need to import the new qgsfunction decorator function from qgis.utils. The qgsfunction decorator will take a normal Python function, wrap it up in the class used to define a function, and register it in the engine.

So what does an empty function look like:

from qgis.utils import qgsfunction
from qgis.core import QGis

@qgsfunction(0, "Python")
def vertices(values, feature, parent):
	pass

@qgsfunction(0, "Python") means we are defining a new vertices function that takes 0 args and lives in the “python” group in the expression builder UI. Any custom function must take (values, feature, parent) as python args. values is a list of QVariants passed into the function, feature is the current QgsFeature, and parent is expression engine node (you use this to raise errors).

Lets stick some more logic in there:

from qgis.utils import qgsfunction
from qgis.core import QGis

@qgsfunction(0, "Python")
def vertices(values, feature, parent):
	"""
		Returns the number of vertices for a features geometry
	"""
	count = None
	geom = feature.geometry()
	if geom is None: return None
	if geom.type() == QGis.Polygon:
		count = 0
		if geom.isMultipart():
		  polygons = geom.asMultiPolygon()
		else:
		  polygons = [ geom.asPolygon() ]
		for polygon in polygons:
		  for ring in polygon:
		    count += len(ring)
	return count

Pretty simple. Get the geometry from the feature, check if it’s a polygon, if it is then count the number of vertices and return that number.

Now that we have that all done we can save it into a file in our .qgis/python folder, lets call it userfunctions.py (note you don’t have to save it here, anywhere that QGIS can find it will do.  Anywhere on PATH)

Lets open QGIS and run import userfunctions.py:

Importing functions from userfunctions.py

Now open the label properties for the layer:

The new function shown in the expression builder

Nice! Notice also that the function doc string is used as the function help. How cool is that.  You can also see the $ sign in front of the function, this is because any functions that take no args are considered special and use the $ sign as a convention, this is all automatic when the function is registered.

And the result is:

The label using the new function

You can even use it in the rule based rendering:

Rule rendering using new function

Enjoy!

Notes

  • You must unregister a function once you are finished with it using QgsExpression.unregisterFunction(name). This mainly applies to plugins where the user might unload your plugin and the code is no longer available. In the above example we could import userfunctions and never unregister because we plan on using it for the whole session.
  • You can’t override the built-in methods.

Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, OSS, qgis, Quantum GIS

FOSS4G-AU un-conference

Another quick announcement for today. The local FOSS4G-AU chapter is holding un-conference and code sprint in Brisbane on the 15 and 16th of November 2012.

Ben Caradoc-Davies made the announcement on the mailing list the other day:

The Free and Open Source Software for Geospatial Australia (FOSS4G-AU)
2012 Unconference and Code Sprint will be held at CSIRO’s Queensland
Centre for Advanced Technologies (QCAT), Pullenvale QLD.
15 November: Unconference
16 November: Code Sprint
This is a participant-organised event: please add your unconference and code sprint topic suggestions to the wiki. Organisers are welcome; please speak up.

See here for details: http://wiki.osgeo.org/wiki/FOSS4G-AU_2012

RSVP essential for building access. Please sign up here:
http://www.meetup.com/osgeo-aust-nz/events/83965312/

Email: [email protected] (sign up here http://lists.osgeo.org/mailman/listinfo/aust-na)

Kind regards,
Ben Caradoc-Davies
Software Engineer
CSIRO Earth Science and Resource Engineering
Australian Resources Research Centre

Being part of osgeo-aust-nz and the local Australian open source GIS movement it’s great to see events like this being organized.

I’ll be there, most likely pimping some QGIS stuff.

Hope to see you there.

Just remember that RSVP is essential to get into the building.


Filed under: Open Source Tagged: foss4g, FOSSGIS, osgeo, qgis

QGIS Training in Brisbane (and other places in Australia)

Just a quick post to let everyone know that DMS will be running QGIS training on the 20 Nov 2012 to the 21 Nov 2012 in Brisbane, QLD. More information can be found at http://mapsolutions.com.au/training/quantum-gis/introduction-to-qgis.aspx

Other dates include:

23 Oct 2012 – 24 Oct 2012 – Perth
13 Nov 2012 – 14 Nov 2012 – Perth
20 Nov 2012 – 21 Nov 2012 – Brisbane
22 Nov 2012 – 23 Nov 2012 – Melbourne


Filed under: Open Source, qgis Tagged: osgeo, qgis, Quantum GIS

Announcing QMap: A simple data collection application using QGIS

I would like to announce QMap: A simple data collection application built using QGIS and Python.

QMap is a QGIS based field data collection application that was built by myself and a work college at Southern Downs Regional Council.  QMap is now opened source under the same licence as QGIS, GPLv2.  The project homepage can be found at http://nathanw2.github.com/qmap/ and source at https://github.com/NathanW2/qmap.

Before I go into to many more details I will preface with: The application is currently under active development and as such there might be bugs or little rough bits that I haven’t cleaned up yet. However having said that, the program is functional and we are using it at work for the purpose it was built.

Features

  • Simple to use
  • Simple to manage
  • Simple to install
  • Forms built using Qt Designer
  • Loads normal QGIS projects
  • Anything QGIS supports QMap does too (snapping, PostGIS, etc)
  • It’s just QGIS with a tablet friendlier interface.
  • Syncing support (MS SQL 2008 only at the moment)

The Story

The program was developed after we looked around and decided that nothing really fit our needs quite right and to our satisfaction. (Within budget of course)

We had a list of, I think simple, requirements:

  • Must be simple to use by field staff
  • Can deal with complex or simple forms
  • Fully offline but with a syncing option
  • GPS support
  • Easy maintenance

The first point for me is a big one.  I have a seen a lot of data collection applications and unfortunately this is where I feel a lot of them fall down.  Most seem to be designed with people like me in mind, people who understand computers, understand menu systems, etc. If you work in local government or with an older age group of outside workforce you will know that this assumption doesn’t hold true.   Most of our field staff are not computer people, a few don’t even have home computer, expecting them to navigate a menu just to enable the GPS, or click on a small 16×16 pixel icon, on a tablet PC is not a option.

I decided to take the same approach as QGIS and use Qt Designer to build the forms. Why invent another tool? Using Qt Designer can also give us the flexibly of creating simple or complex forms in our own layout.

We do have pretty good 3G connection coverage over our region however we only have limited bandwidth to play with and having a solution that is full connected doesn’t really give the best user experience.  I would rather just store everything locally on the device and then sync when needed.  As all our layers are stored in MS SQL Server 2008 for the syncing we decided to use .NET Sync framework.  I would love to have sync support for PostGIS, Spatialite, or any other normal files, but it was out of scope for the project (at the moment).

For me easy maintenance means two things: not having to deal with crap loads of configurations; and having the power to change what I don’t like. For the first point I’m a big fan of convention over configuration.  I like to just drop stuff in a folder with a certain naming convention and it should just pick it up and work.  This also goes for the form bindings, just name the control the same as the field in the layer and QMap will bind it for you.  If I can follow a convention for things I have. Conventions make setup easier and consistent.

Once you have tasted the open source kool aid it can be quite hard to go back. Knowing that if there is a bug in QGIS I can fix it to make my project better is a comforting feeling.  There is also the lack of licence fees which makes open source very attractive for jobs with small budgets.

How does it work?

At the moment the core of the application is built as a QGIS plugin, however there is one little trick here that is worth mentioning. QMap is really a script that loads QGIS and sets the –configpath in order to load all the QGIS settings from a supplied path, inside the supplied path is the plugin. Think of it as a sandboxed QGIS which only loads the QMap plugin.  I’m also using the new customization function to remove all unneeded interface items.

Notes

Here are some notes worth bring up:

The application is still under development so things might change.
There is only point support at the moment, although adding line and region support wouldn’t be hard.
Syncing only works using MS SQL 2008 and the code is a bit rough. Will be cleaned up over time.
The build script only works on Windows and there is some win32 stuff for power management in the code. This is not because I don’t want to support the other platforms just that it was out of scope at work.
You need to be using the latest development build of QGIS (qgis-dev for those using OSGeo4W) this is because there have been a few bug fixes that make the application work as expected that aren’t in 1.8.


Filed under: Open Source, qgis Tagged: FOSSGIS, qgis, Quantum GIS

Five new awesomely awesome QGIS features

Recently there have been some great new additions to the QGIS project. Being part of such a fast moving project is a great feeling, and it’s only going to get better.

This post is going to be a quick over view of some of the newer features that I really like.

HTML Annotations

Well of course I like this one, I just added it. The reason I added this feature was because I really wanted to way to have popup images on the map canvas for flood damage reports on roads. I also wanted it dynamic so I could use template like syntax to replace values at run time.

The HTML annotations use QtWebKit and as a result support full HTML, CSS, and Javascript. The HTML can contain a QgsExpression – the same expression used in the expression labels and layer actions – inside [% %] which is replaced at run time with the data from the underlying feature e.g. [% "roadname" %]

I’ll let you think of some nice use cases for this new addition.

Project macros and non blocking notifications

This new feature comes from Giuseppe Sucameli of faunalia with the work done for ARPA Piemonte.

The task was to add Python macros that run when a project is open, saved, closed. As a side effect of the task the issue of security was raise and how to notify the user that macros are going to run.  For me this was less about security and more about how to present that information to the user without annoying the crap out of them. Most of the time popup dialogs in software are a anti-pattern and are often abused for tasks like this. So knowing I would throw my computer out the window if I had to dismiss yet another dialog I suggested a less intrusive method being used a lot these days. The handy slide out notification bar. Giuseppe  was very welcoming to the idea and implemented it nicely.

 Of course this addition can also expanded into other areas of the program. My first plan is to use it for notifying the user of plugins to failed to load.  There is nothing in QGIS that annoys more then starting and seeing this:

Dear dialog, why are you in my face!

To make matters worse if more then one plugin fails to load then I have to dismiss each dialog. So we can now use the notification bar to present it to the user in a nice non-blocking way. Something like “BTW four plugins failed to load at startup. What would you like me to do?”

Remember each time you use a blocking popup dialog it’s pretty much yelling at the user “OMG GIVE ME ATTENTION!! NO YOU CAN’T KEEP WORKING! GIVE ME ATTENTION!”

I’m working on a patch  to move this stuff into the notification bar just no ETA at the moment as I’m a bit busy.

Labeling improvements

Larry Shaffer has been working on some great improvements to the new labeling engine in order to make our maps look a lot more professional. Larry has been doing a lot of work in this area and is still going so I’m not going to go into all the details. However one new labeling feature that I really like is the ability to to set the  spacing between letters and words.

Before spacing

With a little bit of spacing

There is also the new ability to set the transparency of the label and the buffer.  The buffer transparency is something that I really like as sometimes you need a buffer but a solid buffer can then block out your map features; by adding a 45% transparent buffer I still have the labels pop off the map but not in your face or blocking features.  It’s hard to make a picture to explain it well so you’ll just have to experiment.

Project Templates

This one could be quite handy for people that make a lot of maps with the same base data. Thanks to Etienne Tourigny QGIS can now load projects as a template. This means you can create a project with all your base layers, styles, labels, etc, configured and then load it by default, or from the file menu, and you will have everything setup. All you have to do is save the a normal .qgs project file in

~/.qgis/project_templates folder and the project will be shown in the file menu.

Template list

You can also set the current project as the default template:

Handy!

Symbol Manager

And last but not least. This years GSoC student Arunmozhi got the improvements he had (has) been working on included into the master build. Arun was very welcoming to any feedback that Martin and I gave him about how we would like symbol stuff to work.  Anita Graser has already covered a lot of the new features over on her blog so I’m not going to go over everything again, although one thing she didn’t really touch on was the smart groups and tagging.

The tagging and smart groups are one of my favorite additions to the new symbol manager.

Symbol tagging

I love this new feature as not all the symbols I create belong to a single group so the tagging and smart groups fit this bill well.  I can now tag all the council symbols with ‘SDRC’ and include them a SDRC smart group but at the same time tag the sewer ones with ‘sewer’ and they can live in the sewer style smart group; or how about all sewer symbols that are also SDRC ones:

Smart group with sewer and sdrc symbols

You can then filter by this group in the symbol selector:

Filter based on smart group

Conclusion

I really love how fast QGIS is moving forward.  There almost isn’t a week that goes by that something isn’t getting done or someone is adding something new. Of course the great people on the project make this process a hell of a lot of fun and enjoyable.

Have fun experimenting! (remember that these features are in the master development build and may or may not have bugs)


Filed under: Open Source, qgis Tagged: FOSSGIS, Open Source, qgis, Quantum GIS

Australian QGIS User Group

I’m very please to announce the Australian QGIS User Group. Chris Scott (DMS) and I created this group because the interest in QGIS here is growing fast and it would be nice to have a place where us Aussies can hang out and chat about QGIS stuff, discuss local issues, organize local events, put shrimps on the barbie.

My ultimate goal would be to turn this into a full user group much like the Swiss QGIS User Group but for now we will keep it light.

Everyone is free to view the content of the home page and the Google group, however you have to request to join the Google Group to post.  This is only so that we can keep it local and not reduce the official QGIS mailing lists.

So in saying all that. If you live in Australia. Use QGIS, or are interested in QGIS. Feel free to join the group.


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, osgeo, qgis, Quantum GIS

Don’t forget to migrate your QGIS plugins!

From QGIS 1.8 and onwards the Plugin Installer plugin will no longer include the option to add the 3rd party repositories.  This was by design and intended to move people over to using the official plugin repository at http://plugins.qgis.org/ so we can provide a richer experience and keep everything in one place.

If you have plugins that are still not on the official repository then I would strongly recommend that you migrate them as a lot of new 1.8 users will be missing out on your great work.


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, python, qgis, Quantum GIS

HTML map tips in QGIS

New fresh QGIS feature! So fresh in fact you can still smell the wet paint :)

QGIS (development build) can now display map tips using HTML (a subset anyway).

To enable the new map tips: Open the Layer Properties dialog for a layer and select the Display tab

Display tab to set HTML map tips

In action

Layer properties for HTML map tip

Notice how we can also use a QGIS expression. Anything inside [% %] will be evaluated and replaced with the value in real-time. We can even use a CASE statement. Pretty cool!

And the result when hovering over a feature

HTML in QGIS map tip? Yes! WOOT!

Hold on. Pause the track! We can even use some CSS to make it more fancy.


<style>
h1 {color:red;}
p.question {color:blue;}
</style>
<h1> [% "NAME" %] </h1>
<br>
<img src="[% "image" %]" />
<br>
<p class="question">Is this place a country?</p>
<br>
[% CASE WHEN "TYPE" = 'Country' THEN 'Yes' ELSE 'No. It is a ' || "TYPE" END %]

CSS in a html map tip

Happy Mapping :)


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, mapping, Open Source, qgis, styling, tips

Generating chainage (distance) nodes in QGIS

Something that I need to do now and then is generate points along a line at supplied distance.  I had never really looked into doing it in QGIS until this question poped up on gis.stackexchange.com.  This is a quick blog post because I thought it was a pretty handy little thing to do.

In the development version there is a new method on QgsGeometry called interpolate. This method takes a distance and returns a point along a line at that distance. Perfect! We can then just wrap this in a loop and generate a point increasing the distance as we move along

from qgis.core import (QgsFeature, QgsGeometry,
                       QgsVectorLayer, QgsMapLayerRegistry,
                       QgsField)
from PyQt4.QtCore import QVariant
from qgis.utils import iface

def createPointsAt(distance, geom):
    length = geom.length()
    currentdistance = distance
    feats = []

    while currentdistance < length:
        # Get a point along the line at the current distance
        point = geom.interpolate(currentdistance)
        # Create a new QgsFeature and assign it the new geometry
        fet = QgsFeature()
        fet.setAttributeMap( { 0 : currentdistance } )
        fet.setGeometry(point)
        feats.append(fet)
        # Increase the distance
        currentdistance = currentdistance + distance

    return feats

def pointsAlongLine(distance):
    # Create a new memory layer and add a distance attribute
    vl = QgsVectorLayer("Point", "distance nodes", "memory")
    pr = vl.dataProvider()
    pr.addAttributes( [ QgsField("distance", QVariant.Int) ] )
    layer = iface.mapCanvas().currentLayer()
    # Loop though all the selected features
    for feature in layer.selectedFeatures():
        geom = feature.geometry()
        features = createPointsAt(distance, geom)
        pr.addFeatures(features)
        vl.updateExtents()

    QgsMapLayerRegistry.instance().addMapLayer(vl)

The above code might look a bit scary at first if you have never done any Python/pyqgis but hopefully the comments will ease the pain a little. The main bit is the createPointsAt function.

Cool! If we want to use this we can just stick it in a file in the .qgis/python folder (lets call it pointtools.py) and then run import pointtools in the python console.

So lets have a go. First select some objects then run the following in the Python Console

import pointtools
pointtools.pointsAlongLine(40)

That will generate a point every 40 meters along then selected lines

Distance nodes along line in qgis-dev

To generate nodes along different lines, select the new features, then call pointtools.pointsAlongLine(40) in the Python console.

Simple as that!

(Who knows, someone (maybe me) might even add this as a core object function in QGIS in the future)


Filed under: Open Source, qgis Tagged: language python, Open Source, osgeo, pyqgis, python, qgis, qgis-tips, Quantum GIS, tips

QGIS Style Tricks: Using styles to help fix kerb line directions

We are currently working a kerb line digitization and defect capture project at work.  This process involves looking at the aerial photo along with video of the roads and drawing lines on the kerb layer using QGIS, not overly hard just tedious.  As I mentioned in my Using QGIS in local government post, the defect points are snapped to the lines in order generate the distances, or chainage, along the kerb line for reporting reasons e.g Defect 1 at 10m, Defect 2 at 11.5m.  In order for this to happen the kerb lines must be running the correct direction, the correct direction here is defined by the road direction.    The kerb line also has an attribute to define what side of the road it is on, left or right, in reference to the direction of the road center line.

So we have two conditions:

  1. Must run the same way as the road
  2. Must have the correct side of the road assigned

The problem is how to clean up any lines that are already wrong (we were 90% of the way though when the above conditions were added).

In QGIS we can add line directions by using a two layer symbol for the kerb line:

Line with direction

Showing line direction

Not too bad but I still have to focus a lot to see which direction the lines are going.  Viewing them at this scale is fine but once you start to move the arrows all become a blur after a while.  Plus this also doesn’t let me check the side of road attribute quickly. Yes I can look at the color and the label but still I would like a quick way to look at line and see if it is facing the right way and with the correct side of the road attached.

What we can do is offset the arrows on the line so that they will be on the inside of the kerb line, between the kerb line and the road direction markers when they are facing the correct direction and have the correct side of road attribute.

For the left side we will of set the marker Line offset to 3, and for the right side we offset by -3

Offset arrow

Using the 3 and -3 offsets will mean the arrows are rendered on the inside of the kerb line if the line is facing the correct direction. Lets have a look

Opps no that isn’t right

Ohh no that isn’t right!  See how the line directions are facing the wrong way and it is showing the arrows on the outside of the line, further away from the road line.  This isn’t right.  Lets swap those line directions using a plugin that I wrote called Swap Line Direction (Search for ‘Swap’ in the plugin installer).

Lines facing correct direction

So now the arrows are on the inside of the line and are facing the correct way.

But wait there is more

This styling also helps me check that it is assigned the correct side of the road.  If we assign the top line the value ‘left‘, which is wrong in this case, we will see that the arrows are now on the wrong side of the line

Wrong side of road assigned

Of course here the obvious thing here is that there is two green lines which you can’t have, but also having the arrows on the wrong side of the line lets you quickly see which one is wrong.

It is impossible to get the arrows to be on the inside and facing the correct way.  If we swap the direction of the line the arrows are now on the inside but are facing the wrong way

Wrong side running the wrong way

Using this style trick allows me to quickly see at a glance which sections might be wrong when I have more then a single road in view

Summary

This post is a quick example of how you can use QGIS styles to help you visually validate you data.  The way I have done things in the post my not work for you and you might find it less helpful and more distracting then I did; however I found it worked well with my eyes and reduced strain.


Filed under: Open Source, qgis Tagged: qgis, Quantum GIS, styling

Like the super handy Atlas QGIS plugin? Want to see it as a core part of QGIS?

Atlas is a super handy little QGIS plugin that lets you generate a map series from a composer and a coverage (grid) layer.

The authors of the plugin are now looking for some funding to make it a core part of QGIS.  Making it part of QGIS would mean integration into the composer and maybe APIs for developers to add extensions.

The authors are looking at raising 7000€ in order top get the work done, but the more money raised the more features that are added.

More information can be found at http://www.oslandia.com/?p=1243


Filed under: Open Source, qgis Tagged: FOSSGIS, Open Source, osgeo, qgis, Quantum GIS

Better date and time support in QGIS expressions and styles

Version note: This will only work in the latest dev build of QGIS – not in 1.8

The lack of uni for the next couple of weeks has left me some time at night to work on some features that I really wish QGIS had.  One of these features was better date and time support in the expression engine.  Date and time is an important concept when working on inspection data and not being able to style my features in QGIS using date operations was bugging me.  So in good open source fashion I added some.

Here are the current functions (more to come in the future):

  • $nowreturns the current date and time
  • age({datetime},{datetime}) - returns the difference between the two dates
  • todate({string}) - converts a string to date type
  • totime({string}) – converts a string to time type
  • tointerval({string}) – converts a string to a interval type (details below)
  • day({datetime} or {interval}) – returns the day from a datetime type or the number of days in a interval.
  • hour(…) – Same as above but for hours
  • minute(…)  - Same as above but for minutes
  • second(…)  - Same as above but for seconds
  • day(..)  - Same as above but for days
  • week(..)  - Same as above but for weeks
  • month(…)  - Same as above but for months
  • year(…) - Same as above but for years
  • {datetime} – {interval} = {new datetime} – returns a new datetime subtracting the interval 
  • {datetime} + {interval} = {new datetime} – returns a new datetime adding the interval


The interval type

Functions like age(..), tointerval(), {datetime} -/+ {interval}, day(..), hour(..), etc, use, or return, Intervals.  An Interval is a measure of time that we can use for different things.  An example of an Interval is ’1 Year 2 Months’ this is then converted to a number of seconds and used for any calculations.

For example one can take away 10 days from the current date by doing the following ( -> marks the output ):

todate($now - '10 Days')
-> 2012-06-20

as

todate($now)
-> 2012-06-30

We can also do something like:

todate($now + '2 Years 1 Month 10 Days')
-> 2014-08-10

The age() function will return an interval which we can use extract what information we need.

The number of days between two dates:

day(age('2012-06-30', '2012-06-10'))
-> 20
-- Think of it as '2012-06-30' - '2012-06-10'
-- Note: day(), month(), etc, functions return doubles so you can get
-- 21.135234 days if you are using date & time type rather than just date type
-- wrap the result in toint() to get a more sane output.

Day() will also work on a plain date:

day('2012-06-30')
-> 30

We can even get the number of seconds between two dates:

second(age('2012-06-30', '2012-06-10'))
-> 1728000

Currently the only date format supported is {year}-{month}-{day} as seen in the examples above. Shouldn’t be too hard to add support to the todate(), todatetime(), totime() functions for giving it a pattern to use when converting the string e.g. dd-mm-YYYY, or something like that.

More on this fancy new stuff

When I wrote the new expression builder dialog a while ago I made it dynamic so that any new functions added to the expression engine will show up automatically.  So here they are:

List of new date and time functions.

We can also use these functions in the rule based rending, which is where the power really comes in.  Lets see something like that in action:

Styled using days and years

Should be pretty straight forward to understand. We are using the age() and day() functions to style the events that are older than 30 days, within 30 days, for today, or in the future.  We also check the year of the event using year() and year($now) to make sure we only see this years events, or style them differently depending on if they are last years events or in the future.

This is the result of the above rules:

Result of using date functions in rule based renderer

I’m also using the date functions in the expression based labelling to label the features using the following expression:

CASE
WHEN year( "dateadded") < year($now) THEN
	'Last Year'
WHEN day(age("dateadded", $now)) < 0 THEN
	day(age("dateadded", todate($now))) || ' Days old'
ELSE
	day(age("dateadded", todate($now))) || ' Days to go'
END

Well that’s it. Hope you find it handy in your day-to-day mapping. I know I will be using it a lot.
Thanks to Martin and Jürgen for the code reviews during the process; venturing in an unknown part of the code base always makes me nervous but that is all part of learning, and sometimes you can make some pretty cool stuff.
Some other random notes: The general idea has been modelled of how Postgres handles dates and times, it’s not an exact copy but follows the same kind of ideas. The interval class also uses the same number of seconds for one year that postgres does so that we can be consistent with the output.


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, map-rendering, Open Source, osgeo, qgis, Quantum GIS, styling

Styling temporal (time) data in QGIS

So this years first uni semester is done and dusted, now I have some free time.  Blog all the things!

This is a follow up post for discussion that was started on LinkedIn about showing features older, or newer, then a certain date different colours   The main post was about using free, or low cost, solutions in order to aid in mapping water networks. I recommend that everyone watch it. A very good presentation.

I recommended that you could use the rule based rendering engine but the expression engine in QGIS doesn’t have any date functions yet.  All good we can add them if we need and once I get my head around the expression engine I plan on doing exactly that. But for now we can do it a different way.

We are going to use Spatialite, but any database will do (syntax and process will vary).

Lets have a look an inspection layer we have in our Spatialite database viewed in QGIS:

Pretty boring and hard to see what has been done in the last 30 days.  Now with the lack of support for dates in the expression engine we have to use another methods.  For this example we will use the really handy DBManger plugin that now ships with QGIS from 1.8.  Load it, connect to your database, and run the following query:

SELECT id,
              CASE WHEN DATE("date_checked") > DATE('now', '-30 days') THEN
                         'Within 30 Days'
              ELSE
                         'older'
              END as age, date_checked, geom
FROM  "inspections"

As you can see anything that is within the 30 days now has the “Within 30 Days” string in the age column, or else it has “older”.  CASE statements can be very powerful things in SQL sometimes.

Now load it into QGIS, style, and label it using the new age column

and Bob’s your uncle

You now have a layer that is style based on age but is also dynamic.  Adding a new inspection point will now will be styled according to those rules. (Although you will have to edit the normal inspection layer with it turned off as views/queries are not editable – without some setup anyway)

It might be a simple thing to some but sometimes it’s hard to find the right words to describe what you want when you are looking for this kind of thing. So hopefully this has helped a few people get started with visualizing their time/date based data in QGIS.

Happy mapping!


Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, osgeo, qgis, Quantum GIS, styling

QGIS 1.8 is out!

After almost a year and a lot of hard work QGIS 1.8 is finally out.  This is the best QGIS version so far, packed full of fancy new features.

The official release notice can be found here: http://qgis.org/index.php?option=com_content&view=article&id=149 and downloads can be found at http://download.qgis.org

Here is the change log of all the new stuff in 1.8:

- QGIS Browser - a stand alone app and a new panel in QGIS. The
browser lets you easily navigate your file system and connection based
(PostGIS, WFS etc.) datasets, preview them and drag and drop items
into the canvas.
- DB Manager - the DB manager is now officially part of QGIS core. You
can drag layers from the QGIS Browser into DB Manager and it will
import your layer into your spatial database. Drag and drop tables
between spatial databases and they will get imported. You can use the
DB Manager to execute SQL queries against your spatial database and
then view the spatial output for queries by adding the results to QGIS
as a query layer.
- Action Tool - now there is a tool on the map tools toolbar that will
allow you to click on a vector feature and execute an action.
- MSSQL Spatial Support - you can now connect to your Microsoft SQL
Server spatial databases using QGIS.
- Customization - allows setting up simplified QGIS interface by
hiding various components of main window and widgets in dialogs.
- New symbol layer types - Line Pattern Fill, Point Pattern fill
- Composers - have multiple lines on legend items using a specified character
- Expression based labelling
- Heatmap tool - a new core plugin has been added for generating
raster heatmaps from point data. You may need to activate this plugin
using the plugin manager.
- GPS Tracking - The GPS live tracking user interface was overhauled
and many fixes and improvements were added to it.
- Menu Re-organisation - The menus were re-organised a little – we now
have separate menus for Vector and Raster and many plugins were
updated to place their menus in the new Vector and Raster top level
menus.
- Offset Curves - a new digitising tool for creating offset curves was added.
- Terrain Analysis Plugin - a new core plugin was added for doing
terrain analysis – and it can make really good looking coloured relief
maps.
- Ellipse renderer - symbollayer to render ellipse shapes (and also
rectangles, triangles, crosses by specifying width and height).
Moreover, the symbol layer allows to set all parameters (width,
height, colors, rotation, outline with) from data fields, in mm or map
units
- New scale selector with predefined scales
- Option to add layers to selected or active group
- Pan To Selected tool
- New tools in Vector menu - densify geoemtries, Build spatial index
- Export/add geometry column tool can export info using layer CRS,
project CRS or ellipsoidal measurements
- Model/view based tree for rules in rule-based renderer
- Updated CRS selector dialog
- Improvements in Spatial Bookmarks
- Plugin metadata in metadata.txt
- New plugin repository
- Refactored postgres data provider: support for arbitrary key
(including non-numeric and multi column), support for requesting a
certain geometry type and/or srid in QgsDataSourceURI
added gdal_fillnodata to GDALTools plugin
- Support for PostGIS TopoGeometry datatype
- Python bindings for vector field symbollayer and general updates to
the python bindings.
- New message log window
- Benchmark program
- Row cache for attribute table
- Legend independent drawing order
- UUID generation widget for attribute table
- Added support of editable views in SpatiaLite databases
- Expression based widget in field calculator
- Creation of event layers in analysis lib using linear referencing
- Group selected layers option added to the TOC context menu
- load/save layer style (new symbology) from/to SLD document
- WFS support in QGIS Server
- Option to skip WKT geometry when copying from attribute table
- upport for zipped and gzipped layers
- Test suite now passes all tests on major platforms and nightly tests
- Copy and paste styles between layers
- Set tile size for WMS layers
- Support for nesting projects within other projects

Thanks to all the sponsors and everyone who put a lot of work into this release!


Filed under: Open Source, qgis Tagged: Open Source, osgeo, qgis, Quantum GIS

New QGIS Training Provider in Australia – Digital Mapping Solutions

If you are in Australia and are looking for some QGIS training Digital Mapping Solutions have just announced that they are now providing a  2 day Introduction to QGIS training course.

The course covers such things as:

  • User interface and basic tools
  • QGIS projects and preferences
  • Coordinate Reference Systems and On-The-Fly (OTF) projection
  • Map navigation and QGIS tools
  • Vector map objects: Adding, deleting & modifying
  • Attribute data: Field editing & modification
  • Layer manipulation
  • Digitizing
  • Styles and labelling
  • Object manipulation: Combine, split & buffer
  • Registering raster imagery
  • Creating layouts for printing
  • Utilising WMS and WFS Web Services
  • Plugins
  • Database connectivity

More details and a booking form for the course can be found at http://www.mapsolutions.com.au/training/quantum-gis/introduction-to-qgis.aspx

Training like this is great for QGIS popularity in Australia.  Working in local government I know how hard it can be to get IT to say yes to software unless there is formal training of some sort, and now thanks to DMS there is.

Enjoy :)

 


Filed under: Open Source, qgis

Back to Top

Sustaining Members