3232
3333from qgis .core import (
3434 QgsCategorizedSymbolRenderer ,
35- QgsProject ,
3635 QgsRasterMarkerSymbolLayer ,
3736 QgsRuleBasedRenderer ,
3837 QgsSingleSymbolRenderer ,
@@ -230,11 +229,12 @@ def is_valid_filepath(path: str) -> bool:
230229 return True
231230
232231
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 :
234233 """
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.
236235 Args:
237236 symbol: The QGIS symbol (from a renderer).
237+ home_path: QGIS Project home path.
238238 """
239239 if symbol is None :
240240 return
@@ -248,10 +248,6 @@ def update_symbols_to_embedded(symbol: QgsSymbol, new_path: Path) -> None:
248248
249249 source_path = Path (symbol_layer .path ())
250250
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-
255251 # Check if symbol is already embedded
256252 if str (source_path )[:8 ].startswith ("base64:" ):
257253 continue
@@ -260,20 +256,25 @@ def update_symbols_to_embedded(symbol: QgsSymbol, new_path: Path) -> None:
260256 if not source_path .is_file ():
261257 continue
262258
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 } " )
267267
268268
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
271271) -> None :
272272 """
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 .
274274
275275 Args:
276276 layer: The QgsVectorLayer to update. The layer is a point layer.
277+ project_home: QGIS Project home path.
277278 """
278279
279280 if (
@@ -287,15 +288,10 @@ def embed_layer_symbols_on_project(
287288 if not renderer :
288289 return
289290
290- if new_path is None :
291- project = QgsProject .instance ()
292- project_home = project .homePath ()
293- new_path = Path (project_home )
294-
295291 if isinstance (renderer , QgsSingleSymbolRenderer ):
296292 symbol = renderer .symbol ()
297293 if symbol :
298- update_symbols_to_embedded (symbol = symbol , new_path = new_path )
294+ update_symbols_to_relative_embedded (symbol , project_home )
299295
300296 elif isinstance (renderer , QgsRuleBasedRenderer ):
301297 for rule in renderer .rootRule ().children ():
@@ -305,16 +301,17 @@ def embed_layer_symbols_on_project(
305301 continue
306302
307303 for symbol in symbols :
308- update_symbols_to_embedded (symbol = symbol , new_path = new_path )
304+ update_symbols_to_relative_embedded (symbol , project_home )
309305
310306 elif isinstance (renderer , QgsCategorizedSymbolRenderer ):
311307 categories = renderer .categories ()
312308 if categories :
313309 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.
315312 category = renderer .categories ()[index ]
316313 symbol = category .symbol ().clone ()
317- update_symbols_to_embedded (symbol = symbol , new_path = new_path )
314+ update_symbols_to_relative_embedded (symbol , project_home )
318315 renderer .updateCategorySymbol (index , symbol )
319316
320317 layer .setRenderer (renderer )
0 commit comments