Example #1
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))
Example #2
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)
        )
Example #3
0
def test_from_cf_transverse_mercator(towgs84_test):
    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",
    })
    expected_cf = {
        "semi_major_axis": 6378388.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": 297.0,
        "reference_ellipsoid_name": "International 1909 (Hayford)",
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "grid_mapping_name": "transverse_mercator",
        "latitude_of_projection_origin": 0.0,
        "longitude_of_central_meridian": 15.0,
        "false_easting": 2520000.0,
        "false_northing": 0.0,
        "scale_factor_at_central_meridian": 1.0,
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("BOUNDCRS[")
    assert_almost_equal(cf_dict.pop("towgs84"),
                        _try_list_if_string(towgs84_test))
    assert cf_dict == expected_cf
    # test roundtrip
    expected_cf["towgs84"] = _try_list_if_string(towgs84_test)
    _test_roundtrip(expected_cf, "BOUNDCRS[")
Example #4
0
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,
        "fase_easting": 0,
        "fase_northing": 0,
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
    proj_dict = crs.to_proj4_dict()
    assert proj_dict == {
        "proj": "lcc",
        "lat_1": 25,
        "lat_2": 30,
        "lat_0": 25,
        "lon_0": 265,
        "x_0": 0,
        "y_0": 0,
        "datum": "WGS84",
        "units": "m",
        "no_defs": None,
        "type": "crs",
    }
Example #5
0
def test_geos_crs_fixed_angle_axis():
    crs = CRS.from_cf(
        dict(
            grid_mapping_name="geostationary",
            perspective_point_height=1,
            fixed_angle_axis="y",
        ))
    expected_cf = {
        "semi_major_axis": 6378137.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": crs.ellipsoid.inverse_flattening,
        "reference_ellipsoid_name": "WGS 84",
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "horizontal_datum_name": "World Geodetic System 1984",
        "grid_mapping_name": "geostationary",
        "sweep_angle_axis": "x",
        "perspective_point_height": 1.0,
        "latitude_of_projection_origin": 0.0,
        "longitude_of_projection_origin": 0.0,
        "false_easting": 0.0,
        "false_northing": 0.0,
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == expected_cf
    # test roundtrip
    _test_roundtrip(expected_cf, "PROJCRS[")
Example #6
0
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",
            "units": "m",
            "x_0": 0,
            "y_0": 0,
        }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == {
        "grid_mapping_name": "mercator",
        "longitude_of_projection_origin": 10,
        "standard_parallel": 21.354,
        "false_easting": 0,
        "false_northing": 0,
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
Example #7
0
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,
            "fase_easting": 2520000,
            "fase_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"
    assert cf_dict["latitude_of_projection_origin"] == 0
    assert cf_dict["longitude_of_central_meridian"] == 15
    assert cf_dict["fase_easting"] == 2520000
    assert cf_dict["fase_northing"] == 0
    assert cf_dict["reference_ellipsoid_name"] == "intl"
    assert cf_dict["unit"] == "m"
    assert_almost_equal(cf_dict["towgs84"], list(towgs84_test))
Example #8
0
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",
                "no_defs": None,
                "type": "crs",
            }
        assert cf_dict == dict(
            grid_mapping_name="rotated_latitude_longitude",
            grid_north_pole_latitude=32.5,
            grid_north_pole_longitude=170.0,
            horizontal_datum_name="WGS84",
        )
    else:
        assert cf_dict == {}
        with pytest.warns(UserWarning):
            assert crs.to_dict() == {}
Example #9
0
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,
            "fase_easting": 2520000,
            "fase_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"
    assert cf_dict["latitude_of_projection_origin"] == 0
    assert cf_dict["longitude_of_central_meridian"] == 15
    assert cf_dict["fase_easting"] == 2520000
    assert cf_dict["fase_northing"] == 0
    assert cf_dict["reference_ellipsoid_name"] == "intl"
    assert cf_dict["unit"] == "m"
    assert_almost_equal(cf_dict["towgs84"], list(towgs84_test))
Example #10
0
def get_proj4(grid_mapping):
    cf_parameters = dict(grid_mapping.attrs)
    decode_bytes(cf_parameters)

    crs_dict = CRS.from_cf(cf_parameters).to_dict()
    if 'standard_parallel' in cf_parameters:
        crs_dict['lat_ts'] = cf_parameters['standard_parallel']
    return CRS.from_dict(crs_dict).to_proj4()
Example #11
0
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,
        "azimuth_of_central_line": 0.35,
        "fase_easting": 0,
        "fase_northing": 0,
        "reference_ellipsoid_name": "WGS84",
        "unit": "m",
    }
    assert crs.to_dict() == {
        "proj": "omerc",
        "lat_0": 10,
        "lonc": 15,
        "alpha": 0.35,
        "gamma": 0.35,
        "k": 1,
        "x_0": 0,
        "y_0": 0,
        "ellps": "WGS84",
        "units": "m",
        "no_defs": None,
        "type": "crs",
    }
    # test CRS with input as lon_0 from the user
    lon0crs_cf = CRS(
        {
            "proj": "omerc",
            "lat_0": 10,
            "lon_0": 15,
            "alpha": 0.35,
            "gamma": 0.35,
            "k": 1,
            "x_0": 0,
            "y_0": 0,
            "ellps": "WGS84",
            "units": "m",
            "no_defs": None,
            "type": "crs",
        }
    ).to_cf()
    assert lon0crs_cf.pop("crs_wkt").startswith("PROJCRS[")
    assert lon0crs_cf == cf_dict
