传单添加多条折线

时间:2018-06-14 23:21:54

标签: r leaflet

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

我有一个数据框,每行包含两组Lat / Lon。实际数据包含数百行和多列相关数据。我正在绘制两组Lat / Lon的点,并想绘制将每对连接成一条单独线的线。以下是结果应该是什么样子的示例。

[![在此处输入图像说明] [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')

2 个答案:

答案 0 :(得分:3)

我自己刚开始使用R和传单的地理用途,所以这可能不是解决这个问题的最有效方法。不过它对我来说工作正常... 随时欢迎反馈!

结果

result

样本数据

genvar k;
generate
    for( k = 0; k < 4; k=k+1) begin
      assign led [ 4 + k ] = clocks[k].clock_slow;
    end
endgenerate

我想创建一个spatiallines对象,我可以使用generate for( k = 0; k < 4; k=k+1) begin assign led [ 4 + k ] = clocks[k].clock_slow; end endgenerate在传单中绘制。

首先,我想创建一个只有lat / lon的data.frame,每个事件位置组合都有一个唯一的{path:'',component:EnHomeComponent}, {path:'contact',component:EnContactComponent}, {path:'fr',component:LayoutComponent, children:[ {path:'',component:FrHomeComponent}, {path:'contact',component:FrContactComponent}]}

df <- read.table( text = "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", header = TRUE)

现在创建spatialLines-object

addPolylines

检查。

id

现在,在小册子中绘制点和折线

library(tidyverse)
#craete a column with unique id's per event-location combination
df <- df %>% mutate( id = row_number() )
#create a temporaty df with events
events.df <- df %>% 
  select( id, Event_lat, Event_lon) %>% 
  rename( latitude = Event_lat, longitude = Event_lon)
#create a temporaty df with locations
locations.df <- df %>% 
  select( id, Location_latitude, Location_longitude) %>%
  rename( latitude = Location_latitude, longitude = Location_longitude)
#merge the two temp.df's together
df.sp <- bind_rows( events.df, locations.df )

#    id latitude longitude
# 1   1 40.79115 -124.0540
# 2   2 38.90088 -122.6604
# 3   3 38.92149 -122.6000
# 4   4 38.92149 -122.6000
# 5   5 39.14188 -123.0447
# 6   6 38.92811 -122.6114
# 7   7 39.02361 -122.7220
# 8   8 38.87586 -122.8427
# 9   9 39.23993 -123.1455
# 10 10 39.33070 -123.2217
# 11  1 40.79096 -124.1826
# 12  2 38.87389 -122.7097
# 13  3 38.85111 -122.5933
# 14  4 38.85111 -122.5933
# 15  5 39.22794 -123.0647
# 16  6 38.98950 -122.7696
# 17  7 38.98950 -122.7696
# 18  8 38.98950 -122.7696
# 19  9 39.24139 -123.2164
# 20 10 39.24139 -123.2164

答案 1 :(得分:3)

我建议使用library(sf)library(data.table)的解决方案,其中sf取代spdata.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') 

enter image description here

相关问题