lounisbou/php-distance

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

v1.0.0 2024-09-24 07:52 UTC

This package is auto-updated.

Last update: 2025-05-24 11:34:25 UTC


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.