2014-07-25 25 views
1

我想计算之间和R.使用“area.poly”两个shape文件重叠我使用下面的代码,我的问题是以下几点:Area.poly in R:为什么Shapefile转换为类“list”而不是“gpc.poly”?

我尝试了shapefile2转换成gpc.poly,结果是shapefile进入一个类“列表”。因此,我使用area.poly不起作用。

任何人都知道为什么shapefile1现在想转换成gpc.poly类?

shape文件包含以下扩展名:DBF,PRJ,QPJ,SHP,SHX

我想使用的代码:

- >库(rgeos)

- > P1 < - 如(shapefile1, “gpc.poly”) - > P2 < - 如(shapefile2, “gpc.poly”)

- > area.poly(相交(P1,P2)) 收到错误在这里!

- > STR(shapefile1) 结果:类(名单)

回答

0

你为什么要使用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 

相关问题