2017-10-18 78 views
0

我试图计算伦敦内每个LSOA区域内的点数。我曾试图使用超过功能虽然输出不会产生每LSOA上市数量的计数每个LSOA(多边形)内的计数点

我迄今所进行的代码如下

ldnLSOA <- readOGR(".", "LSOA_2011_London_gen_MHW") 
LondonListings <- read.csv('Londonlistings.csv') 
proj4string(LdnLSOA) <- proj4string(LondonListings) 
plot(ldnLSOA) 
plot(LondonListings, add =T) 

enter image description here

LSOAcounts <- over(LondonListings, ldnLSOA) 

这将生成一个表格,其中不包含比原始ldnLSOA shapefile更多的数据。

我想知道,如果有人知道我将如何能够得到一个表格式:

LSOAname | LSOAcode | Count 

或那种框架。

示例数据:

LondonListings: 
longitude | latituide 
-0.204406 51.52060 
-0.034617 51.45037 
-0.221920 51.46449 
-0.126562 51.47158 
-0.188879 51.57068 
-0.096917 51.49281 

Shape文件:

https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london

+1

请提供一个*重现*例子来处理。 http://stackoverflow.com/help/mcve –

+0

我不确定如何添加信息以使shapefile可以复制,但我添加了LondonListings的示例数据 –

+0

您需要使用内置或公共可用的数据文件 –

回答

1

我删除了我inespecific的答案,又写了一个与您的数据(除了点...但它并不难取代这些数据吧?) 让我知道它是否工作!

#I'm not sure which of this libs are used, since I always have all of them loaded here 
library(rgeos) 
library(rgdal) 
library(sp) 


#Load the shapefile 
ldnLSOA <- readOGR(".", "LSOA_2011_London_gen_MHW") 
plot(ldnLSOA) 

#It's always good to take a look in the data associated to your map 
ldn_data<-as.data.frame([email protected]) 

#Create some random point in this shapefile 
ldn_points<-spsample(ldnLSOA,n=1000, type="random") 

plot(ldnLSOA) 
plot(ldn_points, pch=21, cex=0.5, col="red", add=TRUE) 

#create an empty df with as many rows as polygons in the shapefile 
df<-as.data.frame(matrix(ncol=3, nrow=length([email protected]$LSOA11NM))) 
colnames(df)<- c("LSOA_name","LSOA_code", "pt_Count") 
df$LSOAname<-ldn_data$LSOA11NM 
df$LSOAcode<-ldn_data$LSOA11CD 

# Over = at the spatial locations of object x, 
# retrieves the indexes or attributes from spatial object y 
pt.poly <- over(ldn_points,ldnLSOA) 

# Now let's count 
pt.count<-as.data.frame(table(pt.poly$LSOA11CD)) 

#As it came in alphabetical order, let's put in the same order of data in data frame 
pt.count_ord<-as.data.frame(pt.count[match(df$LSOA_name,pt.count$Var1),]) 

#Fill 3rd col with counts 
df[,3]<-pt.count_ord$Freq