mdbartos / pysheds

:earth_americas: Simple and fast watershed delineation in python.
GNU General Public License v3.0
717 stars 196 forks source link

Still experienceing CRS error. #135

Open Trevy05 opened 3 years ago

Trevy05 commented 3 years ago

When I try loading my flow direction grid I get the error: CRSError Traceback (most recent call last)

in ----> 1 grid.read_raster('C:/Users/18683/Desktop/Datasets/DEM/Flow_direction.tif', data_name='dir') ~\miniconda3\lib\site-packages\pysheds\grid.py in read_raster(self, data, data_name, band, window, window_crs, metadata, **kwargs) 270 with rasterio.open(data, **kwargs) as f: 271 crs = pyproj.Proj(f.crs, preserve_units=True) --> 272 if window is None: 273 shape = f.shape 274 if len(f.indexes) > 1: ~\miniconda3\lib\site-packages\pyproj\proj.py in __init__(self, projparams, preserve_units, **kwargs) 105 '116.366 39.867' 106 """ --> 107 self.crs = CRS.from_user_input(projparams, **kwargs) 108 # make sure units are meters if preserve_units is False. 109 if not preserve_units and "foot" in self.crs.axis_info[0].unit_name: ~\miniconda3\lib\site-packages\pyproj\crs\crs.py in from_user_input(value, **kwargs) 438 if isinstance(value, CRS): 439 return value --> 440 return CRS(value, **kwargs) 441 442 def get_geod(self) -> Optional[Geod]: ~\miniconda3\lib\site-packages\pyproj\crs\crs.py in __init__(self, projparams, **kwargs) 294 projstring = _prepare_from_string(" ".join((projstring, projkwargs))) 295 --> 296 super().__init__(projstring) 297 298 @staticmethod pyproj/_crs.pyx in pyproj._crs._CRS.__init__() CRSError: Invalid projection: : (Internal Proj Error: proj_create: unrecognized format / unknown name)
bmalbusca commented 3 years ago

Hi, can you format the error log to markdown code (using `)? Also, can you show how is your flow direction ?

mdbartos commented 2 years ago

Greetings, I need more info to address this issue.

Was this raster one that you generated yourself using pysheds/rasterio?

T4mmi commented 2 years ago

Hi there, trying out this package for flow accumulation computation ... can't pass importing, seems to be something wrong with pyproj default SRS steps to reproduce (python 3.9, pysheds 0.3.2):

conda create -n testenv python=3.9
conda activate testenv
conda install pysheds ipython -y
ipython
from pysheds.grid import Grid

then get (dont mind conda env, i installed everything in my base env):

In [3]: from pysheds.grid import Grid
D:\Conda\lib\site-packages\pyproj\__init__.py:89: UserWarning: pyproj unable to set database path.
  _pyproj_global_context_initialize()
---------------------------------------------------------------------------
CRSError                                  Traceback (most recent call last)
Input In [3], in <module>
----> 1 from pysheds.grid import Grid

File D:\Conda\lib\site-packages\pysheds\grid.py:7, in <module>
      5     _HAS_NUMBA = False
      6 if _HAS_NUMBA:
----> 7     from pysheds.sgrid import sGrid as Grid
      8 else:
      9     from pysheds.pgrid import Grid as Grid

File D:\Conda\lib\site-packages\pysheds\sgrid.py:29, in <module>
     26 _pyproj_init = '+init=epsg:4326' if _OLD_PYPROJ else 'epsg:4326'
     28 # Import input/output functions
---> 29 import pysheds.io
     31 # Import viewing functions
     32 from pysheds.sview import Raster, MultiRaster

File D:\Conda\lib\site-packages\pysheds\io.py:9, in <module>
      7 from affine import Affine
      8 from distutils.version import LooseVersion
----> 9 from pysheds.sview import Raster, ViewFinder, View
     11 _OLD_PYPROJ = LooseVersion(pyproj.__version__) < LooseVersion('2.2')
     12 _pyproj_crs = lambda Proj: Proj.crs if not _OLD_PYPROJ else Proj

File D:\Conda\lib\site-packages\pysheds\sview.py:304, in <module>
    301         obj.metadata = metadata
    302         return obj
--> 304 class ViewFinder():
    305     """
    306     Class that defines a spatial reference system for a Raster or Grid instance.
    307     The spatial reference is completely defined by an affine transformation matrix (affine),
   (...)
    331     dy_dx : Tuple describing the cell size in the y and x directions.
    332     """
    333     def __init__(self, affine=Affine(1., 0., 0., 0., 1., 0.), shape=(1,1),
    334                  nodata=0, mask=None, crs=pyproj.Proj(_pyproj_init)):

File D:\Conda\lib\site-packages\pysheds\sview.py:334, in ViewFinder()
    304 class ViewFinder():
    305     """
    306     Class that defines a spatial reference system for a Raster or Grid instance.
    307     The spatial reference is completely defined by an affine transformation matrix (affine),
   (...)
    331     dy_dx : Tuple describing the cell size in the y and x directions.
    332     """
    333     def __init__(self, affine=Affine(1., 0., 0., 0., 1., 0.), shape=(1,1),
--> 334                  nodata=0, mask=None, crs=pyproj.Proj(_pyproj_init)):
    335         self.affine = affine
    336         self.crs = crs

File D:\Conda\lib\site-packages\pyproj\proj.py:109, in Proj.__init__(self, projparams, preserve_units, **kwargs)
     47 def __init__(
     48     self, projparams: Any = None, preserve_units: bool = True, **kwargs
     49 ) -> None:
     50     """
     51     A Proj class instance is initialized with proj map projection
     52     control parameter key/value pairs. The key/value pairs can
   (...)
    107     '116.366 39.867'
    108     """
--> 109     self.crs = CRS.from_user_input(projparams, **kwargs)
    110     # make sure units are meters if preserve_units is False.
    111     if not preserve_units and "foot" in self.crs.axis_info[0].unit_name:
    112         # ignore export to PROJ string deprecation warning

File D:\Conda\lib\site-packages\pyproj\crs\crs.py:479, in CRS.from_user_input(cls, value, **kwargs)
    477 if isinstance(value, cls):
    478     return value
--> 479 return cls(value, **kwargs)

File D:\Conda\lib\site-packages\pyproj\crs\crs.py:326, in CRS.__init__(self, projparams, **kwargs)
    324     self._local.crs = projparams
    325 else:
--> 326     self._local.crs = _CRS(self.srs)

File pyproj\_crs.pyx:2352, in pyproj._crs._CRS.__init__()

CRSError: Invalid projection: epsg:4326: (Internal Proj Error: proj_create: no database context specified)

In [4]: from pysheds.grid import Grid
---------------------------------------------------------------------------
CRSError                                  Traceback (most recent call last)
Input In [4], in <module>
----> 1 from pysheds.grid import Grid

File D:\Conda\lib\site-packages\pysheds\grid.py:7, in <module>
      5     _HAS_NUMBA = False
      6 if _HAS_NUMBA:
----> 7     from pysheds.sgrid import sGrid as Grid
      8 else:
      9     from pysheds.pgrid import Grid as Grid

File D:\Conda\lib\site-packages\pysheds\sgrid.py:29, in <module>
     26 _pyproj_init = '+init=epsg:4326' if _OLD_PYPROJ else 'epsg:4326'
     28 # Import input/output functions
---> 29 import pysheds.io
     31 # Import viewing functions
     32 from pysheds.sview import Raster, MultiRaster

File D:\Conda\lib\site-packages\pysheds\io.py:9, in <module>
      7 from affine import Affine
      8 from distutils.version import LooseVersion
----> 9 from pysheds.sview import Raster, ViewFinder, View
     11 _OLD_PYPROJ = LooseVersion(pyproj.__version__) < LooseVersion('2.2')
     12 _pyproj_crs = lambda Proj: Proj.crs if not _OLD_PYPROJ else Proj

File D:\Conda\lib\site-packages\pysheds\sview.py:304, in <module>
    301         obj.metadata = metadata
    302         return obj
--> 304 class ViewFinder():
    305     """
    306     Class that defines a spatial reference system for a Raster or Grid instance.
    307     The spatial reference is completely defined by an affine transformation matrix (affine),
   (...)
    331     dy_dx : Tuple describing the cell size in the y and x directions.
    332     """
    333     def __init__(self, affine=Affine(1., 0., 0., 0., 1., 0.), shape=(1,1),
    334                  nodata=0, mask=None, crs=pyproj.Proj(_pyproj_init)):

File D:\Conda\lib\site-packages\pysheds\sview.py:334, in ViewFinder()
    304 class ViewFinder():
    305     """
    306     Class that defines a spatial reference system for a Raster or Grid instance.
    307     The spatial reference is completely defined by an affine transformation matrix (affine),
   (...)
    331     dy_dx : Tuple describing the cell size in the y and x directions.
    332     """
    333     def __init__(self, affine=Affine(1., 0., 0., 0., 1., 0.), shape=(1,1),
--> 334                  nodata=0, mask=None, crs=pyproj.Proj(_pyproj_init)):
    335         self.affine = affine
    336         self.crs = crs

File D:\Conda\lib\site-packages\pyproj\proj.py:109, in Proj.__init__(self, projparams, preserve_units, **kwargs)
     47 def __init__(
     48     self, projparams: Any = None, preserve_units: bool = True, **kwargs
     49 ) -> None:
     50     """
     51     A Proj class instance is initialized with proj map projection
     52     control parameter key/value pairs. The key/value pairs can
   (...)
    107     '116.366 39.867'
    108     """
--> 109     self.crs = CRS.from_user_input(projparams, **kwargs)
    110     # make sure units are meters if preserve_units is False.
    111     if not preserve_units and "foot" in self.crs.axis_info[0].unit_name:
    112         # ignore export to PROJ string deprecation warning

File D:\Conda\lib\site-packages\pyproj\crs\crs.py:479, in CRS.from_user_input(cls, value, **kwargs)
    477 if isinstance(value, cls):
    478     return value
--> 479 return cls(value, **kwargs)

File D:\Conda\lib\site-packages\pyproj\crs\crs.py:326, in CRS.__init__(self, projparams, **kwargs)
    324     self._local.crs = projparams
    325 else:
--> 326     self._local.crs = _CRS(self.srs)

File pyproj\_crs.pyx:2352, in pyproj._crs._CRS.__init__()

CRSError: Invalid projection: epsg:4326: (Internal Proj Error: proj_create: no database context specified)
T4mmi commented 2 years ago

Ok, this is a pyproj error : https://github.com/geopandas/geopandas/issues/2270 ... but the explanation for fixup is shady and NOT documented : https://pyproj4.github.io/pyproj/stable/gotchas.html#internal-proj-error-sqlite-error-on-select

[edit] : Uninstalling postgis solved my problem !

kavyajeetbora commented 1 year ago

I had the same issue. My application was running in my system but not in remote machine as it had postgis installed in it and PROJ path was redirected to C:\Program Files\PostgreSQL\... I overcame with this simple import in my code:

import geopandas as gpd
import pyproj
pyproj.datadir.set_data_dir('C:\\Users\\<USERNAME>\\Anaconda3\\envs\\geospatial_env\\Library\\share\\proj') ## set the pyproj data directory

Application code.....

Reinstallation of the packages was not required.

Also as it is mentioned in the docs: