Skip to content

Commit 8eb43d6

Browse files
author
Jeff Whitaker
committed
Merge branch 'master' into v2.0.2rel
2 parents 79071d3 + ec6bed8 commit 8eb43d6

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

pyproj/__init__.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def to_latlong(self):
416416
return Proj(self.crs.to_geodetic())
417417

418418

419-
def transform(p1, p2, x, y, z=None):
419+
def transform(p1, p2, x, y, z=None, radians=False):
420420
"""
421421
x2, y2, z2 = transform(p1, p2, x1, y1, z1)
422422
@@ -427,7 +427,10 @@ def transform(p1, p2, x, y, z=None):
427427
transformed to x2,y2,z2 in the coordinate system defined by p2.
428428
429429
z1 is optional, if it is not set it is assumed to be zero (and
430-
only x2 and y2 are returned).
430+
only x2 and y2 are returned). If the optional keyword
431+
'radians' is True (default is False), then all input and
432+
output coordinates will be in radians instead of the default
433+
of degrees for geographic input/output projections.
431434
432435
In addition to converting between cartographic and geographic
433436
projection coordinates, this function can take care of datum
@@ -489,6 +492,11 @@ def transform(p1, p2, x, y, z=None):
489492
>>> x3, y3 = transform("epsg:4326", "epsg:3857", 33, 98)
490493
>>> "%.3f %.3f" % (x3, y3)
491494
'10909310.098 3895303.963'
495+
>>> pj = Proj(init="epsg:4214")
496+
>>> pjx, pjy = pj(116.366, 39.867)
497+
>>> xr, yr = transform(pj, Proj(4326), pjx, pjy, radians=True)
498+
>>> "%.3f %.3f" % (xr, yr)
499+
'2.031 0.696'
492500
"""
493501
# check that p1 and p2 are valid
494502
if not isinstance(p1, Proj):
@@ -504,7 +512,7 @@ def transform(p1, p2, x, y, z=None):
504512
else:
505513
inz = None
506514
# call pj_transform. inx,iny,inz buffers modified in place.
507-
_proj._transform(p1, p2, inx, iny, inz)
515+
_proj._transform(p1, p2, inx, iny, inz, radians)
508516
# if inputs were lists, tuples or floats, convert back.
509517
outx = _convertback(xisfloat, xislist, xistuple, inx)
510518
outy = _convertback(yisfloat, yislist, xistuple, iny)
@@ -515,7 +523,7 @@ def transform(p1, p2, x, y, z=None):
515523
return outx, outy
516524

517525

518-
def itransform(p1, p2, points, switch=False):
526+
def itransform(p1, p2, points, switch=False, radians=False):
519527
"""
520528
points2 = transform(p1, p2, points1)
521529
Iterator/generator version of the function pyproj.transform.
@@ -534,7 +542,9 @@ def itransform(p1, p2, points, switch=False):
534542
- a generator of coordinates (xi,yi) for 2d points or (xi,yi,zi) for 3d
535543
536544
If optional keyword 'switch' is True (default is False) then x, y or lon,lat coordinates
537-
of points are switched to y, x or lat, lon.
545+
of points are switched to y, x or lat, lon. If the optional keyword 'radians' is True
546+
(default is False), then all input and output coordinates will be in radians instead
547+
of the default of degrees for geographic input/output projections.
538548
539549
540550
Example usage:
@@ -555,6 +565,10 @@ def itransform(p1, p2, points, switch=False):
555565
'2221638.801 2637034.372'
556566
'2212924.125 2619851.898'
557567
'2238294.779 2703763.736'
568+
>>> pj = Proj(init="epsg:4214")
569+
>>> pjx, pjy = pj(116.366, 39.867)
570+
>>> for pt in itransform(pj, Proj(4326), [(pjx, pjy)]): '{:.3f} {:.3f}'.format(*pt)
571+
'2.031 0.696'
558572
"""
559573
if not isinstance(p1, Proj):
560574
p1 = CRS.from_user_input(p1)
@@ -582,7 +596,7 @@ def itransform(p1, p2, points, switch=False):
582596
if len(buff) == 0:
583597
break
584598

