phpgeo 提供了地理坐标的抽象(包括对不同椭球体的支持),并允许您高精度地计算坐标之间的地理距离。
所需的最低 PHP 版本是 8.1。 phpgeo已测试至 PHP 8.3。
新功能只会进入主分支,不会向后移植。
可以为较旧的 PHP 版本安装较旧版本的phpgeo 。兼容性矩阵请参见下表:
PHP版本 | phpgeo版本 | 支持状态 | 作曲家安装 |
---|---|---|---|
8.3 | 5.x | 积极的 | composer require mjaschen/phpgeo |
8.2 | 5.x | 积极的 | composer require mjaschen/phpgeo |
8.1 | 5.x | 积极的 | composer require mjaschen/phpgeo |
8.0 | 4.x | composer require mjaschen/phpgeo:^4.0 | |
7.4 | 4.x | composer require mjaschen/phpgeo:^4.0 | |
7.3 | 4.x | composer require mjaschen/phpgeo:^4.0 | |
7.2 | 3.x | 生命终结 | composer require mjaschen/phpgeo:^3.0 |
7.1 | 2.x | 生命终结 | composer require mjaschen/phpgeo:^2.0 |
7.0 | 2.x | 生命终结 | composer require mjaschen/phpgeo:^2.0 |
5.6 | 1.x | 生命终结 | composer require mjaschen/phpgeo:^1.0 |
5.5 | 1.x | 生命终结 | composer require mjaschen/phpgeo:^1.0 |
5.4 | 1.x | 生命终结 | composer require mjaschen/phpgeo:^1.0 |
该文档可在 phpgeo.marcusjaschen.de 上获取。
使用 Composer,只需运行以下命令将其添加到您的composer.json
:
composer require mjaschen/phpgeo
更新项目的composer.json
中的版本约束并运行composer update
或通过运行以下命令需要新版本:
composer require mjaschen/phpgeo:^5.0
phpgeo在 5.x 版本中进行了一些重大更改。请参阅以下列表,了解已更改的内容以及升级代码所需执行的操作。
改变 | 描述 | 行动 |
---|---|---|
从Line 中删除了setPoint1() 和setPoint2() 方法 | Line 类现在是不可变的。 | 使用构造函数创建Line 的新实例。 |
删除了对 PHP 7.3、7.4 和 8.0 的支持 | 不再支持较旧的 PHP 版本。 | 至少升级到 PHP 8.1。 |
从版本 2.0.0 开始,phpgeo 根据 MIT 许可证获得许可。旧版本是 GPL 许可的。
信息:请访问文档站点以获取包含许多示例的完整且最新的文档!
phpgeo 提供以下功能(请点击链接查看示例):
18° 54′ 41″ -155° 40′ 42″
)此列表不完整,请访问文档站点以获取完整的文档和示例!
直接使用计算器对象:
<?php
use Location Coordinate ;
use Location Distance Vincenty ;
$ coordinate1 = new Coordinate ( 19.820664 , - 155.468066 ); // Mauna Kea Summit
$ coordinate2 = new Coordinate ( 20.709722 , - 156.253333 ); // Haleakala Summit
$ calculator = new Vincenty ();
echo $ calculator -> getDistance ( $ coordinate1 , $ coordinate2 ); // returns 128130 . 850 (meters ; ≈ 128 kilometers)
或通过注入计算器对象来调用坐标对象的getDistance()
方法:
<?php
use Location Coordinate ;
use Location Distance Vincenty ;
$ coordinate1 = new Coordinate ( 19.820664 , - 155.468066 ); // Mauna Kea Summit
$ coordinate2 = new Coordinate ( 20.709722 , - 156.253333 ); // Haleakala Summit
echo $ coordinate1 -> getDistance ( $ coordinate2 , new Vincenty ()); // returns 128130 . 850 (meters ; ≈ 128 kilometers)
可以简化折线以节省存储空间或带宽。使用 Ramer–Douglas–Peucker 算法(又名 Douglas-Peucker 算法)进行简化。
<?php
use Location Coordinate ;
use Location Polyline ;
use Location Distance Vincenty ;
$ polyline = new Polyline ();
$ polyline -> addPoint ( new Coordinate ( 10.0 , 10.0 ));
$ polyline -> addPoint ( new Coordinate ( 20.0 , 20.0 ));
$ polyline -> addPoint ( new Coordinate ( 30.0 , 10.0 ));
$ processor = new Simplify ( $ polyline );
// remove all points which perpendicular distance is less
// than 1500 km from the surrounding points .
$ simplified = $ processor -> simplify ( 1500000 );
// simplified is the polyline without the second point ( which
// perpendicular distance is ~ 1046 km and therefore below
// the simplification threshold)
phpgeo 有一个多边形实现,可用于确定其中是否包含一个点。多边形至少由三个点组成。点是Coordinate
类的实例。
警告:如果多边形在 180/-180 度子午线两侧都有点,则计算会给出错误结果。
<?php
use Location Coordinate ;
use Location Polygon ;
$ geofence = new Polygon ();
$ geofence -> addPoint ( new Coordinate (- 12.085870 ,- 77.016261 ));
$ geofence -> addPoint ( new Coordinate (- 12.086373 ,- 77.033813 ));
$ geofence -> addPoint ( new Coordinate (- 12.102823 ,- 77.030938 ));
$ geofence -> addPoint ( new Coordinate (- 12.098669 ,- 77.006476 ));
$ outsidePoint = new Coordinate (- 12.075452 , - 76.985079 );
$ insidePoint = new Coordinate (- 12.092542 , - 77.021540 );
var_dump ( $ geofence -> contains ( $ outsidePoint )); // returns bool ( false ) the point is outside the polygon
var_dump ( $ geofence -> contains ( $ insidePoint )); // returns bool ( true ) the point is inside the polygon
您可以使用不同的样式设置坐标格式。
<?php
use Location Coordinate ;
use Location Formatter Coordinate DecimalDegrees ;
$ coordinate = new Coordinate ( 19.820664 , - 155.468066 ); // Mauna Kea Summit
echo $ coordinate -> format ( new DecimalDegrees ());
<?php
use Location Coordinate ;
use Location Formatter Coordinate DMS ;
$ coordinate = new Coordinate ( 18.911306 , - 155.678268 ); // South Point , HI , USA
$ formatter = new DMS ();
echo $ coordinate -> format ( $ formatter ); // 18° 54′ 41″ -155° 40′ 42″
$ formatter -> setSeparator ( " , " )
-> useCardinalLetters ( true )
-> setUnits ( DMS :: UNITS_ASCII );
echo $ coordinate -> format ( $ formatter ); // 18° 54' 41" N , 155° 40 ' 42" W
<?php
use Location Coordinate ;
use Location Formatter Coordinate GeoJSON ;
$ coordinate = new Coordinate ( 18.911306 , - 155.678268 ); // South Point , HI , USA
echo $ coordinate -> format ( new GeoJSON ()); // { " type " : " point " , " coordinates " : [ -155.678268, 18.911306 ] }
在提交拉取请求之前,请务必运行所有检查和测试并确保一切都是绿色的。
composer ci:lint
composer ci:psalm
composer ci:tests
要立即运行所有检查和测试,只需使用composer ci
。
当然,可以直接使用测试运行器,例如对于 PHPUnit:
./vendor/bin/phpunit
诗篇:
./vendor/bin/psalm
可以使用act在本地运行整个 CI 测试矩阵:
act --rm -P ubuntu-latest=shivammathur/node:latest