#!/usr/bin/python from qgis.core import * from PyQt4.QtGui import * from PyQt4.QtCore import * from pprint import pprint import sys, os if __name__ == '__main__': layerType = { QgsMapLayer.VectorLayer: "Vector", QgsMapLayer.RasterLayer: "Raster"} #oswgeo4w = os.environ.get("OSGEO4W_ROOT"); #print oswgeo4w QgsApplication.setPrefixPath("/usr", True); QgsApplication.initQgis() print 'QGis initialized' a = QApplication( sys.argv ) # load a project p = QgsProject.instance() qgsInfo = QFileInfo( "wms.qgs" ) isLoaded = p.read( qgsInfo ) print 'IsLoaded '+str(isLoaded) print 'Title: ' + p.title() # Open MapRegistry mlr = QgsMapLayerRegistry.instance() print print 'Layers count: ' + str(mlr.count()) layers = mlr.mapLayers(); # Display some layer information #pprint(layers) for layerId in layers: layer = layers[layerId] print 'Layer name: ' + layer.name() print 'Layer abstract: ' + layer.abstract() print 'Layer type: ' + layerType[layer.type()] # Add a layer print print "Add Layer" tifInfo = QFileInfo( "img.tif" ) if (tifInfo.exists()): print "File exists!" rasterLayer = QgsRasterLayer(tifInfo.absoluteFilePath(), "david" ) crs = QgsCoordinateReferenceSystem(); rasterLayer.setCrs(crs) mlr.addMapLayer(rasterLayer, True) print print 'Layers count: ' + str(mlr.count()) layers = mlr.mapLayers(); for layerId in layers: layer = layers[layerId] print 'Layer name: ' + layer.name() print 'Layer abstract: ' + layer.abstract() print 'Layer type: ' + layerType[layer.type()] # Save project print print "Save project" p.write() QgsApplication.exit() print "Done."