Crop for SpatialPolygonsDataFrame

后端 未结 4 1047
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 09:21

I have two SpatialPolygonsDataFrame files: dat1, dat2

extent(dat1)
class       : Extent 
xmin        : -180 
xmax        : 180 
ymin        : -9         


        
4条回答
  •  我在风中等你
    2020-12-01 10:00

    Here is an example of how to do this with rgeos using the world map as an example

    This comes from Roger Bivand on R-sig-Geo mailing list. Roger is one of the authors of the sp package.

    Using the world map as an example

    library(maptools)
    
    data(wrld_simpl)
    
    # interested in the arealy bounded by the following rectangle
    # rect(130, 40, 180, 70)
    
    library(rgeos)
    # create  a polygon that defines the boundary
    bnds <- cbind(x=c(130, 130, 180, 180, 130), y=c(40, 70, 70, 40, 40))
    # convert to a spatial polygons object with the same CRS
    SP <- SpatialPolygons(list(Polygons(list(Polygon(bnds)), "1")),
    proj4string=CRS(proj4string(wrld_simpl)))
    # find the intersection with the original SPDF
    gI <- gIntersects(wrld_simpl, SP, byid=TRUE)
    # create the new spatial polygons object.
    out <- vector(mode="list", length=length(which(gI)))
    ii <- 1
    for (i in seq(along=gI)) if (gI[i]) {
      out[[ii]] <- gIntersection(wrld_simpl[i,], SP)
      row.names(out[[ii]]) <- row.names(wrld_simpl)[i]; ii <- ii+1
    }
    # use rbind.SpatialPolygons method to combine into a new object.
    out1 <- do.call("rbind", out)
    # look here is Eastern Russia and a bit of Japan and China.
    plot(out1, col = "khaki", bg = "azure2")
    

    enter image description here

提交回复
热议问题