如何在R中使用诺基亚HERE API

时间:2015-09-08 23:45:32

标签: r here-api shapefile

我正在尝试从诺基亚HERE Enterprise API下载反向流等值线(https://developer.here.com/rest-apis/documentation/enterprise-routing/topics/resource-reverse-flow.html)的坐标。

我可以运行查询,并获得一个长的纬度+经度列表,但是当我尝试创建和绘制SpatialPolygon时,我看到点的顺序似乎是随机的。图表如下所示:http://i.stack.imgur.com/rVxql.png

my.coord.pairs <- points.from.nokia.here.api

  pol <- spChFIDs(block[1,], paste("newid", 3324234, sep = ""))

  pol@polygons[[1]]@Polygons[[1]]@coords <- matrix(my.coord.pairs, nrow = length(my.coord.pairs)/2, ncol = 2, byrow = TRUE)

  ggplot(pol, aes(long, lat, group = group)) + 
        theme(panel.background = element_blank()) + 
         geom_polygon(colour = "black", size = 1) + coord_equal() + 
        labs(x = "Easting", y = "Northing", fill = "Population") + 
         scale_fill_manual("Test", values = c(alpha("Red", 0.4), "white"), labels = c("a", "b"))

P.S。:“block”是我从美国人口普查shapefile获得的Polygon。

P.S.2:我知道这里存在geocodeHERE R包,但它仍然非常不成熟,不提供等值线和逆流计算。

1 个答案:

答案 0 :(得分:2)

谢谢Scott Chamberlain,我同意你的意见,我的问题没有得到很好的解决。

答案是我对逆流资源的理解是错误的。它返回像Calculate Isoline资源一样的多边形。此外,它现在是API的遗留资源。

检索我想要的正确方法 - 表示来自的所有地点的多边形,您可以开始驾驶并在给定的限制内到达给定的目的地点,无论是行程时间还是行程距离 - 使用带有目的地参数的Calculate Isoline资源而不是 start 参数。

计算等值线资源的这个双重功能(目标/开始)在API文档中有点不被注意,但它可以正常工作:https://developer.here.com/rest-apis/documentation/routing/topics/resource-calculate-isoline.html#resource-calculate-isoline

下面是我的代码片段,它向Calculate Isoline资源发出请求,然后解压缩返回的数据以获取多边形(即形成多边形的点列表)。请注意,该代码需要您在创建HERE API帐户时获得的APP ID和APP代码。

# These variables below are just to compose the example code.
latitude <- 34.9859619
longitude <- -78.5900116
range <- 1800 # 30 minutes, in seconds
resolution <- 5

# Now comes the real code
response_parsed <- RJSONIO::fromJSON(RCurl::getURL(paste0("http://isoline.route.cit.api.here.com/routing/7.2/calculateisoline.json",
"?app_id=", App_id, "&app_code=", App_code, "&range=", range, "&rangetype=time", "&destination=geo!", latitude, ",",
longitude, "&mode=fastest;car;traffic:enabled")))

if(length(response_parsed) == 0)
  stop('Error in JSON request.')

polygon <- data.frame(long=numeric, lat=numeric)

# The points (latitude+longitude) come together as characters separated by a comma. We need to split them.
for(rsh in response_parsed$response$isoline[[1]]$component[[1]]$shape) {
  split <- strsplit(rsh, ",")[[1]]
  polygon <- rbind(polygon, data.frame(long=as.numeric(split[[2]]), lat=as.numeric(split[[1]])))
}
# Now the polygon data frame contains all points that form the polygon returned by Calculate Isoline.