def __call__(self, *args, **kw): # ,lon,lat,inverse=False,errcheck=False): """ Calling a Proj class instance with the arguments lon, lat will convert lon/lat (in degrees) to x/y native map projection coordinates (in meters). If optional keyword 'inverse' is True (default is False), the inverse transformation from x/y to lon/lat is performed. If optional keyword 'errcheck' is True (default is False) an exception is raised if the transformation is invalid. If errcheck=False and the transformation is invalid, no exception is raised and 1.e30 is returned. Inputs should be doubles (they will be cast to doubles if they are not, causing a slight performance hit). Works with numpy and regular python array objects, python sequences and scalars, but is fastest for array objects. """ inverse = kw.get("inverse", False) errcheck = kw.get("errcheck", False) # if len(args) == 1: # latlon = np.array(args[0], copy=True, # order='C', dtype=float, ndmin=2) # if inverse: # _proj.Proj._invn(self, latlon, radians=radians, errcheck=errcheck) # else: # _proj.Proj._fwdn(self, latlon, radians=radians, errcheck=errcheck) # return latlon lon, lat = args # process inputs, making copies that support buffer API. inx, xisfloat, xislist, xistuple = _copytobuffer(lon) iny, yisfloat, yislist, yistuple = _copytobuffer(lat) # call proj4 functions. inx and iny modified in place. if inverse: self._inv(inx, iny, errcheck=errcheck) else: self._fwd(inx, iny, errcheck=errcheck) # if inputs were lists, tuples or floats, convert back. outx = _convertback(xisfloat, xislist, xistuple, inx) outy = _convertback(yisfloat, yislist, xistuple, iny) return outx, outy
def transform( self, xx, yy, zz=None, tt=None, radians=False, errcheck=False, direction=TransformDirection.FORWARD, ): """ Transform points between two coordinate systems. Parameters ---------- xx: scalar or array (numpy or python) Input x coordinate(s). yy: scalar or array (numpy or python) Input y coordinate(s). zz: scalar or array (numpy or python), optional Input z coordinate(s). tt: scalar or array (numpy or python), optional Input time coordinate(s). radians: boolean, optional If True, will expect input data to be in radians and will return radians if the projection is geographic. Default is False (degrees). Ignored for pipeline transformations. errcheck: boolean, optional (default False) 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: ~pyproj.enums.TransformDirection, optional The direction of the transform. Default is :attr:`~pyproj.enums.TransformDirection.FORWARD`. Example: >>> from pyproj import Transformer >>> transformer = Transformer.from_crs("epsg:4326", "epsg:3857") >>> x3, y3 = transformer.transform(33, 98) >>> "%.3f %.3f" % (x3, y3) '10909310.098 3895303.963' >>> pipeline_str = "+proj=pipeline +step +proj=longlat +ellps=WGS84 +step +proj=unitconvert +xy_in=rad +xy_out=deg" >>> pipe_trans = Transformer.from_pipeline(pipeline_str) >>> xt, yt = pipe_trans.transform(2.1, 0.001) >>> "%.3f %.3f" % (xt, yt) '120.321 0.057' >>> transproj = Transformer.from_proj({"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'}, '+init=EPSG:4326') >>> xpj, ypj, zpj = transproj.transform(-2704026.010, -4253051.810, 3895878.820, radians=True) >>> "%.3f %.3f %.3f" % (xpj, ypj, zpj) '-2.137 0.661 -20.531' >>> transprojr = Transformer.from_proj('+init=EPSG:4326', {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'}) >>> 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_proj("epsg:4326", 4326, skip_equivalent=True) >>> xeq, yeq = transformer.transform(33, 98) >>> "%.0f %.0f" % (xeq, yeq) '33 98' """ # process inputs, making copies that support buffer API. inx, xisfloat, xislist, xistuple = _copytobuffer(xx) iny, yisfloat, yislist, yistuple = _copytobuffer(yy) if zz is not None: inz, zisfloat, zislist, zistuple = _copytobuffer(zz) else: inz = None if tt is not None: intime, tisfloat, tislist, tistuple = _copytobuffer(tt) else: intime = None # call pj_transform. inx,iny,inz buffers modified in place. self._transformer._transform( inx, iny, inz=inz, intime=intime, direction=direction, radians=radians, errcheck=errcheck, ) # if inputs were lists, tuples or floats, convert back. outx = _convertback(xisfloat, xislist, xistuple, inx) outy = _convertback(yisfloat, yislist, xistuple, iny) return_data = (outx, outy) if inz is not None: return_data += (_convertback(zisfloat, zislist, zistuple, inz), ) if intime is not None: return_data += (_convertback(tisfloat, tislist, tistuple, intime), ) return return_data
def get_factors( self, longitude: Any, latitude: Any, radians: bool = False, errcheck: bool = False, ) -> Factors: """ .. versionadded:: 2.6.0 Calculate various cartographic properties, such as scale factors, angular distortion and meridian convergence. Depending on the underlying projection values will be calculated either numerically (default) or analytically. The function also calculates the partial derivatives of the given coordinate. Parameters ---------- longitude: scalar or array (numpy or python) Input longitude coordinate(s). latitude: scalar or array (numpy or python) Input latitude coordinate(s). radians: boolean, optional If True, will expect input data to be in radians. Default is False (degrees). errcheck: boolean, optional If True an exception is raised if the errors are found in the process. By default errcheck=False and ``inf`` is returned. Returns ------- Factors """ # process inputs, making copies that support buffer API. inx, xisfloat, xislist, xistuple = _copytobuffer(longitude) iny, yisfloat, yislist, yistuple = _copytobuffer(latitude) # calculate the factors factors = self._transformer._get_factors(inx, iny, radians=radians, errcheck=errcheck) # if inputs were lists, tuples or floats, convert back. return Factors( meridional_scale=_convertback(xisfloat, xislist, xistuple, factors.meridional_scale), parallel_scale=_convertback(xisfloat, xislist, xistuple, factors.parallel_scale), areal_scale=_convertback(xisfloat, xislist, xistuple, factors.areal_scale), angular_distortion=_convertback(xisfloat, xislist, xistuple, factors.angular_distortion), meridian_parallel_angle=_convertback( xisfloat, xislist, xistuple, factors.meridian_parallel_angle), meridian_convergence=_convertback(xisfloat, xislist, xistuple, factors.meridian_convergence), tissot_semimajor=_convertback(xisfloat, xislist, xistuple, factors.tissot_semimajor), tissot_semiminor=_convertback(xisfloat, xislist, xistuple, factors.tissot_semiminor), dx_dlam=_convertback(xisfloat, xislist, xistuple, factors.dx_dlam), dx_dphi=_convertback(xisfloat, xislist, xistuple, factors.dx_dphi), dy_dlam=_convertback(xisfloat, xislist, xistuple, factors.dy_dlam), dy_dphi=_convertback(xisfloat, xislist, xistuple, factors.dy_dphi), )
def transform( self, xx: Any, yy: Any, zz: Any = None, tt: Any = None, radians: bool = False, errcheck: bool = False, direction: Union[TransformDirection, str] = TransformDirection.FORWARD, ) -> Any: """ Transform points between two coordinate systems. .. versionadded:: 2.1.1 errcheck .. versionadded:: 2.2.0 direction Parameters ---------- xx: scalar or array (numpy or python) Input x coordinate(s). yy: scalar or array (numpy or python) Input y coordinate(s). zz: scalar or array (numpy or python), optional Input z coordinate(s). tt: scalar or array (numpy or python), optional Input time coordinate(s). radians: boolean, optional If True, will expect input data to be in radians and will return radians if the projection is geographic. Default is False (degrees). Ignored for pipeline transformations with pyproj 2, but will work in pyproj 3. errcheck: boolean, optional 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: pyproj.enums.TransformDirection, optional The direction of the transform. Default is :attr:`pyproj.enums.TransformDirection.FORWARD`. Example: >>> from pyproj import Transformer >>> transformer = Transformer.from_crs("epsg:4326", "epsg:3857") >>> x3, y3 = transformer.transform(33, 98) >>> f"{x3:.3f} {y3:.3f}" '10909310.098 3895303.963' >>> pipeline_str = ( ... "+proj=pipeline +step +proj=longlat +ellps=WGS84 " ... "+step +proj=unitconvert +xy_in=rad +xy_out=deg" ... ) >>> pipe_trans = Transformer.from_pipeline(pipeline_str) >>> xt, yt = pipe_trans.transform(2.1, 0.001) >>> f"{xt:.3f} {yt:.3f}" '2.100 0.001' >>> transproj = Transformer.from_crs( ... {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'}, ... "EPSG:4326", ... always_xy=True, ... ) >>> xpj, ypj, zpj = transproj.transform( ... -2704026.010, ... -4253051.810, ... 3895878.820, ... radians=True, ... ) >>> f"{xpj:.3f} {ypj:.3f} {zpj:.3f}" '-2.137 0.661 -20.531' >>> transprojr = Transformer.from_crs( ... "EPSG:4326", ... {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'}, ... always_xy=True, ... ) >>> xpjr, ypjr, zpjr = transprojr.transform(xpj, ypj, zpj, radians=True) >>> f"{xpjr:.3f} {ypjr:.3f} {zpjr:.3f}" '-2704026.010 -4253051.810 3895878.820' >>> transformer = Transformer.from_proj("epsg:4326", 4326) >>> xeq, yeq = transformer.transform(33, 98) >>> f"{xeq:.0f} {yeq:.0f}" '33 98' """ # process inputs, making copies that support buffer API. inx, xisfloat, xislist, xistuple = _copytobuffer(xx) iny, yisfloat, yislist, yistuple = _copytobuffer(yy) if zz is not None: inz, zisfloat, zislist, zistuple = _copytobuffer(zz) else: inz = None if tt is not None: intime, tisfloat, tislist, tistuple = _copytobuffer(tt) else: intime = None # call pj_transform. inx,iny,inz buffers modified in place. self._transformer._transform( inx, iny, inz=inz, intime=intime, direction=direction, radians=radians, errcheck=errcheck, ) # if inputs were lists, tuples or floats, convert back. outx = _convertback(xisfloat, xislist, xistuple, inx) outy = _convertback(yisfloat, yislist, yistuple, iny) return_data = (outx, outy) if inz is not None: return_data += ( # type: ignore _convertback(zisfloat, zislist, zistuple, inz), ) if intime is not None: return_data += ( # type: ignore _convertback(tisfloat, tislist, tistuple, intime), ) return return_data
def test__copytobuffer__invalid(): with pytest.raises(TypeError): _copytobuffer("invalid")
def test__copytobuffer(in_data, is_float, is_list, is_tuple): assert _copytobuffer(in_data) == (array("d", [1]), is_float, is_list, is_tuple)