请求openrouteservice api服务的python客户端

openrouteservice的Python项目详细描述


Build statusCoveralls coverageDocumentation StatusPyPI versionConda installMyBinder

快速启动

说明

openrouteservice库允许您轻松访问openrouteservice(ors)路由api。 它对我们的api执行以下请求

有关详细信息,请访问:

我们还有一个repo,其中包含几个有用的示例here

如需支持,请咨询我们的forum

通过使用这个库,您同意ORSterms and conditions

要求

openrouteservice py是根据cpython 2.7、3.4、3.5、3.6、3.7和3.8-dev以及py py 3.5进行测试的。

要设置测试环境,请安装requirements-dev.txt

pip install -r requirements-dev.txt

安装

要从pypi安装,只需使用pip:

pip install openrouteservice

要从源安装最新的和最好的,请执行以下操作:

pip install git+git://github.com/GIScience/openrouteservice-py@development

对于conda用户:

conda install -c nilsnolde openrouteservice

此命令组将把库安装到全局环境中。也适用于虚拟环境。

测试

如果要运行单元测试,请参见Requirementscd到库目录并运行:

nosetests -v

-v详细输出标志(推荐)。

使用量

对于交互式jupyter笔记本,请查看mybinder.org

基本示例
importopenrouteservicecoords=((8.34234,48.23424),(8.34423,48.26424))client=openrouteservice.Client(key='')# Specify your personal API keyroutes=client.directions(coords)print(routes)

为了方便起见,所有执行请求的模块方法都包装在client类中。这个有 缺点是,IDE不能自动显示 不同的方法。还有很多!

保留IDE的智能功能,稍微详细一点的替代方案是

importopenrouteservicefromopenrouteservice.directionsimportdirectionscoords=((8.34234,48.23424),(8.34423,48.26424))client=openrouteservice.Client(key='')# Specify your personal API keyroutes=directions(client,coords)# Now it shows you all arguments for .directions

优化路线

如果你想在一个简单的Traveling Salesman Problem中优化多个航路点的顺序, 您可以传递一个optimize_waypoints参数:

importopenrouteservicecoords=((8.34234,48.23424),(8.34423,48.26424),(8.34523,48.24424),(8.41423,48.21424))client=openrouteservice.Client(key='')# Specify your personal API keyroutes=client.directions(coords,profile='cycling-regular',optimize_waypoints=True)print(routes)

解码多段线

默认情况下,方向api返回encoded polylines。 要解码为dict,这是一个geojson几何体对象,只需执行

importopenrouteservicefromopenrouteserviceimportconvertcoords=((8.34234,48.23424),(8.34423,48.26424))client=openrouteservice.Client(key='')# Specify your personal API key# decode_polyline needs the geometry onlygeometry=client.directions(coords)['routes'][0]['geometry']decoded=convert.decode_polyline(geometry)print(decoded)

干运行

尽管查询创建中的错误应该得到很好的处理,但是您可以进行一次试运行来打印请求及其参数:

importopenrouteservicecoords=((8.34234,48.23424),(8.34423,48.26424))client=openrouteservice.Client()client.directions(coords,dry_run='true')

本地ORS实例

如果您托管自己的ORS实例,则可以更改base_url参数以适合您自己的实例:

importopenrouteservicecoords=((8.34234,48.23424),(8.34423,48.26424))# key can be omitted for local hostclient=openrouteservice.Client(base_url='http://localhost/ors')# Only works if you didn't change the ORS endpoints manuallyroutes=client.directions(coords)# If you did change the ORS endpoints for some reason# you'll have to pass url and required parameters explicitly:routes=client.request(url='/new_url',post_json={'coordinates':coords,'profile':'driving-car','format':'geojson'})

支持

有关一般支持和问题,请联系我们的forum

有关问题/错误/增强建议,请使用https://github.com/GIScience/openrouteservice-py/issues

确认

这个库基于googlemaps中非常优雅的代码基。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在一个问题被连续正确回答三次/并添加差异后,我如何将程序循环回开始   Java中未实例化的匿名类   java如何在Android中录制视频,只允许横向模式和最长时间录制时间   java从另一个活动发送实时消息   多线程java线程和互斥   java禁用Spring安全日志   JAVA伊奥。StreamCorruptedException:在与子级和父级ProcessBuilder通信时写入子级中的标准输出时,流头无效   使用Java(HttpURLConnection)对Restheart进行身份验证(对于Mongodb)   java如何解决Jenkins中的SAXParseException?   java为什么我需要mockito来测试Spring应用程序?   计算sin-cos和tan时缺乏精度(java)   java Hibernate。不同项目中相同一对一映射的不同行为   java图像滑块:如何使用JavaFX将图像放在另一个图像上   java Mockito在使用when时抛出NotAMockException   http Java servlet发送回响应