How to resolve "Cannot find proj.db" error using pytest-qgis?

53 Views Asked by At

Issue

I wrote a test code expression like

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

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

Believing this to be the cause, do you know how to fix this error?

I would prefer not to use the method of manually entering environment variables, as it compromises the flexibility to work across various environments.

Thank you for taking the time to read this.

Steps to reproduce the issue

  1. Clone the pytest-qgis repository using the following command:

    $ git clone [email protected]:GispoCoding/pytest-qgis.git

  2. Move to pytest-qgis

    $ cd pytest-qgis

  3. 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.
    $ /your/QGIS/python/path -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
    
  4. 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
    
  5. Execute the test files using the following command:

    $ pytest tests/test_foo.py -v -s
    
1

There are 1 best solutions below

3
Adam Basha On

This error message means that proj.db doesn't exists. You can resolve it by create the file and placing the values. Or if you don't want to manually create the file, you can write a python script to create the file. Also I think this might help you: https://gis.stackexchange.com/questions/378463/cannot-find-proj-db-and-error-1-proj-proj-create-from-database-cannot-buil

import sqlite3

# Connect to the database (creates a new database if not exists)
conn = sqlite3.connect('proj.db')

# Create a cursor object to execute SQL queries
cursor = conn.cursor()

# Create a table
cursor.execute('''CREATE TABLE IF NOT EXISTS example_table (
                    id INTEGER PRIMARY KEY,
                    name TEXT,
                    age INTEGER
                )''')

# Insert some values into the table
data = [
    ('John', 30),
    ('Alice', 25),
    ('Bob', 35)
]

cursor.executemany('INSERT INTO example_table (name, age) VALUES (?, ?)', data)

# Commit changes and close the connection
conn.commit()
conn.close()

print("Values inserted successfully.")

And don't forget to change the data and table name.