Skip to content

Commit acc6271

Browse files
committed
[processing] added algs to set style of layer
1 parent 68e2def commit acc6271

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
from PointsToPaths import PointsToPaths
100100
from PostGISExecuteSQL import PostGISExecuteSQL
101101
from ImportIntoPostGIS import ImportIntoPostGIS
102+
from SetVectorStyle import SetVectorStyle
103+
from SetRasterStyle import SetRasterStyle
102104
# from VectorLayerHistogram import VectorLayerHistogram
103105
# from VectorLayerScatterplot import VectorLayerScatterplot
104106
# from MeanAndStdDevPlot import MeanAndStdDevPlot
@@ -154,7 +156,8 @@ def __init__(self):
154156
RandomPointsLayer(), RandomPointsPolygonsFixed(),
155157
RandomPointsPolygonsVariable(),
156158
RandomPointsAlongLines(), PointsToPaths(),
157-
PostGISExecuteSQL(), ImportIntoPostGIS()
159+
PostGISExecuteSQL(), ImportIntoPostGIS(),
160+
SetVectorStyle(), SetRasterStyle(),
158161
# ------ raster ------
159162
# CreateConstantRaster(),
160163
# ------ graphics ------
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SelectByLocation.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Victor Olaya'
21+
__date__ = 'August 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from PyQt4.QtXml import *
30+
from qgis.core import *
31+
from processing.core.GeoAlgorithm import GeoAlgorithm
32+
from processing.parameters.ParameterFile import ParameterFile
33+
from processing.parameters.ParameterRaster import ParameterRaster
34+
from processing.outputs.OutputRaster import OutputRaster
35+
from processing.tools import dataobjects
36+
from qgis.utils import iface
37+
38+
class SetRasterStyle(GeoAlgorithm):
39+
40+
INPUT = 'INPUT'
41+
STYLE = 'STYLE'
42+
OUTPUT = 'OUTPUT'
43+
44+
45+
def defineCharacteristics(self):
46+
self.allowOnlyOpenedLayers = True
47+
self.name = 'Set style for raster layer'
48+
self.group = 'Raster general tools'
49+
self.addParameter(ParameterRaster(self.INPUT, 'Vector layer'))
50+
self.addParameter(ParameterFile(self.STYLE,
51+
'Style file', False, False, 'qml'))
52+
self.addOutput(OutputRaster(self.OUTPUT, 'Styled layer', True))
53+
54+
def processAlgorithm(self, progress):
55+
filename = self.getParameterValue(self.INPUT)
56+
layer = dataobjects.getObjectFromUri(filename)
57+
58+
style = self.getParameterValue(self.STYLE)
59+
with open(style) as f:
60+
xml = "".join(f.readlines())
61+
d = QDomDocument();
62+
d.setContent(xml);
63+
n = d.firstChild();
64+
layer.readSymbology(n, '')
65+
self.setOutputValue(self.OUTPUT, filename)
66+
iface.mapCanvas().refresh()
67+
iface.legendInterface().refreshLayerSymbology(layer)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SelectByLocation.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Victor Olaya'
21+
__date__ = 'August 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from qgis.core import *
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.parameters.ParameterVector import ParameterVector
32+
from processing.outputs.OutputVector import OutputVector
33+
from processing.parameters.ParameterFile import ParameterFile
34+
from processing.tools import dataobjects
35+
from qgis.utils import iface
36+
37+
class SetVectorStyle(GeoAlgorithm):
38+
39+
INPUT = 'INPUT'
40+
STYLE = 'STYLE'
41+
OUTPUT = 'OUTPUT'
42+
43+
44+
def defineCharacteristics(self):
45+
self.allowOnlyOpenedLayers = True
46+
self.name = 'Set style for vector layer'
47+
self.group = 'Vector general tools'
48+
self.addParameter(ParameterVector(self.INPUT, 'Vector layer',
49+
[ParameterVector.VECTOR_TYPE_ANY]))
50+
self.addParameter(ParameterFile(self.STYLE,
51+
'Style file', False, False, 'qml'))
52+
self.addOutput(OutputVector(self.OUTPUT, 'Styled layer', True))
53+
54+
def processAlgorithm(self, progress):
55+
filename = self.getParameterValue(self.INPUT)
56+
layer = dataobjects.getObjectFromUri(filename)
57+
58+
style = self.getParameterValue(self.STYLE)
59+
layer.loadNamedStyle(style)
60+
61+
self.setOutputValue(self.OUTPUT, filename)
62+
iface.mapCanvas().refresh()
63+
iface.legendInterface().refreshLayerSymbology(layer)

0 commit comments

Comments
 (0)