r - 使用 ggmap 创建正交 map

标签 r google-maps maps map-projections ggmap

在以下脚本中,我读入了许多点对并在 map 上绘制了线条。我正在使用 ggmap 从谷歌提取 map 来绘制这条线:

source('./library/latlong2state.R')

library(maps)
library(mapproj)
library(mapdata)
library(geosphere)
library(ggmap)

fileName = "_CanadaData/CanadaHospitalComplete.csv"

getLineColor <- function(val) {
  pal <- colorRampPalette(lineColours)
  colors <- pal(80)
  val.log <- log(val)

  if (val > 50) {
    col <- colors[80]
  } else {
    colindex <- max(1, round( 80 * val / 50))
    col <- colors[colindex]
  }
  return(col)
}

# Load the data
location <- read.csv(fileName, stringsAsFactors=FALSE)

# Omit locations that are not on the map of focus (not needed for city maps unless they are on a border)
location$state <- latlong2state(data.frame(location$lng, location$lat))
location$nearstate <- latlong2state(data.frame(location$lngnear, location$latnear))
location <- na.omit(location)

createMap <- function(bbox, thedata, mapzoom=3, linesize=0.6, pointsize=2) {
  basemap <- get_map(location=bbox, zoom=mapzoom, source='google', maptype="roadmap", color="color")
  ggmap(basemap) + geom_segment(aes(x=lng, xend=lngnear, y=lat, yend=latnear, color=dist_miles), size=0.6, data=thedata) + geom_point(aes(x=lngnear, y=latnear), size=2, color="#000000", border="black", data=thedata) + scale_color_gradient(low="blue", high="red", limits=c(0, max(thedata$dist_miles))) + coord_map("orthographic")
}

# Country bounding box c(left, bottom, right, top)
canada <- c(-140.920514, 42.016722, -52.524864, 83.2911)
createMap(canada, location)

不幸的是,由于加拿大的北纬,这会导致 map 顶部距离的严重扭曲:

Distances to hospitals in Canada

这可以通过切换到正交图轻松解决,我可以通过添加 projection=mapprojection(orthographic) 来更改线的绘制方式的投影。在 createMap 函数内部,但我无法更改从 Google 获取的 map 图像的投影 - 它卡在 Web Mercator 投影中。有没有办法使用 ggmap 来做到这一点,还是我必须尝试不同的包?如果是这样,你有什么建议?

最佳答案

最简洁的答案是不”。你被谷歌所谓的“网络墨卡托”投影困住了。 (它不是 WGS84。那是一个基准,而不是一个投影。)

我不知道是否有任何 R 库,但您可能需要使用 WMS 服务器。一个不错的选择可能是 GeoBase,它是加拿大特有的。 http://www.geobase.ca/geobase/en/wms/index.html

此外,我建议避免使用正投影,而是使用适合加拿大的投影,例如为加拿大 map 集选择的投影:EPSG:42304。

关于r - 使用 ggmap 创建正交 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20936792/

相关文章:

Android 导入com.google 无法解决

javascript - 在 Google map v3 API 中按酒店名称搜索

r - 哪些数字存在于向量中但不存在于另一个向量中

r - 从 ymdhms 变量中删除 ymd

r - R中的双积分实现

python - RPy 中 R 的 pdIndent 函数

javascript - 带有 Dart 的谷歌地图 : retain function is not defined

javascript - 是否可以异步初始化 Google map 而不创建全局回调函数?

javascript - 如何使 Google Maps API (.js) 文件成为移动设备小部件的一部分

javascript - 要访问 Google maps api,我什么时候需要使用以下脚本?