serafim / geokit
PHP 地理工具包
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.22
Replaces
- geokit/geokit: ^1.0
README
这是对 https://github.com/nickdnk/geokit 1.x 分支的分支,兼容 PHP 8.0+。
Geokit 是一个用于解决与地理相关的任务的 PHP 工具包,例如:
安装
使用 Composer 安装最新版本。
composer require serafim/geokit
查看 Packagist 页面 了解所有可用版本。
参考
数学
Math 实例可用于在 LatLng 和 Bounds 实例上执行地理计算。由于此类计算依赖于一个 地球椭球体,您可以将 Geokit\Ellipsoid
实例传递给其构造函数。如果没有提供 Ellipsoid 实例,它将使用默认的 WGS 86 椭球体。
$math = new Geokit\Math(); $mathAiry = new Geokit\Math(Geokit\Ellipsoid::airy1830());
距离计算
Math 实例提供两种方法来计算地球表面上两点之间的距离
distanceHaversine($from, $to)
:使用哈夫曼公式计算两点之间的近似海平面大圆(地球)距离。distanceVincenty($from, $to)
:使用椭球体的 Vincenty 反向公式计算两点之间的测地距离。
$distance1 = $math->distanceHaversine($from, $to); $distance2 = $math->distanceVincenty($from, $to);
两种方法都返回一个 Distance 实例。
转换
通过 expand
和 shrink
方法,您可以通过距离扩展/缩小给定的 Bounds 或 LatLng 实例。
$expandedBounds1 = $math->expand(['lat' => 49.50042565 'lng' => 8.50207515], '10km'); $expandedBounds2 = $math->expand(Geokit\Bounds::normalize('-45 179 45 -179'), '10km'); $shrinkedBounds = $math->shrink($expandedBounds2, '10km');
其他计算
其他有用的方法是
heading($from, $to)
:计算从第一个点到第二个点的(初始)方向,以度为单位。midpoint($from, $to)
:计算两点之间测地线上的一个中间点。endpoint($start, $heading, $distance)
:根据给定的起始点、初始方向和距离,计算测地线上的目的地点。
距离
Distance 实例允许方便地表示距离单位。
$distance = new Geokit\Distance(1000); // or $distance = new Geokit\Distance(1, Geokit\Distance::UNIT_KILOMETERS); $meters = $distance->meters(); $kilometers = $distance->kilometers(); $miles = $distance->miles(); $feet = $distance->feet(); $nauticalMiles = $distance->nautical();
或者,您可以通过其静态 normalize 方法创建 Distance 实例。此方法接受任何看起来像距离的东西,并从中生成 Distance 对象。
$distance = Geokit\Distance::normalize(1000); // Defaults to meters $distance = Geokit\Distance::normalize('1000m'); $distance = Geokit\Distance::normalize('1km'); $distance = Geokit\Distance::normalize('100 miles'); $distance = Geokit\Distance::normalize('1 foot'); $distance = Geokit\Distance::normalize('234nm');
经纬度
LatLng 实例代表地理点,以纬度和经度坐标表示。
- 纬度介于 -90 和 90 度之间,包括。高于 90 或低于 -90 的纬度将被限制,而不是环绕。例如,100 将被限制为 90 度。
- 经度介于 -180 和 180 度之间,包括。高于 180 或低于 -180 的经度将被环绕。例如,480、840 和 1200 都将被环绕到 120 度。
$latLng = new Geokit\LatLng(1, 2); $latitude = $latLng->getLatitude(); // or $latitude = $latLng['latitude']; $longitude = $latLng->getLongitude(); // or $longitude = $latLng['longitude'];
或者,您可以通过其静态 normalize 方法创建 LatLng 实例。此方法接受任何看起来像坐标的东西,并从中生成 LatLng 对象。
$latLng = Geokit\LatLng::normalize('1 2'); $latLng = Geokit\LatLng::normalize('1, 2'); $latLng = Geokit\LatLng::normalize(array('latitude' => 1, 'longitude' => 2)); $latLng = Geokit\LatLng::normalize(array('lat' => 1, 'lng' => 2)); $latLng = Geokit\LatLng::normalize(array('lat' => 1, 'lon' => 2)); $latLng = Geokit\LatLng::normalize(array(1, 2));
边界
Bounds 实例代表地理坐标中的矩形,包括跨越 180 度经线的那一个。
它由其左下角(西南角)和右上角(东北角)的点组成。
$southWest = new Geokit\LatLng(1, 2); $northEast = new Geokit\LatLng(1, 2); $bounds = new Geokit\Bounds($southWest, $northEast); $southWestLatLng = $bounds->getSouthWest(); // or $southWestLatLng = $bounds['south_west']; $northEastLatLng = $bounds->getNorthEast(); // or $northEastLatLng = $bounds['north_east']; $centerLatLng = $bounds->getCenter(); // or $centerLatLng = $bounds['center']; $spanLatLng = $bounds->getSpan(); // or $spanLatLng = $bounds['span']; $boolean = $bounds->contains($latLng); $newBounds = $bounds->extend($latLng); $newBounds = $bounds->union($otherBounds);
或者,您可以通过其静态 normalize 方法创建 Bounds 实例。此方法接受任何看起来像边界的东西,并从中生成 Bounds 对象。
$bounds = Geokit\Bounds::normalize('1 2 3 4'); $bounds = Geokit\Bounds::normalize('1 2, 3 4'); $bounds = Geokit\Bounds::normalize('1, 2, 3, 4'); $bounds = Geokit\Bounds::normalize(array('south_west' => $southWestLatLng, 'north_east' => $northEastLatLng)); $bounds = Geokit\Bounds::normalize(array('south_west' => array(1, 2), 'north_east' => array(3, 4))); $bounds = Geokit\Bounds::normalize(array('southwest' => $southWestLatLng, 'northeast' => $northEastLatLng)); $bounds = Geokit\Bounds::normalize(array('southWest' => $southWestLatLng, 'northEast' => $northEastLatLng)); $bounds = Geokit\Bounds::normalize(array($southWestLatLng, $northEastLatLng));
多边形
Polygon 实例代表二维的连接线段形状,可以是闭合的(第一个点和最后一个点是相同的)或开放的。
$polygon = new Geokit\Polygon([ new Geokit\LatLng(0, 0), new Geokit\LatLng(0, 1), new Geokit\LatLng(1, 1) ]); $latLng1 = $polygon[0]; $latLng2 = $polygon[1]; $latLng3 = $polygon[2]; $closedPolygon = $polygon->close(); $latLng4 = $closedPolygon[3]; // LatLng(0, 0) /** @var Geokit\LatLng $latLng */ foreach ($polygon as $latLng) { } $polygon->contains(LatLng(0.5, 0.5)); // true /** @var Geokit\Bounds $bounds */ $bounds = $polygon->toBounds();
许可证
版权所有 (c) 2011-2016 Jan Sorgalla。在 MIT 许可证 下发布。
致谢
Geokit 受以下库的启发并/或包含移植的代码