簡體   English   中英

修復太平洋中心(0°-360°經度)顯示的地圖庫數據

[英]Fixing maps library data for Pacific centred (0°-360° longitude) display

我正在使用R maps包在世界地圖上繪制一些點,例如:

世界地圖,-180°至180°經度

繪制基本地圖的命令是:

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

但我需要顯示太平洋中心地圖。 我使用map("world2",等來使用來自maps包的太平洋中心底圖,並將我的數據框( df )中的數據點的坐標轉換為:

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

如果我不使用fill選項,但是fill 0°的多邊形會導致問題,這是有效的。

世界地圖,0°至360°經度

我想我需要從轉換多邊形數據maps庫以某種方式排序了這一點,但我不知道怎么弄這個。

我理想的解決方案是繪制一個左邊界為-20°,右邊界為-30°(即330°)的地圖。 以下內容將正確的點和海岸線放到地圖上,但是交叉零問題是相同的

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()函數中再次使用。 我將創建一個包含單個多邊形的列表,檢查哪些具有非常不同的經度值,然后重新排列這些多邊形。 我在下面的函數*中給出了這種方法的一個例子,它允許類似於:

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

要得到

修正了地圖中心180

如果我是你,我會更多地改變一切,比如:

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

修正了地圖中心200

功能 :

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,...)
}

*請注意,此功能僅采用正中心值。 它很容易適應兩個方向的中心值,但我不再煩惱,因為這是微不足道的。

安裝最新版本的地圖(3.2.0)。

做這個:

d$lon2 <- ifelse(d$lon < -25, d$lon + 360, d$lon) # where d is your df
mapWorld <- map_data('world', wrap=c(-25,335), ylim=c(-55,75))

ggplot() +
geom_polygon(data = mapWorld, aes(x=long, y = lat, group = group)) +
geom_point(data = d, aes(x = lon2, y = lat))

有點晚了,但你也可以使用投影創建一個移位的地圖(需要mapproj包):

  map("world", projection="rectangular", parameter=0, 
      orientation=c(90,0,180), wrap=TRUE, fill=T, resolution=0,col=0)

這將移動180度。 但與'world2'的不同之處在於經度坐標會有所不同([-pi,pi])。 該套餐的所有預測均以0為中心。 在這種情況下,'wrap'選項可以正確檢測跳轉。

'resolution = 0'有助於獲得更清晰的邊界。

您可以通過更改投影描述中的“180”值輕松更改中心經度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM