2017-10-18 82 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