传单添加多条折线 [英] leaflet add multiple polylines

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

问题描述

我见过类似的问题,但到目前为止,没有一个问题符合我的需求(至少在我理解的范围内),因此,如果已经回答了这个问题,我将提前道歉.我也是R新手.

我有一个数据帧,每行包含两组纬度/经度.实际数据包含数百行和许多列的相关数据.我正在绘制两组纬度/经度的点,并希望绘制将每一对连接为一条单独的线的线.这是一个结果看起来像的例子.

[![在此处输入图片描述] [1]] [1]

这是数据的简化示例.将有重复的事件"和位置"值.

Event_lat   Event_lon   Event   Location    Location_latitude   Location_longitude
40.791151   -124.054008 704832643   60005   40.790961   -124.1825609
38.900882   -122.660353 704653051   60009   38.873889   -122.709722
38.921488   -122.600049 704681147   60011   38.85111099 -122.593333
38.921488   -122.600049 704681147   60011   38.85111099 -122.593333
39.141877   -123.044724 706777142   60012   39.22794396 -123.064722
38.928113   -122.611386 708644013   60016   38.98950003 -122.7695828
39.02361    -122.72195  708582623   60016   38.98950003 -122.7695828
38.87586    -122.842684 708336092   60016   38.98950003 -122.7695828
39.239926   -123.145497 709020144   60017   39.24138798 -123.2163878
39.3307 -123.221674 708875205   60017   39.24138798 -123.2163878

以下是用于映射点的代码的简化示例:

library(leaflet)
myData <-read.csv("Book1.csv",header=TRUE, sep=",")
leaflet()%>%
  addTiles() %>%
  addCircles(myData,lng = myData$lsr_lon,lat = myData$lsr_lat, radius=20, color = "red",group = "events") %>% 
  addCircles(myData,lng = myData$site_longitude,lat = myData$site_latitude, radius=20, color = "blue",group = 'Locations')

我提出了一种使用library(sf)library(data.table)的解决方案,其中sf替代了sp,而data.table用于有效地重塑数据.

我正在使用Wimpel在其解决方案中提供的数据.

重塑

创建sf对象非常简单.我们需要为数据的每一行创建一个LINESTRING(作为一个sfg对象),然后转换为sf

library(sf)
library(data.table)

setDT(df)   

## create an 'id' / index value. This assumes each row of your data is a separate line. 
df[, idx := .I]

## create an `sfc` column (where each row is an `sfg` object)
sf <- df[
    , {
        geometry <- sf::st_linestring(x = matrix(c(Event_lon, Event_lat, Location_longitude, Location_latitude), ncol = 2, byrow = T))
        geometry <- sf::st_sfc(geometry)
        geometry <- sf::st_sf(geometry = geometry)
    }
    , by = idx
]

## convert to sf
sf <- sf::st_as_sf(sf)

绘图

有了这个sf对象,您现在可以在传单中绘图(使用与Wimpel类似的代码)

library(leaflet)

leaflet() %>%
    addTiles() %>%
    addPolylines(data = sf) %>%
    addCircles(data = df, lng = ~Event_lon, lat = ~Event_lat, radius=20, color = "red", group = "events") %>% 
    addCircles(data = df, lng = ~Location_longitude, lat = ~Location_latitude, radius=20, color = "blue", group = 'Locations') 

I have seen similar questions but so far none that fit my needs (at least to the extent I understand them) so I will apologize in advance if this has already been answered. I am also kind of an R novice.

I have a data frame containing two sets of Lat/Lon in each row. The actual data contains hundreds of rows and many columns of related data. I am plotting the points for both sets of Lat/Lon and want to draw lines connecting each pair as a separate line. Here is an example of what the results should look like.

[![enter image description here][1]][1]

Here is a simplified example of the data. There will be duplicate Event and Location values.

Event_lat   Event_lon   Event   Location    Location_latitude   Location_longitude
40.791151   -124.054008 704832643   60005   40.790961   -124.1825609
38.900882   -122.660353 704653051   60009   38.873889   -122.709722
38.921488   -122.600049 704681147   60011   38.85111099 -122.593333
38.921488   -122.600049 704681147   60011   38.85111099 -122.593333
39.141877   -123.044724 706777142   60012   39.22794396 -123.064722
38.928113   -122.611386 708644013   60016   38.98950003 -122.7695828
39.02361    -122.72195  708582623   60016   38.98950003 -122.7695828
38.87586    -122.842684 708336092   60016   38.98950003 -122.7695828
39.239926   -123.145497 709020144   60017   39.24138798 -123.2163878
39.3307 -123.221674 708875205   60017   39.24138798 -123.2163878

Here is a simplified sample of the code to map the points:

library(leaflet)
myData <-read.csv("Book1.csv",header=TRUE, sep=",")
leaflet()%>%
  addTiles() %>%
  addCircles(myData,lng = myData$lsr_lon,lat = myData$lsr_lat, radius=20, color = "red",group = "events") %>% 
  addCircles(myData,lng = myData$site_longitude,lat = myData$site_latitude, radius=20, color = "blue",group = 'Locations')

解决方案

I propose a solution using library(sf) and library(data.table), where sf has superseeded sp, and data.table is used for efficient reshaping of data.

I am using the data provided by Wimpel in their solution.

Reshaping

Creating an sf object is fairly straight forward. We need to create a LINESTRING for each row of your data (as an sfg object), then convert to sf

library(sf)
library(data.table)

setDT(df)   

## create an 'id' / index value. This assumes each row of your data is a separate line. 
df[, idx := .I]

## create an `sfc` column (where each row is an `sfg` object)
sf <- df[
    , {
        geometry <- sf::st_linestring(x = matrix(c(Event_lon, Event_lat, Location_longitude, Location_latitude), ncol = 2, byrow = T))
        geometry <- sf::st_sfc(geometry)
        geometry <- sf::st_sf(geometry = geometry)
    }
    , by = idx
]

## convert to sf
sf <- sf::st_as_sf(sf)

Plotting

With this sf object you can now plot in leaflet (using similar code to Wimpel)

library(leaflet)

leaflet() %>%
    addTiles() %>%
    addPolylines(data = sf) %>%
    addCircles(data = df, lng = ~Event_lon, lat = ~Event_lat, radius=20, color = "red", group = "events") %>% 
    addCircles(data = df, lng = ~Location_longitude, lat = ~Location_latitude, radius=20, color = "blue", group = 'Locations') 

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

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