File tree Expand file tree Collapse file tree 5 files changed +53
-7
lines changed
Expand file tree Collapse file tree 5 files changed +53
-7
lines changed Original file line number Diff line number Diff line change 11include " proj.pxi"
22
3-
43cdef PJ_CONTEXT* get_pyproj_context()
Original file line number Diff line number Diff line change 11
22from libc.stdlib cimport malloc, free
33
4- from pyproj.compat import cstrencode
4+ from pyproj.compat import cstrencode, pystrdecode
55from pyproj.datadir import get_data_dir
6+ from pyproj.exceptions import ProjError
7+
8+ cdef void pyproj_log_function(void * user_data, int level, const char * error_msg):
9+ """
10+ Log function for proj.4 errors with CRS class.
11+ """
12+ if level == PJ_LOG_ERROR:
13+ ProjError.internal_proj_error = pystrdecode(error_msg)
614
715
816cdef PJ_CONTEXT* get_pyproj_context():
@@ -16,11 +24,13 @@ cdef PJ_CONTEXT* get_pyproj_context():
1624 b_data_dir = cstrencode(data_dir_list[iii])
1725 c_data_dir[iii] = b_data_dir
1826 proj_context_set_search_paths(pyproj_context, len (data_dir_list), c_data_dir)
19- proj_context_use_proj4_init_rules(pyproj_context, 1 )
2027 except :
2128 if pyproj_context != NULL :
2229 proj_context_destroy(pyproj_context)
2330 raise
2431 finally :
2532 free(c_data_dir)
33+ proj_context_use_proj4_init_rules(pyproj_context, 1 )
34+ proj_log_func(pyproj_context, NULL , pyproj_log_function)
35+
2636 return pyproj_context
Original file line number Diff line number Diff line change 44"""
55
66
7- class CRSError (RuntimeError ):
8- """Raised when a CRS error occurs."""
9-
10-
117class ProjError (RuntimeError ):
128 """Raised when a Proj error occurs."""
139
10+ internal_proj_error = None
11+
12+ def __init__ (self , error_message ):
13+ if self .internal_proj_error is not None :
14+ error_message = (
15+ "{error_message}: (Internal Proj Error: {internal_proj_error})"
16+ ).format (
17+ error_message = error_message ,
18+ internal_proj_error = self .internal_proj_error ,
19+ )
20+ self .internal_proj_error = None
21+ super (ProjError , self ).__init__ (error_message )
22+
23+
24+ class CRSError (ProjError ):
25+ """Raised when a CRS error occurs."""
26+
1427
1528class GeodError (RuntimeError ):
1629 """Raised when a Geod error occurs."""
Original file line number Diff line number Diff line change @@ -24,6 +24,16 @@ cdef extern from "proj.h":
2424 ctypedef struct PJ_CONTEXT
2525 PJ_CONTEXT * proj_context_create ()
2626 PJ_CONTEXT * proj_context_destroy (PJ_CONTEXT * ctx)
27+
28+ ctypedef enum PJ_LOG_LEVEL:
29+ PJ_LOG_NONE = 0
30+ PJ_LOG_ERROR = 1
31+ PJ_LOG_DEBUG = 2
32+ PJ_LOG_TRACE = 3
33+ PJ_LOG_TELL = 4
34+ ctypedef void (* PJ_LOG_FUNCTION)(void * , int , const char * )
35+ void proj_log_func (PJ_CONTEXT * ctx, void * app_data, PJ_LOG_FUNCTION logf)
36+
2737 int proj_errno (const PJ * P)
2838 int proj_context_errno (PJ_CONTEXT * ctx)
2939 const char * proj_errno_string (int err)
Original file line number Diff line number Diff line change 1+ import pytest
2+
3+ from pyproj import CRS , Proj
4+ from pyproj .exceptions import CRSError , ProjError
5+
6+
7+ def test_proj_exception ():
8+ with pytest .raises (ProjError , match = "Internal Proj Error" ):
9+ Proj ("+proj=bobbyjoe" )
10+
11+
12+ def test_crs_exception ():
13+ with pytest .raises (CRSError , match = "Internal Proj Error" ):
14+ CRS ("+proj=bobbyjoe" )
You can’t perform that action at this time.
0 commit comments