使用R从澳大利亚的lat/lon点网站提取高程

 imba-Y_685 发布于 2023-01-29 09:29

G'day所有人,

我试图得到一些高程数据,我有700点.我想我可能会使用提供给同一个问题的代码(转换为纬度/经度到R的高度),不幸的是我在使用geonames包时遇到错误,并且最佳答案提供的网站没有澳大利亚海拔数据(错误)在FYI下面提供).

我发现另一个网站为澳大利亚提供了非常准确的高程数据,但我不知道如何从网页中提取信息.我认为它正在使用谷歌提升API,但我再次不知道如何访问它.

当我将'lat,lon'坐标放入'搜索位置'框时,它会在地图下方提供高程数据.但是,我似乎无法在源页面中找到它.该网站是http://www.daftlogic.com/sandbox-google-maps-find-altitude.htm.

一些示例lon lat值有效:

-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

jbaums.. 9

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多个坐标,第一列为经度,第二列为纬度.

2 个回答
  • 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多个坐标,第一列为经度,第二列为纬度.

    2023-01-29 09:34 回答
  • 包中有?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)
    

    在此输入图像描述

    2023-01-29 09:34 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有