PHPackages                             philiprehberger/php-geo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. philiprehberger/php-geo

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

philiprehberger/php-geo
=======================

Geospatial utilities for distance, bounding box, and point-in-polygon calculations

v1.2.0(3mo ago)138MITPHPPHP ^8.2CI passing

Since Mar 15Pushed 1mo agoCompare

[ Source](https://github.com/philiprehberger/php-geo)[ Packagist](https://packagist.org/packages/philiprehberger/php-geo)[ Docs](https://github.com/philiprehberger/php-geo)[ GitHub Sponsors](https://github.com/philiprehberger)[ RSS](/packages/philiprehberger-php-geo/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (6)Versions (9)Used By (0)

PHP Geo
=======

[](#php-geo)

[![Tests](https://github.com/philiprehberger/php-geo/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/php-geo/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/d4192862b7c5d5cfd41d1a23dc8f9ccf33555efeac4cf916e02280fac64c8c57/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f7068702d67656f2e737667)](https://packagist.org/packages/philiprehberger/php-geo)[![Last updated](https://camo.githubusercontent.com/9574c4419d3e46ac5476f112d29a5dc56a9ac5ef5fb4e5ac3801c948811e4d0c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f7068696c69707265686265726765722f7068702d67656f)](https://github.com/philiprehberger/php-geo/commits/main)

Geospatial utilities for distance, bounding box, and point-in-polygon calculations.

Requirements
------------

[](#requirements)

- PHP 8.2+

Installation
------------

[](#installation)

```
composer require philiprehberger/php-geo
```

Usage
-----

[](#usage)

### Distance Calculation (Haversine)

[](#distance-calculation-haversine)

```
use PhilipRehberger\Geo\Coordinate;
use PhilipRehberger\Geo\Geo;

$nyc = new Coordinate(40.7128, -74.0060);
$la = new Coordinate(33.9425, -118.4081);

$km = Geo::distance($nyc, $la);          // ~3944 km
$miles = Geo::distance($nyc, $la, 'mi'); // ~2451 mi
```

### Vincenty Distance

[](#vincenty-distance)

```
$km = Geo::distanceVincenty($nyc, $la); // More accurate for long distances
```

### Bounding Box

[](#bounding-box)

```
$center = new Coordinate(40.7128, -74.0060);
$box = Geo::boundingBox($center, 10.0); // 10 km radius

$box->contains($center); // true
$box->toArray();          // ['min_lat' => ..., 'max_lat' => ..., 'min_lng' => ..., 'max_lng' => ...]
```

### Point in Polygon

[](#point-in-polygon)

```
$polygon = [
    new Coordinate(40.800, -73.970),
    new Coordinate(40.800, -73.950),
    new Coordinate(40.760, -73.950),
    new Coordinate(40.760, -73.970),
];

$point = new Coordinate(40.780, -73.960);
Geo::contains($polygon, $point); // true
```

### Bearing

[](#bearing)

```
$bearing = Geo::bearing($nyc, $la); // Initial bearing in degrees (0-360)
```

### Midpoint

[](#midpoint)

```
$mid = Geo::midpoint($nyc, $la); // Geographic midpoint
```

### Destination

[](#destination)

```
// Travel 100 km north from the equator
$start = new Coordinate(0.0, 0.0);
$dest = Geo::destination($start, 0.0, 100.0); // bearing=0 (north), 100 km
```

### Compass Direction

[](#compass-direction)

```
$bearing = Geo::bearing($nyc, $la);
$direction = Geo::compassDirection($bearing); // e.g. 'SW'

Geo::compassDirection(0.0);   // 'N'
Geo::compassDirection(90.0);  // 'E'
Geo::compassDirection(225.0); // 'SW'
```

### Elevation

[](#elevation)

```
$coord = new Coordinate(40.7128, -74.0060);
$elevated = $coord->withElevation(100.5); // New Coordinate with elevation
$elevated->getElevation(); // 100.5
$coord->getElevation();    // null (original unchanged)
```

### Polygon Area

[](#polygon-area)

```
$polygon = [
    new Coordinate(0.0, 0.0),
    new Coordinate(0.0, 1.0),
    new Coordinate(1.0, 1.0),
    new Coordinate(1.0, 0.0),
];

$area = Geo::area($polygon); // Area in square meters
```

### Geohash Encoding/Decoding

[](#geohash-encodingdecoding)

```
$coord = new Coordinate(57.64911, 10.40744);
$hash = Geo::encodeGeohash($coord);          // 'u4pruydqqvj8'
$decoded = Geo::decodeGeohash($hash);        // Coordinate near original
$bounds = Geo::geohashBounds($hash);         // BoundingBox of geohash cell
```

### Polyline Encoding/Decoding

[](#polyline-encodingdecoding)

```
$coordinates = [
    new Coordinate(38.5, -120.2),
    new Coordinate(40.7, -120.95),
    new Coordinate(43.252, -126.453),
];

$encoded = Geo::encodePolyline($coordinates);  // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
$decoded = Geo::decodePolyline($encoded);      // Array of Coordinate objects
```

### Route Distance

[](#route-distance)

```
$route = [
    new Coordinate(40.7128, -74.0060),
    new Coordinate(41.8781, -87.6298),
    new Coordinate(33.9425, -118.4081),
];

$total = Geo::routeDistance($route); // Total distance in km
```

### Units

[](#units)

Supported units: `km` (kilometers), `mi` (miles), `m` (meters), `nmi` (nautical miles).

```
Geo::distance($a, $b, 'mi');   // miles
Geo::distance($a, $b, 'm');    // meters
Geo::distance($a, $b, 'nmi');  // nautical miles
```

API
---

[](#api)

MethodDescription`Geo::distance($a, $b, $unit)`Haversine distance between two coordinates`Geo::distanceVincenty($a, $b, $unit)`Vincenty distance between two coordinates`Geo::boundingBox($center, $radius, $unit)`Bounding box around a center point`Geo::contains($polygon, $point)`Point-in-polygon test (ray casting)`Geo::bearing($from, $to)`Initial bearing in degrees`Geo::midpoint($a, $b)`Geographic midpoint`Geo::destination($start, $bearing, $distance, $unit)`Destination from start given bearing and distance`Geo::compassDirection($bearing)`Convert bearing to cardinal/intercardinal direction`Geo::area($polygon)`Polygon area in square meters (Shoelace formula)`Geo::encodeGeohash($coord, $precision)`Encode coordinate to geohash string`Geo::decodeGeohash($hash)`Decode geohash to coordinate (center of cell)`Geo::geohashBounds($hash)`Get bounding box of a geohash cell`Geo::encodePolyline($coordinates)`Encode coordinates to Google polyline string`Geo::decodePolyline($encoded)`Decode Google polyline string to coordinates`Geo::routeDistance($coordinates, $unit)`Total distance along a multi-point route`Coordinate::withElevation($meters)`Return new Coordinate with elevation`Coordinate::getElevation()`Get elevation in meters or nullDevelopment
-----------

[](#development)

```
composer install
vendor/bin/phpunit
vendor/bin/pint --test
```

Support
-------

[](#support)

If you find this project useful:

⭐ [Star the repo](https://github.com/philiprehberger/php-geo)

🐛 [Report issues](https://github.com/philiprehberger/php-geo/issues?q=is%3Aissue+is%3Aopen+label%3Abug)

💡 [Suggest features](https://github.com/philiprehberger/php-geo/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement)

❤️ [Sponsor development](https://github.com/sponsors/philiprehberger)

🌐 [All Open Source Projects](https://philiprehberger.com/open-source-packages)

💻 [GitHub Profile](https://github.com/philiprehberger)

🔗 [LinkedIn Profile](https://www.linkedin.com/in/philiprehberger)

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance87

Actively maintained with recent releases

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

8

Last Release

91d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfd7d24cbbf32400fa13ce0bbe7a31edd2d66a6d4488eafdb3d64c5337bf0435?d=identicon)[philiprehberger](/maintainers/philiprehberger)

---

Top Contributors

[![philiprehberger](https://avatars.githubusercontent.com/u/8218077?v=4)](https://github.com/philiprehberger "philiprehberger (14 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

geogeospatialdistancehaversinepoint in polygonbounding-box

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/philiprehberger-php-geo/health.svg)

```
[![Health](https://phpackages.com/badges/philiprehberger-php-geo/health.svg)](https://phpackages.com/packages/philiprehberger-php-geo)
```

###  Alternatives

[mjaschen/phpgeo

Simple Yet Powerful Geo Library

1.6k9.1M27](/packages/mjaschen-phpgeo)[jmikola/geojson

GeoJSON implementation for PHP

3069.4M85](/packages/jmikola-geojson)[phpmyadmin/shapefile

ESRI ShapeFile library for PHP

281.5M1](/packages/phpmyadmin-shapefile)[matthiasmullie/geo

Geography helper

6379.9k1](/packages/matthiasmullie-geo)[hannesvdvreken/pip

The Polygon class

1838.7k1](/packages/hannesvdvreken-pip)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
