@@ -149,7 +149,7 @@ def read(self, map_prefix, theme_item, edit_datasets):
149149 return {
150150 "project_crs" : self .__project_crs (root ),
151151 "print_templates" : self .__print_templates (root , shortname_map , theme_item ),
152- "visibility_presets" : self .__visibility_presets (root ),
152+ "visibility_presets" : self .__visibility_presets (root , theme_item ),
153153 "layer_metadata" : self .__layer_metadata (root , shortname_map , map_prefix , edit_datasets , theme_item , qgs_dir ),
154154 }
155155
@@ -234,47 +234,50 @@ def __print_templates(self, root, shortname_map, theme_item):
234234 ]
235235
236236
237- def __visibility_presets (self , root ):
237+ def __visibility_presets (self , root , theme_item ):
238238 """ Read layer visibility presets from QGS. """
239239 visibilityPresets = root .find ('./visibility-presets' )
240240 if visibilityPresets is None :
241241 return {}
242242
243243 # layerId => (short)name map
244- layerMap = {}
244+ layer_map = {}
245245 for mapLayer in root .findall ('.//maplayer' ):
246246 layerId = mapLayer .find ('./id' )
247247 if layerId is not None :
248248 if mapLayer .find ('shortname' ) is not None :
249- layerMap [layerId .text ] = mapLayer .find ('shortname' ).text
249+ layer_map [layerId .text ] = mapLayer .find ('shortname' ).text
250250 elif mapLayer .find ('layername' ) is not None :
251- layerMap [layerId .text ] = mapLayer .find ('layername' ).text
252-
253- def map_tree_groups (parent , parent_path = []):
254- for group in parent .findall ('.//layer-tree-group' ):
255- if group .get ('name' ):
256- path = list (parent_path ) + [group .get ('name' )]
257- shortname = group .find ('./shortname' )
258- if shortname is not None :
259- layerMap ["/" .join (path )] = shortname .text
260- else :
261- layerMap ["/" .join (path )] = group .get ('name' )
262- map_tree_groups (group , path )
251+ layer_map [layerId .text ] = mapLayer .find ('layername' ).text
252+
253+ tree = root .find ('layer-tree-group' )
254+ parent_map = {c : p for p in tree .iter () for c in p }
255+
256+ def layer_path (layer_id ):
257+ child = tree .find (".//layer-tree-layer[@id='%s']" % layer_id )
258+ if child is None :
259+ return None
260+ path = [layer_map [layer_id ]]
261+ while (parent := parent_map .get (child )) is not None :
262+ path .insert (0 , parent .get ('name' ))
263+ child = parent
264+ return "/" .join (path [1 :])
263265
264- map_tree_groups (root )
265266
267+ hidden_layers = theme_item .get ('layerTreeHiddenSublayers' , [])
266268 result = {}
267269 for visibilityPreset in visibilityPresets .findall ('./visibility-preset' ):
268270 name = visibilityPreset .get ('name' )
269271 result [name ] = {}
270272 for layer in visibilityPreset .findall ('./layer' ):
271- layerId = layer .get ('id' )
272- if layer .get ('visible' ) == "1" and layerId in layerMap :
273- result [name ][layerMap [layerId ]] = layer .get ('style' )
273+ layer_id = layer .get ('id' )
274+ path = layer_path (layer_id )
275+ if layer_map [layer_id ] not in hidden_layers and layer .get ('visible' ) == "1" and path :
276+ result [name ][path ] = layer .get ('style' )
274277 for checkedGroupNode in visibilityPreset .findall ('./checked-group-nodes/checked-group-node' ):
275278 groupid = checkedGroupNode .get ('id' )
276- if groupid is not None and groupid in layerMap :
277- result [name ][layerMap [ groupid ] ] = ""
279+ if groupid is not None and os . path . basename ( groupid ) not in hidden_layers :
280+ result [name ][groupid ] = ""
278281
279282 return result
280283
0 commit comments