Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions pyproj/_transformer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ cdef class _Transformer:
cdef _Transformer transformer = _Transformer()
transformer.projpj = proj_create_crs_to_crs(
transformer.projctx,
_Transformer._definition_from_object(proj_from),
_Transformer._definition_from_object(proj_to),
cstrencode(proj_from.crs.srs),
cstrencode(proj_to.crs.srs),
NULL)
if transformer.projpj is NULL:
raise ProjError("Error creating CRS to CRS.")
transformer.set_radians_io()
transformer.projections_exact_same = proj_from.is_exact_same(proj_to)
transformer.projections_equivalent = proj_from == proj_to
transformer.projections_exact_same = proj_from.crs.is_exact_same(proj_to.crs)
transformer.projections_equivalent = proj_from.crs == proj_to.crs
transformer.skip_equivalent = skip_equivalent
transformer.is_pipeline = False
return transformer
Expand All @@ -62,17 +62,6 @@ cdef class _Transformer:
transformer.output_geographic = proj_to.crs.is_geographic
return transformer

@staticmethod
def from_crs(crs_from, crs_to, skip_equivalent=False):
if not isinstance(crs_from, CRS):
crs_from = CRS.from_user_input(crs_from)
if not isinstance(crs_to, CRS):
crs_to = CRS.from_user_input(crs_to)
transformer = _Transformer._init_crs_to_crs(crs_from, crs_to, skip_equivalent=skip_equivalent)
transformer.input_geographic = crs_from.is_geographic
transformer.output_geographic = crs_to.is_geographic
return transformer

@staticmethod
def from_pipeline(const char *proj_pipeline):
cdef _Transformer transformer = _Transformer()
Expand All @@ -85,22 +74,6 @@ cdef class _Transformer:
transformer.is_pipeline = True
return transformer

@staticmethod
def _definition_from_object(in_proj):
"""
Parameters
----------
in_proj: :obj:`pyproj.Proj` or :obj:`pyproj.CRS`

Returns
-------
char*: Definition string for `proj_create_crs_to_crs`.

"""
if isinstance(in_proj, Proj):
return cstrencode(in_proj.crs.srs)
return cstrencode(in_proj.srs)

def _transform(self, inx, iny, inz, intime, radians, errcheck=False):
if self.projections_exact_same or (self.projections_equivalent and self.skip_equivalent):
return
Expand Down Expand Up @@ -130,7 +103,7 @@ cdef class _Transformer:
else:
buflent = bufleny

if not (buflenx == bufleny == buflenz == buflent):
if not buflenx or not (buflenx == bufleny == buflenz == buflent):
raise ProjError('x,y,z, and time must be same size')
xx = <double *>xdata
yy = <double *>ydata
Expand Down
10 changes: 3 additions & 7 deletions pyproj/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ def from_crs(crs_from, crs_to, skip_equivalent=False):
:obj:`pyproj.Transformer`

