0

I wrote a script that takes a vector layer already present in my QGIS project and refactor fields as I wish. I created the script to avoid doing plenty of times the same refactorization manually every time, since this kind of refactoization occurs sistematically. That's the code I wrote:

#!/usr/bin/python
# -*- coding: ascii -*-

from qgis.core import QgsVectorLayer, QgsProject
import processing

input = "Compendio_Fx"
output = ".\\Compendio_NomeComune_invio_dati.gpkg"  

processing.run("native:refactorfields", {'INPUT': input, 'FIELDS_MAPPING': 
[{'expression':'\"ID_OGGETTO\"','name':"ID_OGGETTO_COMPARTO", 'length':17, 'precision':0,'type':10},
{'expression': '\"SUL_realiz\"','name': 'SUL_REALIZZATA','length': 10,'precision': 3,'type': 6},
{'expression': '\"VOL_potenz\"','name': 'VOLUME_POTENZIALE','length': 10,'precision': 3,'type': 6},
{'expression': '\"Trav_VOL\"','name': 'TRAVASO_VOLUME','length': 10,'precision': 3,'type': 6},
{'expression': '\"VOL_realiz\"','name': 'VOLUME_REALIZZATO','length': 10,'precision': 3,'type': 6},
{'expression': '\"Computo\"','name': 'TIPO_COMPUTO_SUL_R_VOL_R','length': 255,'precision': 0,'type': 10},
{'expression': '\"TX_SFRU\"','name': 'TASSO_SFRUTTAMENTO','length': 10,'precision': 3,'type': 6},
{'expression': '\"Stat_costr\"','name': 'STATO_COSTRUZIONE','length': 255,'precision': 0,'type': 10},
{'expression': '\"Stat_urb\"','name': 'STATO_URBANIZZAZIONE','length': 255,'precision': 0,'type': 10},
{'expression': '\"COD_ZonaTI\"','name': 'COD_ZONA_CANTONE','length': 30,'precision': 0,'type': 10},
{'expression': '\"Note\"','name': 'TESTO_SUPPLEMENTARE','length': 255,'precision': 0,'type': 10},
{'expression': '\"IS_Fx\"','name': 'INDICE_SFRUTTAMENTO_MASSIMO','length': 10,'precision': 3,'type': 6},
{'expression': '\"IE_Fx\"','name': 'INDICE_EDIFICABILITA_MASSIMO','length': 10,'precision': 3,'type': 6},
{'expression': '\"G.A.\"','name': 'CORREZIONE_POTENZIALE_SUL_VOL','length': 10,'precision': 3,'type': 6},
{'expression': '\"Consumo_AB\"','name': 'CONSUMO_TEO_UI_ABITAZIONE','length': 10,'precision': 3,'type': 6},
{'expression': '\"Consumo_LA\"','name': 'CONSUMO_TEO_UI_LAVORO','length': 10,'precision': 3,'type': 6},
{'expression': '\"Consumo_TU\"','name': 'CONSUMO_TEO_UI_TURISMO','length': 10,'precision': 3,'type': 6},
{'expression': '\"Quota_AB\"','name': 'IPOTESI_QUOTA_ABITANTI','length': 10,'precision': 3,'type': 6},
{'expression': '\"Quota_LA\"','name': 'IPOTESI_QUOTA_LAVORO','length': 10,'precision': 3,'type': 6},
{'expression': '\"Quota_TU\"','name': 'IPOTESI_QUOTA_TURISMO','length': 10,'precision': 3,'type': 6},
{'expression': '\"TX_SFRU_15\"','name': 'TASSO_SFRUTTAMENTO_15_ANNI','length': 10,'precision': 3,'type': 6}], 
'OUTPUT': output})

Everithing works fine as long as I'm working on my Windows machine, where I also elaborated the script (and done all my tries before to succeed the task).

As I shared the code to a friend of mine working on the same project, but with a Mac computer instead, but the execution of the same script, on the same project contained in the same folder (so no problems of path compatibility) gives him this error message:

Traceback (most recent call last):
  File “/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/code.py”, line 90, in runcode
    exec(code, self.locals)
  File “<input>“, line 1, in <module>
  File “<string>“, line 10, in <module>
  File “/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/plugins/processing/tools/general.py”, line 108, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)
  File “/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/plugins/processing/core/Processing.py”, line 174, in runAlgorithm
    ret, results = execute(alg, parameters, context, feedback, catch_exceptions=False)
  File “/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/plugins/processing/gui/AlgorithmExecutor.py”, line 71, in execute
    results, ok = alg.run(parameters, context, feedback, {}, False)
_core.QgsProcessingException: Impossibile creare il layer .\Compendio_NomeComune_invio_dati.gpkg: Creazione della sorgente dati fallita (errore OGR: sqlite3_open(.\Compendio_NomeComune_invio_dati.gpkg) failed: unable to open database file)

Someone can explain me why this happens on Mac and how to solve this kind of issue?

2
  • Remove the backslash? output = "Compendio_NomeComune_invio_dati.gpkg"
    – user2856
    May 13, 2022 at 8:11
  • It doesn't work. I've also tried these options: output = "./Compendio_NomeComune_invio_dati.gpkg" output = ".//Compendio_NomeComune_invio_dati.gpkg" output = ".\Compendio_NomeComune_invio_dati.gpkg" none of these options worked... I thought that the first option I wrote (output = "./Compendio_NomeComune_invio_dati.gpkg") was the one, since relative paths on mac can be defined this way. Unfortunately it's not the case, apparently.
    – MiroD
    May 16, 2022 at 7:17

1 Answer 1

0

I found a solution, even if it doesn't solve all my doubts.

The mac computer does not seem to recognise the relative path given, in any of the tried formulations (".\", ".", ".//", "./"). To bypass the problem, I called the function returning the path of the project I'm working on:

proj = QgsProject.instance().fileName()

and then another function, that simply gives me the path of the directory of the path:

path = os.path.dirname(proj)

Now, instead of providing the relative path, I can compose the path of the output file as I wish in this way:

output = path + "/Compendio_NomeComune_invio_dati.gpkg" 

Anyway, if someone can help me understand what is the probem when passing a relative path to a apple machine, it will be great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.