dmgctrlr / lara-osrm
Requires
- php: ^7.3|^8
- guzzlehttp/guzzle: ^6.3||^7
- illuminate/support: 5.7.*||^6||^7||^8
Requires (Dev)
- orchestra/testbench: ^3.7||^5||^6
- phpunit/phpunit: ^9.0
README
此包是查询OSRM的一个简单包装器。它假定您有一个可用的OSRM v5.x服务器。它支持路线、最近、表格、匹配和行程服务。目前仅支持驾驶模式。
安装
您可以通过composer安装此包
composer require dmgctrlr/lara-osrm
发布配置文件(config/lara-osrm.config)
php artisan vendor:publish --tag="config" --provider="Dmgctrlr\LaraOsrm\LaraOsrmServiceProvider"
您也可以在.env文件中覆盖默认设置
OSRM_HOST=localhost OSRM_PORT=5000 OSRM_VERSION=v1
使用方法
获取请求服务
根据您的喜好和情况,有多种方式获取请求服务。
直接创建
您可以直接创建它们 - 如果您想覆盖从config()读取,可以传递一个数组(包括'host'和'port'键)
// Create a ServiceRequest based on the service you want: RouteServiceRequest, MatchServiceRequest, TripServiceRequest use Dmgctrlr\LaraOsrm\RouteServiceRequest; // Pass config to overwrite the defaults and your laravel config/lara-osrm.php $config = [ 'host' => 'localhost', // Hostname of your OSRM server 'port' => 5000, // Port for your OSRM server ]; $request = new RouteServiceRequest($config);
依赖注入
LaraOSRM注册到Laravel的依赖注入器,因此如果您在控制器、作业或类似位置使用LaraOSRM,您只需将其添加为要求即可。它将使用在config/lara-osrm.php和/或.env中定义的配置设置进行设置
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Dmgctrlr\LaraOsrm\LaraOsrm; use Dmgctrlr\LaraOsrm\Models\LatLng; class Test extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'lara-osrm:test'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle(LaraOsrm $osrm) { // Create your ServiceRequest based on the method you call // e.g. $osrm->route() creates a RouteServiceRequest // $osrm->match(), $osrm->trip() etc. $request = $osrm->route(); $request->setCoordinates([ new LatLng(33.712053, -112.068195), new LatLng(33.602053, -112.065295), new LatLng(33.626367, -112.023641) ]); $response = $request->send(); echo $response->getStatus(); // "Ok" } }
路线计算
请参阅:http://project-osrm.org/docs/v5.22.0/api/#route-service
use Dmgctrlr\LaraOsrm\Models\LatLng; // See (Getting the Request Service)[#getting-the-request-service] to get your $request $request->setCoordinates([ new LatLng(33.712053, -112.068195), new LatLng(33.602053, -112.065295), new LatLng(33.626367, -112.023641) ]); // you can override the default options for each supported service // This is the same as calling setOptions() multiple times. Pass each option // a parameter, use an array if you want to set the value to something other than "true" $request->setOptions('steps', [ 'annotations' => false ], ['overview' => 'full'], ['geometries' => 'geojson']); // `send()` returns a Dmgctrlr\LaraOsrm\Responses\RouteServiceResponse (since we made a RouteServiceRequest). $response = $request->send(); $status = $response->getStatus(); // "Ok" $status = $response->getMessage(); // Mostly useful for getting the error message if there's a problem. $routes = $response->getRoutes(); // Returns an array of Dmgctrlr\LaraOsrm\Models\Route // @var Dmgctrlr\LaraOsrm\Models\Route $recommendedRoute **/ $recommendedRoute = $response->getFirstRoute(); // Returns the first/primary route $recommendedRoute->getDistance(); // Returns in meters $recommendedRoute->getDistance('km'); // Returns in kilometers $recommendedRoute->getDistance('miles', 4); // Returns in miles ronded to 4 decimal places
匹配服务
请参阅:http://project-osrm.org/docs/v5.22.0/api/#match-service
use Dmgctrlr\LaraOsrm\Models\LatLng; // See (Getting the Request Service)[#getting-the-request-service] to get your $request $request->setCoordinates([ new LatLng(33.712053, -112.068195), new LatLng(33.626367, -112.023641) ]); // you can override the default options for each supported service $request->setOptions('steps', 'annotations', ['overview' => 'full'], ['geometries' => 'geojson']); // `send()` returns a Dmgctrlr\LaraOsrm\Responses\RouteServiceResponse (since we made a RouteServiceRequest). $response = $request->send(); $status = $response->getStatus(); // "Ok" $status = $response->getMessage(); // Mostly useful for getting the error message if there's a problem. $routes = $response->getTracepoints(); // Returns an array of Dmgctrlr\LaraOsrm\Models\LatLng // @var Dmgctrlr\LaraOsrm\Models\Route $recommendedRoute **/ $recommendedRoute = $response->getFirstRoute(); // Returns the first/primary matching. $recommendedRoute->getDistance(); // Returns in meters $recommendedRoute->getDistance('km'); // Returns in kilometers $recommendedRoute->getDistance('miles', 4); // Returns in milesr ronded to 4 decimal places
SendChunk - 发送大型RouteServiceRequests
注意:这是一个实验性功能,欢迎您的贡献
如果您要请求包含超过几百个waypoints的路线,当库向OSRM发送GET请求时,会遇到URI长度限制。
为了解决这个问题,您可以使用sendChunk() - 这是一个实验性且不完整的尝试,将您的巨大请求拆分为更小的请求并将它们组合起来。
要使用sendChunk()
- 请阅读
SendChunk的在线文档(src/BaseServiceRequest.php) - 确认sendChunk处理并返回您想要的信息(它不会处理所有信息)
- 只需在您的RouteRequest对象上用
sendChunk()替换send() - 测试以检查
sendChunk()的返回值是否足够准确。
sendChunk()的已知限制(以及如何帮助)
这实际上是一个已知可以(可能)工作的列表。sendChunk的复杂性的核心在于重新组合和拼接我们发出的多个请求。
最终,我们希望能够执行send()和sendChunk()并得到完全相同的结果。我们可以通过使用足够小(例如,10而不是100)的块大小,并使用足够小的测试waypoints列表(足够小以供send()处理)来实现这一点。
测试目前都在:RouteServiceTest::testSendChunkReturnsSameAsSend,在底部有一系列被注释掉的断言。下一步是让这些断言生效,然后宣布另一部分结果为OK!。
- sendChunk仅适用于RouteServiceRequests
- sendChunk仅在'geometries'设置为'geojson'时工作
- sendChunk仅适用于'waypoints'返回值
- 其他返回的数据不应被信任(它可能是部分的)。
- sendChunk在尝试误用它时可能不会抛出错误。请添加错误报告。
测试
如果您遇到像Failed asserting that 429 matches expected 400.或代码429的其他提及,那么您使用的服务器可能很忙(或正在限制您的速率)。
默认情况下,测试将使用OSRM演示服务器,要使用自己的或另一个服务器,请执行 cp phpunit.xml.dist phpunit.xml 并编辑环境变量。
如果您在使用自己的服务器时收到有关路线没有距离的错误,请检查服务器是否已正确配置且没有返回非零距离。对于这些测试,您必须安装 "berlin" 区域。
composer test
或者(在Windows上更好)
vendor/bin/phpunit
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全
如果您发现任何与安全相关的问题,请发送电子邮件至 dm@mediavariance.com,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。
Laravel包模板
本包是使用Laravel包模板生成的。