[R-sig-Geo] recentering a map to arbitrary center while keeping polygons / nowrapRecenter()

Peter Ellis (Wellington) Peter.Ellis at med.govt.nz
Fri Jul 6 05:26:19 CEST 2012


Hi

I'm trying to recenter a world map without breaking the polygons.  If x1 is a SpatialPolygons or SpatialPolygonsDataFrame object that I have (for example) created from a shapefile then:

	plot(nowrapRecenter(x1))

does exactly what I want, with one exception that by making the boundaries 0 and 360 degrees longitude we end up with the UK on the far right of the map and the rest of Europe on the far left (the "world2" map in the maps package has the same issue).  So what I really want is something that can frame the map left to right with an arbitrary set of horizontal coordinates - say -20 to 340.  

>From reading the help file I would have thought that

	plot(nowrapRecenter(x1, offset=-20))

would do the job, but this does two things that surprise me:  
1) The objects on the map don't shift to the right, relative to when offset=0
2) more scratched polygons appear - apparently from polygons that go across the longitude=0

Reproducible example:

library(maps)
library(maptools)
library(rgeos)
library(rgdal)
library(gpclib)

world <- map("world", fill=TRUE, col="transparent", plot=FALSE)
worldSpP <- map2SpatialPolygons(world, world$names, CRS("+proj=longlat"))

# remove incomplete polygons that go over the offset
worldSpP <- worldSpP[-grep("Greenland", row.names(worldSpP)),]
worldSpP <- worldSpP[-grep("Iceland", row.names(worldSpP)),]
worldSpP <- worldSpP[-grep("Antarctica", row.names(worldSpP)),]
worldSpPnr <- nowrapRecenter(worldSpP, offset=-20)
plot(worldSpPnr, col=colors())

My very basic guess of what might be going wrong is that nowrapRecenter has basically done what it intends to, but the plot method is still trying to draw the map from 0 to 360.  However, fixing this goes beyond my troubleshooting powers.


Alternatively, there's two different answers on StackOverflow that will redraw a world map with an arbitrary center very neatly (one using ggplot2 and one using sp), but in both cases the country boundary information is basically lost:
http://stackoverflow.com/questions/10620862/use-different-center-than-the-prime-meridian-in-plotting-a-world-map

So my question reduces to how do I use nowrapRecenter() to center a map arbitrarily; or is there another way to move a world map like that, while keeping country information.

Thanks!

PE



newzealand.govt.nz - connecting you to New Zealand central & local government services

Any opinions expressed in this message are not necessarily those of the Ministry of Business, Innovation and Employment. This message and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivery to the intended recipient, be advised that you have received this message in error and that any use is strictly prohibited. Please contact the sender and delete the message and any attachment from your computer.



More information about the R-sig-Geo mailing list