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_is_projected():
assert CRS("EPSG:3857").is_projected is True
lcc_crs = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 "
"+units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
assert CRS.from_user_input(lcc_crs).is_projected is True
wgs84_crs = CRS.from_string("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
assert CRS.from_user_input(wgs84_crs).is_projected is False
def test_is_projected():
assert CRS("EPSG:3857").is_projected is True
lcc_crs = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 "
"+units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
assert CRS.from_user_input(lcc_crs).is_projected is True
wgs84_crs = CRS.from_string("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
assert CRS.from_user_input(wgs84_crs).is_projected is False
def test_from_user_input_custom_crs_class():
assert CRS.from_user_input(CustomCRS()) == CRS.from_epsg(4326)
if self.crs is None:
raise ValueError(
"Cannot transform naive geometries. "
"Please set a crs on the object first."
)
if crs is None:
try:
crs = from_epsg(epsg)
except (TypeError, ValueError):
raise ValueError("Invalid epsg: {}".format(epsg))
# skip transformation if the input CRS and output CRS are the exact same
if _PYPROJ_VERSION >= LooseVersion("2.1.2") and pyproj.CRS.from_user_input(
self.crs
).is_exact_same(pyproj.CRS.from_user_input(crs)):
return self
if _PYPROJ_VERSION >= LooseVersion("2.2.0"):
# if availale, use always_xy=True to preserve GIS axis order
transformer = pyproj.Transformer.from_crs(self.crs, crs, always_xy=True)
project = transformer.transform
elif _PYPROJ_VERSION >= LooseVersion("2.1.0"):
# use transformer for repeated transformations
transformer = pyproj.Transformer.from_crs(self.crs, crs)
project = transformer.transform
else:
proj_in = pyproj.Proj(self.crs, preserve_units=True)
proj_out = pyproj.Proj(crs, preserve_units=True)
project = partial(pyproj.transform, proj_in, proj_out)
result = self.apply(lambda geom: transform(project, geom))
result.__class__ = GeoSeries
# remove old grid maping coordinate if exists
grid_mapping_name = (
self.grid_mapping if grid_mapping_name is None else grid_mapping_name
)
try:
del data_obj.coords[grid_mapping_name]
except KeyError:
pass
if data_obj.rio.crs is None:
raise MissingCRS(
"CRS not found. Please set the CRS with 'set_crs()' or 'write_crs()'."
)
# add grid mapping coordinate
data_obj.coords[grid_mapping_name] = xarray.Variable((), 0)
grid_map_attrs = pyproj.CRS.from_user_input(data_obj.rio.crs).to_cf()
# spatial_ref is for compatibility with GDAL
crs_wkt = crs_to_wkt(data_obj.rio.crs)
grid_map_attrs["spatial_ref"] = crs_wkt
grid_map_attrs["crs_wkt"] = crs_wkt
if transform is not None:
grid_map_attrs["GeoTransform"] = " ".join(
[str(item) for item in transform.to_gdal()]
)
data_obj.coords[grid_mapping_name].rio.set_attrs(grid_map_attrs, inplace=True)
return data_obj.rio.write_grid_mapping(
grid_mapping_name=grid_mapping_name, inplace=True
)
def to_geoparquet(self: GeoDataFrame, path: str):
"""
Given a geopandas GeoDataFrame, serialise geometry as WKB, store geometry
column name and CRS in Apache Arrow metadata, and write to parquet file.
Metadata about geometry columns is stored in a key named 'geometry_fields'
in the same way that pandas-specific metadata is stored in a key named
'pandas'.
"""
# capture geometry column name
field_name = self.geometry.name
# capture CRS
crs = pyproj.CRS.from_user_input(self.crs).to_wkt(version="WKT2_2018")
crs_format = "WKT2_2018"
# capture geometry types
geometry_types = self.geometry.geom_type.unique().tolist()
# serialise geometry
self = self._serialise_geometry(field_name)
# convert to pyarrow Table
self = pa.Table.from_pandas(self)
# set pyarrow Table metadata with geometry column name and CRS
geometry_metadata = {
"geometry_fields": [
{
"field_name": field_name,
"geometry_format": "wkb",
"geometry_types": geometry_types,
"crs": crs,
"crs_format": crs_format,
raise TypeError("Must set either crs or epsg for output.")
if self.crs is None:
raise ValueError(
"Cannot transform naive geometries. "
"Please set a crs on the object first."
)
if crs is None:
try:
crs = from_epsg(epsg)
except (TypeError, ValueError):
raise ValueError("Invalid epsg: {}".format(epsg))
# skip transformation if the input CRS and output CRS are the exact same
if _PYPROJ_VERSION >= LooseVersion("2.1.2") and pyproj.CRS.from_user_input(
self.crs
).is_exact_same(pyproj.CRS.from_user_input(crs)):
return self
if _PYPROJ_VERSION >= LooseVersion("2.2.0"):
# if availale, use always_xy=True to preserve GIS axis order
transformer = pyproj.Transformer.from_crs(self.crs, crs, always_xy=True)
project = transformer.transform
elif _PYPROJ_VERSION >= LooseVersion("2.1.0"):
# use transformer for repeated transformations
transformer = pyproj.Transformer.from_crs(self.crs, crs)
project = transformer.transform
else:
proj_in = pyproj.Proj(self.crs, preserve_units=True)
proj_out = pyproj.Proj(crs, preserve_units=True)
project = partial(pyproj.transform, proj_in, proj_out)
If true, the transform method will accept as input and return as output
coordinates using the traditional GIS order, that is longitude, latitude
for geographic CRS and easting, northing for most projected CRS.
Default is false.
area_of_interest: :class:`~pyproj.transformer.AreaOfInterest`, optional
The area of interest to help select the transformation.
Returns
-------
:obj:`~Transformer`
"""
return Transformer(
_Transformer.from_crs(
CRS.from_user_input(crs_from),
CRS.from_user_input(crs_to),
skip_equivalent=skip_equivalent,
always_xy=always_xy,
area_of_interest=area_of_interest,
)
The area of interest to help order the transformations based on the
best operation for the area.
Example:
>>> from pyproj.transformer import TransformerGroup
>>> trans_group = TransformerGroup(4326, 2964)
>>> trans_group
- transformers: 8
- unavailable_operations: 1
"""
super(TransformerGroup, self).__init__(
CRS.from_user_input(crs_from),
CRS.from_user_input(crs_to),
skip_equivalent=skip_equivalent,
always_xy=always_xy,
area_of_interest=area_of_interest,
)
for iii, transformer in enumerate(self._transformers):
self._transformers[iii] = Transformer(transformer)