宣传单行 [英] Leaflet separate lines

查看:111
本文介绍了宣传单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用传单绘制线条,但是在分割线段时遇到了一些困难.我有一个看起来像这样的物体

I am trying to plot lines using leaflet, however I am encountering some difficulties in separating the segment. I have an object that looks like this

> head(trips, n=15)
   time.start   time.end long.start long.end lat.start  lat.end  distance time.diff      speed color
1  1476450598 1476450713    9.03913  9.03924  45.61335 45.61362  31.25292       115  0.9783524 green
2  1476450713 1476450727    9.03924  9.03995  45.61362 45.61365  55.38651        14 14.2422459 green
3  1476450727 1476450751    9.03995  9.04005  45.61365 45.61340  28.89870        24  4.3348057 green
4  1476450751 1476450777    9.04005  9.04017  45.61340 45.61406  74.06267        26 10.2548313 green
5  1476450777 1476450873    9.04017  9.03949  45.61406 45.61419  54.89125        96  2.0584219 green
6  1476450873 1476450920    9.03949  9.03496  45.61419 45.61319 369.88687        47 28.3317600 green
7  1476450920 1476450930    9.03496  9.03440  45.61319 45.61295  51.13973        10 18.4103034 green
8  1476450930 1476450932    9.03440  9.03448  45.61295 45.61285  12.75643         2 22.9615714 green
9  1476450932 1476450982    9.03448  9.03495  45.61285 45.61241  61.14351        50  4.4023330 green
10 1476451362 1476451363    9.03553  9.03559  45.61197 45.61188  11.05462         1 39.7966396 green
11 1476451363 1476451373    9.03559  9.03606  45.61188 45.61129  75.18742        10 27.0674701 green
12 1476451373 1476451382    9.03606  9.03712  45.61129 45.61127  82.57276         9 33.0291031 green
13 1476451382 1476451405    9.03712  9.04059  45.61127 45.61095 272.54942        23 42.6599094 green
14 1476451405 1476451412    9.04059  9.04115  45.61095 45.61091  43.83450         7 22.5434586 green
15 1476451412 1476451431    9.04115  9.04440  45.61091 45.61064 254.85994        19 48.2892512 green

此文件以两次旅行为例(还有很多,但这只是一个想法),理想情况下,不应将第9点(第一次旅行的终点)链接到第10点(起点)第二次旅行).我用命令完成了

This file represents two trips as an example (there are many more but it is just to give an idea), ideally the point no.9 (end of 1st trip) shouldn't be linked to point no.10 (start of 2nd trip). I got this done with the command

   ggmap(mapImageData)+
  geom_segment(data=trips, mapping=aes(y=trips$lat.start, x=trips$long.start, 
                                       yend=trips$lat.end, xend=trips$long.end),color=trips$color,size=1)

这段代码中的

mapImageData相当于Leaflet中的图块,然后我用命令geom_segment添加点,指定每个点的初始和最终位置.

in this code mapImageData is the equivalent of the tiles in Leaflet and then I am adding points with the command geom_segment, specifying the initial and final position of each point.

使用传单,我必须使用命令addPolylines(data = mydata, lng = ~long, lat = ~lat, weight=1,color="purple").区别在于此命令需要一列经度和一列纬度,在我的示例中,像这样

With leaflet I have to use the command addPolylines(data = mydata, lng = ~long, lat = ~lat, weight=1,color="purple"). The difference is that this command wants a column of longitude and a column of latitude, in my example something like this

 head(mydata, n=15)
        lat    long
1  45.61335 9.03913
2  45.61362 9.03924
3  45.61365 9.03995
4  45.61340 9.04005
5  45.61406 9.04017
6  45.61419 9.03949
7  45.61319 9.03496
8  45.61295 9.03440
9  45.61285 9.03448
10 45.61197 9.03553
11 45.61188 9.03559
12 45.61129 9.03606
13 45.61127 9.03712
14 45.61095 9.04059
15 45.61091 9.04115

但是,这将连接所有点,如何确定何时不连接点?例如位置编号9和10不应相互链接. 谢谢, 马可

This, however will connect all the points, how can I tell when not to connect the dots? for example position no. 9 and 10 shouldn't linked to each other. Thanks, Marco

推荐答案

首先,我们需要一种区分不同旅程的方法.
我用

First of all we need a way to distinguish the different trips.
I used

df$group <- c(rep(1, 9), rep(2, 6))

根据您所说的内容,但根据需要修改df.

based on what you said, but modify your df as it suits.

此后,我们将为每个组添加一条折线:

After this we are going to add a polyline for each group:

m <- leaflet(df) %>% 
    addTiles() 

for (i in unique(df$group)) {
    m <- m %>% 
        addPolylines(data = df[df$group == i, ], 
                     lng = ~long.start, 
                     lat = ~lat.start)
}

结果:

尽管是for loop,但速度非常快.为了加快速度,我们可以使用lapply:

Despite being a for loop this is quite fast. To speed it up a notch we can use a lapply:

m <- leaflet(df) %>% 
        addTiles()

lapply(unique(df$group), 
       function(x) {
            addPolylines(m, 
                         data = df[df$group == x, ], 
                         lng = ~long.start, 
                         lat = ~ lat.start)
        })

rbenchmark::benchmark(
    apply = {

        m <- leaflet(df) %>% 
            addTiles()

        lapply(unique(df$group), function(x) {
            addPolylines(m, 
                         data = df[df$group == x, ], 
                         lng = ~long.start, 
                         lat = ~ lat.start)
        })
    },
    forcycle = {

        m <- leaflet(df) %>% 
            addTiles() 

        for (i in unique(df$group)) {
            addPolylines(m, 
                         data = df[df$group == i, ], 
                         lng = ~long.start, 
                         lat = ~lat.start)
        }}, 
    replications = 1000)
#>       test replications elapsed relative user.self sys.self user.child
#> 1    apply         1000    2.91    1.000      2.92        0         NA
#> 2 forcycle         1000    3.04    1.045      3.00        0         NA

这篇关于宣传单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