使用openrouteservice...
并在他们的wiki page上修改建议的url示例...
如果在浏览器中运行以下url ...
http://openrouteservice.org/index.php?start=7.0892567,50.7265543&end=7.0986258,50.7323634&lat=50.72905&lon=7.09574&zoom=15&pref=Fastest&lang=en
我得到了一条相关的路线...
但我想消除对浏览器的依赖,这样我就能够使用R以编程方式下载XML、路由配置文件和GPX (在左侧可用),通过提供任何给定的经度/经度组合并在URL中添加适当的参数。
我的想法是将httr包与GET或POST命令一起使用,因为我不想使用RSelenium。
使用Google Chrome的inspect元素似乎不能产生清晰的URL...so,我不太确定如何开始这样做。
发布于 2014-12-05 21:08:11
确定它(Chrome)支持…查找http://openrouteservice.org/php/OpenLSRS_DetermineRoute.php
library(httr)
params <- list(Start="7.0892567,50.7265543",
End="7.0986258,50.7323634",
Via="",
lang="en",
distunit="KM",
routepref="Fastest",
avoidAreas="",
useTMC="",
noMotorways="false",
noTollways="false",
instructions="true",
`_`="")
resp <- POST("http://openrouteservice.org/php/OpenLSRS_DetermineRoute.php",
body=params, encode="form")
content(resp)
## <xls:XLS xmlns:xls="http://www.opengis.net/xls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" version="1.1" xsi:schemaLocation="http://www.opengis.net/xls http://schemas.opengis.net/ols/1.1.0/RouteService.xsd">
## <xls:ResponseHeader xsi:type="xls:ResponseHeaderType"/>
## <xls:Response xsi:type="xls:ResponseType" requestID="123456789" version="1.1" numberOfResponses="1">
## <xls:DetermineRouteResponse xsi:type="xls:DetermineRouteResponseType">
## <xls:RouteSummary>
## <xls:TotalTime>PT2M39S</xls:TotalTime>
## <xls:TotalDistance uom="KM" value="2.2"/>
## <xls:BoundingBox srsName="EPSG:4326">
## <gml:pos>7.0892519 50.7254372</gml:pos>
## <gml:pos>7.1039336 50.7323788</gml:pos>
## </xls:BoundingBox>
## </xls:RouteSummary>
## <xls:RouteGeometry>
## <gml:LineString srsName="EPSG:4326">
## <gml:pos>7.0892567 50.7265543</gml:pos>
## <gml:pos>7.089251870496228 50.72654506565571</gml:pos>
## ....发布于 2018-04-24 20:59:31
在openrouteservice包的帮助下,现在可以很容易地从R连接openrouteservice应用程序接口。不再需要手动构建查询!考虑以下示例,并查看包vignette以获取所提供功能的概述。
library(openrouteservice)
# one-time API key set-up
# ors_api_key("<your-api-key>")
# query for coordinates
locations <- lapply(c("Heidelberg", "Kraków"), ors_geocode)
coordinates <- lapply(locations, function(x) x$features[[1]]$geometry$coordinates)
# find route
route <- ors_directions(coordinates, format="geojson")
# route length in kilometres and duration in hours
unlist(route$features[[1]]$properties$summary) / c(1000, 3600)
## distance duration
## 1051.861300 9.205167
# draw on map using leaflet
library(leaflet)
leaflet() %>%
addTiles() %>%
addGeoJSON(route, fill=FALSE) %>%
fitBBox(route$bbox)

https://stackoverflow.com/questions/27315357
复制相似问题