Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot resolve GPKG paths in custom path pre-processor #35650

Closed
cxcandid opened this issue Apr 7, 2020 · 9 comments
Closed

Cannot resolve GPKG paths in custom path pre-processor #35650

cxcandid opened this issue Apr 7, 2020 · 9 comments
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! PyQGIS Related to the PyQGIS API

Comments

@cxcandid
Copy link

cxcandid commented Apr 7, 2020

When I try to resolve invalid GPKG paths during opening of a QGIS project, which is stored in the same GPKG file, nothing happens (see code below). Resolving of PostgreSQL layers works fine.

from qgis.core import QgsPathResolver,QgsProject
import re

def my_pathpreprocessor(path):
    projectFileName = QgsProject.instance().fileName()
    newPath = path

    if '.gpkg|' in path and path.startswith('./'):
        gpkgProject = re.findall(r'[^/\\]+\.gpkg',projectFileName)[0]
        gpkgProjectPath = re.sub(r'^[^\:]+\:(.+/)[^/]+\.gpkg.+',r'\1',projectFileName)
        print('Old path: '+path)
        if not gpkgProject in path:
            newPath = re.sub(r'[^/\\]+\.gpkg',gpkgProject,path)
            newPath = gpkgProjectPath + newPath[2:]
            print('New path: '+newPath)

    return newPath

QgsPathResolver.setPathPreprocessor(my_pathpreprocessor)

@cxcandid cxcandid added the Bug Either a bug report, or a bug fix. Let's hope for the latter! label Apr 7, 2020
@cxcandid cxcandid changed the title Cannot resolve GPKG paths in custom path preprocessor (QGIS 3.12.1 Windows) Cannot resolve GPKG paths in custom path pre-processor (QGIS 3.12.1 Windows) Apr 7, 2020
@nyalldawson
Copy link
Collaborator

What do you mean "nothing happens"? You'll need to provide more debugging information here.

I'd suggest moving the "print (Old path)" line outside of the if '.gpkg' block for better debugging info.

@nyalldawson nyalldawson added the Feedback Waiting on the submitter for answers label Apr 7, 2020
@cxcandid
Copy link
Author

cxcandid commented Apr 7, 2020

Thanks for your fast reply!

It's easy to reproduce: store some layers with relative paths together with your QGIS project inside of a GPKG file. Copy the file to another location and rename it. With my code I was trying to repair the broken layer paths.

@gioman gioman removed the Feedback Waiting on the submitter for answers label Apr 7, 2020
@cxcandid
Copy link
Author

cxcandid commented Apr 9, 2020

Ok, it's definitely no bug ... I found the reason for "nothing happens" here: http://osgeo-org.1560.x6.nabble.com/QGIS-Developer-Bug-in-QgsPathResolver-td5402292.html. I'm using relative paths, which is not supported yet. Unfortunately it's not totally clear from API description, that relative paths are not possible to resolve.

It would be nice, if we could resolve relative paths in a future release. Mainly for the reason of automatically repairing QGIS projects being stored in a GPKG together with their data (when the GPKGs are copied and renamed, the relative layer source paths are broken).

@elpaso
Copy link
Contributor

elpaso commented Apr 9, 2020

The relative paths are resolved for GPKGs, what breaks your workflow is the file rename: the project file stored into the geopackage does not know that its datasource paths pointing to itself are not valid anymore. I'd say this is a bug (or an enhancement, if you prefer).

@cxcandid
Copy link
Author

cxcandid commented Apr 9, 2020

@elpaso: you are totally right ... my last comment was wrong... I was a little bit confused and mixed up things. It's not a problem with path resolver here, it's a problem with the custom path pre-processor result being ignored, if the data source path is relative. And this could be a bug ;-)

@elpaso
Copy link
Contributor

elpaso commented Apr 9, 2020

a bug in QGIS or a bug in your custom pre-processor?

@cxcandid
Copy link
Author

cxcandid commented Apr 9, 2020

good question ;-) ... at least the print statements ("Old path:","New path:") show the right values. After the new values are returned, no path change is happening and the bad layer handler is popping up.

@gioman
Copy link
Contributor

gioman commented Jan 2, 2021

What is the status of this report?

@gioman gioman added Feedback Waiting on the submitter for answers PyQGIS Related to the PyQGIS API labels Jan 2, 2021
@cxcandid
Copy link
Author

cxcandid commented Jan 4, 2021

From my perspective, I can live with a simple workaround using a custom Python plugin:

from qgis.core import QgsPathResolver,QgsProject,QgsMessageLog,Qgis
import re

class changeGpkgPath:

    def __init__(self, iface):
        self.gpkgProjectName = ''
        QgsProject.instance().metadataChanged.connect(self.changedMetadata)

    def initGui(self):
        self.processor = QgsPathResolver.setPathPreprocessor(self.my_processor)

    def unload(self):
        QgsPathResolver.removePathPreprocessor(self.processor)

    def changedMetadata(self):
        self.gpkgProjectName = ''
        m = re.search('[^/]+\.gpkg',QgsProject.instance().fileName(),flags=re.IGNORECASE)
        if m:
            self.gpkgProjectName = m.group(0)

    def my_processor(self,path):
        # Replacing GPKG datasources if project is stored in GPKG
        if self.gpkgProjectName:
            if re.search('[^/]+\.gpkg',path,flags=re.IGNORECASE):
                if not re.search(self.gpkgProjectName,path,flags=re.IGNORECASE):
                    QgsMessageLog.logMessage('---------------------------------------------','GPKG',Qgis.Info)
                    QgsMessageLog.logMessage('Replace Layer Source: %s' % path,'GPKG',Qgis.Info)
                    path = re.sub('[^/]+\.gpkg',self.gpkgProjectName,path,flags=re.IGNORECASE)
                    QgsMessageLog.logMessage('with: %s' % path,'GPKG',Qgis.Info)

        return path

But for the majority of users it would be nice to have an inbuild mechanism for storing relative GPKG project layers, that won't get lost after a GPKG project file rename.

@gioman gioman changed the title Cannot resolve GPKG paths in custom path pre-processor (QGIS 3.12.1 Windows) Cannot resolve GPKG paths in custom path pre-processor Jan 4, 2021
@gioman gioman removed the Feedback Waiting on the submitter for answers label Jan 4, 2021
@cxcandid cxcandid closed this as completed Feb 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Either a bug report, or a bug fix. Let's hope for the latter! PyQGIS Related to the PyQGIS API
Projects
None yet
Development

No branches or pull requests

4 participants