示例#1
0
文件: crs.py 项目: edpop/pyproj
    def to_cf(self, wkt_version="WKT2_2018", errcheck=False):
        """
        .. versionadded:: 2.2.0

        This converts a :obj:`~pyproj.crs.CRS` object
        to a Climate and Forecast (CF) Grid Mapping Version 1.8 dict.

        .. warning:: The full projection will be stored in the
            crs_wkt attribute. However, other parameters may be lost
            if a mapping to the CF parameter is not found.

        Parameters
        ----------
        wkt_version: str
            Version of WKT supported by ~CRS.to_wkt.
        errcheck: bool, optional
            If True, will warn when parameters are ignored. Defaults to False.

        Returns
        -------
        dict: CF-1.8 version of the projection.

        """

        cf_dict = {"crs_wkt": self.to_wkt(wkt_version)}
        missing_names = ("unknown", "unnamed")
        if self.is_geographic and self.name not in missing_names:
            cf_dict["geographic_crs_name"] = self.name
        elif self.is_projected and self.name not in missing_names:
            cf_dict["projected_crs_name"] = self.name

        # ignore warning here as WKT string provided with projection
        with warnings.catch_warnings():
            warnings.filterwarnings(
                "ignore",
                "You will likely lose important projection information",
                UserWarning,
            )
            proj_dict = self.to_dict()

        if not proj_dict:
            return cf_dict
        proj_name = proj_dict.pop("proj")
        lonlat_possible_names = ("lonlat", "latlon", "longlat", "latlong")
        if proj_name in lonlat_possible_names:
            grid_mapping_name = "latitude_longitude"
        else:
            grid_mapping_name = INVERSE_GRID_MAPPING_NAME_MAP.get(
                proj_name, "unknown")

        if grid_mapping_name == "rotated_latitude_longitude":
            if proj_dict.pop("o_proj") not in lonlat_possible_names:
                grid_mapping_name = "unknown"

        # derive parameters from the coordinate operation
        if (grid_mapping_name == "unknown" and self.coordinate_operation
                and self.coordinate_operation.method_name
                in METHOD_NAME_TO_CF_MAP):
            grid_mapping_name = METHOD_NAME_TO_CF_MAP[
                self.coordinate_operation.method_name]
            for param in self.coordinate_operation.params:
                cf_dict[PARAM_TO_CF_MAP[param.name]] = param.value

        cf_dict["grid_mapping_name"] = grid_mapping_name

        # get best match for lon_0 value for projetion name
        lon_0 = proj_dict.pop("lon_0", None)
        if lon_0 is not None:
            try:
                cf_dict[LON_0_MAP[grid_mapping_name]] = lon_0
            except KeyError:
                cf_dict[LON_0_MAP["DEFAULT"]] = lon_0

        # get best match for k_0 value for projetion name
        k_0 = proj_dict.pop("k_0", None)
        if k_0 is not None:
            try:
                cf_dict[K_0_MAP[grid_mapping_name]] = k_0
            except KeyError:
                cf_dict[K_0_MAP["DEFAULT"]] = k_0

        # format the lat_1 and lat_2 for the standard parallel
        if "lat_1" in proj_dict and "lat_2" in proj_dict:
            cf_dict["standard_parallel"] = [
                proj_dict.pop("lat_1"),
                proj_dict.pop("lat_2"),
            ]
        elif "lat_1" in proj_dict:
            cf_dict["standard_parallel"] = proj_dict.pop("lat_1")
        elif "lat_ts" in proj_dict:
            cf_dict["standard_parallel"] = proj_dict.pop("lat_ts")

        skipped_params = []
        for proj_param, proj_val in proj_dict.items():
            try:
                cf_dict[INVERSE_PROJ_PARAM_MAP[proj_param]] = proj_val
            except KeyError:
                skipped_params.append(proj_param)

        if errcheck and skipped_params:
            warnings.warn("PROJ parameters not mapped to CF: {}".format(
                tuple(skipped_params)))
        return cf_dict
