请求openrouteservice api服务的python客户端
openrouteservice的Python项目详细描述
快速启动
说明
openrouteservice库允许您轻松访问openrouteservice(ors)路由api。 它对我们的api执行以下请求
- directions
- isochrones
- matrix routing calculations
- places
- elevation
- Pelias geocoding
- Pelias reverse geocoding
- Pelias structured geocoding
- Pelias autocomplete
- Optimization
有关详细信息,请访问:
我们还有一个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
此命令组将把库安装到全局环境中。也适用于虚拟环境。
使用量
对于交互式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'})
确认
这个库基于googlemaps中非常优雅的代码基。