固定地图库数据以太平洋中心(经度0°-360°)显示 [英] Fixing maps library data for Pacific centred (0°-360° longitude) display

查看:395
本文介绍了固定地图库数据以太平洋中心(经度0°-360°)显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R maps程序包在世界地图上绘制一些点,例如:

I'm plotting some points on a map of the world using the R maps package, something like:

绘制底图的命令是:

map("world", fill=TRUE, col="white", bg="gray", ylim=c(-60, 90), mar=c(0,0,0,0))

但是我需要显示以太平洋为中心的地图.我使用map("world2",等从地图包中使用以太平洋为中心的底图,并使用以下命令转换数据框(df)中数据点的坐标:

But I need to display Pacific centred map. I use map("world2", etc to use the Pacific centred basemap from the maps package, and convert the coordinates of the data points in my dataframe (df) with:

df$longitude[df$longitude < 0] = df$longitude[df$longitude < 0] + 360

如果我不使用fill选项,这将起作用,但是对于fill,交叉0°的多边形会引起问题.

This works if I don't use the fill option, but with fill the polygons which cross 0° cause problems.

我想我需要以某种方式转换maps库中的多边形数据,但是我不知道该怎么做.

I guess I need to transform the polygon data from the maps library somehow to sort this out, but I have no idea how to get at this.

我的理想解决方案是绘制一个地图,其左边界为-20°,右边界为-30°(即330°).以下内容可将正确的点和海岸线显示在地图上,但过零问题是相同的

My ideal solution would be to draw a maps with a left boundary at -20° and a right boundary at -30° (i.e. 330°). The following gets the correct points and coastlines onto the map, but the crossing-zero problem is the same

df$longitude[df$longitude < -20] = df$longitude[d$longitude < -20] + 360
map("world", fill=TRUE, col="white", bg="gray", mar=c(0,0,0,0),
  ylim=c(-60, 90), xlim=c(-20, 330))
map("world2", add=TRUE, col="white", bg="gray", fill=TRUE, xlim=c(180, 330))

任何帮助将不胜感激.

推荐答案

您可以使用以下事实:在内部,可以重新计算map()函数返回的map对象,并在map()函数中再次使用该对象.我将创建一个包含各个多边形的列表,检查哪些多边形的经度值非常不同,然后重新排列这些多边形.我在下面的函数*中提供了这种方法的示例,该函数允许类似:

You could use the fact that internally, a map object returned by the map() function can be recalculated and used again in the map() function. I'd create a list with individual polygons, check which ones have very different longitude values, and rearrange those ones. I gave an example of this approach in the function below*, which allows something like :

plot.map("world", center=180, col="white",bg="gray",
   fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))

获得

如果我是你,我会把所有事情都转移一点,就像:

If I were you, I'd shift everything a bit more, like in :

plot.map("world", center=200, col="white",bg="gray",
   fill=TRUE,ylim=c(-60,90),mar=c(0,0,0,0))

功能:

plot.map<- function(database,center,...){
    Obj <- map(database,...,plot=F)
    coord <- cbind(Obj[[1]],Obj[[2]])

    # split up the coordinates
    id <- rle(!is.na(coord[,1]))
    id <- matrix(c(1,cumsum(id$lengths)),ncol=2,byrow=T)
    polygons <- apply(id,1,function(i){coord[i[1]:i[2],]})

    # split up polygons that differ too much
    polygons <- lapply(polygons,function(x){
        x[,1] <- x[,1] + center
        x[,1] <- ifelse(x[,1]>180,x[,1]-360,x[,1])
        if(sum(diff(x[,1])>300,na.rm=T) >0){
          id <- x[,1] < 0
          x <- rbind(x[id,],c(NA,NA),x[!id,])
       }
       x
    })
    # reconstruct the object
    polygons <- do.call(rbind,polygons)
    Obj[[1]] <- polygons[,1]
    Obj[[2]] <- polygons[,2]

    map(Obj,...)
}

*请注意,此功能仅采用正中心值.可以很容易地进行调整,以允许在两个方向上都具有中心值,但是我不再打扰,因为这很简单.

*Note that this function only takes positive center values. It's easily adapted to allow for center values in both directions, but I didn't bother anymore as that's trivial.

这篇关于固定地图库数据以太平洋中心(经度0°-360°)显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