modifyqgs.py

David AMAR, 2014-07-01 02:53 AM

Download (1.75 KB)

 
1
#!/usr/bin/python
2
from qgis.core import * 
3
from PyQt4.QtGui import * 
4
from PyQt4.QtCore import * 
5
from pprint import pprint 
6
import sys, os 
7

    
8
if __name__ == '__main__': 
9
  layerType = { QgsMapLayer.VectorLayer: "Vector", QgsMapLayer.RasterLayer: "Raster"}  
10
  #oswgeo4w = os.environ.get("OSGEO4W_ROOT"); 
11
  #print oswgeo4w 
12
  QgsApplication.setPrefixPath("/usr", True); 
13

    
14
  QgsApplication.initQgis() 
15
  print 'QGis initialized' 
16
  a = QApplication( sys.argv ) 
17
  # load a project 
18
  p = QgsProject.instance() 
19
  qgsInfo = QFileInfo( "wms.qgs" ) 
20
  
21
  isLoaded = p.read( qgsInfo ) 
22
  print 'IsLoaded '+str(isLoaded) 
23
  print 'Title: ' + p.title() 
24
  
25
  # Open MapRegistry 
26
  mlr = QgsMapLayerRegistry.instance() 
27
  print 
28
  print 'Layers count: ' + str(mlr.count()) 
29
  layers = mlr.mapLayers(); 
30
  
31
  # Display some layer information 
32
  #pprint(layers) 
33
  for layerId in layers: 
34
    layer = layers[layerId] 
35
    print 'Layer name: ' + layer.name() 
36
    print 'Layer abstract: ' + layer.abstract() 
37
    print 'Layer type: ' + layerType[layer.type()] 
38

    
39
  # Add a layer 
40
  print 
41
  print "Add Layer" 
42
  tifInfo = QFileInfo( "img.tif" ) 
43
 
44
  if (tifInfo.exists()):
45
    print "File exists!"
46

    
47
    rasterLayer = QgsRasterLayer(tifInfo.absoluteFilePath(), "david" ) 
48
    
49
    crs = QgsCoordinateReferenceSystem(); 
50
    rasterLayer.setCrs(crs) 
51
    
52
    mlr.addMapLayer(rasterLayer, True) 
53
    
54
    print 
55
    print 'Layers count: ' + str(mlr.count()) 
56
    layers = mlr.mapLayers(); 
57
    for layerId in layers: 
58
      layer = layers[layerId] 
59
      print 'Layer name: ' + layer.name() 
60
      print 'Layer abstract: ' + layer.abstract() 
61
      print 'Layer type: ' + layerType[layer.type()]
62
  
63
  # Save project 
64
  print 
65
  print "Save project" 
66
  p.write() 
67
  
68
  QgsApplication.exit() 
69
  print "Done."
70