Example #12
0
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,
        "azimuth_of_central_line": 0.35,
        "fase_easting": 0,
        "fase_northing": 0,
        "reference_ellipsoid_name": "WGS84",
        "unit": "m",
    }
    assert crs.to_proj4_dict() == {
        "proj": "omerc",
        "lat_0": 10,
        "lonc": 15,
        "alpha": 0.35,
        "gamma": 0.35,
        "k": 1,
        "x_0": 0,
        "y_0": 0,
        "ellps": "WGS84",
        "units": "m",
        "no_defs": None,
        "type": "crs",
    }
    # test CRS with input as lon_0 from the user
    lon0crs_cf = CRS(
        {
            "proj": "omerc",
            "lat_0": 10,
            "lon_0": 15,
            "alpha": 0.35,
            "gamma": 0.35,
            "k": 1,
            "x_0": 0,
            "y_0": 0,
            "ellps": "WGS84",
            "units": "m",
            "no_defs": None,
            "type": "crs",
        }
    ).to_cf()
    assert lon0crs_cf.pop("crs_wkt").startswith("PROJCRS[")
    assert lon0crs_cf == cf_dict
Example #13
0
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",
    }
Example #14
0
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",
    }
Example #15
0
def test_cf_from_utm():
    crs = CRS.from_cf(dict(crs_wkt="epsg:32615"))
    with pytest.warns(UserWarning):
        cf_dict = crs.to_cf(errcheck=True)
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == {
        "projected_crs_name": "WGS 84 / UTM zone 15N",
        "grid_mapping_name": "unknown",
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
Example #16
0
def test_cf_from_utm():
    crs = CRS.from_cf(dict(crs_wkt="epsg:32615"))
    with pytest.warns(UserWarning):
        cf_dict = crs.to_cf(errcheck=True)
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == {
        "projected_crs_name": "WGS 84 / UTM zone 15N",
        "grid_mapping_name": "unknown",
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
Example #17
0
    def set_cf_grid_mapping(self, grid_mapping_var, errcheck=False):
        """Set CRS information based on CF standard 'grid_mapping' variable.

        See :meth:`pyproj.crs.CRS.from_cf` for details. Argument can be
        DataArray or Variable object for the grid mapping variable or a
        dictionary of CF standard grid mapping attributes.

        """
        # XXX: Should this just be part of the CRS setter? kwargs can't be passed then
        if not isinstance(grid_mapping_var, dict):
            grid_mapping_var = grid_mapping_var.attrs
        self._crs = CRS.from_cf(grid_mapping_var, errcheck=errcheck)
Example #18
0
def _test_roundtrip(expected_cf, wkt_startswith):
    crs = CRS.from_cf(expected_cf)
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith(wkt_startswith)
    assert_almost_equal(cf_dict.pop("semi_minor_axis"),
                        expected_cf.pop("semi_minor_axis"))
    assert_almost_equal(cf_dict.pop("inverse_flattening"),
                        expected_cf.pop("inverse_flattening"))
    if "towgs84" in expected_cf:
        assert_almost_equal(cf_dict.pop("towgs84"), expected_cf.pop("towgs84"))

    assert cf_dict == expected_cf
Example #19
0
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
Example #20
0
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,
        ))
    expected_cf = {
        "semi_major_axis": 6378137.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": crs.ellipsoid.inverse_flattening,
        "reference_ellipsoid_name": "WGS 84",
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "grid_mapping_name": "oblique_mercator",
        "latitude_of_projection_origin": 10.0,
        "longitude_of_projection_origin": 15.0,
        "azimuth_of_central_line": 0.35,
        "scale_factor_at_projection_origin": 1.0,
        "false_easting": 0.0,
        "false_northing": 0.0,
        "geographic_crs_name": "undefined",
        "projected_crs_name": "undefined",
        "horizontal_datum_name": "undefined",
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == expected_cf
    # test roundtrip
    _test_roundtrip(expected_cf, "PROJCRS[")
    with pytest.warns(UserWarning):
        assert crs.to_dict() == {
            "proj": "omerc",
            "lat_0": 10,
            "lonc": 15,
            "alpha": 0.35,
            "gamma": 0,
            "k": 1,
            "x_0": 0,
            "y_0": 0,
            "ellps": "WGS84",
            "units": "m",
            "no_defs": None,
            "type": "crs",
        }
Example #21
0
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
Example #22
0
def test_cf_from_utm():
    crs = CRS.from_cf(dict(crs_wkt="epsg:32615"))
    with pytest.warns(UserWarning):
        cf_dict = crs.to_cf(errcheck=True)
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == {
        "projected_crs_name": "WGS 84 / UTM zone 15N",
        "latitude_of_projection_origin": 0.0,
        "longitude_of_central_meridian": -93.0,
        "scale_factor_at_central_meridian": 0.9996,
        "false_easting": 500000.0,
        "false_northing": 0.0,
        "grid_mapping_name": "transverse_mercator",
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
Example #23
0
def test_cf_rotated_latlon__grid():
    crs = CRS.from_cf(
        dict(
            grid_mapping_name="rotated_latitude_longitude",
            grid_north_pole_latitude=32.5,
            grid_north_pole_longitude=170.0,
            north_pole_grid_longitude=0,
        ))
    assert crs.to_proj4_dict() == {
        "proj": "ob_tran",
        "o_proj": "latlon",
        "o_lat_p": 32.5,
        "o_lon_p": 170.0,
        "lon_0": 0,
        "type": "crs",
    }
Example #24
0
def test_build_custom_datum__default_ellipsoid():
    cf_dict = {
        "prime_meridian_name": "Paris",
        "grid_mapping_name": "oblique_mercator",
        "latitude_of_projection_origin": 0.0,
        "longitude_of_projection_origin": 13.809602948622212,
        "azimuth_of_central_line": 8.998112717187938,
        "scale_factor_at_projection_origin": 1.0,
        "false_easting": 0.0,
        "false_northing": 0.0,
    }
    crs = CRS.from_cf(cf_dict)
    assert crs.datum.name == "undefined"
    assert crs.ellipsoid.name == "WGS 84"
    assert crs.prime_meridian.name == "Paris"
    assert str(crs.prime_meridian.longitude).startswith("2.")
Example #25
0
    def _set_crs(self, crs=None, variables=None):
        """Set CRS for this Dataset's variables.

        Parameters
        ----------
        crs : pyproj.crs.CRS
            Set CRS for specified variables to this CRS object. If not
            provided then the variables metadata is used to determine the
            best CRS (CF 'grid_mapping' variable, 'crs' attribute, etc).
        variables : iterable
            Names of variables that will have CRS information applied or
            determined. If not provided then all variables will be used or
            checked.

        Returns
        -------
        dict
            Map of variable names to DataArray objects with CRS objects
            applied.

        """
        if variables is None:
            variables = self._obj.variables.keys()

        applied_vars = {}
        gmap_names = {}
        for var_name in variables:
            var = self._obj[var_name]
            if crs is not None:
                var.geo.crs = crs
                applied_vars[var_name] = var
                continue

            # distribute grid_mapping variables to data variables if present
            gmap_name = var.attrs.get("grid_mapping")
            if gmap_name in self._obj.variables:
                gmap_var = self._obj[gmap_name]
                if gmap_name not in gmap_names:
                    gmap_crs = CRS.from_cf(gmap_var.attrs)
                    gmap_names[gmap_name] = gmap_crs
                var.geo.crs = gmap_names[gmap_name]
                applied_vars[var_name] = var
                continue

            # let the variable determine its own CRS
            applied_vars[var_name] = var
        return applied_vars
Example #26
0
def test_cf_lambert_conformal_conic_2sp(standard_parallel):
    crs = CRS.from_cf(
        dict(
            grid_mapping_name="lambert_conformal_conic",
            standard_parallel=standard_parallel,
            longitude_of_central_meridian=265.0,
            latitude_of_projection_origin=25.0,
        ))
    expected_cf = {
        "semi_major_axis": 6378137.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": crs.ellipsoid.inverse_flattening,
        "reference_ellipsoid_name": "WGS 84",
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "horizontal_datum_name": "World Geodetic System 1984",
        "grid_mapping_name": "lambert_conformal_conic",
        "standard_parallel": (25.0, 30.0),
        "latitude_of_projection_origin": 25.0,
        "longitude_of_central_meridian": 265.0,
        "false_easting": 0.0,
        "false_northing": 0.0,
        "geographic_crs_name": "undefined",
        "projected_crs_name": "undefined",
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == expected_cf
    # test roundtrip
    _test_roundtrip(expected_cf, "PROJCRS[")

    with pytest.warns(UserWarning):
        proj_dict = crs.to_dict()
    assert proj_dict == {
        "proj": "lcc",
        "lat_1": 25,
        "lat_2": 30,
        "lat_0": 25,
        "lon_0": 265,
        "x_0": 0,
        "y_0": 0,
        "datum": "WGS84",
        "units": "m",
        "no_defs": None,
        "type": "crs",
    }
Example #27
0
def test_cf_from_latlon__named():
    crs = CRS.from_cf(dict(spatial_ref="epsg:4326"))
    expected_cf = {
        "semi_major_axis": 6378137.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": crs.ellipsoid.inverse_flattening,
        "reference_ellipsoid_name": "WGS 84",
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "geographic_crs_name": "WGS 84",
        "grid_mapping_name": "latitude_longitude",
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("GEOGCRS[")
    assert cf_dict == expected_cf
    # test roundtrip
    _test_roundtrip(expected_cf, "GEOGCRS[")
Example #28
0
def test_cf_rotated_latlon__grid():
    crs = CRS.from_cf(
        dict(
            grid_mapping_name="rotated_latitude_longitude",
            grid_north_pole_latitude=32.5,
            grid_north_pole_longitude=170.0,
            north_pole_grid_longitude=0,
        )
    )
    assert crs.to_proj4_dict() == {
        "proj": "ob_tran",
        "o_proj": "latlon",
        "o_lat_p": 32.5,
        "o_lon_p": 170.0,
        "lon_0": 0,
        "type": "crs",
    }
Example #29
0
def test_build_custom_datum__default_prime_meridian():
    cf_dict = {
        "semi_major_axis": 6370997.0,
        "semi_minor_axis": 6370997.0,
        "inverse_flattening": 0.0,
        "grid_mapping_name": "oblique_mercator",
        "latitude_of_projection_origin": 0.0,
        "longitude_of_projection_origin": 13.809602948622212,
        "azimuth_of_central_line": 8.998112717187938,
        "scale_factor_at_projection_origin": 1.0,
        "false_easting": 0.0,
        "false_northing": 0.0,
    }
    crs = CRS.from_cf(cf_dict)
    assert crs.datum.name == "undefined"
    assert crs.ellipsoid.name == "undefined"
    assert crs.prime_meridian.name == "Greenwich"
    assert crs.prime_meridian.longitude == 0
Example #30
0
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",
        "unit": "m",
    }
Example #31
0
def test_cf_from_latlon():
    crs = CRS.from_cf(
        dict(
            grid_mapping_name="latitude_longitude",
            semi_major_axis=6378137.0,
            inverse_flattening=298.257223,
        ))
    expected_cf = {
        "semi_major_axis": 6378137.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": crs.ellipsoid.inverse_flattening,
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "grid_mapping_name": "latitude_longitude",
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("GEOGCRS[")
    assert cf_dict == expected_cf
    # test roundtrip
    _test_roundtrip(expected_cf, "GEOGCRS[")
Example #32
0
def test_mercator_b():
    crs = CRS.from_cf({
        "grid_mapping_name": "mercator",
        "longitude_of_projection_origin": 10,
        "standard_parallel": 21.354,
        "false_easting": 0,
        "false_northing": 0,
    })
    expected_cf = {
        "semi_major_axis": 6378137.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": crs.ellipsoid.inverse_flattening,
        "reference_ellipsoid_name": "WGS 84",
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "horizontal_datum_name": "World Geodetic System 1984",
        "grid_mapping_name": "mercator",
        "standard_parallel": 21.354,
        "longitude_of_projection_origin": 10.0,
        "false_easting": 0.0,
        "false_northing": 0.0,
        "geographic_crs_name": "undefined",
        "projected_crs_name": "undefined",
    }
    with pytest.warns(UserWarning):
        assert crs.to_dict() == {
            "datum": "WGS84",
            "lat_ts": 21.354,
            "lon_0": 10,
            "no_defs": None,
            "proj": "merc",
            "type": "crs",
            "units": "m",
            "x_0": 0,
            "y_0": 0,
        }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == expected_cf
    # test roundtrip
    _test_roundtrip(expected_cf, "PROJCRS[")
Example #33
0
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,
        ))
    expected_cf = {
        "semi_major_axis": 6378137.0,
        "semi_minor_axis": crs.ellipsoid.semi_minor_metre,
        "inverse_flattening": crs.ellipsoid.inverse_flattening,
        "reference_ellipsoid_name": "WGS 84",
        "longitude_of_prime_meridian": 0.0,
        "prime_meridian_name": "Greenwich",
        "horizontal_datum_name": "World Geodetic System 1984",
        "grid_mapping_name": "rotated_latitude_longitude",
        "grid_north_pole_latitude": 32.5,
        "grid_north_pole_longitude": 170.0,
        "north_pole_grid_longitude": 0.0,
        "geographic_crs_name": "undefined",
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("GEOGCRS[")
    assert cf_dict == expected_cf
    # test roundtrip
    _test_roundtrip(expected_cf, "GEOGCRS[")
    with pytest.warns(UserWarning):
        proj_dict = crs.to_dict()
    if LooseVersion(__proj_version__) >= LooseVersion("6.3.0"):
        assert proj_dict == {
            "proj": "ob_tran",
            "o_proj": "longlat",
            "o_lat_p": 32.5,
            "o_lon_p": 170.0,
            "lon_0": 0,
            "datum": "WGS84",
            "no_defs": None,
            "type": "crs",
        }
    else:
        assert proj_dict == {}
Example #34
0
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,
        ))
    assert crs.to_proj4_dict() == {
        "proj": "ob_tran",
        "o_proj": "latlon",
        "o_lat_p": 32.5,
        "o_lon_p": 170.0,
        "type": "crs",
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == dict(
        grid_mapping_name="rotated_latitude_longitude",
        grid_north_pole_latitude=32.5,
        grid_north_pole_longitude=170.0,
    )
Example #35
0
    def _get_grid(self, ds):
        from numpy import meshgrid, ndarray
        from pyproj import CRS, Proj

        proj_dict = ds.goes_imager_projection.attrs
        for i in proj_dict.keys():
            if type(proj_dict[i]) is ndarray:
                proj_dict[i] = proj_dict[i][0]
        crs = CRS.from_cf(proj_dict)
        ds.attrs["projection"] = crs.to_wkt()
        proj = Proj(crs)
        satellite_height = ds.goes_imager_projection.perspective_point_height
        xx, yy = meshgrid(ds.x.values * satellite_height,
                          ds.y.values * satellite_height)
        lon, lat = proj(xx, yy, inverse=True)
        ds["latitude"] = (("y", "x"), lat)
        ds["longitude"] = (("y", "x"), lon)
        ds["longitude"] = ds.longitude.where(ds.longitude < 400).fillna(1e30)
        ds["latitude"] = ds.latitude.where(ds.latitude < 100).fillna(1e30)
        ds = ds.set_coords(["latitude", "longitude"])
        return ds
Example #36
0
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,
        )
    )
    assert crs.to_proj4_dict() == {
        "proj": "ob_tran",
        "o_proj": "latlon",
        "o_lat_p": 32.5,
        "o_lon_p": 170.0,
        "type": "crs",
    }
    cf_dict = crs.to_cf()
    assert cf_dict.pop("crs_wkt").startswith("PROJCRS[")
    assert cf_dict == dict(
        grid_mapping_name="rotated_latitude_longitude",
        grid_north_pole_latitude=32.5,
        grid_north_pole_longitude=170.0,
    )
