Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is it possible to reach the Spatial Query plugin functionality in QGIS from a Python script (PyQGIS)? If so, how do I accomplish this? Haven't found any documentation for it, even in the C++ API docs...

Regards, Andreas

share|improve this question
    
Apart from the suggestions from Sylvester, I'd like to understand if it's possible to reach other QGIS plugins from PyQGIS or just the native API? Is it really impossible to use the Spatial Query plugin from PyQGIS? How about the fTools plugins? I would e.g. like to reach "Random Points" in the Vector->Research Tools menu from script, isn't that possible? –  Andreas Mar 14 '12 at 7:31
add comment

4 Answers

Yes, you can access plugin functions from a Python script e.g. to access fTools try in the QGIS Python console:

from fTools.fTools import fToolsPlugin

or more generic for other plugins:

from <plugin>.<file> import <class>
share|improve this answer
add comment

You can write your own function in Python easily enough using one of the many excellent Python APIs available. Have a look at GDAL/OGR. The documentation is focussed on C++ but you should be able to understand it enough to figure out the Pythonic equivalent. A great tutorial on this can be found here. Alternatively try 'Shapely'. If you are comfortable with databases have a look at PostGIS or SpatiaLite.

All of these have Python API that will allow you to construct your own queries for all the spatial queries that you can find in QGIS or ArcGIS or almost any GIS. To get started I recommend going through the tutorial for GDAL/OGR above and then branching out.

share|improve this answer
    
Ok, thanks for the links. Will look into this. –  Andreas Mar 14 '12 at 7:28
add comment

C++ Plugins (Spatial Query)

It is not possible to get access to the spatial query plugin, because it is a C++ plugin without python bindings.

Python Plugins (FTools, Processing et al)

For python plugins in contrast, it is possible to import other plugin's interfaces by using the method outlined by webrian

from <plugin>.<file> import <class>

You can also get access to loaded and instantiated plugins by using

qgis.utils.plugins['pluginname']

In this case, the best shot is to write logic based on existing libraries as outlined by MappaGnosis.

share|improve this answer
add comment

It's not that difficult to write your own 'Spatial Query PlugIn' in the Python Console.

Iterate over alle features and perform the query on the geometries of the features by yourself.

Sample Code for QGIS 2.0! Goal: Get all points that lie within the features of a polygon layer.

lyrs = iface.legendInterface().layers()
lyrPoly = lyrs[1] #polygon layer
lyrPnts = lyrs[0] #point layer

featsPoly = lyrPoly.getFeatures() #get all features of poly layer
#featsPoly = lyrPoly.selectedFeatures() #for testing, use selected features only

for featPoly in featsPoly: #iterate poly features
    zipPoly = featPoly["POSTCODE"] #get attribute of poly layer
    geomPoly = featPoly.geometry() #get geometry of poly layer
    #performance boost: get point features by poly bounding box first
    featsPnt = lyrPnts.getFeatures(QgsFeatureRequest().setFilterRect(geomPoly.boundingBox()))
    for featPnt in featsPnt:
        #iterate preselected point features and perform exact check with current polygon
        if featPnt.geometry().within(geomPoly):
            print '"' + zipPoly + '"' + ';' + featPnt["Zipcode"]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.