1

我一直在疯狂尝试使用 R 将本地图像(如在我计算机中的图像文件中)添加到我的传单地图中。我已经绘制了大约 500 个坐标来分析一些图像,我希望在单击(弹出)时显示该特定图像。

leaflet(pics) %>%
  addTiles() %>%
  addCircleMarkers(
    fillOpacity = 0.8, radius = 5,
    lng = ~GPSLongitude, lat =~GPSLatitude, 
    color = ~pal(Married),
    popup = ~SourceFile, # WISH TO ADD EMBEDDED LOCAL IMAGE IN HERE
    label = mapply(function(x, y) {
      HTML(sprintf("<em>%s</em></br> %s", htmlEscape(x), htmlEscape(y)))},
      pics$Address, pics$DateTimeOriginal, SIMPLIFY = F),
    labelOptions = lapply(1:nrow(pics), function(x) {
      labelOptions(direction='auto')
    }))

我附上 2 个屏幕截图:一个悬停鼠标,另一个单击特定位置。理想情况下,我希望在单击每个图像时显示图像和图像文件名。那可能吗?

徘徊 克里克

我还可以通过示例向您展示 RPub:http ://rpubs.com/laresbernardo/photomap

希望您能够帮助我。谢谢!

_________________________ 更新 _________________________

本示例使用的所有代码。基本上我扫描所有带有地理标签的图像,将地址添加到标签上,然后绘制所有坐标。当我点击一个坐标时,我希望看到那张图片。

wd <- "/Users/bernardo/Dropbox (Personal)/Documentos/R/R Mapping/GPS Photos"

# ------------------------------------------- get the pics with geotags

library(exifr)
library(dplyr)
library(lubridate)
library(beepr)
library(maps)

time <- Sys.time(); print(time)
setwd("/Users/bernardo/Dropbox (Personal)/Imágenes")
files <- list.files(pattern = "*.jpg|*.JPG|*.png|*.PNG", recursive=T)
exif <- read_exif(files, tags = c("SourceFile", "DateTimeOriginal", "GPSLongitude", "GPSLatitude"))
pics <- exif %>% filter(!is.na(GPSLongitude)) %>%
  mutate(DateTimeOriginal = ymd_hms(DateTimeOriginal))
pics$Owner <- ifelse(grepl("iPhone Maru", pics$SourceFile), "Maru", "Ber")
pics$Married <- ifelse(as.Date(pics$DateTimeOriginal) >= '2016-04-30', TRUE, FALSE)
pics$Country <- maps::map.where(database="world", pics$GPSLongitude, pics$GPSLatitude)

#lares::freqs(pics %>% filter(!is.na(Country)), Country)

# Save pics with geotags
setwd(wd)
write.csv(pics, "with_geotags.csv", row.names = F)
print(Sys.time() - time)
beepr::beep()


# ------------------------------------------- get the addresses from files

# GET ALL ADDRESSES
library(ggmap)
options(warn=-1)
setwd(wd)

pics <- read.csv("with_geotags.csv")
addresses <- read.csv("with_address.csv")

pics_to_search <- pics %>% filter(!SourceFile %in% addresses$SourceFile)
print(paste0("Without address: ",round(100 * nrow(pics_to_search)/nrow(pics), 2),"% | ", nrow(pics_to_search)))

out <- data.frame()
for (i in 1:nrow(pics_to_search)) {
  Address <- revgeocode(cbind(pics_to_search$GPSLongitude, pics_to_search$GPSLatitude)[i,], output="address")[1]
  if (!is.na(Address)) {
    out <- rbind(out, cbind(SourceFile=as.character(pics_to_search$SourceFile[i]), Address))
    print(paste(i, Address, sep=" - ")) 
  }
}
# Save pics with geotags
pics_with_address <- rbind(out, addresses)
write.csv(pics_with_address, "with_address.csv", row.names = F)


# ------------------------------------------- Map all coordinates with leaflet

setwd(wd)

library(leaflet)
library(htmltools)
library(mapview)

pics <- read.csv("with_geotags.csv")
address <- read.csv("with_address.csv")

pal <- colorFactor(c("green4", "navy"), domain = c(FALSE, TRUE))
pics <- left_join(pics, address, by=c("SourceFile"))
pics$Content <- paste("Dirección:","<em>", pics$Address,"</em>", "<br/> Fecha:", as.Date(pics$DateTimeOriginal))

leaflet(pics) %>%
  addTiles() %>%
  addCircleMarkers(
    fillOpacity = 0.8, radius = 5,
    lng = ~GPSLongitude, lat =~GPSLatitude, 
    color = ~pal(Married),
    popup = popupImage(as.character(pics$SourceFile), src = "local"),
    label = mapply(function(x, y) {
      HTML(sprintf("<em>%s</em></br> %s", htmlEscape(x), htmlEscape(y)))},
      pics$Address, pics$DateTimeOriginal, SIMPLIFY = F),
    labelOptions = lapply(1:nrow(pics), function(x) {
      labelOptions(direction='auto')
    }))

但... 图像错误

我什至安装了最新版本devtools::install_github("r-spatial/mapview@develop")

4

2 回答 2

1

没有可重现的例子,这很难,但以这个为例:

library(leaflet)
library(mapview)

# make-up dataset
data_df <- data.frame(lat = as.numeric(c("35.68705", "35.88705")), long = as.numeric(c("51.38", "53.35")))

# Loaded random pictures on my laptop
images <- c("/PathToImage1/download.jpeg",
             "/PathToImage2/download1.jpeg")

leaflet(data_df) %>%
  addTiles() %>%
  addCircleMarkers(
    fillOpacity = 0.8, radius = 5,
    lng = ~long, lat =~lat, 
    popup = popupImage(images)
    )

单击每个点以查看不同的图像。确保以与数据框相同的顺序加载图像。

于 2018-05-22T19:39:47.903 回答
0

最后,在这个问题上浪费了很多时间之后,我设法解决了这个问题。感谢@MLavoie 和@TimSalabim3(通过 Twitter)的支持。

就是这样:如果你运行的是 macOS,你应该已经安装了一个名为gdal. 我真的只是安装了它,运行了原始脚本并且它工作了。不知道那是gdal做什么的,但它确实起到了作用!

于 2018-05-23T16:12:13.883 回答