omelya/phpgeo

简单而强大的地理库

v1.0.0 2023-08-18 10:47 UTC

This package is auto-updated.

Last update: 2024-09-18 14:30:47 UTC


README

phpgeo 为地理坐标(包括对不同椭球体的支持)提供了抽象,并允许您以高精度计算坐标之间的地理距离。

Latest Stable Version Total Downloads phpgeo Tests Scrutinizer Code Quality License

目录

要求

最低要求的PHP版本是7.3。 phpgeo 完全支持PHP 8,并已测试至PHP 8.2。

3.x版本要求PHP >= 7.2,但不再获得功能更新。错误修复将被回滚。

2.x版本要求PHP >= 7.0,但不再获得功能更新。错误修复不会回滚。

1.x版本支持PHP >= 5.4。错误修复不会回滚。

文档

文档可在https://phpgeo.marcusjaschen.de/ 获取

安装

使用 Composer,只需将其添加到您的 composer.json 文件中,然后运行

composer require mjaschen/phpgeo

升级

更新项目的 composer.json 中的版本约束,并运行 composer update 或通过运行来要求新版本

composer require mjaschen/phpgeo:^4.0

许可证

从版本2.0.0开始,phpgeo采用MIT许可证。旧版本采用GPL许可证。

功能

信息:请访问文档网站,以获取完整且最新的文档以及许多示例!

phpgeo提供以下功能(点击链接查看示例)

示例/用法

此列表不完整,请访问文档站点以获取完整的文档和示例!

两个坐标之间的距离(Vincenty公式)

直接使用计算器对象

<?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)

多边形包含一个点(例如“GPS地理围栏”)

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());

度/分/秒(DMS)

<?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

GeoJSON

<?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检查PHP文件中的语法错误
  • 使用Psalm进行静态分析并报告错误:composer ci:psalm
  • 使用PHPUnit运行单元测试:composer ci:tests

要一次性运行所有检查和测试,只需使用composer ci

当然,可以直接使用测试运行器,例如PHPUnit

./vendor/bin/phpunit

Psalm

./vendor/bin/psalm

在本地运行GitHub Actions

可以在本地使用act运行整个CI测试矩阵

act --rm -P ubuntu-latest=shivammathur/node:latest

致谢