585-
_proj._transform_sequence(p1, p2, stride, buff, switch)
599+
_proj._transform_sequence(p1, p2, stride, buff, switch, radians)
586600

587601
for pt in zip(*([iter(buff)] * stride)):
588602
yield pt

pyproj/_proj.pyx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,14 @@ cdef class TransProj:
298298
return in_proj.definition
299299
return cstrencode(CRS.from_user_input(in_proj).to_wkt())
300300

301-
def _transform(p1, p2, inx, iny, inz):
301+
302+
def is_geographic(proj):
303+
if hasattr(proj, "crs"):
304+
proj = proj.crs
305+
return proj.is_geographic
306+
307+
308+
def _transform(p1, p2, inx, iny, inz, radians):
302309
pj_trans = TransProj(p1, p2)
303310
# private function to call pj_transform
304311
cdef void *xdata
@@ -327,10 +334,11 @@ def _transform(p1, p2, inx, iny, inz):
327334
else:
328335
zz = NULL
329336
npts = buflenx//8
330-
if proj_angular_input(pj_trans.projpj, PJ_FWD):
337+
338+
if radians and is_geographic(p1):
331339
for i from 0 <= i < npts:
332-
xx[i] = xx[i]*_DG2RAD
333-
yy[i] = yy[i]*_DG2RAD
340+
xx[i] = xx[i]*_RAD2DG
341+
yy[i] = yy[i]*_RAD2DG
334342

335343
proj_trans_generic(
336344
pj_trans.projpj,
@@ -344,13 +352,13 @@ def _transform(p1, p2, inx, iny, inz):
344352
if errno:
345353
raise ProjError("proj_trans_generic error: {}".format(
346354
pystrdecode(proj_errno_string(errno))))
347-
if proj_angular_output(pj_trans.projpj, PJ_FWD):
348-
for i from 0 <= i < npts:
349-
xx[i] = xx[i]*_RAD2DG
350-
yy[i] = yy[i]*_RAD2DG
351355

356+
if radians and is_geographic(p2):
357+
for i from 0 <= i < npts:
358+
xx[i] = xx[i]*_DG2RAD
359+
yy[i] = yy[i]*_DG2RAD
352360

353-
def _transform_sequence(p1, p2, Py_ssize_t stride, inseq, bint switch):
361+
def _transform_sequence(p1, p2, Py_ssize_t stride, inseq, bint switch, radians):
354362
pj_trans = TransProj(p1, p2)
355363
# private function to itransform function
356364
cdef:
@@ -370,11 +378,11 @@ def _transform_sequence(p1, p2, Py_ssize_t stride, inseq, bint switch):
370378
coords = <double*>buffer
371379
npts = buflen // (stride * _DOUBLESIZE)
372380

373-
if proj_angular_input(pj_trans.projpj, PJ_FWD):
381+
if radians and is_geographic(p1):
374382
for i from 0 <= i < npts:
375383
j = stride*i
376-
coords[j] *= _DG2RAD
377-
coords[j+1] *= _DG2RAD
384+
coords[j] *= _RAD2DG
385+
coords[j+1] *= _RAD2DG
378386

379387
if not switch:
380388
x = coords
@@ -402,8 +410,8 @@ def _transform_sequence(p1, p2, Py_ssize_t stride, inseq, bint switch):
402410
raise ProjError("proj_trans_generic error: {}".format(
403411
proj_errno_string(errno)))
404412

405-
if proj_angular_output(pj_trans.projpj, PJ_FWD):
413+
if radians and is_geographic(p2):
406414
for i from 0 <= i < npts:
407415
j = stride*i
408-
coords[j] *= _RAD2DG
409-
coords[j+1] *= _RAD2DG
416+
coords[j] *= _DG2RAD
417+
coords[j+1] *= _DG2RAD

sphinx/history.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Change Log
44
~~~~~
55
* add filter for boolean values in dict2string so "no_rot=True" works (issue #183).
66
* make sure .pxd files included in source tarball.
7+
* add radians flag back in for transform/itransform (issue #185).
78

89
2.0.1
910
~~~~~

0 commit comments

Comments
 (0)