R传单情节船方向
R Leaflet plot ship direction
我有关于 vessels/ships 的位置和他们正在转向的路线的信息。我想用看起来像船的自定义图标将它们绘制在传单地图上。我发现 glyphicons
中的 "tags"
图标最合适。这里有一些数据可供使用:
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
表示地面上的路线,图标转换为旋转角度。我正在使用以下代码绘制船只的位置和根据船只转向的路线进行的旋转:
shipIcon <- makeAwesomeIcon("tag",iconRotate = x$cog)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(lng=x$lon,lat=x$lat,icon=shipIcon,popup = x$boatName)
但是如您所见,makeAwesomeIcon
向图标添加了我不想要的背景标记。我看过 this as well as this ,后者正是我想要做的。我怎样才能完成显示自定义船舶图标的任务,根据它在没有标记背景的情况下转向的路线旋转?
概述
为了绘制图标方向,我遵循了三个步骤:
为了将 HTML 图标渲染到传单地图上,我交换了 leaflet::makeAwesomeIcon()
for leaflet::makeIcon()
。
为了启用图标旋转,我存储了 a leaflet.rotatedMarker.js file and registered this plugin as a leaflet object 的本地副本。
最后,为了指定图标应旋转多少度,我将 cog
变量放在 markerOptions()
中的 rotationAngle
参数内 leaflet::addMarkers()
.
注意:第 2 步和第 3 步取自对 SO 问题 icon rotation in leaflet package 的答案和评论。所有功劳归功于@rrs 和@Ufos。
在做任何事情之前,我运行你的代码并得到了以下传单地图:
使用 boat.icon
,图标被旋转但更难阅读:
最终,我决定使用明亮的 o运行ge leaflet::addCircleMarkers()
和指北针图标来显示位置和旋转角度:
代码
# 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 信息
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
我有关于 vessels/ships 的位置和他们正在转向的路线的信息。我想用看起来像船的自定义图标将它们绘制在传单地图上。我发现 glyphicons
中的 "tags"
图标最合适。这里有一些数据可供使用:
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
表示地面上的路线,图标转换为旋转角度。我正在使用以下代码绘制船只的位置和根据船只转向的路线进行的旋转:
shipIcon <- makeAwesomeIcon("tag",iconRotate = x$cog)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(lng=x$lon,lat=x$lat,icon=shipIcon,popup = x$boatName)
但是如您所见,makeAwesomeIcon
向图标添加了我不想要的背景标记。我看过 this as well as this ,后者正是我想要做的。我怎样才能完成显示自定义船舶图标的任务,根据它在没有标记背景的情况下转向的路线旋转?
概述
为了绘制图标方向,我遵循了三个步骤:
为了将 HTML 图标渲染到传单地图上,我交换了
leaflet::makeAwesomeIcon()
forleaflet::makeIcon()
。为了启用图标旋转,我存储了 a leaflet.rotatedMarker.js file and registered this plugin as a leaflet object 的本地副本。
最后,为了指定图标应旋转多少度,我将
cog
变量放在markerOptions()
中的rotationAngle
参数内leaflet::addMarkers()
.
注意:第 2 步和第 3 步取自对 SO 问题 icon rotation in leaflet package 的答案和评论。所有功劳归功于@rrs 和@Ufos。
在做任何事情之前,我运行你的代码并得到了以下传单地图:
使用 boat.icon
,图标被旋转但更难阅读:
最终,我决定使用明亮的 o运行ge leaflet::addCircleMarkers()
和指北针图标来显示位置和旋转角度:
代码
# 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 信息
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