2017-03-01 33 views
0

我之前询问过this question,但没有得到回复,所以我会尽力做好这一次的工作!在R中创建缓冲区和计数点

我想用R分析加油站点的空间密度。我需要在加油站周围创建一个缓冲区(比方说1000米)并计算缓冲区内的加油站数量。然后,我需要充分利用缓冲距离来查看什么是合理的缓冲区,以查看有趣的内容。我不会发布整体形状文件,因为它是相当凌乱,但这是该数据的样子:

all <- readShapePoints("sbc_gas.shp") 
all.df <- as(all, "data.frame") 
head(all) 

OBJECTID Fuellocati    Name Latitude  Longitude  
     1  34828  WORLD OIL #104 34.44190  -119.8304  
     2  48734 STOP AND SHOP GAS 34.41962  -119.6768  
     3  51276 EL RANCHERO MARKET 34.41911  -119.7162  
     4  52882 EDUCATED CAR WASH 34.44017  -119.7439  
     5  74038   CIRCLE K 34.63925  -120.4406  
     6  103685 7-ELEVEN #23855 34.40506  -119.5296  

我能够创建点周围的缓冲用下面的代码,但现在我该怎么办计算缓冲区内的点数?

require(sp) 
require(rdgal) 
require(geosphere) 

coordinates(all) <- c("Longitude", "Latitude") 
pc <- spTransform(all, CRS("+init=epsg:3347")) 
distInMeters <- 1000 
pc100km <- gBuffer(pc, width=100*distInMeters, byid=TRUE) 
# Add data, and write to shapefile 
pc100km <- SpatialPolygonsDataFrame(pc100km, [email protected]) 
writeOGR(pc100km, "pc100km", "pc100km", driver="ESRI Shapefile") 

plot(pc100km) 

enter image description here

我打开其他的方式去了解这一点。

+1

你能给更多信息你是否收到任何错误输出?你能进入调试模式并检查变量持有的是什么吗? –

+0

添加错误消息以提问。 – JAG2024

+1

对于初学者来说,它看起来像'distm'没有被定义。你的意思是把'distInMeters'放在那里吗? –

回答

1

想出了我自己的问题一个(简单的)解决方案使用geosphere:“不能让任何工作”

cbind(coordinates(all.df), X=rowSums(distm (coordinates(all.df)[,1:2], fun = distHaversine)/1000 <= 10)) # number of points within distance 10 km 
    Longitude Latitude X 
0 -119.8304 34.44190 25 
1 -119.6768 34.41962 29 
2 -119.7162 34.41911 34 
3 -119.7439 34.44017 39 
4 -120.4406 34.63925 13 
5 -119.5296 34.40506 7 
6 -120.4198 34.93860 26 
7 -119.8221 34.43598 30