Example #37
0
def test_cf_lambert_conformal_conic():
    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,
        "fase_easting": 0,
        "fase_northing": 0,
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
Example #38
0
def test_cf_rotated_latlon__grid():
    crs = CRS.from_cf(
        dict(
            grid_mapping_name="rotated_latitude_longitude",
            grid_north_pole_latitude=32.5,
            grid_north_pole_longitude=170.0,
            north_pole_grid_longitude=0,
        ))
    with pytest.warns(UserWarning):
        if LooseVersion(proj_version_str) >= LooseVersion("6.3.0"):
            assert crs.to_dict() == {
                "proj": "ob_tran",
                "o_proj": "longlat",
                "o_lat_p": 32.5,
                "o_lon_p": 170.0,
                "lon_0": 0,
                "datum": "WGS84",
                "no_defs": None,
                "type": "crs",
            }
        else:
            assert crs.to_dict() == {}
Example #39
0
def test_cf_lambert_conformal_conic():
    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,
        "fase_easting": 0,
        "fase_northing": 0,
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
Example #40
0
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,
        "fase_easting": 0,
        "fase_northing": 0,
        "horizontal_datum_name": "WGS84",
        "unit": "m",
    }
    proj_dict = crs.to_proj4_dict()
    assert proj_dict == {
        "proj": "lcc",
        "lat_1": 25,
        "lat_2": 30,
        "lat_0": 25,
        "lon_0": 265,
        "x_0": 0,
        "y_0": 0,
        "datum": "WGS84",
        "units": "m",
        "no_defs": None,
        "type": "crs",
    }