在R中绘制全局投影栅格

问题描述

我正在尝试在等距地球投影中绘制全局(lon:-180-180; lat -90- 90)栅格(无关紧要-可能是Winkel Tripel或Robinson),但边界在两侧都重复(见图)。如何避免这种情况?

This SO questionthis thread给出的答案仅适用于Mollweide中的绘图,而没有其他投影。

这是一个可复制的示例。

#include <iostream>
#include <array>
#include <vector>
using namespace std;

int main()
{
    array<unsigned int,20> n = {1,2,5,4,3,1,2};
    vector<bool> visited(n.size(),false);

    for (size_t i = 0; i < n.size(); i++)
    {
        int count = 0;
        // Skip this element if already processed
        if (visited[i] == true) continue;

        for (size_t j = 0; j < n.size(); j++){ 
            if (n[i] == n[j]){ 
                count++;
                // marking indexes already visited
                visited[j] = true;
            }
        }
        cout << "Frequency of " << n[i] << " is " << count << endl;
    }
    return 0;
}

非常感谢!

enter image description here

解决方法

您可以在mask=TRUE中使用terra::project参数

library(maptools)
library(terra)
data("wrld_simpl")
w <- vect(wrld_simpl)
r <- rast(ncol=180,nrow=90) 
r <- rasterize(w,r,field="UN") 
x <- project(r,"+proj=wintri",mask=TRUE)