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_cf_rotated_latlon():
crs = CRS.from_cf(
dict(
grid_mapping_name="rotated_latitude_longitude",
grid_north_pole_latitude=32.5,
grid_north_pole_longitude=170.0,
)
)
cf_dict = crs.to_cf()
assert cf_dict.pop("crs_wkt").startswith("GEOGCRS[")
if LooseVersion(proj_version_str) >= LooseVersion("6.3.0"):
with pytest.warns(UserWarning):
assert crs.to_dict() == {
"proj": "ob_tran",
"o_proj": "longlat",
"o_lat_p": 32.5,
"o_lon_p": 170.0,
"datum": "WGS84",
def test_cf_from_latlon__named():
crs = CRS.from_cf(dict(spatial_ref="epsg:4326"))
with pytest.warns(UserWarning):
cf_dict = crs.to_cf(errcheck=True)
assert cf_dict.pop("crs_wkt").startswith("GEOGCRS[")
assert cf_dict == {
"geographic_crs_name": "WGS 84",
"grid_mapping_name": "latitude_longitude",
"horizontal_datum_name": "WGS84",
}
def test_mercator():
crs = CRS.from_cf(
{
"grid_mapping_name": "mercator",
"longitude_of_projection_origin": 10,
"standard_parallel": 21.354,
"false_easting": 0,
"false_northing": 0,
}
)
with pytest.warns(UserWarning):
assert crs.to_dict() == {
"datum": "WGS84",
"lat_ts": 21.354,
"lon_0": 10,
"no_defs": None,
"proj": "merc",
"type": "crs",
def test_cf_from_latlon():
crs = CRS.from_cf(
dict(
grid_mapping_name="latitude_longitude",
semi_major_axis=6378137.0,
inverse_flattening=298.257223,
)
)
with pytest.warns(UserWarning):
cf_dict = crs.to_cf(errcheck=True)
assert len(cf_dict) == 4
assert cf_dict["crs_wkt"].startswith("GEOGCRS[")
assert cf_dict["grid_mapping_name"] == "latitude_longitude"
assert cf_dict["semi_major_axis"] == 6378137.0
assert cf_dict["inverse_flattening"] == 298.257223
def test_oblique_mercator():
crs = CRS.from_cf(
dict(
grid_mapping_name="oblique_mercator",
azimuth_of_central_line=0.35,
latitude_of_projection_origin=10,
longitude_of_projection_origin=15,
reference_ellipsoid_name="WGS84",
false_easting=0.0,
false_northing=0.0,
)
)
cf_dict = crs.to_cf()
assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
assert cf_dict == {
"grid_mapping_name": "oblique_mercator",
"latitude_of_projection_origin": 10,
"longitude_of_projection_origin": 15,
def test_from_cf_transverse_mercator():
towgs84_test = (-122.74, -34.27, -22.83, -1.884, -3.4, -3.03, -15.62)
crs = CRS.from_cf(
{
"grid_mapping_name": "transverse_mercator",
"latitude_of_projection_origin": 0,
"longitude_of_central_meridian": 15,
"false_easting": 2520000,
"false_northing": 0,
"reference_ellipsoid_name": "intl",
"towgs84": towgs84_test,
"unit": "m",
}
)
with pytest.warns(UserWarning):
cf_dict = crs.to_cf(errcheck=True)
assert len(cf_dict) == 9
assert cf_dict["crs_wkt"].startswith("BOUNDCRS[")
assert cf_dict["grid_mapping_name"] == "transverse_mercator"
def test_cf_lambert_conformal_conic_1sp():
crs = CRS.from_cf(
dict(
grid_mapping_name="lambert_conformal_conic",
standard_parallel=25.0,
longitude_of_central_meridian=265.0,
latitude_of_projection_origin=25.0,
)
)
with pytest.warns(UserWarning):
cf_dict = crs.to_cf(errcheck=True)
assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
assert cf_dict == {
"grid_mapping_name": "lambert_conformal_conic",
"longitude_of_central_meridian": 265,
"scale_factor_at_projection_origin": 1,
"standard_parallel": 25,
"latitude_of_projection_origin": 25,
def test_crs_fixed_angle_axis():
crs = CRS.from_cf(
dict(
grid_mapping_name="geostationary",
perspective_point_height=1,
fixed_angle_axis="y",
)
)
cf_dict = crs.to_cf()
assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
assert cf_dict == {
"grid_mapping_name": "geostationary",
"longitude_of_projection_origin": 0,
"sweep_angle_axis": "x",
"perspective_point_height": 1,
"false_easting": 0,
"false_northing": 0,
"horizontal_datum_name": "WGS84",
def test_cf_lambert_conformal_conic_2sp():
crs = CRS.from_cf(
dict(
grid_mapping_name="lambert_conformal_conic",
standard_parallel=[25.0, 30.0],
longitude_of_central_meridian=265.0,
latitude_of_projection_origin=25.0,
)
)
with pytest.warns(UserWarning):
cf_dict = crs.to_cf(errcheck=True)
assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
assert cf_dict == {
"grid_mapping_name": "lambert_conformal_conic",
"longitude_of_central_meridian": 265,
"standard_parallel": [25, 30],
"latitude_of_projection_origin": 25,
"false_easting": 0,
# check the variable exists
try:
v = nc_handle[grid_mapping_varname]
except KeyError:
raise KeyError("Variable '{}' does not exist in netCDF file".format(grid_mapping_varname))
# check this indeed is a supported grid mapping variable
try:
if v.grid_mapping_name not in _valid_cf_type_of_grid_mapping:
raise ValueError("Not a valid CF grid_mapping variable ({})".format(grid_mapping_varname))
except AttributeError:
# no :grid_mapping_name thus it cannot be a valid grid_mapping variable
raise ValueError("Not a valid CF grid_mapping variable ({})".format(grid_mapping_varname))
# use pyproj to load the CRS
return pyproj.CRS.from_cf(v.attrs)