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

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


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

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

Map of the world, -180° to 180° longitude

The command to draw the base map is:

繪制基本地圖的命令是:

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

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:

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

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

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

如果我不使用填充選項,這可以工作,但填充0°交叉的多邊形會導致問題。

Map of the world, 0° to 360° longitude

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.

我想我需要以某種方式從地圖庫中轉換多邊形數據來對其進行排序,但我不知道如何解決這個問題。

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

我理想的解決方案是繪制一個左邊界為-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))

Any help would be greatly appreciated.

任何幫助將不勝感激。

3 个解决方案

#1


17  

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 :

您可以使用內部事實,map()函數返回的地圖對象可以重新計算並在map()函數中再次使用。我將創建一個包含單個多邊形的列表,檢查哪些具有非常不同的經度值,並重新排列這些多邊形。我在下面的函數*中給出了這種方法的一個例子,它允許類似於:

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

to get

要得到

Corrected map center 180

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))

Corrected map center 200

The function :

功能 :

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.

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

#2


2  

A bit late, but you can also create a shifted map by using a projection (requires the mapproj package):

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

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

This will shift by 180 degrees. But the difference with 'world2' is that the longitude co-ordinate will be different ([-pi,pi]). All projections of this package put 0 at the centre. And in that case, the 'wrap' option detects the jump correctly.

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

'resolution=0' helps to get cleaner borders.

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

You can easily change the centre longitude by changing the '180' value in the projection description.

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

#3


1  

install the latest version of maps (3.2.0).

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

do this:

做這個:

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))

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/03/18/720f6e3f076b7a9385d0740ac7f77e4d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com