Skip to content

Commit

Permalink
Merge 952edad into b0a1f07
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed May 3, 2019
2 parents b0a1f07 + 952edad commit 3b14b26
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 49 deletions.
52 changes: 30 additions & 22 deletions pyproj/_crs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ from collections import OrderedDict

from pyproj.compat import cstrencode, pystrdecode
from pyproj._datadir cimport get_pyproj_context
from pyproj.enum import WktVersion, ProjVersion
from pyproj.exceptions import CRSError


cdef cstrdecode(const char *instring):
if instring != NULL:
return pystrdecode(instring)
return None


cdef decode_or_undefined(const char* instring):
pystr = cstrdecode(instring)
if pystr is None:
return "undefined"
return pystr


def is_wkt(proj_string):
"""
Check if the input projection string is in the Well-Known Text format.
Expand All @@ -33,7 +37,7 @@ def is_wkt(proj_string):
return proj_context_guess_wkt_dialect(NULL, tmp_string) != PJ_GUESSED_NOT_WKT


cdef _to_wkt(PJ_CONTEXT* projctx, PJ* projobj, version="WKT2_2018", pretty=False):
cdef _to_wkt(PJ_CONTEXT* projctx, PJ* projobj, version=WktVersion.WKT2_2018, pretty=False):
"""
Convert a PJ object to a wkt string.
Expand All @@ -50,16 +54,16 @@ cdef _to_wkt(PJ_CONTEXT* projctx, PJ* projobj, version="WKT2_2018", pretty=False
"""
# get the output WKT format
supported_wkt_types = {
"WKT2_2015": PJ_WKT2_2015,
"WKT2_2015_SIMPLIFIED": PJ_WKT2_2015_SIMPLIFIED,
"WKT2_2018": PJ_WKT2_2018,
"WKT2_2018_SIMPLIFIED": PJ_WKT2_2018_SIMPLIFIED,
"WKT1_GDAL": PJ_WKT1_GDAL,
"WKT1_ESRI": PJ_WKT1_ESRI
WktVersion.WKT2_2015: PJ_WKT2_2015,
WktVersion.WKT2_2015_SIMPLIFIED: PJ_WKT2_2015_SIMPLIFIED,
WktVersion.WKT2_2018: PJ_WKT2_2018,
WktVersion.WKT2_2018_SIMPLIFIED: PJ_WKT2_2018_SIMPLIFIED,
WktVersion.WKT1_GDAL: PJ_WKT1_GDAL,
WktVersion.WKT1_ESRI: PJ_WKT1_ESRI
}
cdef PJ_WKT_TYPE wkt_out_type
try:
wkt_out_type = supported_wkt_types[version.upper()]
wkt_out_type = supported_wkt_types[WktVersion(version)]
except KeyError:
raise ValueError(
"Invalid version supplied '{}'. "
Expand All @@ -82,27 +86,28 @@ cdef _to_wkt(PJ_CONTEXT* projctx, PJ* projobj, version="WKT2_2018", pretty=False
return cstrdecode(proj_string)


cdef _to_proj4(PJ_CONTEXT* projctx, PJ* projobj, version=4):
cdef _to_proj4(PJ_CONTEXT* projctx, PJ* projobj, version=ProjVersion.PROJ_4):
"""
Convert the projection to a PROJ.4 string.
Parameters
----------
version: int
The version of the PROJ.4 output. Default is 4.
version: ~pyproj.enum.ProjVersion
The version of the PROJ.4 output.
Default is :attr:`~pyproj.enum.ProjVersion.PROJ_4`.
Returns
-------
str: The PROJ.4 string.
"""
# get the output PROJ.4 format
supported_prj_types = {
4: PJ_PROJ_4,
5: PJ_PROJ_5,
ProjVersion.PROJ_4: PJ_PROJ_4,
ProjVersion.PROJ_5: PJ_PROJ_5,
}
cdef PJ_PROJ_STRING_TYPE proj_out_type
try:
proj_out_type = supported_prj_types[version]
proj_out_type = supported_prj_types[ProjVersion(version)]
except KeyError:
raise ValueError(
"Invalid version supplied '{}'. "
Expand Down Expand Up @@ -307,8 +312,9 @@ cdef class Base:
Parameters
----------
version: str
The version of the WKT output. Default is WKT2_2018.
version: ~pyproj.enum.WktVersion
The version of the WKT output.
Default is :attr:`~pyproj.enum.WktVersion.WKT2_2018`.
pretty: bool
If True, it will set the output to be a multiline string. Defaults to False.
Expand Down Expand Up @@ -1037,14 +1043,15 @@ cdef class CoordinateOperation(Base):
)
return self._grids

def to_proj4(self, version=5):
def to_proj4(self, version=ProjVersion.PROJ_5):
"""
Convert the projection to a PROJ.4 string.
Parameters
----------
version: int
The version of the PROJ.4 string. Default is 5.
version: ~pyproj.enum.ProjVersion
The version of the PROJ.4 output.
Default is :attr:`~pyproj.enum.ProjVersion.PROJ_5`.
Returns
-------
Expand Down Expand Up @@ -1270,14 +1277,15 @@ cdef class _CRS(Base):

return self._sub_crs_list

def to_proj4(self, version=4):
def to_proj4(self, version=ProjVersion.PROJ_4):
"""
Convert the projection to a PROJ.4 string.
Parameters
----------
version: int
The version of the PROJ.4 output. Default is 4.
version: ~pyproj.enum.ProjVersion
The version of the PROJ.4 output.
Default is :attr:`~pyproj.enum.ProjVersion.PROJ_4`.
Returns
-------
Expand Down
22 changes: 4 additions & 18 deletions pyproj/_transformer.pyx
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
include "base.pxi"

from pyproj._crs cimport _CRS
from pyproj.compat import cstrencode, pystrdecode
from pyproj._datadir cimport get_pyproj_context
from pyproj.compat import cstrencode, pystrdecode
from pyproj.enum import TransformDirection
from pyproj.exceptions import ProjError


_PJ_DIRECTION_MAP = {
"forward": PJ_FWD,
"inverse": PJ_INV,
"ident": PJ_IDENT,
}

cdef PJ_DIRECTION get_direction(direction):
try:
return _PJ_DIRECTION_MAP[direction]
except KeyError:
raise ValueError(
"Invalid direction supplied '{}'. "
"Only {} are supported."
.format(direction, tuple(_PJ_DIRECTION_MAP)))

cdef class _Transformer:
def __cinit__(self):
self.projpj = NULL
Expand Down Expand Up @@ -92,7 +78,7 @@ cdef class _Transformer:
def _transform(self, inx, iny, inz, intime, direction, radians, errcheck):
if self.projections_exact_same or (self.projections_equivalent and self.skip_equivalent):
return
cdef PJ_DIRECTION pj_direction = get_direction(direction)
cdef PJ_DIRECTION pj_direction = <PJ_DIRECTION>direction
# private function to call pj_transform
cdef void *xdata
cdef void *ydata
Expand Down Expand Up @@ -183,7 +169,7 @@ cdef class _Transformer:
):
if self.projections_exact_same or (self.projections_equivalent and self.skip_equivalent):
return
cdef PJ_DIRECTION pj_direction = get_direction(direction)
cdef PJ_DIRECTION pj_direction = <PJ_DIRECTION>direction
# private function to itransform function
cdef:
void *buffer
Expand Down
48 changes: 48 additions & 0 deletions pyproj/enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
This module contains enumerations used in pyproj.
"""
from enum import Enum, IntEnum


class WktVersion(Enum):
"""
Supported CRS WKT string versions
"""

#: WKT Version 2 from 2015
WKT2_2015 = "WKT2_2015"
#: WKT Version 2 from 2015 Simplified
WKT2_2015_SIMPLIFIED = "WKT2_2015_SIMPLIFIED"
#: WKT Version 2 from 2018
WKT2_2018 = "WKT2_2018"
#: WKT Version 2 from 2018 Simplified
WKT2_2018_SIMPLIFIED = "WKT2_2018_SIMPLIFIED"
#: WKT Version 1 GDAL Style
WKT1_GDAL = "WKT1_GDAL"
#: WKT Version 1 ESRI Style
WKT1_ESRI = "WKT1_ESRI"


class ProjVersion(IntEnum):
"""
Supported CRS PROJ string versions
"""

#: PROJ String version 4
PROJ_4 = 4
#: PROJ String version 5
PROJ_5 = 5


class TransformDirection(IntEnum):
"""
Supported transform directions
"""

#: Forward direction
FORWARD = 1
#: Inverse direction
INVERSE = -1
#: Do nothing
IDENT = 0
11 changes: 6 additions & 5 deletions pyproj/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pyproj import CRS, Proj
from pyproj._transformer import _Transformer
from pyproj.compat import cstrencode
from pyproj.enum import TransformDirection
from pyproj.utils import _convertback, _copytobuffer

try:
Expand Down Expand Up @@ -125,7 +126,7 @@ def transform(
tt=None,
radians=False,
errcheck=False,
direction="forward",
direction=TransformDirection.FORWARD,
):
"""
Transform points between two coordinate systems.
Expand All @@ -148,9 +149,9 @@ def transform(
If True an exception is raised if the transformation is invalid.
By default errcheck=False and an invalid transformation
returns ``inf`` and no exception is raised.
direction: str, optional
The direction of the transform ("forward", "inverse", "ident").
Default is "forward".
direction: ~pyproj.enum.TransformDirection, optional
The direction of the transform.
Default is :attr:`~pyproj.enum.TransformDirection.FORWARD`.
Example:
Expand Down Expand Up @@ -217,7 +218,7 @@ def itransform(
time_3rd=False,
radians=False,
errcheck=False,
direction="forward",
direction=TransformDirection.FORWARD,
):
"""
Iterator/generator version of the function pyproj.Transformer.transform.
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,5 @@ def get_version():
packages=["pyproj"],
ext_modules=get_extension_modules(),
package_data=get_package_data(),
install_requires=["enum34"] if sys.version_info < (3, 4) else None,
)
13 changes: 13 additions & 0 deletions sphinx/api/enum.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Enumerations
============

