serafim/geokit
Composer 安装命令:
composer require serafim/geokit
包简介
Geo-Toolkit for PHP
关键字:
README 文档
README
This is fork of 1.x branch of https://github.com/nickdnk/geokit compatible with PHP 8.0+.
Geokit is a PHP toolkit to solve geo-related tasks like:
-
Distance calculations.
-
Heading, midpoint and endpoint calculations.
-
Rectangular bounds calculations.
Installation
Install the latest version with Composer.
composer require serafim/geokit
Check the Packagist page for all available versions.
Reference
Math
A Math instance can be used to perform geographic calculations on LatLng and
Bounds instances. Since such calculations depend on an
Earth Ellipsoid, you can pass an
instance of Geokit\Ellipsoid to its constructor. If no Ellipsoid instance is
provided, it uses the default
WGS 86 Ellipsoid.
$math = new Geokit\Math(); $mathAiry = new Geokit\Math(Geokit\Ellipsoid::airy1830());
Distance calculations
A math instance provides two methods to calculate the distance between 2 points on the Earth's surface:
distanceHaversine($from, $to): Calculates the approximate sea level great circle (Earth) distance between two points using the Haversine formula.distanceVincenty($from, $to): Calculates the geodetic distance between two points using the Vincenty inverse formula for ellipsoids.
$distance1 = $math->distanceHaversine($from, $to); $distance2 = $math->distanceVincenty($from, $to);
Both methods return a Distance instance.
Transformations
With the expand and shrink methods, you can expand/shrink a given Bounds or
LatLng instance by a distance.
$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');
Other calculations
Other useful methods are:
heading($from, $to): Calculates the (initial) heading from the first point to the second point in degrees.midpoint($from, $to): Calculates an intermediate point on the geodesic between the two given points.endpoint($start, $heading, $distance): Calculates the destination point along a geodesic, given an initial heading and distance, from the given start point.
Distance
A Distance instance allows for a convenient representation of a distance unit of measure.
$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();
Alternatively, you can create a Distance instance through its static normalize method. This method takes anything which looks like distance and generates a Distance object from it.
$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
A LatLng instance represents a geographical point in latitude/longitude coordinates.
- Latitude ranges between -90 and 90 degrees, inclusive. Latitudes above 90 or below -90 are capped, not wrapped. For example, 100 will be capped to 90 degrees.
- Longitude ranges between -180 and 180 degrees, inclusive. Longitudes above 180 or below -180 are wrapped. For example, 480, 840 and 1200 will all be wrapped to 120 degrees.
$latLng = new Geokit\LatLng(1, 2); $latitude = $latLng->getLatitude(); // or $latitude = $latLng['latitude']; $longitude = $latLng->getLongitude(); // or $longitude = $latLng['longitude'];
Alternatively, you can create a LatLng instance through its static normalize method. This method takes anything which looks like a coordinate and generates a LatLng object from it.
$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
A Bounds instance represents a rectangle in geographical coordinates, including one that crosses the 180 degrees longitudinal meridian.
It is constructed from its left-bottom (south-west) and right-top (north-east) corner points.
$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);
Alternatively, you can create a Bounds instance through its static normalize method. This method takes anything which looks like bounds and generates a Bounds object from it.
$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
A Polygon instance represents a two-dimensional shape of connected line segments and may either be closed (the first and last point are the same) or open.
$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();
License
Copyright (c) 2011-2016 Jan Sorgalla. Released under the MIT License.
Credits
Geokit has been inspired and/or contains ported code from the following libraries:
serafim/geokit 适用场景与选型建议
serafim/geokit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.51k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「geo」 「geometry」 「geography」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 serafim/geokit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 serafim/geokit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 serafim/geokit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Show geo marker on a mapbox map
Show geo shape on a mapbox map with drawing capabilities
PHP FFI bindings for Uber's H3 hexagonal hierarchical geospatial indexing system
Geo-related tools PHP 7.3+ library
PHP Countries and Currencies
Add a weather widget to Flarum
统计信息
- 总下载量: 5.51k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-10