SkillAgentSearch skills...

Phpgeo

Simple Yet Powerful Geo Library for PHP

Install / Use

/learn @mjaschen/Phpgeo

README

phpgeo - A Simple Geo Library for PHP

phpgeo provides abstractions to geographical coordinates (including support for different ellipsoids) and allows you to calculate geographical distances between coordinates with high precision.

Latest Stable Version Total Downloads phpgeo Tests Documentation Status Scrutinizer Code Quality License

Requirements

The minimum required PHP version is 8.2. phpgeo is tested up to PHP 8.5.

New features will only go into the main branch and won't be backported.

It's possible to install older versions of phpgeo for older PHP versions. Please refer to the following table for the compatibility matrix:

| PHP Version | phpgeo Version | Support Status | Composer Install | |:-----------:|:--------------:|:----------------:|-----------------------------------------| | 8.5 | 6.x | ✅ active | composer require mjaschen/phpgeo | | 8.4 | 6.x | ✅ active | composer require mjaschen/phpgeo | | 8.3 | 6.x | ✅ active | composer require mjaschen/phpgeo | | 8.2 | 6.x | ✅ active | composer require mjaschen/phpgeo | | 8.1 | 5.x | ⚠️ security only | composer require mjaschen/phpgeo:^5.0 | | 8.0 | 4.x | ⚠️ security only | composer require mjaschen/phpgeo:^4.0 | | 7.4 | 4.x | ⚠️ security only | composer require mjaschen/phpgeo:^4.0 | | 7.3 | 4.x | ⚠️ security only | composer require mjaschen/phpgeo:^4.0 | | 7.2 | 3.x | ❌ end of life | composer require mjaschen/phpgeo:^3.0 | | 7.1 | 2.x | ❌ end of life | composer require mjaschen/phpgeo:^2.0 | | 7.0 | 2.x | ❌ end of life | composer require mjaschen/phpgeo:^2.0 | | 5.6 | 1.x | ❌ end of life | composer require mjaschen/phpgeo:^1.0 | | 5.5 | 1.x | ❌ end of life | composer require mjaschen/phpgeo:^1.0 | | 5.4 | 1.x | ❌ end of life | composer require mjaschen/phpgeo:^1.0 |

Documentation

The documentation is available at phpgeo.marcusjaschen.de.

Installation

Using Composer, just add it to your composer.json by running:

composer require mjaschen/phpgeo

Upgrading

Update the version constraint in the project's composer.json and run composer update or require the new version by running:

composer require mjaschen/phpgeo:^6.0

Upgrading to 6.x

phpgeo has some breaking changes in the 6.x release line. Please refer to the following list to see what has changed and what you need to do to upgrade your code.

| Change | Description | Action | |--------------------------------------------------------------------------------|---------------------------------------------------------------|-----------------------------------------------------------------------------------| | Line, Polygon, and Polyline classes are now implementing a new interface | GeometryLinesInterface provides the getSegments() method. | There's no need to change anything if you don't extend those classes. | | getBounds() method was added to GeometryInterface | | Ensure your class has a getBounds() method if you implement GeometryInterface | | removed support for PHP 8.1 | Older PHP versions are no longer supported. | Upgrade to at least PHP 8.2 or keep using phpgeo 5.x |

Upgrading to 5.x

phpgeo has some breaking changes in the 5.x release line. Please refer to the following list to see what has changed and what you need to do to upgrade your code.

| Change | Description | Action | |-------------------------------------------------------------|---------------------------------------------|---------------------------------------------------------| | setPoint1() and setPoint2() methods removed from Line | The Line class now is immutable. | Use the constructor to create a new instance of Line. | | removed support for PHP 7.3, 7.4 and 8.0 | Older PHP versions are no longer supported. | Upgrade to at least PHP 8.1. |

License

Starting with version 2.0.0 phpgeo is licensed under the MIT license. Older versions were GPL-licensed.

Features

Info: Please visit the documentation site for complete and up-to-date documentation with many examples!

phpgeo provides the following features (follow the links for examples):

Examples/Usage

This list is incomplete, please visit the documentation site for the full monty of documentation and examples!

Distance between two coordinates (Vincenty's Formula)

Use the calculator object directly:

<?php

use Location\Coordinate;
use Location\Distance\Vincenty;

$coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
$coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit

$calculator = new Vincenty();

echo $calculator->getDistance($coordinate1, $coordinate2); // returns 128130.850 (meters; ≈128 kilometers)

or call the getDistance() method of a Coordinate object by injecting a calculator object:

<?php

use Location\Coordinate;
use Location\Distance\Vincenty;

$coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
$coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit

echo $coordinate1->getDistance($coordinate2, new Vincenty()); // returns 128130.850 (meters; ≈128 kilometers)

Simplifying a polyline

Polylines can be simplified to save storage space or bandwidth. Simplification is done with the Ramer–Douglas–Peucker algorithm (AKA Douglas-Peucker algorithm).

<?php

use Location\Coordinate;
use Location\Polyline;
use Location\Distance\Vincenty;

$polyline = new Polyline();
$polyline->addPoint(new Coordinate(10.0, 10.0));
$polyline->addPoint(new Coordinate(20.0, 20.0));
$polyline->addPoint(new Coordinate(30.0, 10.0));

$processor = new Simplify($polyline);

// remove all points which perpendicular distance is less
// than 1500 km from the surrounding points.
$simplified = $processor->simplify(1500000);

// simplified is the polyline without the second point (which
// perpendicular distance is ~1046 km and therefore below
// the simplification threshold)

Polygon contains a point (e.

Related Skills

View on GitHub
GitHub Stars1.6k
CategoryDevelopment
Updated25d ago
Forks200

Languages

PHP

Security Score

100/100

Audited on Mar 7, 2026

No findings