定制 lounisbou/php-distance 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

lounisbou/php-distance

最新稳定版本:v1.0.0

Composer 安装命令:

composer require lounisbou/php-distance

包简介

A PHP library for calculating the distance between two geographical points on Earth using various Earth radius values.

README 文档

README

php-distance is a simple PHP library for calculating the distance between two geographical points on Earth. It supports different Earth radius values and various distance calculation formulas.

Installation

Use Composer to install the library:

composer require lounisbou/phpdistance

Usage

Define Points Define geographical points using the Point class:

use PHPDistance\Point;

$start = new Point(52.5200, 13.4050); // Berlin
$end = new Point(48.8566, 2.3522); // Paris

Calculate Distance Using Haversine Formula

Use the Route class with the HaversineCalculator to calculate the distance using the Haversine formula:

use PHPDistance\Route;
use PHPDistance\HaversineCalculator;
use PHPDistance\Enums\EarthRadius;

$start = new Point(52.5200, 13.4050); // Berlin
$end = new Point(48.8566, 2.3522); // Paris
$calculator = new HaversineCalculator(EarthRadius::MEAN_RADIUS);
$route = new Route($start, $end, $calculator);
$distance = Route::getHumanReadableDistance($haversineCalculator->calculate($route));
echo "Distance using Haversine formula with mean radius: " . $distance;

Calculate Distance Using Vincenty Formula

Use the Route class with the VincentyCalculator to calculate the distance using Vincenty's formula:

use PHPDistance\Route;
use PHPDistance\VincentyCalculator;

$start = new Point(52.5200, 13.4050); // Berlin
$end = new Point(48.8566, 2.3522); // Paris
$calculator = new VincentyCalculator();
$route = new Route($start, $end, $calculator);
$distance = Route::getHumanReadableDistance($vincentyCalculator->calculate($route));
echo "Distance using Vincenty formula: " . $distance;

Distance Calculation Formulas

Haversine Formula

The Haversine formula is an equation giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is useful for calculating the shortest distance over the earth's surface.

Vincenty Formula

Vincenty's formulae are two related iterative methods used in geodesy to calculate the distance between two points on the surface of an ellipsoid. They are accurate for long distances.

MySQL Formula

The MySQL formula is a simplified version of the Haversine formula that can be used in MySQL queries to calculate distances between two points.

Custom Earth Radius

You can specify a custom Earth radius if needed:

$customRadius = 6356752.3142; // Custom radius in meters
$calculator = new HaversineCalculator($customRadius);
$route = new Route($start, $end, $calculator);

echo "Distance using Haversine formula with custom radius: " . $route->calculateDistance() . " meters\n";

Adding More Formulas

You can extend the library by adding more distance calculation formulas. Implement the DistanceCalculatorInterface to create a new calculator:

use PHPDistance\Point;
use PHPDistance\DistanceCalculatorInterface;

class NewFormulaCalculator implements DistanceCalculatorInterface
{
public function calculate(Point $from, Point $to): int
{
// Implement the new formula here
return 0; // Placeholder
}
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request or open an Issue on GitHub.

License

This library is licensed under the MIT License. See the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-24