QGIS Planet

Identify feature on map

A very awaited feature is now available in the master version of QGIS: identifying features in the map!

You can define the class of the map tool as follows:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *

class IdentifyGeometry(QgsMapToolIdentify):
 def __init__(self, canvas):
  self.canvas = canvas
  QgsMapToolIdentify.__init__(self, canvas)

 def canvasReleaseEvent(self, mouseEvent):
  results = self.identify(mouseEvent.x(),mouseEvent.y(), self.TopDownStopAtFirst, self.VectorLayer)
  if len(results) > 0:
   self.emit( SIGNAL( "geomIdentified" ), results[0].mLayer, results[0].mFeature)

This class will try to identify a feature of any visible vector layer and returning the first found feature (using layer order). Then, it will emit the signal with the layer and the feature identified.
To customize this, you can use the identify method with different arguments:

  • type of layer
  • type of identification (current layer, top-down, top-down stop at first or the QGIS setting)
  • list of layers

There is two ways of calling the identify methods:

  • identify (x, y, layerList=[], IdentifyMode mode=self.DefaultQgsSetting)
  • identify (x, y, identifyMode, layerType=AllLayers)

Identify mode and layer types are defined here. Mainly the options can be:

  • Identify mode: self.DefaultQgsSetting, self.ActiveLayer, self.TopDownStopAtFirst, self.TopDownAll
  • Layer type: self.AllLayers, self.VectorLayer, self.RasterLayer

Both methods return a structure IdentifyResult defined in the API. Mainly, it contains:

  • the feature (mFeature) if the identified layer is a vector layer
  • the corresponding layer (mLayer)
  • the derived attributes (mDerivedAttributes): the raster value for raster layers

In your plugin main code, you can define a toolbox button to enable your map tool:

class myPlugin():
 def initGui(self):
  self.mapToolAction = QAction(QIcon(":/plugins/myPlugin/icons/myIcon.png"), "My Plugin", self.iface.mainWindow())
  self.mapToolAction.setCheckable(True)
  QObject.connect(self.mapToolAction, SIGNAL("triggered()"), self.mapToolInit)
  self.iface.addToolBarIcon(self.mapToolAction)
  self.iface.addPluginToMenu("&My Plugin", self.mapToolAction)

 def mapToolInit(self):
  canvas = self.iface.mapCanvas()
  if self.mapToolAction.isChecked() is False:
   canvas.unsetMapTool(self.mapTool)
   return
  self.mapToolAction.setChecked( True )
  self.mapTool = IdentifyGeometry(canvas)
  QObject.connect(self.mapTool , SIGNAL("geomIdentified") , self.doSometing )
  canvas.setMapTool(self.mapTool)
  QObject.connect( canvas, SIGNAL( "mapToolSet(QgsMapTool *)" ), self.mapToolChanged)</em>

 def doSomething(self, layer, feature):
  # do something

If you want your plugin to be back compatible with version before 1.9, you can select the features at the clicked point using a given tolerance and using the current layer:

try:
 from qgis.gui import QgsMapToolIdentify
except:
 from qgis.gui import QgsMapTool as QgsMapToolIdentify

class IdentifyGeometry(QgsMapToolIdentify):
 def __init__(self, canvas):
  self.canvas = canvas
  QgsMapToolIdentify.__init__(self, canvas)

 def canvasReleaseEvent(self, mouseEvent):
  try:
  results = self.identify(mouseEvent.x(),mouseEvent.y(), self.TopDownStopAtFirst, self.VectorLayer)
  if len(results) > 0:
   self.emit( SIGNAL( "geomIdentified" ), results[0].mLayer, results[0].mFeature)
  except: # qgis <1.9
   point = self.toMapCoordinates( mouseEvent.pos() )
   layer = self.canvas.currentLayer()
   if layer == None:
    return
   if layer.type() != QgsMapLayer.VectorLayer:
    return
   point = self.canvas.mapRenderer().mapToLayerCoordinates(layer, point)
   pixTolerance = 6
   mapTolerance = pixTolerance * self.canvas.mapUnitsPerPixel()
   rect = QgsRectangle(point.x()-mapTolerance,point.y()-mapTolerance,point.x()+mapTolerance,point.y()+mapTolerance)
   provider = layer.dataProvider()
   provider.select([], rect, True, True)
   subset = []
   f = QgsFeature()
   while (provider.nextFeature(f)):
    subset.append(f)
    if len(subset) == 0:
     return
    if len(subset) > 1:
     idx = QgsSpatialIndex()
    for f in subset:
     idx.insertFeature(f)
     nearest = idx.nearestNeighbor( point, 1 )
     layer.featureAtId(nearest[0],f, True, False)
    self.emit( SIGNAL( "geomIdentified" ), layer, f)

Note, that this last code (for version <1.9) does not consider scale dependent visibility and can therefore return a feature which is not visible in the map!

