Re: [Qgis-user] Python scripting
Alessandro et al., > I'm also interested in using python and QGIS for simple scripts > espacially for batch processing, and some examples could be very useful! Attached is a python script for performing some basic geoprocessing functions on vector layers. The header of the file contains usage examples to help get you started... it's probably not the cleanest python code, nor the most pythonistic... but you get the idea ;-) Hope this helps to get you started... The other good resource for python scripting is: http://wiki.qgis.org/qgiswiki/PythonBindings Cheers, Carson Many thanks Carson and Alex, your help has been invaluable, and I am sure not just for myself. Thanks Carson for the example, more than I asked for and much appreciated. regards George -- View this message in context: http://n2.nabble.com/Python-scripting-tp2577010p2577897.html Sent from the qgis-user mailing list archive at Nabble.com. ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] Python scripting
Alessandro et al., I'm also interested in using python and QGIS for simple scripts espacially for batch processing, and some examples could be very useful! Attached is a python script for performing some basic geoprocessing functions on vector layers. The header of the file contains usage examples to help get you started... it's probably not the cleanest python code, nor the most pythonistic... but you get the idea ;-) Hope this helps to get you started... The other good resource for python scripting is: http://wiki.qgis.org/qgiswiki/PythonBindings Cheers, Carson Thanks Ale georgew ha scritto: ThanKs Alex, the problem is I have not been able to find any example that is free standing, all seem to be plugins or use QT to generate a UI. Can you point me to a working example, however simple (e.g. read shapefile, project,write shapefile) that can set me off on the right path? Many thanks G. ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user -- Carson J. Q. Farmer ISSP Doctoral Fellow National Centre for Geocomputation (NCG), National University of Ireland, Maynooth, Email: carson.far...@gmail.com Web: http://www.carsonfarmer.com/ http://www.ftools.ca/ from PyQt4.QtCore import * from qgis.core import * class GeoprocessingEngine( ): ''' USAGE: GeoprocessingEngine( func, layerA, layerB, outPath, encoding, param, merge ) func : geoprocessing function 1: Buffer 2: Convex Hull 3: Difference 4: Dissolve 5: Intersection 6: Union 7: Symetrical Difference 8: Clip layerA : QgsVectorLayer layerB : QgsVectorLayer outPath : string -> "/home/cfarmer/Desktop/test.shp" encoding : default is 'System' param : a parameter, may be used for buffer, and dissolve merge : should the results be merged (buffer) EXAMPLE: # import engine from Geoprocessing import GeoprocessingEngine layerA = QgsVectorLayer("/path/to/layer.shp", "layerA", "ogr") layerB = QgsVectorLayer("/path/to/layer.shp", "layerB", "ogr") # create geo engine geo = GeoprocessingEngine( 5, layerA, layerB, '/home/cfarmer/Desktop/test.shp') # compute geoprocessing event, and output result outlayer = geo.compute() # compute() returns the output layer as a QgsVectorLayer This alows us to link multiple runs together: geo1 = GeoprocessingEngine( 5, layerA, layerB, '/home/cfarmer/Desktop/test1.shp') outlayer = geo1.compute() geo2 = GeoprocessingEngine( 3, layerA, outlayer, '/home/cfarmer/Desktop/test2.shp') outlayer = geo2.compute() ... ... temp files are created for better usage of memory when using large layers ''' def __init__( self, function, layerA, layerB, outputName, encoding = 'System', param = 0, merge = False ): self.myFunction = function self.success = False self.layerA = layerA self.layerB = layerB self.param = param self.merge = merge self.outputName = outputName self.encoding = encoding def compute( self ): check = QFile( self.outputName ) if check.exists(): if not QgsVectorFileWriter.deleteShapeFile( self.outputName ): return None self.geoprocessing( self.myFunction, self.layerA, self.layerB, self.param, self.merge, self.outputName, self.encoding ) return QgsVectorLayer( self.outputName, "temp", "ogr" ) def geoprocessing( self, function, myLayerA, myLayerB, myParam, myMerge, myName, myEncoding ): self.myFunction = function self.myLayerA = myLayerA self.myLayerB = myLayerB self.myParam = myParam self.myMerge = myMerge self.myName = myName self.myEncoding = myEncoding self.vlayerA = self.myLayerA if self.myFunction == 1 or self.myFunction == 2 or self.myFunction == 4: ( self.myParam, useField ) = self.checkParameter( self.vlayerA, self.myParam ) if not self.myParam is None: if self.myFunction == 1: geos, feature, match = self.buffering( useField ) elif self.myFunction == 2: geos, feature, match = self.convex_hull( useField ) elif self.myFunction == 4: geos, feature, match = self.dissolve( useField ) else: self.vlayerB = self.myLayerB if self.myFunction == 3: geos, feature, match = self.difference() elif self.myFunction == 5: geos, feature, match = self.intersect() elif self.myFunction == 6: geos, feature, match = self.union() elif self.myFunction == 7: geos, feature, match = self.symetrical_difference() elif self.myFunction == 8: geos, feature, match = self.clip() def buffering( self, useField ): GEOS_EXCEPT = True FEATURE_EXCEPT = True vproviderA = self.vlaye
Re: [Qgis-user] Python scripting
I'm also interested in using python and QGIS for simple scripts espacially for batch processing, and some examples could be very useful! Thanks Ale georgew ha scritto: ThanKs Alex, the problem is I have not been able to find any example that is free standing, all seem to be plugins or use QT to generate a UI. Can you point me to a working example, however simple (e.g. read shapefile, project,write shapefile) that can set me off on the right path? Many thanks G. ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] Python scripting
georgew wrote: > > > georgew wrote: >> Hi all, I have not been able to find an answer to this question: Using the >> latest stable version of QGIS from OSGeo4W on WinXP SP3 is it possible to >> write free standing Python programs/scripts that will access QGIS objects >> (vectors, shapefiles, etc) and process them in batch, without the need for >> GUI or user intervention other than to run the script from Python command >> line (or within an IDE)? > > It should be possible, I believe OSGeo4w installs it's own python and > hence all python libraries would be on it's path not on your system > installed python. > > To use the libraries with your regular python install you probably need > to and to the PYTHONPATH environment variable of your system the path to > something like C:\OSGEO4W\QGIS\ > > Then import qgis.core and such should work. > > Alex > ___ > Qgis-user mailing list > Qgis-user@lists.osgeo.org > http://lists.osgeo.org/mailman/listinfo/qgis-user > > > ThanKs Alex, the problem is I have not been able to find any example that is > free standing, all seem to be plugins or use QT to generate a UI. Can you > point me to a working example, however simple (e.g. read shapefile, > project,write shapefile) that can set me off on the right path? > Many thanks > G. Sorry, I don't have an example. But python is much more flexible than you a giving it credit for. All of the plugins will work as examples for you, just ignore the parts of the code that involve the UI. In python if you import qgis.core and then start writing code that uses functions from the core it will just work. Alex ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] Python scripting
georgew wrote: > Hi all, I have not been able to find an answer to this question: Using the > latest stable version of QGIS from OSGeo4W on WinXP SP3 is it possible to > write free standing Python programs/scripts that will access QGIS objects > (vectors, shapefiles, etc) and process them in batch, without the need for > GUI or user intervention other than to run the script from Python command > line (or within an IDE)? It should be possible, I believe OSGeo4w installs it's own python and hence all python libraries would be on it's path not on your system installed python. To use the libraries with your regular python install you probably need to and to the PYTHONPATH environment variable of your system the path to something like C:\OSGEO4W\QGIS\ Then import qgis.core and such should work. Alex ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user ThanKs Alex, the problem is I have not been able to find any example that is free standing, all seem to be plugins or use QT to generate a UI. Can you point me to a working example, however simple (e.g. read shapefile, project,write shapefile) that can set me off on the right path? Many thanks G. -- View this message in context: http://n2.nabble.com/Python-scripting-tp2577010p2577236.html Sent from the qgis-user mailing list archive at Nabble.com. ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
Re: [Qgis-user] Python scripting
georgew wrote: > Hi all, I have not been able to find an answer to this question: Using the > latest stable version of QGIS from OSGeo4W on WinXP SP3 is it possible to > write free standing Python programs/scripts that will access QGIS objects > (vectors, shapefiles, etc) and process them in batch, without the need for > GUI or user intervention other than to run the script from Python command > line (or within an IDE)? It should be possible, I believe OSGeo4w installs it's own python and hence all python libraries would be on it's path not on your system installed python. To use the libraries with your regular python install you probably need to and to the PYTHONPATH environment variable of your system the path to something like C:\OSGEO4W\QGIS\ Then import qgis.core and such should work. Alex ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user
[Qgis-user] Python scripting
Hi all, I have not been able to find an answer to this question: Using the latest stable version of QGIS from OSGeo4W on WinXP SP3 is it possible to write free standing Python programs/scripts that will access QGIS objects (vectors, shapefiles, etc) and process them in batch, without the need for GUI or user intervention other than to run the script from Python command line (or within an IDE)? -- View this message in context: http://n2.nabble.com/Python-scripting-tp2577010p2577010.html Sent from the qgis-user mailing list archive at Nabble.com. ___ Qgis-user mailing list Qgis-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/qgis-user