[R-sig-Geo] making wrld_simpl longitude borders something other than (-180, 180)

Michael Sumner mdsumner at gmail.com
Mon Sep 14 08:42:31 CEST 2015


I get reasonable results with this:

library(maptools)
data(wrld_simpl)
library(raster)
library(rgeos)

## this crop needs a buffer to work
w1 <- intersect(gBuffer(wrld_simpl, width = 0.00001), as(extent(-180, 100,
-90, 90), "SpatialPolygons"))
w2 <- intersect(wrld_simpl, as(extent(100,180, -90, 90), "SpatialPolygons"))

shiftw1 <- elide(w1, shift = c(360, 0))
plot(shiftw1, xlim = c(100, 100 + 360), col = "grey", border = NA)
plot(w2, add = TRUE, col = "grey", border = NA)


Sometimes rgeos needs a small buffer - I think it comes down to alignment
of segments when the numeric precision is not sufficient. If you need
borders you need to carefully re-construct the pieces. and how best to do
that depends on your actual data (wrld_simpl is but one data set, not
perfect for all) and your actual map. I just smashed the "DataFrame" part
here, it's possible to keep it all but you might not need that.

Cheers, Mike.


On Mon, 14 Sep 2015 at 16:25 Nick Matzke <matzke at nimbios.org> wrote:

> Although, unfortunately, the exact code, but switching "moll" to "latlong",
> produces screwups (horizontal lines throughout the plot). Any advice?
>
> Works:
> ====================
> pdffn = "wrld_simpl_moll.pdf"
> pdf(file=pdffn, width=12, height=6)
>
> data(wrld_simpl) #The world as a SpatialPolygonsDataFrame
> #To avoid the lines crossing the map after reprojection we need to cut the
> polygons at the new break:
> w <- nowrapRecenter(wrld_simpl, offset = 180-90, avoidGEOS=TRUE)
> #Then proceed with the reprojection (note the proj4 string for a mollweide
> projection with 150°E as the new center)
> wrld_reCentered <- spTransform(w, CRS("+proj=moll +lon_0=-90"))
> plot(wrld_reCentered, border="grey", lwd=0.5)
>
>
> dev.off()
> cmdstr = paste0("open ", pdffn)
> system(cmdstr)
> ====================
>
>
> Produces screwups:
> ====================
> pdffn = "wrld_simpl_latlong.pdf"
> pdf(file=pdffn, width=12, height=6)
>
> data(wrld_simpl) #The world as a SpatialPolygonsDataFrame
> #To avoid the lines crossing the map after reprojection we need to cut the
> polygons at the new break:
> w <- nowrapRecenter(wrld_simpl, offset = 180-90, avoidGEOS=TRUE)
> #Then proceed with the reprojection (note the proj4 string for a mollweide
> projection with 150°E as the new center)
> wrld_reCentered <- spTransform(w, CRS("+proj=longlat +lon_0=-90"))
> plot(wrld_reCentered, border="grey", lwd=0.5)
>
>
> dev.off()
> cmdstr = paste0("open ", pdffn)
> system(cmdstr)
> ====================
>
> On Mon, Sep 14, 2015 at 4:04 PM, Nick Matzke <matzke at nimbios.org> wrote:
>
> > Ah, this did it:
> >
> http://stackoverflow.com/questions/17718928/how-to-switch-to-a-map-that-is-centered-on-the-china-instead-of-the-default-set
> >
> > (plotting to PDF helps speed it up)
> >
> > library(geosphere)
> > library(gdata) # for read.xls
> > library(maptools) # for e.g. wrld_simpl
> > library(rgdal)
> > library(gpclib)   # for polygon clipping
> >
> > pdffn = "wrld_simpl.pdf"
> > pdf(file=pdffn, width=12, height=6)
> >
> > data(wrld_simpl) #The world as a SpatialPolygonsDataFrame
> > #To avoid the lines crossing the map after reprojection we need to cut
> the
> > polygons at the new break:
> > w <- nowrapRecenter(wrld_simpl, offset = 180-100, avoidGEOS=TRUE)
> > #Then proceed with the reprojection (note the proj4 string for a
> mollweide
> > projection with 150°E as the new center)
> > wrld_reCentered <- spTransform(w, CRS("+proj=moll +lon_0=-100"))
> > plot(wrld_reCentered, border="grey", lwd=0.5)
> >
> >
> > dev.off()
> > cmdstr = paste0("open ", pdffn)
> > system(cmdstr)
> >
> >
> >
> >
> > On Mon, Sep 14, 2015 at 3:46 PM, Nick Matzke <matzke at nimbios.org> wrote:
> >
> >> Apologies, I got my wires crossed.
> >>
> >> What I meant was, how would I make a map where the left edge was at
> >> longitude 100 (e.g. Thailand), and the right edge is at, say 99
> >> (again Thailand).  The Pacific Ocean, and the International Dateline,
> would
> >> be in the middle of this map.
> >>
> >> I tried the obvious thing:
> >> plot(wrld_simpl, xlim = c(100, 99))
> >>
> >> ...but that produces a flipped map...
> >>
> >> Cheers!
> >> Nick
> >>
> >>
> >> On Mon, Sep 14, 2015 at 3:38 PM, Frede Aakmann Tøgersen <
> frtog at vestas.com
> >> > wrote:
> >>
> >>> Hi Nick
> >>>
> >>> To be honest I do not understand your question so I am guessing that
> you
> >>> probably only need to set the xlim to c(-100, 100). Here is an example:
> >>>
> >>> > data(wrld_simpl)
> >>> > plot(wrld_simpl)
> >>> > plot(wrld_simpl, xlim = c(-100, 100))
> >>>
> >>> Why would one wants to redefine the longitudes from the interval (-180,
> >>> 180) to (-100, 100)?
> >>>
> >>>
> >>> Yours sincerely / Med venlig hilsen
> >>>
> >>> Frede Aakmann Tøgersen
> >>> Specialist, M.Sc., Ph.D.
> >>> Plant Performance & Modeling
> >>>
> >>> Technology & Service Solutions
> >>> T +45 9730 5135
> >>> M +45 2547 6050
> >>> frtog at vestas.com
> >>> http://www.vestas.com
> >>>
> >>> Company reg. name: Vestas Wind Systems A/S
> >>> This e-mail is subject to our e-mail disclaimer statement.
> >>> Please refer to www.vestas.com/legal/notice
> >>> If you have received this e-mail in error please contact the sender.
> >>>
> >>>
> >>> -----Original Message-----
> >>> From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of
> >>> Nick Matzke
> >>> Sent: 14. september 2015 06:51
> >>> To: r-sig-geo at stat.math.ethz.ch
> >>> Subject: [R-sig-Geo] making wrld_simpl longitude borders something
> other
> >>> than (-180, 180)
> >>>
> >>> Hi all,
> >>>
> >>> I would like to plot wrld_simpl, but have the left and right edges of
> the
> >>> map be at something like 100 degrees longitude, rather than (-180,
> 180).
> >>>
> >>> I tried messing with the bounding box with no luck.
> >>>
> >>> I can imagine something like burrowing into the object and subtracting
> 80
> >>> degrees from all the longitudes, but I suspect there must be a better
> >>> way?
> >>>
> >>> Any help appreciated!
> >>> Nick
> >>>
> >>>         [[alternative HTML version deleted]]
> >>>
> >>> _______________________________________________
> >>> R-sig-Geo mailing list
> >>> R-sig-Geo at r-project.org
> >>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >>>
> >>
> >>
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list