【问题标题】:How to rotate symbols (representing spatial point data) based on feature with tmap and sf?如何使用 tmap 和 sf 基于特征旋转符号(表示空间点数据)?
【发布时间】:2019-06-06 07:38:48
【问题描述】:

我想以交互方式(基于传单的方法)在地图上表示 2 艘船以及它们的一些元数据。自然,船舶的元数据通常包含船舶的航向,而最容易理解的表示方式是相应地旋转点数据的符号。

经过一些初步试验后,我设法按照icon rotation in leaflet package 重新创建它,使用纯粹的传单和一个插件,让我可以用rotationAngle=~T_heading 这样的流畅语法编写。这种方式的结果如下所示。

但是,我正在寻找一种将 sf 和 tmap 包用作语法的方法(tmap 的交互式视图也是基于传单的),因为它们是用于管理和可视化地理空间信息的更完整和更复杂的框架。

让我们再次重新创建两个容器的样本数据并绘制它们。

aship<-c(23.622333,37.937489,'Santa Maria',8,300)
bship<-c(23.621303,37.937430,'Vancouver CC',10,35)
shipdata<-data.frame(rbind(aship,bship))
colnames(shipdata)<-c('long','lat','VesselName','sog','T_heading')
shipdata$long<-as.numeric(as.character(shipdata$long))
shipdata$lat<-as.numeric(as.character(shipdata$lat))
shipdata$sog<-as.numeric(as.character(shipdata$sog))
shipdata$T_heading<-as.numeric(as.character(shipdata$T_heading))
#Simple features transformation 
ship_sf<-shipdata%>%
st_as_sf(coords=c('long','lat'))%>%
st_set_crs(4326)
# Use tmap and plot the vessels using a vessel_icon of my own
map_s1<-tm_basemap(leaflet::providers$OpenStreetMap)+
tm_shape(ship_sf)+
tm_markers(shape = vessel_icon)+
tm_view(set.view=c(lon=23.622333,lat=37.937489,zoom=17))

到目前为止,我还没有找到一种以 tmap 方式旋转标记的方法。有什么我错过的简单解决方案吗?

非常感谢

【问题讨论】:

    标签: r leaflet geospatial sf tmap


    【解决方案1】:

    这是基于leaflet 的解决方案。可能不是非常令人满意,但在我看来,它比其他解决方案(thisthis)更不老套。正如你所说,tmap 使用了leaflet,所以一个简单的解决方案是使用addAwesomeMarkers 并稍微调整一下图标。像您一样使用tmap 构建您的地图,并使用tmap_leaflet() 制作一个传单小部件。请注意,这是一个类似的案例:R Leaflet plot ship direction

    您的数据和tmap 对象:

    library(tmap)
    library(sf)
    #> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
    library(leaflet)
    
    tmap_mode("view")
    #> tmap mode set to interactive viewing
    
    # Your data
    shipdata <- data.frame(long = c(23.622333, 23.62130),
                           lat = c(37.93749, 37.93743),
                           VesselName = c("Santa Maria", "Vancouver CC"),
                           sog = c(8, 10),
                           T_heading = c(300, 35))
    ship_sf <- st_as_sf(x = shipdata, 
                        coords = c('long', 'lat'), 
                        crs = "+proj=longlat +datum=WGS84")
    
    # Make use of tmap functionality to produce the map as you posted
    map_s1 <-
      tm_basemap(leaflet::providers$OpenStreetMap) +
      tm_shape(ship_sf) +
      tm_symbols(size = 0) + # need to add a layer after tm_shape(), so add "empty" points
      tm_view(set.view = c(lon = 23.622333, lat = 37.937489, zoom = 17))
    

    现在,制作一些图标;另请参阅https://ionicons.com 只有向上箭头图标适用于给定的旋转角度;其他箭头已经有自己的旋转可能会搞砸。然后将tmap 对象通过管道传输到tmap_leaflet() 以创建传单小部件。最后,添加自定义图标:

    shipIcon <- makeAwesomeIcon(icon = "arrow-up",
                                iconRotate = shipdata$T_heading,
                                squareMarker = TRUE,
                                markerColor = "black")
    
    map_s1 %>% 
      tmap_leaflet() %>% 
      addAwesomeMarkers(lng = shipdata$lon, 
                        lat = shipdata$lat, 
                        icon = shipIcon, 
                        popup = shipdata$VesselName)
    

    额外的想法

    另一个想法是使用iconList() 函数(例如提到here),定义一系列对应于几个旋转类的旋转图像/图标。在这个方向上的另一种方法是here

    reprex package (v0.2.1) 于 2019-01-21 创建

    【讨论】:

      猜你喜欢
      • 2020-01-11
      • 2020-09-02
      • 2021-01-12
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 2020-11-06
      相关资源
      最近更新 更多