.. autoclass:: pyproj.enum.WktVersion
:members:


.. autoclass:: pyproj.enum.ProjVersion
:members:


.. autoclass:: pyproj.enum.TransformDirection
:members:
3 changes: 2 additions & 1 deletion sphinx/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ API Documentation
geod
transformer
list
enum
datadir
exceptions
exceptions
18 changes: 18 additions & 0 deletions test/test_crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pyproj import CRS
from pyproj.crs import CoordinateOperation, Datum, Ellipsoid, PrimeMeridian
from pyproj.enum import ProjVersion, WktVersion
from pyproj.exceptions import CRSError


Expand Down Expand Up @@ -550,3 +551,20 @@ def test_coordinate_operation_towgs84_seven():
def test_coordinate_operation_towgs84_missing():
crs = CRS(init="epsg:3004")
assert crs.coordinate_operation.towgs84 == []


def test_to_wkt_enum():
crs = CRS.from_epsg(4326)
assert crs.to_wkt("WKT1_GDAL") == crs.to_wkt(WktVersion.WKT1_GDAL)
assert crs.to_wkt("WKT2_2018") == crs.to_wkt(WktVersion.WKT2_2018)
assert crs.to_wkt("WKT2_2018_SIMPLIFIED") == \
crs.to_wkt(WktVersion.WKT2_2018_SIMPLIFIED)
assert crs.to_wkt("WKT2_2015") == crs.to_wkt(WktVersion.WKT2_2015)
assert crs.to_wkt("WKT2_2015_SIMPLIFIED") == \
crs.to_wkt(WktVersion.WKT2_2015_SIMPLIFIED)


