codificar/phpgeo

简单的地理库

1.0.4 2022-05-03 14:00 UTC

This package is auto-updated.

Last update: 2024-08-30 01:21:57 UTC


README

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

要求

最低要求的PHP版本是5.4。版本0.4发布后删除了PHP 5.3的兼容性。

Laravel 5的Google Maps API Web服务集合:https://github.com/alexpechkarev/google-maps

文档

文档可在https://phpgeo.marcusjaschen.de/找到

同样,API文档也可用:https://phpgeo.marcusjaschen.de/api/

安装

使用Composer,只需运行以下命令将其添加到您的composer.json

composer require codificar/phpgeo

功能

信息:请访问文档网站以获取完整和最新的文档!

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 ] }

鸣谢