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_crs_exception():
with pytest.raises(CRSError, match="Internal Proj Error"):
CRS("+proj=bobbyjoe")
def test_cf_from_invalid():
with pytest.raises(CRSError):
CRS.from_cf(
dict(
longitude_of_central_meridian=265.0, latitude_of_projection_origin=25.0
)
)
with pytest.raises(CRSError):
CRS.from_cf(
dict(grid_mapping_name="invalid", latitude_of_projection_origin=25.0)
)
def test_cf_from_invalid():
with pytest.raises(CRSError):
CRS.from_cf(
dict(
longitude_of_central_meridian=265.0, latitude_of_projection_origin=25.0
)
)
with pytest.raises(CRSError):
CRS.from_cf(
dict(grid_mapping_name="invalid", latitude_of_projection_origin=25.0)
)
def _prepare_from_string(in_crs_string):
if not in_crs_string:
raise CRSError("CRS is empty or invalid: {!r}".format(in_crs_string))
elif "{" in in_crs_string:
# may be json, try to decode it
try:
crs_dict = json.loads(in_crs_string, strict=False)
except ValueError:
raise CRSError("CRS appears to be JSON but is not valid")
if not crs_dict:
raise CRSError("CRS is empty JSON")
return _prepare_from_dict(crs_dict)
elif is_proj(in_crs_string):
in_crs_string = re.sub(r"[\s+]?=[\s+]?", "=", in_crs_string.lstrip())
# make sure the projection starts with +proj or +init
starting_params = ("+init", "+proj", "init", "proj")
if not in_crs_string.startswith(starting_params):
kvpairs = []
first_item_inserted = False
for kvpair in in_crs_string.split():
if not first_item_inserted and (kvpair.startswith(starting_params)):
kvpairs.insert(0, kvpair)
first_item_inserted = True
else:
kvpairs.append(kvpair)
in_crs_string = " ".join(kvpairs)
def _prepare_from_string(in_crs_string):
if not in_crs_string:
raise CRSError("CRS is empty or invalid: {!r}".format(in_crs_string))
elif "{" in in_crs_string:
# may be json, try to decode it
try:
crs_dict = json.loads(in_crs_string, strict=False)
except ValueError:
raise CRSError("CRS appears to be JSON but is not valid")
if not crs_dict:
raise CRSError("CRS is empty JSON")
return _prepare_from_dict(crs_dict)
elif is_proj(in_crs_string):
in_crs_string = re.sub(r"[\s+]?=[\s+]?", "=", in_crs_string.lstrip())
# make sure the projection starts with +proj or +init
starting_params = ("+init", "+proj", "init", "proj")
if not in_crs_string.startswith(starting_params):
kvpairs = []
first_item_inserted = False
for kvpair in in_crs_string.split():
if not first_item_inserted and (kvpair.startswith(starting_params)):
kvpairs.insert(0, kvpair)
first_item_inserted = True
else:
False
"""
if isinstance(projparams, str):
projstring = _prepare_from_string(projparams)
elif isinstance(projparams, dict):
projstring = _prepare_from_dict(projparams)
elif kwargs:
projstring = _prepare_from_dict(kwargs)
elif isinstance(projparams, int):
projstring = _prepare_from_epsg(projparams)
elif isinstance(projparams, (list, tuple)) and len(projparams) == 2:
projstring = _prepare_from_authority(*projparams)
elif hasattr(projparams, "to_wkt"):
projstring = projparams.to_wkt()
else:
raise CRSError("Invalid CRS input: {!r}".format(projparams))
super(CRS, self).__init__(projstring)
def _prepare_from_string(in_crs_string):
if not in_crs_string:
raise CRSError("CRS is empty or invalid: {!r}".format(in_crs_string))
elif "{" in in_crs_string:
# may be json, try to decode it
try:
crs_dict = json.loads(in_crs_string, strict=False)
except ValueError:
raise CRSError("CRS appears to be JSON but is not valid")
if not crs_dict:
raise CRSError("CRS is empty JSON")
return _prepare_from_dict(crs_dict)
elif is_proj(in_crs_string):
in_crs_string = re.sub(r"[\s+]?=[\s+]?", "=", in_crs_string.lstrip())
# make sure the projection starts with +proj or +init
starting_params = ("+init", "+proj", "init", "proj")
if not in_crs_string.startswith(starting_params):
kvpairs = []
----------
other: Any
Check if the other object is equivalent to this object.
If the other object is not a CRS, it will try to create one.
On Failure, it will return False.
ignore_axis_order: bool, optional
If True, it will compare the CRS class and ignore the axis order.
Default is False.
Returns
-------
bool
"""
try:
other = CRS.from_user_input(other)
except CRSError:
return False
return super(CRS, self).equals(other, ignore_axis_order=ignore_axis_order)
grid_mapping_variable = None
variable_is_itself_gridmapping = False
# test if the variable has a grid_mapping attribute
if hasattr(nc_handle[variable], 'grid_mapping'):
# good. attempt to load the grid_mapping information into a pyproj object
crs = _load_crs_from_cf_gridmapping(nc_handle, nc_handle[variable].grid_mapping)
grid_mapping_variable = nc_handle[variable].grid_mapping
elif hasattr(nc_handle[variable], 'grid_mapping_name') and \
nc_handle[variable].grid_mapping_name in _valid_cf_type_of_grid_mapping:
# this looks like a valid grid_mapping variable
try:
# try to load it
crs = _load_crs_from_cf_gridmapping(nc_handle, variable)
grid_mapping_variable = variable
variable_is_itself_gridmapping = True
except pyproj.exceptions.CRSError as ex:
raise ValueError("ERROR: pyproj didn't manage to load the CRS: {}".format(ex))
else:
# fallback position: maybe the variable is on a basic lat/lon grid with no
# grid_mapping. Note: there is no default CRS in CF, we choose WGS84
grid_mapping_variable = "latlon_default"
crs = pyproj.CRS.from_string('+proj=latlon +datum=WGS84 +ellps=WGS84')
# return
return crs, grid_mapping_variable, variable_is_itself_gridmapping