def test_to_proj4_enum():
crs = CRS.from_epsg(4326)
assert crs.to_proj4(4) == crs.to_proj4(ProjVersion.PROJ_4)
assert crs.to_proj4(5) == crs.to_proj4(ProjVersion.PROJ_5)
8 changes: 5 additions & 3 deletions test/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pyproj
from pyproj import Proj, Transformer, itransform, transform
from pyproj.enum import TransformDirection
from pyproj.exceptions import ProjError


Expand Down Expand Up @@ -306,7 +307,7 @@ def test_4d_transform__inverse():
yy=778956.4532640711,
zz=5248216.453456361,
tt=2008.75,
direction="inverse",
direction=TransformDirection.INVERSE,
),
(3513638.19380, 778956.45250, 5248216.46900, 2008.75),
)
Expand All @@ -316,7 +317,8 @@ def test_transform_direction():
forward_transformer = Transformer.from_crs(4326, 3857)
inverse_transformer = Transformer.from_crs(3857, 4326)
assert inverse_transformer.transform(
-33, 24, direction="inverse"
-33, 24, direction=TransformDirection.INVERSE
) == forward_transformer.transform(-33, 24)
ident_transformer = Transformer.from_crs(4326, 3857)
ident_transformer.transform(-33, 24, direction="ident") == (-33, 24)
ident_transformer.transform(-33, 24, direction=TransformDirection.IDENT) ==\
(-33, 24)

0 comments on commit 3b14b26

Please sign in to comment.