使用传单和R将本地图像发布到弹出窗口中

时间:2018-05-21 17:28:25

标签: html r leaflet r-leaflet

我一直在疯狂地尝试使用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个截图:一个悬停鼠标,另一个点击特定位置。理想情况下,当我点击每个图像时,我希望显示图像和图像文件名。这可能吗?

Hover Clic

我还可以使用示例:http://rpubs.com/laresbernardo/photomap

向您显示RPub 希望你能帮助我。谢谢!

_________________________更新_________________________

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

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')
    }))

但是... Error in image

我甚至安装了devtools::install_github("r-spatial/mapview@develop")

的最新版本

2 个答案:

答案 0 :(得分: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)
    )

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

答案 1 :(得分:0)

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

就是这样:如果你正在运行macOS,你应该安装一个名为gdal的驱动程序。我只是安装它,运行原始脚本,它工作。不知道gdal做了什么,但确实做到了这一点!