【问题标题】:ggmap in R - keep google copyright information on cropped mapR中的ggmap - 在裁剪地图上保留谷歌版权信息
【发布时间】:2021-02-02 03:34:15
【问题描述】:

我想知道是否有人知道如何在 R 中的 ggmap 绘制(使用新比例)的地图上保留谷歌版权信息?

例如:

library(ggmap)
library(ggplot2)

# download basemap
ggmap::register_google(key="xxx") # insert your hey here xxx
ggmap::ggmap_show_api_key()
base_map <- get_googlemap(center = c(lon= -2.325278, lat=54.6000000), zoom = 5, style = 'element:labels|visibility:off',color= "bw")

当我在不改变比例的情况下绘制base_map 时,版权信息会插入到图的底部,如下所示:

ggmap(base_map, extent = "panel")

但是,当我用新的xy axis 绘制base_map 以放大英国大陆时。我还裁剪了版权信息。如下:

ggmap(base_map, extent = "panel")+
  scale_x_continuous(limits = c(-7, 3), expand = c(0, 0)) +
  scale_y_continuous(limits = c(49.5, 59), expand = c(0, 0))

有谁知道我如何才能获得第二个情节,但要获得出版所需的版权信息?

我尝试更改 get_googlemap() 函数中的 zoom= 参数,但它似乎总是切断英国的顶部或底部,我需要能够在绘制数据时看到整个区域.

非常感谢您提前提供的帮助!

【问题讨论】:

    标签: r google-maps ggplot2 ggmap


    【解决方案1】:

    我使用了很多 ggmap,而这种类型的问题我当时无法以一种优雅的方式解决。但是有一种方法可以实现您想要的东西(手动执行)。 geom_rect 制作灯箱,geom_text 编写版权信息。

    ggmap(base_map, extent = "panel")+
      scale_x_continuous(limits = c(-7, 3), expand = c(0, 0)) +
      scale_y_continuous(limits = c(49.5, 59), expand = c(0, 0)) +
      geom_rect(ymin = 49.5,ymax = 49.7, fill = 'white', alpha = 0.1, xmin = -3.35, xmax = 3) +
      geom_text(y = 49.6, x = -0.2, size = 2.5,
                 label = 'Map data ©2020 GeoBasis-DE/BKG (©2009), Google, Inst. Geogr Nacional') 
    ggsave('Map.jpg', width = 7, height = 9, units = 'in')
    

    geom_rectgeom_text的大小和限制必须根据保存文件的大小手动编辑。在这个例子中,我提供了我用于它的ggsave,并以一种很好的方式绘制。

    【讨论】:

    • 谢谢你:)我已经接受了这个答案,因为我可以使用我所追求的地图的精确裁剪。
    【解决方案2】:

    这看似简单,其实是一个相当复杂的问题。我做了一些研究,发现你可以通过调整sizescale 参数来实现这一点。但是,如果您像我一样使用ggmap v3.0.0,您会发现指定非方形尺寸只会给您一个“嘈杂的图像”,如下所示:

    base_map <- ggmap::get_googlemap(center = c(lon= -2.325278, lat=54.6000000), zoom = 5, size = c(331, 367), style = 'element:labels|visibility:off', color= "bw")
    ggmap(base_map, extent = "panel")
    

    这涉及ggmap 包中的已知bug。它尚未得到解决。尽管存在here 中提到的解决方案,但它只能部分解决问题,因为该解决方案在此post 中提到的某些情况下失败了。因此,我建议重写该函数以使其以稳健的方式工作。幸运的是,在研究了源代码之后,我发现这个问题并不难处理。该问题是由get_goolemap 函数中的某些图像处理失败引起的。因此,将该功能中的图像处理外包给专用包是一种简单的解决方法。

    考虑一下这个get_googlemap2(为了简单起见,我忽略了原始get_goolemap 中的所有这些参数检查,因此请注意您的输入)

    require(RgoogleMaps)
    require(httr)
    require(magick)
    require(urltools)
    require(tibble)
    
    get_googlemap2 <- function(
      api_key = "Your API Key", 
      center = c(lon = -95.3632715, lat = 29.7632836), 
      zoom = 10, size = c(640, 640), scale = 2, 
      maptype = c("terrain", "satellite", "roadmap", "hybrid"), 
      grayscale = FALSE, style
    ) {
      maptype <- match.arg(maptype)
      
      params <- c(
        center = paste0(center[c(2L, 1L)], collapse = ","), 
        zoom = zoom, 
        size = paste0(size, collapse = "x"), 
        scale = scale, 
        maptype = maptype, 
        style = style, 
        key = api_key
      )
      url <- "https://maps.googleapis.com/maps/api/staticmap"
      urltools::parameters(url) <- paste(names(params), params, sep = "=", collapse = "&")
      url <- URLencode(url)
      
      message("Souce: ", url)
      img <- magick::image_read(httr::content(httr::GET(url)))
      if (grayscale) img <- magick::image_quantize(img, colorspace = "gray")
      ll <- RgoogleMaps::XY2LatLon(
        list(lat = center[2], lon = center[1], zoom = zoom),
        -size[1]/2 + 0.5, -size[2]/2 - 0.5
      )
      ur <- RgoogleMaps::XY2LatLon(
        list(lat = center[2], lon = center[1], zoom = zoom),
        size[1]/2 + 0.5, size[2]/2 - 0.5
      )
      structure(
        as.raster(img), class = c("ggmap", "raster"),
        source = "google", maptype = maptype, zoom = zoom,
        bb = tibble::tibble(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2])
      )
    }
    

    我用这个新功能尝试了sizescale的一些规格,发现以下规格可以渲染出最好的地图。

    base_map <- get_googlemap2(
      "Your API Key", 
      center = c(lon = -2.325278, lat = 54.6000000), zoom = 5, size = c(330, 380), scale = 2, style = 'element:labels|visibility:off', grayscale = TRUE
    )
    ggmap(base_map, extent = "panel")
    

    输出

    我希望这是你想要的。我将此称为最佳结果,因为如果您尝试进一步缩小宽度,例如缩小到 250,则属性文本将与徽标重叠。

    base_map <- get_googlemap2(
      "Your API Key", 
      center = c(lon = -2.325278, lat = 54.6000000), zoom = 5, size = c(250, 380), scale = 2, style = 'element:labels|visibility:off', grayscale = TRUE
    )
    ggmap(base_map, extent = "panel")
    

    据我所知,这是 Google 的问题,而不是 ggmap 的问题。我没有办法解决它。另一种解决方法是从图像中删除归属文本,但在内容中将其作为纯文本重新引入,如 Google 的 attribution guidlines 中所述。但是,由于 Google 的徽标仍然需要存在,因此您必须弄清楚如何将其粘贴到地图上。 IMO,使用纯文本为您提供了更大的页面布局灵活性,因此可能是更好的选择。

    【讨论】:

    • 感谢您的详细回答 :D 运行get_googlemap2() 函数时似乎出现错误?如下:Error in tibble(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2]) : could not find function "tibble" Called from: structure(as.raster(img), class = c("ggmap", "raster"), source = "google", maptype = maptype, zoom = zoom, bb = tibble(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2]))
    • 抱歉,错过了require(tibble)@QPaps
    • 我接受了上面的答案,因为它允许我使用我正在寻找的地图的精确裁剪。但是,您的回答(我也赞成)非常有用,我相信将来会很有用-再次感谢您:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多