Skip to content

Commit

Permalink
REF: Refactor to not depend on cython compile time conditional
Browse files Browse the repository at this point in the history
statements
  • Loading branch information
snowman2 committed Sep 13, 2022
1 parent f202662 commit ddc6be5
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 59 deletions.
6 changes: 6 additions & 0 deletions pyproj/_array_backup.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
non-cpython array utilities
https://github.com/pyproj4/pyproj/issues/854
"""
cdef empty_array(int npts)
7 changes: 7 additions & 0 deletions pyproj/_array_backup.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
non-cpython array utilities
https://github.com/pyproj4/pyproj/issues/854
"""
cdef empty_array(int npts):
return array.array("d", [float("NaN")] * npts)
9 changes: 9 additions & 0 deletions pyproj/_array_cpython.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
cpython array utilities
https://github.com/pyproj4/pyproj/issues/854
"""
from cpython cimport array


cdef array.array empty_array(int npts)
13 changes: 13 additions & 0 deletions pyproj/_array_cpython.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
cpython array utilities
https://github.com/pyproj4/pyproj/issues/854
"""

from cpython cimport array


cdef array.array _ARRAY_TEMPLATE = array.array("d", [])

cdef array.array empty_array(int npts):
return array.clone(_ARRAY_TEMPLATE, npts, zero=False)
8 changes: 0 additions & 8 deletions pyproj/_compat.pxd
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
cdef str cstrdecode(const char *instring)
cpdef bytes cstrencode(str pystr)

IF CTE_PYTHON_IMPLEMENTATION == "CPython":
from cpython cimport array
cdef array.array empty_array(int npts)

ELSE:
# https://github.com/pyproj4/pyproj/issues/854
cdef empty_array(int npts)
14 changes: 0 additions & 14 deletions pyproj/_compat.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,3 @@ cdef str cstrdecode(const char *instring):
if instring != NULL:
return instring
return None


IF CTE_PYTHON_IMPLEMENTATION == "CPython":
from cpython cimport array

cdef array.array _ARRAY_TEMPLATE = array.array("d", [])

cdef array.array empty_array(int npts):
return array.clone(_ARRAY_TEMPLATE, npts, zero=False)

ELSE:
# https://github.com/pyproj4/pyproj/issues/854
cdef empty_array(int npts):
return array.array("d", [float("NaN")] * npts)
3 changes: 2 additions & 1 deletion pyproj/_geod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ include "base.pxi"
cimport cython
from libc.math cimport ceil, isnan, round

from pyproj._compat cimport cstrencode, empty_array
from pyproj._array cimport empty_array
from pyproj._compat cimport cstrencode

from collections import namedtuple

Expand Down
50 changes: 25 additions & 25 deletions pyproj/_transformer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ proj_version_str = f"{PROJ_VERSION_MAJOR}.{PROJ_VERSION_MINOR}.{PROJ_VERSION_PAT
PROJ_VERSION = (PROJ_VERSION_MAJOR, PROJ_VERSION_MINOR, PROJ_VERSION_PATCH)
_AUTH_CODE_RE = re.compile(r"(?P<authority>\w+)\:(?P<code>\w+)")

IF (CTE_PROJ_VERSION_MAJOR, CTE_PROJ_VERSION_MINOR) >= (9, 1):
cdef extern from "proj.h" nogil:
PJ* proj_trans_get_last_used_operation(PJ *P)
cdef extern from "pyproj.h" nogil:
PJ* proj_trans_get_last_used_operation(PJ *P)


