你为什么要使用intersect(...)
和area.poly(...)
?改为使用gIntersection(...)
和gArea(...)
。
请注意,大部分这只是设置演示。显然,你已经有了两个形状文件,所以你可以在最后开始。
library(raster) # for getData(...); just for this example
library(rgdal) # for spTransform
library(rgeos) # for gIntersection(...), gArea(...), etc.
# map of the UK - just an example
UK <- getData("GADM",country="GBR",level=0)
OSGB.36 <- "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs"
UK <- spTransform(UK,CRS(OSGB.36)) # need projection with meaningful units
# circle with 300km radius from centroid of UK
ctr <- coordinates(gCentroid(UK))
th <- 2*pi*seq(0,.99,by=0.01)
pts <- data.frame(long=ctr[1]+3e5*cos(th),lat=ctr[2]+3e5*sin(th))
pts <- rbind(pts,pts[1,])
circle <- SpatialPolygons(list(Polygons(list(Polygon(pts)),1)),
proj4string=CRS(proj4string(UK)))
## you start here...
plot(UK)
plot(circle,add=T)
sp <- gIntersection(UK,circle) # calculate intersection
plot(sp,add=T, col="red")
gArea(sp)/1e6 # calculate area of intersection in sq km
# [1] 149638.8