spyder-ide / spyder

Official repository for Spyder - The Scientific Python Development Environment
https://www.spyder-ide.org
MIT License
8.26k stars 1.61k forks source link

I have errors using pyproj in Spyder but not in Jupiter lab in the same conda environment #15563

Closed solisgb closed 3 years ago

solisgb commented 3 years ago

In the same conda environment, if a I run a function in Jupyter lab using pyproj module it runs correctly, but if I try in Syper it raises an error

OS Windows 10. Python 3.8, conda 4.9.2, spyder 4.2.0, pyproj 3.0.1. I also have QGIS 3.16.6

The complete function is:

def geographics2proyected(crsg, crsp, lat_lon):
    """
    Transforms a list of geographic coordinates xyg from geographic
    coordinate reference system crsg to a proyected crsp one
    Parameters
    ----------
    crsg : int
        geographic coordinate reference system
    crsp : int
        proyected coordinate reference system
    lat_lon : List of lists of str (latitude, longitude)
        latitude is a str with the format DDMMSSC
        longitude is a str with the format DDMMSSC
        where DD degres, MM minutes, SS seconds and C is a cardinal direction:
        N, S, E, W
    Returns
    -------
    xyp List of list of [x, y] in the projected crsp
    """
  import pyproj as proj
  import numpy as np

  if proj.crs.CRS(crsg).is_projected:
      raise ValueError(f'crs {crsg:d} is not geographic')
  if proj.crs.CRS(crsp).is_geographic:
      raise ValueError(f'crs {crsp:d} is not projected')
  transformer = proj.Transformer.from_crs(crsg, crsp)
  xyp = np.empty((len(lat_lon), 2), np.float32)
  for i, lat_lon1 in enumerate(lat_lon):
      for j, item in enumerate(lat_lon1):
          dg = float(item[0:2])
          mn = float(item[2:4])
          sc = float(item[4:6])
          d = item[6:7]
          if j == 0:
              if d not in 'NS':
                  raise ValueError (f'{item} is not a longiude')
              lat_deg = dg + (mn/60) + (sc/3600)
              if d == 'S':
                  lat_deg = -1. * lat_deg
          else:
              if d not in 'WE':
                  raise ValueError (f'{item} is not a latitude')
              lon_deg = dg + mn/60 + sc/3600
              if d == 'W':
                  lon_deg = -1. * lon_deg
              xp1, yp1 = transformer.transform(lat_deg, lon_deg)
              xyp[i,:] = [xp1, yp1]
  return xyp.tolist()

This is the first error, as you know, 4258 and 25830 are both valid epsg codes File "C:\Users...\gis_utils\gis_utils.py", line 31, in geographics2proyected if proj.crs.CRS(crsg).is_projected: File "C:\Users\solis\miniconda3\envs\env01\lib\site-packages\pyproj\crs\crs.py", line 296, in init super().init(projstring) File "pyproj_crs.pyx", line 2309, in pyproj._crs._CRS.init pyproj.exceptions.CRSError: Invalid projection: epsg:4258: (Internal Proj Error: proj_create: cannot build geodeticCRS 4258: SQLite error on SELECT extent.description, extent.south_lat, extent.north_lat, extent.west_lon, extent.east_lon, scope.scope, (CASE WHEN scope.scope LIKE '%large scale%' THEN 0 ELSE 1 END) AS score FROM usage JOIN extent ON usage.extent_auth_name = extent.auth_name AND usage.extent_code = extent.code JOIN scope ON usage.scope_auth_name = scope.auth_name AND usage.scope_code = scope.code WHERE object_table_name = ? AND object_auth_name = ? AND object_code = ? ORDER BY score, usage.auth_name, usage.code: no such table: usage)

From a jupiter notebook I can run this function but not under Syper env.

def main

# -*- coding: utf-8 -*-
"""
Created on Sun Nov  1 13:08:40 2020
"""
  from time import time
  import traceback
  import gis_utils as gis

  if __name__ == "__main__":

      try:
          startTime = time()

          lat_lon = [['373604N', '005916W'],['374642N', '004821W']]
          xysp = gis.geographics2proyected(4258, 25830, lat_lon)
          print(xysp)

    except ValueError:
        msg = traceback.format_exc()
      except Exception:
          msg = traceback.format_exc()
      finally:
          xtime = time() - startTime
          print(f'El script tardó {xtime:0.1f} s')

I'd appreciate very much your help

ccordoba12 commented 3 years ago

Hey @solisgb, thanks for reporting. As far as I can see, the error you reported above has nothing to do with Spyder but with pyproj.

Unless you're able to better determine why Spyder is failing, I'm afraid we won't be able to help you, sorry.

solisgb commented 3 years ago

Hi Cordoba12:

Thanks for your interest

I'm confused with this

When I call the function from Spyder, the first line that tries to execute the pyproj module raises module an error, but the function runs correctly when I call the exact same function in Jupyter lab. As the Python enviroment is the same, I assumed that the result should be the same. Perhaps you could tell me what kind of additional information I can send you, because I am completely confused with this behavior.

ccordoba12 commented 3 years ago

I'm really sorry but we I have no idea what happens here. What you're asking us to do is to debug this problem for you, which doesn't seem to be located in Spyder (as I said).

Unfortunately, we don't have time for that.