32
32
33
33
from qgis .core import (
34
34
QgsCategorizedSymbolRenderer ,
35
- QgsProject ,
36
35
QgsRasterMarkerSymbolLayer ,
37
36
QgsRuleBasedRenderer ,
38
37
QgsSingleSymbolRenderer ,
@@ -230,11 +229,12 @@ def is_valid_filepath(path: str) -> bool:
230
229
return True
231
230
232
231
233
- def update_symbols_to_embedded (symbol : QgsSymbol , new_path : Path ) -> None :
232
+ def update_symbols_to_relative_embedded (symbol : QgsSymbol , home_path : Path ) -> None :
234
233
"""
235
- Update SVG or Raster symbols layer to embed it in the QGIS project.
234
+ Update SVG or Raster symbols layer to relative path or embed it in the QGIS project.
236
235
Args:
237
236
symbol: The QGIS symbol (from a renderer).
237
+ home_path: QGIS Project home path.
238
238
"""
239
239
if symbol is None :
240
240
return
@@ -248,10 +248,6 @@ def update_symbols_to_embedded(symbol: QgsSymbol, new_path: Path) -> None:
248
248
249
249
source_path = Path (symbol_layer .path ())
250
250
251
- # If the symbol's path is already relative, we have nothing to do
252
- if source_path .is_relative_to (new_path ):
253
- continue
254
-
255
251
# Check if symbol is already embedded
256
252
if str (source_path )[:8 ].startswith ("base64:" ):
257
253
continue
@@ -260,20 +256,25 @@ def update_symbols_to_embedded(symbol: QgsSymbol, new_path: Path) -> None:
260
256
if not source_path .is_file ():
261
257
continue
262
258
263
- with open (source_path , "rb" ) as file :
264
- file_data = file .read ()
265
- encoded_data = base64 .b64encode (file_data ).decode ()
266
- symbol_layer .setPath (f"base64:{ encoded_data } " )
259
+ # If the symbol's path is already relative, we have nothing to do
260
+ if source_path .is_relative_to (str (home_path )):
261
+ symbol_layer .setPath (str (source_path .relative_to (home_path )))
262
+ else :
263
+ with open (source_path , "rb" ) as file :
264
+ file_data = file .read ()
265
+ encoded_data = base64 .b64encode (file_data ).decode ()
266
+ symbol_layer .setPath (f"base64:{ encoded_data } " )
267
267
268
268
269
- def embed_layer_symbols_on_project (
270
- layer : QgsVectorLayer , new_path : Optional [ Path ] = None
269
+ def set_relative_embed_layer_symbols_on_project (
270
+ layer : QgsVectorLayer , project_home : Path
271
271
) -> None :
272
272
"""
273
- Update the paths of symbols to embedded symbols in the QGIS project.
273
+ Update the paths of symbols to relative or embedded symbols in the QGIS project if not relative to project home .
274
274
275
275
Args:
276
276
layer: The QgsVectorLayer to update. The layer is a point layer.
277
+ project_home: QGIS Project home path.
277
278
"""
278
279
279
280
if (
@@ -287,15 +288,10 @@ def embed_layer_symbols_on_project(
287
288
if not renderer :
288
289
return
289
290
290
- if new_path is None :
291
- project = QgsProject .instance ()
292
- project_home = project .homePath ()
293
- new_path = Path (project_home )
294
-
295
291
if isinstance (renderer , QgsSingleSymbolRenderer ):
296
292
symbol = renderer .symbol ()
297
293
if symbol :
298
- update_symbols_to_embedded (symbol = symbol , new_path = new_path )
294
+ update_symbols_to_relative_embedded (symbol , project_home )
299
295
300
296
elif isinstance (renderer , QgsRuleBasedRenderer ):
301
297
for rule in renderer .rootRule ().children ():
@@ -305,16 +301,17 @@ def embed_layer_symbols_on_project(
305
301
continue
306
302
307
303
for symbol in symbols :
308
- update_symbols_to_embedded (symbol = symbol , new_path = new_path )
304
+ update_symbols_to_relative_embedded (symbol , project_home )
309
305
310
306
elif isinstance (renderer , QgsCategorizedSymbolRenderer ):
311
307
categories = renderer .categories ()
312
308
if categories :
313
309
for index in range (len (categories )):
314
- # Get a fresh category. The renderer doesn't update in-place modifications.
310
+ # Get a fresh category.
311
+ # The renderer doesn't update in-place modifications on categorized.
315
312
category = renderer .categories ()[index ]
316
313
symbol = category .symbol ().clone ()
317
- update_symbols_to_embedded (symbol = symbol , new_path = new_path )
314
+ update_symbols_to_relative_embedded (symbol , project_home )
318
315
renderer .updateCategorySymbol (index , symbol )
319
316
320
317
layer .setRenderer (renderer )
0 commit comments