6

大家好,

我正在尝试获取大约 700 个点的高程数据。我想我可能会使用提供给同一问题的代码(在 R 中将纬度/经度转换为高度),不幸的是,我在使用 geonames 包时遇到错误,并且提供的最佳答案网站没有可用的澳大利亚高程数据(错误仅供参考)。

我找到了另一个为澳大利亚提供非常准确的海拔数据的网站,但我不知道如何从网页中提取信息。我认为它正在使用谷歌海拔 API,但我也不知道如何访问它。

当我将“纬度,经度”坐标放入“搜索位置”框中时,它会给出地图下方的高程数据。但是,我似乎无法在源页面中找到它。该网站是http://www.daftlogic.com/sandbox-google-maps-find-altitude.htm

一些有效的经纬度值示例:

-36.0736, 146.9442

-36.0491、146.4622

我想知道是否有人可以帮助我从 R 查询该站点并提取高程数据?还是看起来很麻烦?我意识到网站上有一个批处理功能(最多 100 个位置),但是能够从 R 中执行此操作会很酷。

谢谢大家,对不起,如果这非常明显。

干杯,亚当

错误

使用地名时:

elevation <- GNgtopo30(adult$lat, adult$lon)
Error in getJson("gtopo30JSON", list(lat = lat, lng = lng)) : 
  error code 10 from server: Please add a username to each call in order for geonames to    be able to identify the calling application and count the credits usage.
In addition: Warning message:
In readLines(u) :
  incomplete final line found on 'http://ws.geonames.org/gtopo30JSON?  lat=-36.0736&lng=146.9442'

使用查询代码时:

library(RCurl)
library(XML)
url <- paste("http://earthtools.org/height", adult$lat, adult$lon, sep = '/')
page <- getURL(url)
ans <- xmlTreeParse(page, useInternalNodes = TRUE)
Space required after the Public Identifier
SystemLiteral " or ' expected
SYSTEM or PUBLIC, the URI is missing
Extra content at the end of the document
Error: 1: Space required after the Public Identifier
2: SystemLiteral " or ' expected
3: SYSTEM or PUBLIC, the URI is missing
4: Extra content at the end of the document
4

3 回答 3

10

Google 提供了一个Elevation API,它返回 JSON 或 XML 响应。这是一个使用 JSON 响应的示例,fromJSONRJSONIO包中解析。

googEl <- function(locs)  {
  require(RJSONIO)
  locstring <- paste(do.call(paste, list(locs[, 2], locs[, 1], sep=',')),
                     collapse='|')
  u <- sprintf('http://maps.googleapis.com/maps/api/elevation/json?locations=%s&sensor=false',
               locstring)
  res <- fromJSON(u)
  out <- t(sapply(res[[1]], function(x) {
    c(x[['location']]['lat'], x[['location']]['lng'], 
      x['elevation'], x['resolution']) 
  }))    
  rownames(out) <- rownames(locs)
  return(out)
}

m <- matrix(c(146.9442, 146.4622, -36.0736, -36.0491), nc=2)

googEl(m)

      lat     lng      elevation resolution
[1,] -36.0736 146.9442 163       152.7032  
[2,] -36.0491 146.4622 171.7301  152.7032  

googEl函数需要一个matrixdata.frame坐标,第一列是经度,第二列是纬度。

于 2014-02-06T04:54:47.780 回答
7

包装中有?getDataSRTM 海拔。raster

例如:

library(raster)
m <- data.frame(lon = c(146.9442, 146.4622), lat = c(-36.0736, -36.0491))

x <- getData('alt', country = "AUS")

cbind(m, alt = extract(x, m))
       lon      lat alt
1 146.9442 -36.0736 164
2 146.4622 -36.0491 172

在单元格中使用插值而不是最近邻:

cbind(m, alt = extract(x, m, method = "bilinear"))
       lon      lat      alt
1 146.9442 -36.0736 164.9519
2 146.4622 -36.0491 172.1293

下载步骤只是找到之前保存在工作目录中的文件,因此只需执行一次。

数据对象是您可以使用等RasterLayer进行可视化的对象:plot

plot(x)
points(m)
x
class       : RasterLayer 
dimensions  : 5496, 5568, 30601728  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : 112.8, 159.2, -54.9, -9.1  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 
data source : C:\temp\AUS_msk_alt.grd 
names       : AUS_msk_alt 
values      : -43, 2143  (min, max)

在此处输入图像描述

于 2014-02-06T07:20:46.703 回答
2

我编写了googleway包以使@jbaums 的答案更易于实施。对于高程数据,您可以使用该google_elevation()功能

library(googleway)

## you need an API key
key <- "your_api_key"

## data:
df <- data.frame(lat = c(-36.0736, -36.0491),
                 lon = c(146.9442, 146.4622))

google_elevation(df_locations = df, 
                 location_type = "individual",
                 key = key)

# $results
# elevation location.lat location.lng resolution
# 1  163.0000     -36.0736     146.9442   152.7032
# 2  171.7301     -36.0491     146.4622   152.7032

而且,只是为了好玩,您也可以在地图上绘制位置

key <- "your_api_key"

google_map(data = df, key = key) %>%
    add_markers()

在此处输入图像描述

于 2016-10-16T21:22:08.643 回答