Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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.4105305015, 743254.1014318303, 2019.0),
)
def test_equivalent_proj__disabled():
transformer = Transformer.from_proj(3857, pyproj.Proj(3857).crs.to_proj4())
assert not transformer._transformer.skip_equivalent
assert not transformer._transformer.projections_equivalent
assert not transformer._transformer.projections_exact_same
def test_equivalent_proj__different():
transformer = Transformer.from_proj(3857, 4326, skip_equivalent=True)
assert transformer._transformer.skip_equivalent
assert not transformer._transformer.projections_equivalent
assert not transformer._transformer.projections_exact_same
def test_equivalent_proj__different():
transformer = Transformer.from_proj(3857, 4326, skip_equivalent=True)
assert transformer._transformer.skip_equivalent
assert not transformer._transformer.projections_equivalent
assert not transformer._transformer.projections_exact_same
import math
import pyproj
merc_proj = pyproj.Proj(init='epsg:3857')
latlng_proj = pyproj.Proj(proj='latlong')
# since 2.1.0, the preferred method of calling transform() is via a Transformer
# object, which caches and re-uses intermediate structures for speed. this
# makes about a 20x difference in the speed of running our test suite!
#
# see https://github.com/pyproj4/pyproj/issues/187
#
LL_TO_MERC = pyproj.Transformer.from_proj(latlng_proj, merc_proj)
MERC_TO_LL = pyproj.Transformer.from_proj(merc_proj, latlng_proj)
def serialize_coord(coord):
return '%d/%d/%d' % (coord.zoom, coord.column, coord.row)
def deserialize_coord(coord_string):
fields = coord_string.split('/')
if len(fields) != 3:
return None
# z/x/y -> /zoom/col/row
try:
zoom, col, row = map(int, fields)
except ValueError:
return None
coord = Coordinate(row=row, column=col, zoom=zoom)
def _repr_html_(self):
title = f"<b>{self.name} [{self.designator}] ({self.type})</b>"
shapes = ""
title += "<ul>"
bounds = self.bounds
projection = pyproj.Proj(
proj="aea", # equivalent projection
lat_1=bounds[1],
lat_2=bounds[3],
lat_0=(bounds[1] + bounds[3]) / 2,
lon_0=(bounds[0] + bounds[2]) / 2,
)
for polygon in self:
transformer = pyproj.Transformer.from_proj(
pyproj.Proj("epsg:4326"), projection, always_xy=True
)
projected_shape = transform(transformer.transform, polygon.polygon)
title += f"<li>{polygon.lower}, {polygon.upper}</li>"
shapes += projected_shape.simplify(1e3)._repr_svg_()
title += "</ul>"
no_wrap_div = '<div style="white-space: nowrap; width: 12%">{}</div>'
return title + no_wrap_div.format(shapes)
def get_transform_function(self, other):
""" Returns a function for transforming geometrical objects from one CRS to another. The function will support
transformations between any objects that pyproj supports.
For better time performance this method will cache results of 10 most recently used pairs of CRS classes.
:param self: Initial CRS
:type self: CRS
:param other: Target CRS
:type other: CRS
:return: A projection function obtained from pyproj package
:rtype: function
"""
if pyproj.__version__ >= '2':
return pyproj.Transformer.from_proj(self.projection(), other.projection(), skip_equivalent=True).transform
return functools.partial(pyproj.transform, self.projection(), other.projection())
return deepcopy(self)
# make sure the CRS defines vertical units
if "alt" in self.udims and not to_crs.is_vertical:
raise ValueError("Altitude dimension is defined, but CRS to transform does not contain vertical unit")
if "lat" in self.udims and "lon" not in self.udims:
raise ValueError("Cannot transform lat coordinates without lon coordinates")
if "lon" in self.udims and "lat" not in self.udims:
raise ValueError("Cannot transform lon coordinates without lat coordinates")
if "lat" in self.dims and "lon" in self.dims and abs(self.dims.index("lat") - self.dims.index("lon")) != 1:
raise ValueError("Cannot transform coordinates with nonadjacent lat and lon, transpose first")
transformer = pyproj.Transformer.from_proj(from_crs, to_crs, always_xy=True)
# Collect the individual coordinates
cs = [c for c in self.values()]
if "lat" in self.dims and "lon" in self.dims:
# try to do a simplified transform (resulting in unstacked lat-lon coordinates)
tc = self._simplified_transform(crs, transformer)
if tc:
cs[self.dims.index("lat")] = tc[0]
cs[self.dims.index("lon")] = tc[1]
# otherwise convert lat-lon to dependent coordinates
else:
ilat = self.dims.index("lat")
ilon = self.dims.index("lon")
if ilat == ilon - 1: