【问题标题】:How can i count the passengers at the subway station?如何计算地铁站的乘客数量?
【发布时间】:2019-03-27 15:49:45
【问题描述】:

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

here is an example

i want it like this

【问题讨论】:

  • 您在底部的链接已损坏,似乎适用于您的本地桌面。有没有其他方法可以共享图像,也许使用 SO 的 imgur 集成?
  • 我添加了一个截图希望它可以工作。

标签: r r-leaflet


【解决方案1】:

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

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

【讨论】:

  • 感谢您的快速答复!这是个好主意,但我有大量的车站和乘客。
  • 如果是这样的话,那么我建议使用分治算法的方法来将问题减少为一些可管理的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多