GispoCoding / pytest-qgis

A pytest plugin for testing QGIS python plugins
GNU General Public License v2.0
29 stars 8 forks source link

"pytest-qgis" can't find proj.db. #58

Closed nakamori1024 closed 3 months ago

nakamori1024 commented 5 months ago

Dear developers.

Describe the bug "pytest-qgis" can't find proj.db.

To Reproduce Steps to reproduce the behavior:

  1. Follow the steps below to set up the environment, as outlined in the 'Development Environment' section:

    # Create a virtual environment using the Python that comes with QGIS.
    $ /Applications/QGIS-LTR.app/Contents/MacOS/bin/python3 -m venv .venv --system-site-packages
    $ source .venv/bin/activate
    $ python -m pip install -U pip setuptools
    $ pip install pip-tools
    $ pip-sync requirements.txt requirements-dev.txt
    $ pre-commit install
  2. Place the following files in the 'tests' directory. test_foo.py

    
    from qgis.core import QgsCoordinateReferenceSystem

def test_trans(): dst_crs = QgsCoordinateReferenceSystem("EPSG:4326") print(dst_crs) assert True


3. Execute the test files using the following command:

$ pytest tests/test_foo.py -v -s


**Expected behavior**
I expected to get <QgsCoordinateReferenceSystem: EPSG:4326>, but instead, I received <QgsCoordinateReferenceSystem: invalid>.

After reviewing the output, I encountered the following message:

proj_create_from_database: Cannot find proj.db


**Environment (please complete the following information):**
 - OS: MacOS 14.2
 - Python: 3.9
 - QGIS: 3.28.15

**Additional context**
Add any other context about the problem here.

I think this issue is occurring in the following section of the utils.py file:

DEFAULT_CRS = QgsCoordinateReferenceSystem("EPSG:4326")


It worked after I added a process to search for the path of proj.db like this:

import os import sys

found = False for pypath in sys.path: if found: break check_path = os.path.dirname( os.path.dirname(os.path.dirname(os.path.dirname(pypath))) ) for root, dirs, files in os.walk(check_path): if "proj.db" in files: proj_path = os.path.join(root, "proj.db") os.environ["PROJ_LIB"] = os.path.dirname(proj_path) found = True break



However, I believe the above code is not a universal solution.

I would appreciate your opinion on whether the above should be resolved, including whether it should be addressed.

Thank you for taking the time to read this.
Joonalai commented 5 months ago

Thank you for the detailed issue! This is probably related to this one: https://github.com/GispoCoding/pytest-qgis/issues/56 but now that you have found a proper workaround it definitely helps finding the solution for the bug.

nakamori1024 commented 5 months ago

Thank you for your quick response! I initially thought this might be a separate issue due to differences in QGIS versions. However, I appreciate the reference to issue #56 and will review it for any potential overlap. Looking forward to any updates on this issue. Thank you again for your efforts and for maintaining such a valuable project.

Joonalai commented 5 months ago

Looking forward to any updates on this issue. Thank you again for your efforts and for maintaining such a valuable project.

Thank you! :) I'll do my best to get this fixed soon.

I initially thought this might be a separate issue due to differences in QGIS versions.

I completely missed your QGIS version, you are probably right and this might not be related to that after all.

Joonalai commented 4 months ago

I tried to reproduce the problem with multiple different QGIS versions on Windows 10 and multiple Linux distros including docker image of QGIS 3.28.15 (based on Ubuntu 22.04.3). However I could not reproduce the issue since QGIS was always able to find proj.db.

I tried the script you provided for finding the proj.db automatically, but It tried to find proj.db from weirdest places, including:

Also wrong proj.db versions caused some unwanted error messages, like

GDAL ERROR 1: PROJ: proj_create_from_database: /home//.local/share/containers/storage/volumes/buildx_buildkit_default_state/_data/runc-overlayfs/snapshots/snapshots/22/fs/usr/share/proj/proj.db contains DATABASE.LAYOUT.VERSION.MINOR = 0 whereas a number >= 3 is expected. It comes from another PROJ installation.

Although some libraries, like rasterio provide bundled proj.db, I don't think it is meaningful in this case since correct proj is installed when installing QGIS in most cases.

I think that the most robust way would be to export LIB_PROJ environment variable manually in your environment before running the tests.

nakamori1024 commented 3 months ago

Sorry for the delayed response.

Unfortunately, I still cannot find the proj.db in my environment. However, since you confirmed that it worked in multiple environments without any issues, it might be a problem specific to my environment.

Thank you very much for your support.