0

使用 openrouteservice 时...

并在他们的wiki 页面上调整建议的 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(在左侧可用)进入网址。

我的想法是使用带有or命令的httr包,因为我不想使用.GETPOSTRSelenium

使用谷歌浏览器的检查元素似乎不会导致一个明确的 URL ......所以我不太确定如何开始这样做。

4

2 回答 2

1

当然它(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>
## ....
于 2014-12-05T13:08:11.243 回答
1

在openrouteservice包的帮助下, openrouteservice API 现在可以轻松地从 R 接口。不再需要手动构建查询!考虑以下示例,并查看包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)

在此处输入图像描述

于 2018-04-24T12:59:30.690 回答