你好,这是我关于stackoverflow的第一个问题,我希望我做的每件事都是正确的。我想数一下地铁站的乘客人数。我有四个向量,包括乘客和车站的经度和纬度。我用半径为20米的圆圈表示车站,用记号笔表示乘客。现在我想知道圆圈里有多少标记。我已经尝试使用markerClusterOption对标记求和,但是圆外部的标记被添加到圆内的标记中,我只想对圆内的标记求和。我的目标是将圆圈内的标记数加起来。
library(leaflet)
#lat and long of the Subway stations
SubwayStation_long<-c(174.764164,174.764290)
SubwayStation_lat<-c(-36.877022,-36.877844)
#lat and long of the Passengers
Passagier_long<-c(174.764,
174.764436,
174.764336,
174.764044,
174.764034,
174.763,
174.7641,
174.7645,
174.764290,
174.764068,
174.764352,
174.764467)
Passagier_lat<-c(-36.877,
-36.8770099,
-36.8770199,
-36.8770189,
-36.8770189,
-36.876,
-36.8779,
-36.8778,
-36.877844,
-36.877102,
-36.877814,
-36.877900)
tiles = getAllLeafletTiles()
tiles = tiles[c(1,3,27)]
map <- leaflet()
for (provider in tiles) {
map <- map %>% addProviderTiles(provider, group = provider)
}
map <- addLayersControl(
map,
baseGroups = tiles,c("Station","Passengers"),
options = layersControlOptions(collapsed = FALSE))
map <- addMeasure(map, primaryLengthUnit = "kilometers", primaryAreaUnit = "sqmeters", activeColor = "#3D535D", completedColor = "#006400")
map<-addCircles(map,SubwayStation_long,SubwayStation_lat, group = "Station",radius = 15)
map<-addMarkers(map,Passagier_long,Passagier_lat,group = "Passengers",
clusterOptions = markerClusterOptions(freezeAtZoom = FALSE))
map发布于 2019-03-28 03:23:07
在给定两点的经度和纬度的情况下,R有一个用于计算距离的包"geosphere“。这个问题是计算每个车站20米内有哪些乘客。
library(geosphere)
#create dataframe of data, first column longitude, second latitude
subway<-data.frame(SubwayStation_long, SubwayStation_lat)
passagier<-data.frame(Passagier_long, Passagier_lat)
#proceed row by row and caluclate the distance between the station and each passenger
#return TRUE if the distance is within 20 meters
#first column of output goes with first row of station data.
within20m<-apply(subway, 1, function(x){distGeo(x, passagier)<20})
#number within 20 meters of each station
colSums(within20m)apply语句的输出示例。
[,1] [,2]
[1,] TRUE FALSE
[2,] FALSE FALSE
[3,] TRUE FALSE
[4,] TRUE FALSE
[5,] TRUE FALSE
[6,] FALSE FALSE
[7,] FALSE TRUE
[8,] FALSE TRUE
[9,] FALSE TRUE
[10,] TRUE FALSE
[11,] FALSE TRUE
[12,] FALSE TRUE这应该为合理数量的车站和乘客提供可接受的性能。如果您有大量车站和大量乘客,则此解决方案将遇到性能问题,并且需要其他工具和技术。
https://stackoverflow.com/questions/55381381
复制相似问题