Skip to main content

用于请求 openrouteservice API 服务的 Python 客户端

项目描述

构建状态 工作服覆盖范围 文件状态 PyPI 版本 康达构建 康达版 我的活页夹

快速开始

描述

openrouteservice 库让您可以轻松访问openrouteservice (ORS) 路由 API。它针对我们的 API 执行请求

欲了解更多详情,请访问:

我们还有一个包含一些有用示例的repo

如需支持,请咨询我们的论坛

使用此库即表示您同意 ORS条款和条件

要求

openrouteservice-py 针对 CPython 3.7、3.8 和 3.9 以及 PyPy3 进行了测试。

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

pip install -r requirements-dev.txt

安装

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

pip install openrouteservice

从源代码安装最新最好的:

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

测试

如果要运行单元测试,请参阅要求cd到库目录并运行:

nosetests -v

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

用法

如需交互式 Jupyter 笔记本,请访问mybinder.org

基本示例

import openrouteservice

coords = ((8.34234,48.23424),(8.34423,48.26424))

client = openrouteservice.Client(key='') # Specify your personal API key
routes = client.directions(coords)

print(routes)

为方便起见,所有请求执行模块方法都包装在客户端类中。这有一个缺点,即您的 IDE 无法自动显示不同方法的所有位置和可选参数。而且还有很多!

稍微冗长的替代方案,保留 IDE 的智能功能,是

import openrouteservice
from openrouteservice.directions import directions

    coords = ((8.34234,48.23424),(8.34423,48.26424))

    client = openrouteservice.Client(key='') # Specify your personal API key
    routes = directions(client, coords) # Now it shows you all arguments for .directions

优化路线

如果你想在一个简单的旅行商问题中优化多个航点的顺序,你可以传递一个optimize_waypoints参数:

import openrouteservice

coords = ((8.34234,48.23424),(8.34423,48.26424), (8.34523,48.24424), (8.41423,48.21424))

client = openrouteservice.Client(key='') # Specify your personal API key
routes = client.directions(coords, profile='cycling-regular', optimize_waypoints=True)

print(routes)

解码折线

默认情况下,路线 API 返回编码的折线。要解码为一个 GeoJSON 几何对象的dict,只需执行

import openrouteservice
from openrouteservice import convert

coords = ((8.34234,48.23424),(8.34423,48.26424))

client = openrouteservice.Client(key='') # Specify your personal API key

# decode_polyline needs the geometry only
geometry = client.directions(coords)['routes'][0]['geometry']

decoded = convert.decode_polyline(geometry)

print(decoded)

试运行

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

import openrouteservice

coords = ((8.34234,48.23424),(8.34423,48.26424))

client = openrouteservice.Client()
client.directions(coords, dry_run='true')

本地 ORS 实例

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

import openrouteservice

coords = ((8.34234,48.23424),(8.34423,48.26424))

# key can be omitted for local host
client = openrouteservice.Client(base_url='http://localhost/ors')

# Only works if you didn't change the ORS endpoints manually
routes = 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'
  })

支持

如需一般支持和问题,请联系我们的论坛

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

致谢

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

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

openrouteservice-2.3.3.tar.gz (29.1 kB 查看哈希

已上传 source

内置分布

openrouteservice-2.3.3-py3-none-any.whl (33.7 kB 查看哈希

已上传 py3