cdef dict _PJ_DIRECTION_MAP = {
Expand Down Expand Up @@ -291,9 +290,9 @@ cdef PJ* proj_create_crs_to_crs(
options[options_index] = b"ALLOW_BALLPARK=NO"
options_index += 1
if force_over:
IF CTE_PROJ_VERSION_MAJOR >= 9:
if PROJ_VERSION_MAJOR >= 9:
options[options_index] = b"FORCE_OVER=YES"
ELSE:
else:
raise NotImplementedError("force_over requires PROJ 9+.")

cdef PJ* transform = proj_create_crs_to_crs_from_pj(
Expand Down Expand Up @@ -432,27 +431,28 @@ cdef class _Transformer(Base):
return self._operations

def get_last_used_operation(self):
IF (CTE_PROJ_VERSION_MAJOR, CTE_PROJ_VERSION_MINOR) >= (9, 1):
cdef PJ* last_used_operation = proj_trans_get_last_used_operation(self.projobj)
if last_used_operation == NULL:
raise ProjError(
"Last used operation not found. "
"This is likely due to not initiating a transform."
)
cdef PJ_CONTEXT* context = NULL
try:
context = pyproj_context_create()
except:
proj_destroy(last_used_operation)
raise
proj_assign_context(last_used_operation, context)
return _Transformer._from_pj(
context,
last_used_operation,
False,
if (PROJ_VERSION_MAJOR, PROJ_VERSION_MINOR) < (9, 1):
raise NotImplementedError(
"PROJ 9.1+ required to get last used operation."
)
ELSE:
raise NotImplementedError("PROJ 9.1+ required to get last used operation.")
cdef PJ* last_used_operation = proj_trans_get_last_used_operation(self.projobj)
if last_used_operation == NULL:
raise ProjError(
"Last used operation not found. "
"This is likely due to not initiating a transform."
)
cdef PJ_CONTEXT* context = NULL
try:
context = pyproj_context_create()
except:
proj_destroy(last_used_operation)
raise
proj_assign_context(last_used_operation, context)
return _Transformer._from_pj(
context,
last_used_operation,
False,
)

@property
def is_network_enabled(self):
Expand Down
27 changes: 16 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,35 @@ def get_extension_modules():

proj_version = get_proj_version(proj_dir)
check_proj_version(proj_version)
proj_version_major, proj_version_minor, proj_version_patch = parse_version(
proj_version
).base_version.split(".")

# setup extension options
ext_options = {
"include_dirs": include_dirs,
"include_dirs": ["./src"] + include_dirs,
"library_dirs": library_dirs,
"runtime_library_dirs": (
library_dirs if os.name != "nt" and sys.platform != "cygwin" else None
),
"libraries": get_libraries(library_dirs),
}
if platform.python_implementation() == "CPython":
src_array_pyx = Path("pyproj/_array_cpython.pyx")
src_array_pxd = Path("pyproj/_array_cpython.pxd")
else:
src_array_pyx = Path("pyproj/_array_backup.pyx")
src_array_pxd = Path("pyproj/_array_backup.pxd")
array_pyx = Path("pyproj/_array.pyx")
array_pxd = Path("pyproj/_array.pxd")
shutil.copy(src_array_pyx, array_pyx)
shutil.copy(src_array_pxd, array_pxd)
# setup cythonized modules
return cythonize(
modules = cythonize(
[
Extension("pyproj._geod", ["pyproj/_geod.pyx"], **ext_options),
Extension("pyproj._crs", ["pyproj/_crs.pyx"], **ext_options),
Extension(
"pyproj._transformer", ["pyproj/_transformer.pyx"], **ext_options
),
Extension("pyproj._array", ["pyproj/_array.pyx"], **ext_options),
Extension("pyproj._compat", ["pyproj/_compat.pyx"], **ext_options),
Extension("pyproj.database", ["pyproj/database.pyx"], **ext_options),
Extension("pyproj._datadir", ["pyproj/_datadir.pyx"], **ext_options),
Expand All @@ -196,14 +204,11 @@ def get_extension_modules():
Extension("pyproj._sync", ["pyproj/_sync.pyx"], **ext_options),
],
quiet=True,
compile_time_env={
"CTE_PROJ_VERSION_MAJOR": int(proj_version_major),
"CTE_PROJ_VERSION_MINOR": int(proj_version_minor),
"CTE_PROJ_VERSION_PATCH": int(proj_version_patch),
"CTE_PYTHON_IMPLEMENTATION": platform.python_implementation(),
},
**get_cythonize_options(),
)
array_pyx.unlink()
array_pxd.unlink()
return modules


def get_package_data() -> Dict[str, List[str]]:
Expand Down
7 changes: 7 additions & 0 deletions src/pyproj.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "proj.h"

#if PROJ_VERSION_MAJOR < 9 && PROJ_VERSION_MINOR < 1
PJ* proj_trans_get_last_used_operation(PJ *P) {
return nullptr;
}
#endif

0 comments on commit ddc6be5

Please sign in to comment.