将地理位置(坐标或城市)映射到世界银行气候 API 流域 ID

Mapping geo-location (coordinates or city) to WorldBank climate API river basin id

我对城市级别的历史平均气温感兴趣。 WorldBank climate API provides such historical data on the country or river basin level. Country level is too course for me. River basin level would be very good, but I haven't found a way how to convert geo-coordinate to river basins ids to map cities to the river basins. Shape files of the basins are provided by waterbase.org。欢迎任何将地理坐标映射到世界银行流域 ID 的帮助。

尽管缺少原始代码,但您可以按照以下方法获得完整的 SpatialPolygonsDataFrame,每个多边形都有一个 basin_id

他们 API 的一部分允许您指定盆地 ID,并且 returns 该区域的 KML 文件。所以你可以查询所有这些并将它们粘在一起:

library(httr)
library(sp)
library(rgdal)
library(pbapply)

basin_list <- pblapply(1:468, function(basin_id) {

  res <- GET(sprintf("http://climatedataapi.worldbank.org/climateweb/rest/v1/basin/kml/%d", basin_id))
  fil <- tempfile(fileext=".xml")
  writeLines(content(res, as="text"), fil)
  kml <- readOGR(fil, "Layer #0", verbose=FALSE, stringsAsFactors=FALSE)
  unlink(fil)
  kml$basin_id = basin_id
  # these are useless
  kml$Name <- kml$Description <-NULL
  kml

})

basin_list <- c(basin_list, makeUniqueIDs=TRUE)
basins <- do.call(rbind, basin_list)
basins@data

##    basin_id
## 0         1
## 01        2
## 02        3
## 03        4
## 04        5
## 05        6
## 06        7
## 07        8
## 08        9
## 09       10

显然我并没有为所有 468 分钟坐很多分钟,只有 10 分钟。

(Actually, I let it run while doing other work and saved you the trouble of having to generate the files. You can get them at this github repo)

看到它正在获取所有盆地:

plot(basins) 

巨型数据框存在拓扑错误问题,因此如果您确实需要确定特定 lat/lon 对所在的多边形(以便您可以将其映射到盆地 ID),那么您必须稍微努力一下:

library(rgeos)

# pick a random point in basin 3
spot <- spsample(basin_list[[3]], 1, "random")

# now try to find it in reverse
which(sapply(1:468, function(i) gContains(basin_list[[i]], spot)))

这些不是复杂的多边形,所以速度非常快。

其他人可能有时间稍微简化一下(即更正 rbinded 多边形拓扑错误)。

此外,请考虑善待世界银行 API 服务器并同时执行以下操作:

save(basin_list, basins, file="basins.rda")

writeOGR(basins, ".", "basins", driver="ESRI Shapefile")
# or 
geojsonio::geojson_write(basins, geometry="polygon", group="basin_id", "basins.json")

因此您在本地拥有数据集,不需要重新创建它们。