R传单情节船方向 [英] R Leaflet plot ship direction

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

问题描述

我有关于船只/船只的位置以及它们正在驾驶的航线的信息.我想将它们绘制在带有自定义图标的传单地图上,该图标看起来像一艘船.我发现 glyphicons 中的 "tags" 图标最合适.以下是一些可供使用的数据:

dput(head(x))结构(列表(船名 = c(康提本格拉",阳光湾",阳光地平线","FMT URLA", "齐翔 22", "STI 团结"), lat = c(37.115365,38.4772017, 14.632, 56.80515, 51.31172, -2.2783283), lon = c(15.2682183,-8.7888783, -79.5806667, 7.601885, -143.5678933, 46.6328383),cog = c("16", "331", "182", "21", "288", "72")), row.names = c(NA,6L), 类 = "data.frame")

cog 表示地面上的路线,图标转换为旋转角度.我正在使用以下代码来绘制当前船舶的位置和根据船舶转向的方向的旋转:

shipIcon <- makeAwesomeIcon("tag",iconRotate = x$cog)传单() %>% addTiles() %>%addAwesomeMarkers(lng=x$lon,lat=x$lat,icon=shipIcon,popup = x$boatName)

但是,如您所见,makeAwesomeIcon 为我不想要的图标添加了背景标记.我看过

使用 boat.icon,图标被旋转但更难阅读:

最终,我决定使用亮橙色

代码

# 加载必要的包库(htmltools)库(htmlwidgets)图书馆(传单)# 这取自:https://gist.github.com/jcheng5/c084a59717f18e947a17955007dc5f92# Leaflet.rotatedMarker"取自 https://raw.githubusercontent.com/bbecquet/Leaflet.RotatedMarker/master/leaflet.rotatedMarker.js旋转标记 <-htmlDependency( name = "Leaflet.rotatedMarker" # 注意:这个 .js 文件必须被复制并存储在你当前的工作目录中,版本=0.1.2", src = normalizePath( 路径 = getwd() ), 脚本 = "leaflet.rotatedMarker.js" )# 这取自:https://gist.github.com/jcheng5/c084a59717f18e947a17955007dc5f92registerPlugin <- 功能(地图,插件){map$dependencies <- c(map$dependencies, list(plugin))地图}# 存储数据df <-结构(列表(船名 = c(康提本格拉",阳光湾",阳光地平线","FMT URLA", "齐翔 22", "STI 团结"), lat = c(37.115365,38.4772017, 14.632, 56.80515, 51.31172, -2.2783283), lon = c(15.2682183,-8.7888783, -79.5806667, 7.601885, -143.5678933, 46.6328383),cog = c("16", "331", "182", "21", "288", "72")), row.names = c(NA,6L), 类 = "data.frame")# 商店船pngboat.file <- "http://cdn.onlinewebfonts.com/svg/download_498189.png"# 存储指北针pngnorth.arrow.file <-https://upload.wikimedia.org/wikipedia/commons/a/aa/Aiga_uparrow.svg"# 制作船图标船.icon <-makeIcon(iconUrl =boat.file, 图标宽度 = 25, 图标高度 = 25 )# 制作指北针图标北箭头图标 <-makeIcon(iconUrl = north.arrow.file, 图标宽度 = 10, 图标高度 = 10)# 显示传单地图传单(数据 = df)%>%addProviderTiles(provider = "OpenStreetMap.BlackAndWhite") %>%registerPlugin( 插件 = 旋转标记) %>%addCircleMarkers(lng = ~lon, 纬度 = ~lat, 半径 = 5, 填充颜色 = "橙色", 填充不透明度 = 1, 行程 = FALSE) %>%addMarkers(lng = ~lon, 纬度 = ~lat, 标签 = ~船名, 图标 = north.arrow.icon, 选项 = markerOptions(rotationAngle = ~cog) )#脚本结束#

会话信息

R 版本 3.4.4 (2018-03-15)平台:x86_64-apple-darwin15.6.0(64位)运行于:macOS High Sierra 10.13.2矩阵产品:默认BLAS:/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylibLAPACK:/Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib语言环境:[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8附加的基础包:[1] stats graphics grDevices utils datasets 方法[7] 基地其他附加包:[1] 传单_1.1.0.9000 htmlwidgets_1.0 htmltools_0.3.6通过命名空间加载(未附加):[1] shiny_1.0.5 编译器_3.4.4 magrittr_1.5[4] R6_2.2.2 markdown_0.8 tools_3.4.4[7] yaml_2.1.18 Rcpp_0.12.16 crosstalk_1.0.0[10] jsonlite_1.5 摘要_0.6.15 xtable_1.8-2[13] httpuv_1.3.6.2 mime_0.5

I have information on the location of vessels/ships and the course they are steering. I'd like to plot them on a leaflet map with a custom icon which looks like a ship. I found the "tags" icon from glyphicons to be most suitable. Here's some data to play with:

dput(head(x))
structure(list(boatName = c("Conti Benguela", "Sunny Bay", "Sunny Horizon", 
"FMT URLA", "Qi Xiang 22", "STI Solidarity"), lat = c(37.115365, 
38.4772017, 14.632, 56.80515, 51.31172, -2.2783283), lon = c(15.2682183, 
-8.7888783, -79.5806667, 7.601885, -143.5678933, 46.6328383), 
    cog = c("16", "331", "182", "21", "288", "72")), row.names = c(NA, 
6L), class = "data.frame")

cog indicates the course on ground which for the icon translates to the angle of rotation. I am using the following code for currently plotting the location of the vessel and the rotation as per the course the vessel is steering:

shipIcon <- makeAwesomeIcon("tag",iconRotate = x$cog)
leaflet() %>% addTiles() %>% 
  addAwesomeMarkers(lng=x$lon,lat=x$lat,icon=shipIcon,popup = x$boatName)

However as you can see, makeAwesomeIcon adds a background marker to the icon that I don't want. I've had a look at this as well as this with the latter being exactly what I want to do. How can I achieve my task of showing a custom ship icon with rotation as per the course it is steering without a marker background?

解决方案

Overview

To plot icon directions, I followed three steps:

  1. To render HTML icons onto the leaflet map, I swapped leaflet::makeAwesomeIcon() for leaflet::makeIcon().

  2. To enable icon rotation, I stored a local copy of a leaflet.rotatedMarker.js file and registered this plugin as a leaflet object.

  3. Finally, to specify how many degrees the icon should be rotated, I placed the cog variable inside of the rotationAngle argument within the markerOptions() from leaflet::addMarkers().

Note: Steps 2 and 3 were taken from both the answer and a comment on the SO question icon rotation in leaflet package. All credit goes to both @rrs and @Ufos.

Before doing anything, I ran your code and got the following leaflet map:

Using the boat.icon, the icons were rotated but much harder to read:

Eventually, I decided to use bright orange leaflet::addCircleMarkers() and north arrow icons to show both the location and the angle of rotation:

Code

# load necessary packages
library( htmltools )
library( htmlwidgets )
library( leaflet )

# this is taken from: https://gist.github.com/jcheng5/c084a59717f18e947a17955007dc5f92
# "Leaflet.rotatedMarker" is taken from https://raw.githubusercontent.com/bbecquet/Leaflet.RotatedMarker/master/leaflet.rotatedMarker.js
rotatedMarker <- 
  htmlDependency( name = "Leaflet.rotatedMarker" # note: this .js file has to be copied and stored in your current working directory 
                  , version = "0.1.2"
                  , src = normalizePath( path = getwd() )
                  , script = "leaflet.rotatedMarker.js" )

# this is taken from: https://gist.github.com/jcheng5/c084a59717f18e947a17955007dc5f92
registerPlugin <- function(map, plugin) {
  map$dependencies <- c(map$dependencies, list(plugin))
  map
}

# store data
df <-
  structure(list(boatName = c("Conti Benguela", "Sunny Bay", "Sunny Horizon", 
                              "FMT URLA", "Qi Xiang 22", "STI Solidarity"), lat = c(37.115365, 
                                                                                    38.4772017, 14.632, 56.80515, 51.31172, -2.2783283), lon = c(15.2682183, 
                                                                                                                                                 -8.7888783, -79.5806667, 7.601885, -143.5678933, 46.6328383), 
                 cog = c("16", "331", "182", "21", "288", "72")), row.names = c(NA, 
                                                                                6L), class = "data.frame")

# store boat png
boat.file <- "http://cdn.onlinewebfonts.com/svg/download_498189.png"

# store north arrow png
north.arrow.file <- "https://upload.wikimedia.org/wikipedia/commons/a/aa/Aiga_uparrow.svg"

# make boat icon
boat.icon <- 
  makeIcon( iconUrl = boat.file
            , iconWidth = 25
            , iconHeight = 25 )

# make north arrow icon
north.arrow.icon <-
  makeIcon( iconUrl = north.arrow.file
            , iconWidth = 10
            , iconHeight = 10 )

# display leaflet map
leaflet( data = df ) %>%
  addProviderTiles( provider = "OpenStreetMap.BlackAndWhite" ) %>%
  registerPlugin( plugin = rotatedMarker ) %>%
  addCircleMarkers( lng = ~lon
                    , lat = ~lat
                    , radius = 5
                    , fillColor = "orange"
                    , fillOpacity = 1
                    , stroke = FALSE ) %>%
  addMarkers( lng = ~lon
              , lat = ~lat
              , label = ~boatName
              , icon = north.arrow.icon
              , options = markerOptions( rotationAngle = ~cog ) )

# end of script #

Session Info

R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] leaflet_1.1.0.9000 htmlwidgets_1.0    htmltools_0.3.6   

loaded via a namespace (and not attached):
 [1] shiny_1.0.5     compiler_3.4.4  magrittr_1.5   
 [4] R6_2.2.2        markdown_0.8    tools_3.4.4    
 [7] yaml_2.1.18     Rcpp_0.12.16    crosstalk_1.0.0
[10] jsonlite_1.5    digest_0.6.15   xtable_1.8-2   
[13] httpuv_1.3.6.2  mime_0.5 

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

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