Learn More

The state of QGIS Globe

The Region of Umbria, Italy, sponsored 4 days of work to update QGIS Globe for current QGIS versions. Most of the functionality is working again and the globe is now compatible with osgEarth 1.0 up to 1.3. The bad news is, that the globe plugin is not working on Windows with OSGeo4W. It seems that one of the OSGeo4W libraries (GDAL?) is compiled with an incompatible MS compiler version. Christmas holidays are coming…
Learn More

Littering Your Python Path: The Road to Destruction

Well not quite destruction, but a bit of hair pulling… While working on an update to the Plugin Builder, I encountered a small problem. The Plugin Builder displays the version number in the title bar of its main window. After bumping the version number to 1.8.4 in all the requisite places, it still showed 1.8.3 when testing. Using grep on all the source files revealed no instance of 1.8.3 in any file.
Learn More

New configuration options in QGIS server

In QGIS server, it is now possible to selectively exclude layers from WMS publication. These layers will be available only on the desktop and hidden from WMS clients. Similarly, print layouts can be excluded from WMS publication. Of course, these settings are conveniently accessible from the project properties dialog of QGIS (but you need to have a nightly build or a recent compile): Additionally, attributes per layer can be excluded from WMS or WFS publication in the vector properties dialog:
Learn More

QGIS Gains a Gold Sponsor

The Quantum GIS (QGIS) project is happy to announce that the Asia Air Survey Co., Ltd (AAS), a Japanese international consulting company, has become a Gold Sponsor. AAS has committed to providing 9,000 EUR (~$11,000 US) each of three years, beginning in November 2012. The AAS sponsorship is yet another indication that QGIS is a mature and stable project which continues to provide innovative open source GIS software. The QGIS Project Steering Committee (PSC) wishes to thank AAS for their continuing commitment.
Learn More

Creating png8 images with QGIS server

For providing maps via WMS over the internet, it is important to generate image files with a small size. Because normally, most of the perceived WMS delay comes from transfering large images files over the internet (and not from map rendering itself). Therefore, QGIS server supports the conversion of png24 and png32 images into png8, therefore generating a file with only 1/3 resp. 1/4 of the original size (but with lower quality).
Learn More

QGIS Enterprise

Mit QGIS Enterprise bietet Sourcepole ein komplettes Wartungs- und Support-Paket für eine Geodaten-Infrastruktur (GDI) an, die vollständig auf Open Source Software aufbaut. Der Kern des Angebotes ist die Quantum GIS Suite basierend auf QGIS Desktop, QGIS Server, QGIS WebClient und QGIS Mobile. dauerhafte Infrastruktur Investitionssicherheit hohe Verfügbarkeit GDI-Gesamtpaket Serviceorientierte Architektur keine Lizenzkosten schnelle Bug-Fixes regelmässige Updates kompetente Schulung Nutzen Sie die Vorteile der Hersteller-Unabhängigkeit. Reduzieren sie die Kosten ihrer Geodaten Infrastruktur da keine Software-Lizenzen anfallen.
Learn More

Eigene GIS-Fachapplikationen mit QGIS entwickeln

Als Ergebnis der Zusammenarbeit zwischen der Sourcepole AG und der Hochschule Rapperswil (HSR) ist ein Video entstanden, dass die Entwicklung eigener Fachschalen mit QGIS beschreibt. Am Beispiel der Zonenplanung zeigt dieses Video, wie Sie mit Quantum GIS eine so genannte Fachapplikation erstellen könne. Sehen Sie Schritt für Schritt, wie sie Quantum GIS auf Ihre persönlichen Bedürfnisse anpassen können, passende Erweiterungen finden, eigene Eingabeformulare erstellen und eine Datenbank verwalten. Die Grundlage zu diesem Video war ein Workshop auf dem Geosummit 2012 Bern, den Pirmin Kalberer entwickelt hat.
Learn More

Kursprogramm Herbst 2012

Sourcepole bietet Grundlagen- und Aufbau-Kurse für den Betrieb von Geodaten-Infrastrukturen auf der Basis von PostgreSQL/PostGIS und Quantum GIS an. Detaillierte Informationen zu den Kursen, die im Herbst 2012 stattfinden, entnehmen Sie bitte dem Kursprogramm. Die Anmeldung ist ab sofort online möglich. Wir freuen uns darauf Sie in Zürich begüssen zu können.
Learn More

QGIS Is Ten Years Old

It was ten years ago, on July 19, 2002, that QGIS was officially unveiled. That first release was primitive, supporting only PostGIS layers and having little in the way of navigation controls. Invoking the open source mantra of “release early and release often,” I announced it on Freshmeat and waited. Slowly it began to attract attention—not all of it positive. Some questioned why I was starting a new open source GIS project when there were others I could join.
Learn More