示例#2
0
    def to_cf(self, wkt_version="WKT2_2018", errcheck=False):
        """
        This converts a :obj:`~pyproj.crs.CRS` object
        to a Climate and Forecast (CF) Grid Mapping Version 1.8 dict.

        .. warning:: The full projection will be stored in the
            crs_wkt attribute. However, other parameters may be lost
            if a mapping to the CF parameter is not found.

        Parameters
        ----------
        wkt_version: str
            Version of WKT supported by ~CRS.to_wkt.
        errcheck: bool, optional
            If True, will warn when parameters are ignored. Defaults to False.

        Returns
        -------
        dict: CF-1.8 version of the projection.

        """

        cf_dict = {"crs_wkt": self.to_wkt(wkt_version)}
        if self.is_geographic and self.name != "unknown":
            cf_dict["geographic_crs_name"] = self.name
        elif self.is_projected and self.name != "unknown":
            cf_dict["projected_crs_name"] = self.name

        proj_dict = self.to_dict()
        proj_name = proj_dict.pop("proj")
        lonlat_possible_names = ("lonlat", "latlon", "longlat", "latlong")
        if proj_name in lonlat_possible_names:
            grid_mapping_name = "latitude_longitude"
        else:
            grid_mapping_name = INVERSE_GRID_MAPPING_NAME_MAP.get(
                proj_name, "unknown")

        if grid_mapping_name == "rotated_latitude_longitude":
            if proj_dict.pop("o_proj") not in lonlat_possible_names:
                grid_mapping_name = "unknown"

        cf_dict["grid_mapping_name"] = grid_mapping_name

        # get best match for lon_0 value for projetion name
        lon_0 = proj_dict.pop("lon_0", None)
        if lon_0 is not None:
            try:
                cf_dict[LON_0_MAP[grid_mapping_name]] = lon_0
            except KeyError:
                cf_dict[LON_0_MAP["DEFAULT"]] = lon_0

        # get best match for k_0 value for projetion name
        k_0 = proj_dict.pop("k_0", None)
        if k_0 is not None:
            try:
                cf_dict[K_0_MAP[grid_mapping_name]] = k_0
            except KeyError:
                cf_dict[K_0_MAP["DEFAULT"]] = k_0

        # format the lat_1 and lat_2 for the standard parallel
        if "lat_1" in proj_dict and "lat_2" in proj_dict:
            cf_dict["standard_parallel"] = [
                proj_dict.pop("lat_1"),
                proj_dict.pop("lat_2"),
            ]
        elif "lat_1" in proj_dict:
            cf_dict["standard_parallel"] = proj_dict.pop("lat_1")

        skipped_params = []
        for proj_param, proj_val in proj_dict.items():
            try:
                cf_dict[INVERSE_PROJ_PARAM_MAP[proj_param]] = proj_val
            except KeyError:
                skipped_params.append(proj_param)

        if errcheck and skipped_params:
            warnings.warn("PROJ parameters not mapped to CF: {}".format(
                tuple(skipped_params)))
        return cf_dict
示例#3
0
    def to_cf(self, wkt_version="WKT2_2018", errcheck=False):
        """
        This converts a :obj:`~pyproj.crs.CRS` object
        to a CF-1.8 dict.

        .. warning:: The full projection will be stored in the
            crs_wkt attribute. However, other parameters may be lost
            if a mapping to the CF parameter is not found.

        Parameters
        ----------
        wkt_version: str
            Version of WKT supported by ~CRS.to_wkt.
        errcheck: bool, optional
            If True, will warn when parameters are ignored. Defaults to False.

        Returns
        -------
        dict: CF-1.8 version of the projection.

        """

        cf_dict = {"crs_wkt": self.to_wkt(wkt_version)}
        if self.is_geographic and self.name != "unknown":
            cf_dict["geographic_crs_name"] = self.name
        elif self.is_projected and self.name != "unknown":
            cf_dict["projected_crs_name"] = self.name

        proj_dict = self.to_proj4_dict()
        proj_name = proj_dict.pop("proj")
        if proj_name in ("lonlat", "latlon", "longlat", "latlong"):
            grid_mapping_name = "latitude_longitude"
        else:
            grid_mapping_name = INVERSE_GRID_MAPPING_NAME_MAP.get(proj_name, "unknown")
        cf_dict["grid_mapping_name"] = grid_mapping_name

        # get best match for lon_0 value for projetion name
        lon_0 = proj_dict.pop("lon_0", None)
        if lon_0 is not None:
            try:
                cf_dict[LON_0_MAP[grid_mapping_name]] = lon_0
            except KeyError:
                cf_dict[LON_0_MAP["DEFAULT"]] = lon_0

        # get best match for k_0 value for projetion name
        k_0 = proj_dict.pop("k_0", None)
        if k_0 is not None:
            try:
                cf_dict[K_0_MAP[grid_mapping_name]] = k_0
            except KeyError:
                cf_dict[K_0_MAP["DEFAULT"]] = k_0

        # format the lat_1 and lat_2 for the standard parallel
        if "lat_1" in proj_dict and "lat_2" in proj_dict:
            cf_dict["standard_parallel"] = [
                proj_dict.pop("lat_1"),
                proj_dict.pop("lat_2"),
            ]
        elif "lat_1" in proj_dict:
            cf_dict["standard_parallel"] = proj_dict.pop("lat_1")

        skipped_params = []
        for proj_param, proj_val in proj_dict.items():
            try:
                cf_dict[INVERSE_PROJ_PARAM_MAP[proj_param]] = proj_val
            except KeyError:
                skipped_params.append(proj_param)

        if errcheck and skipped_params:
            warnings.warn(
                "PROJ parameters not mapped to CF: {}".format(tuple(skipped_params))
            )
        return cf_dict