"""
transformer = Transformer()
transformer._transformer = _Transformer.from_crs(
crs_from, crs_to, skip_equivalent
)
return transformer
return Transformer.from_proj(crs_from, crs_to, skip_equivalent=skip_equivalent)

@staticmethod
def from_pipeline(proj_pipeline):
Expand Down Expand Up @@ -154,7 +150,7 @@ def transform(self, xx, yy, zz=None, tt=None, radians=False, errcheck=False):
>>> xpjr, ypjr, zpjr = transprojr.transform(xpj, ypj, zpj, radians=True)
>>> "%.3f %.3f %.3f" % (xpjr, ypjr, zpjr)
'-2704026.010 -4253051.810 3895878.820'
>>> transformer = Transformer.from_crs("epsg:4326", 4326, skip_equivalent=True)
>>> transformer = Transformer.from_proj("epsg:4326", 4326, skip_equivalent=True)
>>> xeq, yeq = transformer.transform(33, 98)
>>> "%.0f %.0f" % (xeq, yeq)
'33 98'
Expand Down Expand Up @@ -230,7 +226,7 @@ def itransform(
>>> transprojr = Transformer.from_proj('+init=EPSG:4326', {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'})
>>> for pt in transprojr.itransform([(-2.137, 0.661, -20.531)], radians=True): '{:.3f} {:.3f} {:.3f}'.format(*pt)
'-2704214.394 -4254414.478 3894270.731'
>>> transproj_eq = Transformer.from_proj('+init=EPSG:4326', 4326, skip_equivalent=True)
>>> transproj_eq = Transformer.from_proj('+init=EPSG:4326', '+proj=longlat +datum=WGS84 +no_defs +type=crs', skip_equivalent=True)
>>> for pt in transproj_eq.itransform([(-2.137, 0.661)]): '{:.3f} {:.3f}'.format(*pt)
'-2.137 0.661'

Expand Down
20 changes: 10 additions & 10 deletions unittest/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ def test_equivalent_proj():
)
assert transformer._transformer.skip_equivalent
assert transformer._transformer.projections_equivalent
assert transformer._transformer.projections_exact_same
assert not transformer._transformer.projections_exact_same


def test_equivalent_proj__disabled():
transformer = Transformer.from_proj(3857, pyproj.Proj(3857).crs.to_proj4())
assert not transformer._transformer.skip_equivalent
assert transformer._transformer.projections_equivalent
assert transformer._transformer.projections_exact_same
assert not transformer._transformer.projections_equivalent
assert not transformer._transformer.projections_exact_same


def test_equivalent_proj__different():
Expand Down Expand Up @@ -135,14 +135,14 @@ def test_4d_transform_crs_obs1():
transformer.transform(
xx=3496737.2679, yy=743254.4507, zz=5264462.9620, tt=2019.0
),
(3496737.2679, 743254.4507, 5264462.962, 2019.0),
(3496737.757717311, 743253.9940103051, 5264462.701132784, 2019.0),
)


def test_4d_transform_orginal_crs_obs1():
assert_almost_equal(
transform(7789, 8401, x=3496737.2679, y=743254.4507, z=5264462.9620, tt=2019.0),
(3496737.2679, 743254.4507, 5264462.962, 2019.0),
(3496737.757717311, 743253.9940103051, 5264462.701132784, 2019.0),
)


Expand All @@ -152,22 +152,22 @@ def test_4d_transform_crs_obs2():
transformer.transform(
xx=3496737.2679, yy=743254.4507, zz=5264462.9620, tt=2019.0
),
(3496737.2679, 743254.4507, 5264462.962, 2019.0),
(3496737.7857162016, 743254.0394113371, 5264462.643659916, 2019.0),
)


def test_2d_with_time_transform_crs_obs2():
transformer = Transformer.from_proj(4896, 7930)
assert_almost_equal(
transformer.transform(xx=3496737.2679, yy=743254.4507, tt=2019.0),
(3496737.2679, 743254.4507, 2019.0),
(3496737.4105305015, 743254.1014318303, 2019.0),
)


def test_2d_with_time_transform_original_crs_obs2():
assert_almost_equal(
transform(4896, 7930, x=3496737.2679, y=743254.4507, tt=2019.0),
(3496737.2679, 743254.4507, 2019.0),
(3496737.4105305015, 743254.1014318303, 2019.0),
)


Expand Down Expand Up @@ -200,7 +200,7 @@ def test_4d_itransform_orginal_crs_obs1():
list(
itransform(7789, 8401, [(3496737.2679, 743254.4507, 5264462.9620, 2019.0)])
),
[(3496737.2679, 743254.4507, 5264462.962, 2019.0)],
[(3496737.757717311, 743253.9940103051, 5264462.701132784, 2019.0)],
)


Expand All @@ -209,7 +209,7 @@ def test_2d_with_time_itransform_original_crs_obs2():
list(
itransform(4896, 7930, [(3496737.2679, 743254.4507, 2019.0)], time_3rd=True)
),
[(3496737.2679, 743254.4507, 2019.0)],
[(3496737.4105305015, 743254.1014318303, 2019.0)],
)


Expand Down