-
Notifications
You must be signed in to change notification settings - Fork 0
/
toc_management.py
60 lines (45 loc) · 2.34 KB
/
toc_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import os.path
from PyQt4 import QtGui, uic
from PyQt4.QtCore import *
from qgis.utils import iface
from qgis.core import QgsMapLayer, QgsRasterLayer, QgsMapLayerRegistry,QgsProject
from symbology import RasterSymbolizer
# http://www.lutraconsulting.co.uk/blog/2014/07/25/qgis-layer-tree-api-part-2/
def AddGroup(sGroupName, parentGroup):
# If no parent group specified then the parent is the ToC tree root
if not parentGroup:
parentGroup = QgsProject.instance().layerTreeRoot()
# Attempt to find the specified group in the parent
thisGroup = parentGroup.findGroup(sGroupName)
if not thisGroup:
thisGroup = parentGroup.insertGroup(0, sGroupName)
return thisGroup
def AddRasterLayer(theRaster):
# Loop over all the parent group layers for this raster
# ensuring they are in the tree in correct, nested order
parentGroup = None
if len(theRaster.data()) > 0:
for aGroup in theRaster.data()["group_layers"]:
parentGroup = AddGroup(aGroup, parentGroup)
assert parentGroup, "All rasters should be nested and so parentGroup should be instantiated by now"
# Only add the layer if it's not already in the registry
if not QgsMapLayerRegistry.instance().mapLayersByName(theRaster.text()):
rOutput = QgsRasterLayer(theRaster.data()["filepath"], theRaster.text())
QgsMapLayerRegistry.instance().addMapLayer(rOutput, False)
parentGroup.addLayer(rOutput)
# call Konrad's symbology method here using data()["symbology"]
RasterSymbolizer(rOutput).render_GCD(theRaster.data()["symbology"])
if theRaster.data()["symbology"].lower() == "dem":
demPath, demExtension = os.path.splitext( theRaster.data()["filepath"])
hillshadePath = demPath + "HS" + demExtension
if os.path.isfile(hillshadePath):
rHillshade = QgsRasterLayer(hillshadePath, "Hillshade")
QgsMapLayerRegistry.instance().addMapLayer(rHillshade, False)
lHillshade = parentGroup.addLayer(rHillshade)
legend = iface.legendInterface()
legend.setLayerExpanded(rHillshade, False)
# if the layer already exists trigger a refresh
else:
print "REFRESJH"
QgsMapLayerRegistry.instance().mapLayersByName(theRaster.text())[0].triggerRepaint()