Skip to content
Prev 16986 / 29559 Next

problems drawing a world map on a levelplot

I happened to write this yesterday, which switches the parts of
wrld_simpl from < 0 longitude to the east (Pacific view). I haven't
investigated what you need in detail, so just ignore if I'm off track.

It's not careful about data or anything, since all I needed was the
raw geometry. It might be of use. "xrange" here can be anything from
[-180, 360] though it's really assuming you don't traverse more than
360 in total.

It would be nice to carefully clean up a copy of wrld_simpl like this
and make a wrld_simpl version like the maps package has.

Cheers, Mike.


xrange <- c(140, 250)
##xrange <- c(0, 360)
yrange <- c(-60, 20)

require(maptools)
require(raster)
require(rgeos)



    ext <- extent(xrange[1], xrange[2], yrange[1], yrange[2])
    data(wrld_simpl, package = "maptools")
    if (xrange[2] > 180.0) {

        eastrange <- c(xrange[1], 180.0)
        westrange <- c(-180.0, xrange[2] - 360)

        ew <- extent(westrange[1], westrange[2], yrange[1], yrange[2])
        ee <- extent(eastrange[1], eastrange[2], yrange[1], yrange[2])

        geome <- as(ee, "SpatialPolygons")
        geomw <- as(ew, "SpatialPolygons")

        ## why does this get dropped?
        proj4string(geome) <- CRS(proj4string(wrld_simpl))
        proj4string(geomw) <- CRS(proj4string(wrld_simpl))

        worlde <- gIntersection(wrld_simpl, geome)
        worldw <- gIntersection(wrld_simpl, geomw)
        worldw <- elide(worldw, shift = c(360.0, 0.0))

        proj4string(worldw) <- CRS(proj4string(wrld_simpl))

        dfe <- data.frame(x = seq_len(length(worlde at polygons)))
        dfw <- data.frame(x = seq_len(length(worldw at polygons)))
        rownames(dfw) <- as.character(as.numeric(rownames(dfe)) + nrow(dfe))

        worlde <- SpatialPolygonsDataFrame(worlde, dfe, match.ID = FALSE)
        worldw <- SpatialPolygonsDataFrame(worldw, dfw, match.ID = FALSE)
        worldw <- spChFIDs(worldw, rownames(dfw))

        world <- spRbind(worlde, worldw)

    } else {

        geom <- as(ext, "SpatialPolygons")
        proj4string(geom) <- CRS(proj4string(wrld_simpl))
        ## TODO: make this spdf if needed
        world <- gIntersection(wrld_simpl, geom)
    }


plot(world)
On Tue, Dec 18, 2012 at 8:15 PM, Tom Roche <Tom_Roche at pobox.com> wrote: