serafim/geokit 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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+.

Latest Stable Version Latest Unstable Version Total Downloads

Geokit is a PHP toolkit to solve geo-related tasks like:

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 serafim/geokit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 28
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-10