[Qgis-user] What can you do with the Python console?
Carson Farmer
carson.farmer at gmail.com
Thu Oct 30 05:15:14 PDT 2008
Maning,
> Hi,
>
> I'm no programmer just a FOSS GIS/RS user. I know most plugins are
> created via python. I'm just curious what can we do with the python
> console? Any examples I can practice? I have learned a lot of bash
> scripting by using GRASS. Maybe I can learn more python with the QGIS
> python console.
>
Anything you can do in QGIS, you can also do via the python console. It
is also a great tool for python plugin developers for testing the
functionality of their plugins. As for a user based example, the
following code will add a feature to the top layer in the TOC (it
assumes the layer is a point layer):
mc = iface.getMapCanvas()
layer = mc.getZpos( 0 )
provider = layer.getDataProvider()
feat = QgsFeature()
feat.setGeometry( QgsGeometry( QgsPoint( -10, 53 ) ) )
provider.addFeatures( [ feat ] )
layer.updateExtents()
mc.refresh()
The above is based on using code from QGIS 0.11
I have also developed a python module that you can import into QGIS via
the console to perform basic geoprocessing functions:
from geoprocessing_engine import Geoprocessing1
mc = iface.mapCanvas()
layer_a = mc.layer( 0 )
layer_b = mc.layer( 1 )
function = 'Intersect'
intersect = Geoprocessing1( function, layer_a, layer_b )
intersect.compute()
intersect.saveToFile( '/home/cfarmer/Desktop/intersect.shp', 'UTF-8' )
The 'geoprocessing_engine' also allows various geoprocessing functions
to be linked together, without having to create intermediate files, and
without having to perform the task more than once:
final_output = Geoprocessing1( 'Difference', Geoprocessing1( function,
layer_a, layer_b ).compute(), layer_c ).compute()
I imagine that others will develop similar modules for various tasks
that might benefit from this type of approach (making bulk geoprocessing
easier for example). This 'geoprocessing_engine' is based on the new
QGIS API, so will not work with older version of QGIS. I will likely
release a decent version of this when QGIS 1.0 comes out...
Hope this gives you an idea of the power of the python console.
Carson
More information about the Qgis-user
mailing list