-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.py
196 lines (188 loc) · 9.16 KB
/
style.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from qgis.core import QgsRasterRenderer, QgsContrastEnhancement, QgsRasterBandStats
from PyQt5 import QtWidgets
def apply_xml_config(layer):
xml_config = """
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis minScale="1e+08" hasScaleBasedVisibilityFlag="0" maxScale="0" version="3.28.9-Firenze" styleCategories="AllStyleCategories">
<flags>
<Identifiable>1</Identifiable>
<Removable>1</Removable>
<Searchable>1</Searchable>
<Private>0</Private>
</flags>
<temporal fetchMode="0" mode="0" enabled="0">
<fixedRange>
<start></start>
<end></end>
</fixedRange>
</temporal>
<elevation symbology="Line" zscale="1" band="1" enabled="0" zoffset="0">
<data-defined-properties>
<Option type="Map">
<Option type="QString" name="name" value=""/>
<Option name="properties"/>
<Option type="QString" name="type" value="collection"/>
</Option>
</data-defined-properties>
<profileLineSymbol>
<symbol force_rhr="0" type="line" clip_to_extent="1" is_animated="0" alpha="1" frame_rate="10" name="">
<data_defined_properties>
<Option type="Map">
<Option type="QString" name="name" value=""/>
<Option name="properties"/>
<Option type="QString" name="type" value="collection"/>
</Option>
</data_defined_properties>
<layer pass="0" class="SimpleLine" locked="0" enabled="1">
<Option type="Map">
<Option type="QString" name="align_dash_pattern" value="0"/>
<Option type="QString" name="capstyle" value="square"/>
<Option type="QString" name="customdash" value="5;2"/>
<Option type="QString" name="customdash_map_unit_scale" value="3x:0,0,0,0,0,0"/>
<Option type="QString" name="customdash_unit" value="MM"/>
<Option type="QString" name="dash_pattern_offset" value="0"/>
<Option type="QString" name="dash_pattern_offset_map_unit_scale" value="3x:0,0,0,0,0,0"/>
<Option type="QString" name="dash_pattern_offset_unit" value="MM"/>
<Option type="QString" name="draw_inside_polygon" value="0"/>
<Option type="QString" name="joinstyle" value="bevel"/>
<Option type="QString" name="line_color" value="225,89,137,255"/>
<Option type="QString" name="line_style" value="solid"/>
<Option type="QString" name="line_width" value="0.6"/>
<Option type="QString" name="line_width_unit" value="MM"/>
<Option type="QString" name="offset" value="0"/>
<Option type="QString" name="offset_map_unit_scale" value="3x:0,0,0,0,0,0"/>
<Option type="QString" name="offset_unit" value="MM"/>
<Option type="QString" name="ring_filter" value="0"/>
<Option type="QString" name="trim_distance_end" value="0"/>
<Option type="QString" name="trim_distance_end_map_unit_scale" value="3x:0,0,0,0,0,0"/>
<Option type="QString" name="trim_distance_end_unit" value="MM"/>
<Option type="QString" name="trim_distance_start" value="0"/>
<Option type="QString" name="trim_distance_start_map_unit_scale" value="3x:0,0,0,0,0,0"/>
<Option type="QString" name="trim_distance_start_unit" value="MM"/>
<Option type="QString" name="tweak_dash_pattern_on_corners" value="0"/>
<Option type="QString" name="use_custom_dash" value="0"/>
<Option type="QString" name="width_map_unit_scale" value="3x:0,0,0,0,0,0"/>
</Option>
<data_defined_properties>
<Option type="Map">
<Option type="QString" name="name" value=""/>
<Option name="properties"/>
<Option type="QString" name="type" value="collection"/>
</Option>
</data_defined_properties>
</layer>
</symbol>
</profileLineSymbol>
<profileFillSymbol>
<symbol force_rhr="0" type="fill" clip_to_extent="1" is_animated="0" alpha="1" frame_rate="10" name="">
<data_defined_properties>
<Option type="Map">
<Option type="QString" name="name" value=""/>
<Option name="properties"/>
<Option type="QString" name="type" value="collection"/>
</Option>
</data_defined_properties>
<layer pass="0" class="SimpleFill" locked="0" enabled="1">
<Option type="Map">
<Option type="QString" name="border_width_map_unit_scale" value="3x:0,0,0,0,0,0"/>
<Option type="QString" name="color" value="225,89,137,255"/>
<Option type="QString" name="joinstyle" value="bevel"/>
<Option type="QString" name="offset" value="0,0"/>
<Option type="QString" name="offset_map_unit_scale" value="3x:0,0,0,0,0,0"/>
<Option type="QString" name="offset_unit" value="MM"/>
<Option type="QString" name="outline_color" value="35,35,35,255"/>
<Option type="QString" name="outline_style" value="no"/>
<Option type="QString" name="outline_width" value="0.26"/>
<Option type="QString" name="outline_width_unit" value="MM"/>
<Option type="QString" name="style" value="solid"/>
</Option>
<data_defined_properties>
<Option type="Map">
<Option type="QString" name="name" value=""/>
<Option name="properties"/>
<Option type="QString" name="type" value="collection"/>
</Option>
</data_defined_properties>
</layer>
</symbol>
</profileFillSymbol>
</elevation>
<customproperties>
<Option type="Map">
<Option type="bool" name="WMSBackgroundLayer" value="false"/>
<Option type="bool" name="WMSPublishDataSourceUrl" value="false"/>
<Option type="int" name="embeddedWidgets/count" value="0"/>
<Option type="QString" name="identify/format" value="Value"/>
</Option>
</customproperties>
<pipe-data-defined-properties>
<Option type="Map">
<Option type="QString" name="name" value=""/>
<Option name="properties"/>
<Option type="QString" name="type" value="collection"/>
</Option>
</pipe-data-defined-properties>
<pipe>
<provider>
<resampling zoomedInResamplingMethod="nearestNeighbour" maxOversampling="2" zoomedOutResamplingMethod="nearestNeighbour" enabled="false"/>
</provider>
<rasterrenderer opacity="1" redBand="1" type="multibandcolor" alphaBand="-1" blueBand="3" greenBand="2" nodataColor="">
<rasterTransparency/>
<minMaxOrigin>
<limits>MinMax</limits>
<extent>WholeRaster</extent>
<statAccuracy>Estimated</statAccuracy>
<cumulativeCutLower>0.02</cumulativeCutLower>
<cumulativeCutUpper>0.98</cumulativeCutUpper>
<stdDevFactor>2</stdDevFactor>
</minMaxOrigin>
<redContrastEnhancement>
<minValue>42</minValue>
<maxValue>255</maxValue>
<algorithm>StretchToMinimumMaximum</algorithm>
</redContrastEnhancement>
<greenContrastEnhancement>
<minValue>57</minValue>
<maxValue>255</maxValue>
<algorithm>StretchToMinimumMaximum</algorithm>
</greenContrastEnhancement>
<blueContrastEnhancement>
<minValue>90</minValue>
<maxValue>255</maxValue>
<algorithm>StretchToMinimumMaximum</algorithm>
</blueContrastEnhancement>
</rasterrenderer>
<brightnesscontrast contrast="0" gamma="1" brightness="0"/>
<huesaturation colorizeStrength="100" invertColors="0" grayscaleMode="0" saturation="0" colorizeRed="255" colorizeBlue="128" colorizeOn="0" colorizeGreen="128"/>
<rasterresampler maxOversampling="2"/>
<resamplingStage>resamplingFilter</resamplingStage>
</pipe>
<blendMode>0</blendMode>
</qgis>
"""
layer.loadNamedStyle(xml_config)
layer.triggerRepaint()
def apply_layer_settings(layer):
# Setting visibility range using custom properties
layer.setCustomProperty("minimumScale", 1e+08)
layer.setCustomProperty("maximumScale", 0)
# Setting contrast enhancement for each band in a multi-band renderer
renderer = layer.renderer()
if isinstance(renderer, QgsMultiBandColorRenderer):
for i in range(1, 4): # Assuming 3 bands: Red, Green, Blue
contrast_enhancement = QgsContrastEnhancement(renderer.dataType(i))
contrast_enhancement.setContrastEnhancementAlgorithm(QgsContrastEnhancement.StretchToMinimumMaximum)
# Fetching statistics to set min and max values
provider = layer.dataProvider()
stats = provider.bandStatistics(i, QgsRasterBandStats.All, layer.extent(), 0)
contrast_enhancement.setMinimumValue(stats.minimumValue)
contrast_enhancement.setMaximumValue(stats.maximumValue)
renderer.setContrastEnhancement(i, contrast_enhancement)
layer.triggerRepaint()
# Setting other custom properties
layer.setCustomProperty("WMSBackgroundLayer", False)
layer.setCustomProperty("WMSPublishDataSourceUrl", False)
layer.setCustomProperty("embeddedWidgets/count", 0)
layer.setCustomProperty("identify/format", "Value")
# Setting map tip visibility
layer.setMapTipTemplate('')