geopandas / contextily

Context geo-tiles in Python
https://contextily.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
520 stars 82 forks source link

CRSError: The EPSG code is unknown. #174

Closed girardea closed 3 years ago

girardea commented 3 years ago

Hi there!

Thanks for all the good work on contextily!

When running this simple script (from the docs),

import contextily as cx
import geopandas
import matplotlib.pyplot as plt

data_url = "https://ndownloader.figshare.com/files/20232174"

db = geopandas.read_file(data_url)

ax = db.plot(color="red", figsize=(9, 9))
cx.add_basemap(ax, crs=db.crs.to_string())

plt.savefig("test.png")

I get the following error:

ERROR 4: Unable to open EPSG support file gcs.csv.  Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/geopandas/geodataframe.py:577: RuntimeWarning: Sequential read of iterator was interrupted. Resetting iterator. This can negatively impact the performance.
  for feature in features_lst:
Traceback (most recent call last):
  File "rasterio/_crs.pyx", line 285, in rasterio._crs._CRS.from_epsg
  File "rasterio/_err.pyx", line 192, in rasterio._err.exc_wrap_int
rasterio._err.CPLE_AppDefinedError: PROJ: proj_create_from_database: Cannot find proj.db

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    cx.add_basemap(ax, crs=db.crs.to_string())
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/contextily/plotting.py", line 139, in add_basemap
    left, right, bottom, top = _reproj_bb(
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/contextily/plotting.py", line 229, in _reproj_bb
    n_l, n_b, n_r, n_t = transform_bounds(s_crs, t_crs, left, bottom, right, top)
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/rasterio/warp.py", line 177, in transform_bounds
    xs, ys = transform(src_crs, dst_crs, in_xs, in_ys)
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/rasterio/env.py", line 387, in wrapper
    return f(*args, **kwds)
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/rasterio/warp.py", line 62, in transform
    return _transform(src_crs, dst_crs, xs, ys, zs)
  File "rasterio/_base.pyx", line 1394, in rasterio._base._transform
  File "rasterio/_base.pyx", line 1439, in rasterio._base._osr_from_crs
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/rasterio/crs.py", line 496, in from_user_input
    return cls.from_string(value, morph_from_esri_dialect=morph_from_esri_dialect)
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/rasterio/crs.py", line 384, in from_string
    return cls.from_epsg(val)
  File "/home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/rasterio/crs.py", line 333, in from_epsg
    obj._crs = _CRS.from_epsg(code)
  File "rasterio/_crs.pyx", line 287, in rasterio._crs._CRS.from_epsg
rasterio.errors.CRSError: The EPSG code is unknown. PROJ: proj_create_from_database: Cannot find proj.db

From digging on the web, it seems this is not the only project when this happens, but I cannot find a way to solve it here anyway.

Can you help?

I am running Python3.8 on Ubuntu within a Windows Terminal (WSL2). I installed contextily in my virtual environment using pipenv install contextily.

jorisvandenbossche commented 3 years ago

Can you show the output of geopandas.show_versions() and pyproj.show_versions() ?

The issue is an installation problem with PROJ / pyproj (it does not find the correct data files with the information about references systems it needs to reproject)

girardea commented 3 years ago

Thanks for the quick reply. Here they are:

>>> geopandas.show_versions()
Failed `CDLL(libgeos_c.so.1)`
Failed `CDLL(libgeos_c.so)`

SYSTEM INFO
-----------
python     : 3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
executable : /home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/bin/python
machine    : Linux-4.19.104-microsoft-standard-x86_64-with-glibc2.29

GEOS, GDAL, PROJ INFO
---------------------
GEOS       : None
GEOS lib   : None
GDAL       : 2.4.4
GDAL data dir: /home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/fiona/gdal_data
PROJ       : 7.2.0
PROJ data dir: /home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/pyproj/proj_dir/share/proj

PYTHON DEPENDENCIES
-------------------
geopandas  : 0.9.0
pandas     : 1.2.3
fiona      : 1.8.18
numpy      : 1.20.1
shapely    : 1.7.1
rtree      : None
pyproj     : 3.0.0.post1
matplotlib : 3.3.4
mapclassify: None
geopy      : 2.1.0
psycopg2   : 2.8.6 (dt dec pq3 ext lo64)
geoalchemy2: None
pyarrow    : 3.0.0
pygeos     : None
>>> pyproj.show_versions()
pyproj info:
    pyproj: 3.0.0.post1
      PROJ: 7.2.0
  data dir: /home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/lib/python3.8/site-packages/pyproj/proj_dir/share/proj
user_data_dir: /home/pgirardeau/.local/share/proj

System:
    python: 3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
executable: /home/pgirardeau/.local/share/virtualenvs/c0082-WJZ3vvHu/bin/python
   machine: Linux-4.19.104-microsoft-standard-x86_64-with-glibc2.29

Python deps:
       pip: 20.1.1
setuptools: 46.4.0
    Cython: None
girardea commented 3 years ago

Hi @jorisvandenbossche, did the outputs above give you a hint? Thanks!

sinia commented 3 years ago

I was experiencing exactly the same issue (on Linux). Managed to fix this by changing the order of imports, for whatever reason it breaks if you import contextily before geopandas. You should do instead:

import geopandas
import contextily as cx
import matplotlib.pyplot as plt
girardea commented 3 years ago

It works!! Thanks so much @sinia!

Le dim. 21 mars 2021 à 21:14, sinia @.***> a écrit :

I was experiencing exactly the same issue (on Linux). Managed to fix this by changing the order of imports, for whatever reason it breaks if you import contextily before geopandas. You should do instead:

import geopandas import contextily as cx import matplotlib.pyplot as plt

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/geopandas/contextily/issues/174#issuecomment-803652443, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG5SEVESHUSTENHGRCUB45LTEZHSXANCNFSM4YUQQUOA .

jorisvandenbossche commented 3 years ago

@girardea sorry for the late reply here, but good to here there is a workaround

This seems to be a similar issue as what was reported recently here https://github.com/Toblerity/Fiona/issues/1007 about needing to import fiona before rasterio, although there the order is actually the other way around ...

girardea commented 3 years ago

Thanks for your reply! Weird indeed but works out in the end!

Le mer. 31 mars 2021 à 14:26, Joris Van den Bossche < @.***> a écrit :

@girardea https://github.com/girardea sorry for the late reply here, but good to here there is a workaround

This seems to be a similar issue as what was reported recently here Toblerity/Fiona#1007 https://github.com/Toblerity/Fiona/issues/1007 about needing to import fiona before rasterio, although there the order is actually the other way around ...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/geopandas/contextily/issues/174#issuecomment-811028138, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG5SEVG3YHTNXCZZMGVBECTTGMIFTANCNFSM4YUQQUOA .

gregorhd commented 3 years ago

A similar error message popped up for me after running a script which worked perfectly fine a few weeks back. As I was totally stumped, I restarted the computer and it worked. Beats me...

Here my error message:

CRSError: The EPSG code is unknown. PROJ: proj_create_from_database: 
SQLite error on SELECT name, coordinate_system_auth_name, coordinate_system_code, 
geodetic_crs_auth_name, geodetic_crs_code, conversion_auth_name, conversion_code, 
area_of_use_auth_name, area_of_use_code, text_definition, deprecated FROM projected_crs 
WHERE auth_name = ? AND code = ?: no such column: area_of_use_auth_name