首页 > 解决方案 > 如何计算地铁站的乘客数量?

问题描述

你好这是我关于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

这是一个例子

我想要这样

标签: rr-leaflet

解决方案


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

这应该为合理数量的车站和乘客提供可接受的性能。如果您有大量车站和大量乘客,那么此解决方案将遇到性能问题,并且需要其他工具和技术。


推荐阅读