数据问题:addAwesomeMarkers需要lng,lat,但我的数据存储在向量中。解决?

问题描述 投票:1回答:1

我正在使用R中的Leaflet在地图上工作。我的数据存储为x,y坐标,现在转换为lng,lat值。

数据集:Dambrug2019(我自己的数据集,只是为了避免在下一个代码示例中混淆)。

           Name Status      x       y
1       Point_1      0 482670 6217698

转变:

Locations <- st_as_sf(Dambrug2019, coords=c("x", "y")) 
%>% st_set_crs(23032) %>% st_transform(4326)

Name Status                  geometry
1         Point_1     1 POINT (8.720061 56.10223)

制作具有相同标记的地图时,一切都很顺利。

Map <- leaflet() %>% addTiles() %>% addMarkers(data=Locations, popup=Locations$Name)

然后我想根据状态为标记着色。

#Color the markers depending on the status.
#Want 0=green, 1=red, 2=orange.

> Color_status <- function(Locations) {sapply(Locations$Status,
   function(Status) {if(Status==0){"green"} else if(Status==1)
     {"red"} else{"orange"} })}

> Status_Icons <- awesomeIcons(icon='circle', iconColor=
   'black', library='ion', markerColor=Color_status(Locations))

> leaflet(Locations) %>% addTiles() %>%
    addAwesomeMarkers(c(Locations$geometry), icon=icons, 
    label=~as.character(Name))

Error in validateCoords(lng, lat, funcName) : 
  addAwesomeMarkers requires numeric longitude values

所以,似乎addAwesomeMarkers无法处理我的点设置方式。有没有简单/快速的方法将点转换为功能可以使用的东西?

或者您是否知道另一种创建不同颜色标记的方法可以使用我当前的数据?

解决方案:

as.numeric(Locations$geometry)
Error: (list) object cannot be coerced to type 'double'

打印结果(Locations $ geometry):

> print(Locations$geometry)
Geometry set for 53 features 
geometry type:  POINT
dimension:      XY
bbox:           xmin: 8.269083 ymin: 54.89636 xmax: 15.13583 ymax: 56.99576
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
First 5 geometries:
POINT (8.720061 56.10223)
POINT (9.611723 56.67828)
POINT (8.633127 55.69979)
POINT (9.671523 56.99576)
POINT (15.13583 55.05837)

非常感谢您的帮助。 :)

r leaflet geocoding r-leaflet
1个回答
0
投票

我对leafletsf包不太熟悉,但尝试一下:

library(st)
library(leaflet)

lng = as.numeric(st_coordinates(Locations$geometry)[,1])
lat = as.numeric(st_coordinates(Locations$geometry)[,2])

leaflet(Locations) %>% addTiles() %>%
  addAwesomeMarkers(lng = lng, lat = lat, icon=icons, 
  label=~as.character(Name))
© www.soinside.com 2019 - 2024. All rights reserved.