PHPackages                             brianfp/geometry-library - 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. brianfp/geometry-library

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

brianfp/geometry-library
========================

PHP Geometry library provides utility functions for the computation of geometric data on the surface of the Earth.

1.0.4(4y ago)022MITPHP

Since Sep 11Pushed 4y agoCompare

[ Source](https://github.com/brianfp/geometry-library)[ Packagist](https://packagist.org/packages/brianfp/geometry-library)[ Docs](https://alexpechkarev.github.io/geometry-library/)[ RSS](/packages/brianfp-geometry-library/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)DependenciesVersions (5)Used By (0)

Geometry Library Google Maps API V3
-----------------------------------

[](#geometry-library-google-maps-api-v3)

PHP Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth. Code ported from Google [Maps Android API](https://github.com/googlemaps/android-maps-utils/).

Features
--------

[](#features)

- [Spherical](https://developers.google.com/maps/documentation/javascript/reference#spherical) contains spherical geometry utilities allowing you to compute angles, distances and areas from latitudes and longitudes.
- [Poly](https://developers.google.com/maps/documentation/javascript/reference#poly) utility functions for computations involving polygons and polylines.
- [Encoding](https://developers.google.com/maps/documentation/javascript/reference#encoding) utilities for polyline encoding and decoding.

Dependency
----------

[](#dependency)

- [PHP 5](http://php.net/)

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

[](#installation)

Issue following command:

```
composer require brianfp/geometry-library:1.0.2
```

Alternatively edit composer.json by adding following line and run **`composer update`**

```
"require": {
		....,
		"brianfp/geometry-library":"1.0.2",

	},
```

Usage
-----

[](#usage)

Here is an example of using GeometryLibrary:

```
$response =  \GeometryLibrary\SphericalUtil::computeHeading(
                ['lat' => 25.775, 'lng' => -80.190], // from array [lat, lng]
                ['lat' => 21.774, 'lng' => -80.190]); // to array [lat, lng]
  echo $response; // -180

$response = \GeometryLibrary\SphericalUtil::computeDistanceBetween(
              ['lat' => 25.775, 'lng' => -80.190], //from array [lat, lng]
              ['lat' => 21.774, 'lng' => -80.190]); // to array [lat, lng]

  echo $response; // 444891.52998049

$response =  \GeometryLibrary\PolyUtil::isLocationOnEdge(
              ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng]
              [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190],
                ['lat' => 18.466, 'lng' => -66.118],
                ['lat' => 32.321, 'lng' => -64.757]
              ])  ;

  echo $response; // true

$response =  \GeometryLibrary\PolyUtil::isLocationOnPath(
              ['lat' => 25.771, 'lng' => -80.190], // point array [lat, lng]
             [ // poligon arrays of [lat, lng]
              ['lat' => 25.774, 'lng' => -80.190],
              ['lat' => 18.466, 'lng' => -66.118],
              ['lat' => 32.321, 'lng' => -64.757]
             ]);

  echo $response; // false

$response =  \GeometryLibrary\PolyUtil::containsLocation(
              ['lat' => 23.886, 'lng' => -65.269], // point array [lat, lng]
             [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190],
                ['lat' => 18.466, 'lng' => -66.118],
                ['lat' => 32.321, 'lng' => -64.757]
             ]);

  echo $response; // false

$response =  \GeometryLibrary\PolyUtil::distanceToLine(
              ['lat' => 61.387002, 'lng' => 23.890636], // point array [lat, lng]
              ['lat' => 61.487002, 'lng' => 23.790636], // line startpoint array [lat, lng]
              ['lat' => 60.48047, 'lng' => 22.052754] // line endpoint array [lat, lng]
             );

  echo $response; // 12325.124046196 in meters

$response =  \GeometryLibrary\PolyUtil::encode(
              [
                ['lat' => 38.5, 'lng' => -120.2],
                ['lat' => 40.7, 'lng' => -120.95],
                ['lat' => 43.252, 'lng' => -126.453]
              ]);

  echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'

$response =  \GeometryLibrary\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@');

  echo $response; /** array (size=3)
                        0 =>
                          array (size=2)
                            'lat' => float 38.5
                            'lng' => float -120.2
                        1 =>
                          array (size=2)
                            'lat' => float 40.7
                            'lng' => float -120.95
                        2 =>
                          array (size=2)
                            'lat' => float 43.252
                            'lng' => float -126.453
                   */

```

Available methods
-----------------

[](#available-methods)

PolyUtil class
--------------

[](#polyutil-class)

- [`containsLocation($point, $polygon, $geodesic = false)`](#containsLocation)
- [`isLocationOnEdge($point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true)`](#isLocationOnEdge)
- [`isLocationOnPath($point, $polyline, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true)`](#isLocationOnPath)
- [`distanceToLine($p, $start, $end)`](#distanceToLine)
- [`decode($encodedPath)`](#decode)
- [`encode($path)`](#encode)

SphericalUtil class
-------------------

[](#sphericalutil-class)

- [`computeHeading($from, $to)`](#computeHeading)
- [`computeOffset($from, $distance, $heading)`](#computeOffset)
- [`computeOffsetOrigin($to, $distance,  $heading)`](#computeOffsetOrigin)
- [`interpolate($from, $to, $fraction)`](#interpolate)
- [`computeDistanceBetween( $from, $to)`](#computeDistanceBetween)
- [`computeLength($path)`](#computeLength)
- [`computeArea($path)`](#computeArea)
- [`computeSignedArea($path)`](#computeSignedArea)

---

**`containsLocation( $point, $polygon, $geodesic = false )`** - To find whether a given point falls within a polygon

- `$point` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2 \]
- `$polygon` - \[ \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\], \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\], \['lat' =&gt; 43.252, 'lng' =&gt; -126.453\]\]
- `$geodesic` - boolean

Returns boolean

```
$response =  \GeometryLibrary\PolyUtil::containsLocation(
              ['lat' => 23.886, 'lng' => -65.269], // point array [lat, lng]
             [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190],
                ['lat' => 18.466, 'lng' => -66.118],
                ['lat' => 32.321, 'lng' => -64.757]
             ]);

  echo $response; // false
```

---

**`isLocationOnEdge( $point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true )`** - To determine whether a point falls on or near a polyline, or on or near the edge of a polygon, within a specified tolerance in meters.

- `$point` - \['lat' =&gt; 25.774, 'lng' =&gt; -80.190 \]
- `$polygon` - \[ \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\], \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\], \['lat' =&gt; 43.252, 'lng' =&gt; -126.453\]\]
- `$tolerance` - tolerance value in degrees
- `$geodesic` - boolean

Returns boolean

```
$response =  \GeometryLibrary\PolyUtil::isLocationOnEdge(
              ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng]
              [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190],
                ['lat' => 18.466, 'lng' => -66.118],
                ['lat' => 32.321, 'lng' => -64.757]
              ])  ;

  echo $response; // true
```

---

**`isLocationOnPath( $point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true )`** - To determine whether a point falls on or near a polyline, within a specified tolerance in meters

- `$point` - \['lat' =&gt; 25.774, 'lng' =&gt; -80.190 \]
- `$polygon` - \[ \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\], \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\], \['lat' =&gt; 43.252, 'lng' =&gt; -126.453\]\]
- `$tolerance` - tolerance value in degrees
- `$geodesic` - boolean

Returns boolean

```
$response =  \GeometryLibrary\PolyUtil::isLocationOnPath(
              ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng]
              [ // poligon arrays of [lat, lng]
                ['lat' => 25.774, 'lng' => -80.190],
                ['lat' => 18.466, 'lng' => -66.118],
                ['lat' => 32.321, 'lng' => -64.757]
              ])  ;

  echo $response; // true
```

---

**`distanceToLine( $p, $start, $end )`** - To calculate distance from a point to line start-&gt;end on sphere.

- `$p` - \['lat' =&gt; 61.387002, 'lng' =&gt; 23.890636\]
- `$start` - \['lat' =&gt; 61.487002, 'lng' =&gt; 23.790636\]
- `$end` - \['lat' =&gt; 60.48047, 'lng' =&gt; 22.052754\]

Returns distance from a point to line

```
$response =  \GeometryLibrary\PolyUtil::distanceToLine(
              ['lat' => 61.387002, 'lng' => 23.890636], // point array [lat, lng]
              ['lat' => 61.487002, 'lng' => 23.790636], // line startpoint array [lat, lng]
              ['lat' => 60.48047, 'lng' => 22.052754] // line endpoint array [lat, lng]
             );

  echo $response; // 12325.124046196 in meters
```

---

**`decode( $encodedPath )`** - Decodes an encoded path string into a sequence of LatLngs.

- `$encodedPath` - string '\_piFps|U\_ulLnnqC\_mqNvxq`@'

Returns array

```
$response =  \GeometryLibrary\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@');

  echo $response; /** array (size=3)
                        0 =>
                          array (size=2)
                            'lat' => float 38.5
                            'lng' => float -120.2
                        1 =>
                          array (size=2)
                            'lat' => float 40.7
                            'lng' => float -120.95
                        2 =>
                          array (size=2)
                            'lat' => float 43.252
                            'lng' => float -126.453
                   */
```

---

**`encode( $path )`** - Encodes a sequence of LatLngs into an encoded path string.

- `$path` - \[ \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\], \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\], \['lat' =&gt; 43.252, 'lng' =&gt; -126.453\] \]

Returns string

```
$response =  \GeometryLibrary\PolyUtil::encode(
              [
                ['lat' => 38.5, 'lng' => -120.2],
                ['lat' => 40.7, 'lng' => -120.95],
                ['lat' => 43.252, 'lng' => -126.453]
              ]);

  echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
```

---

**`computeHeading( $from, $to )`** - Returns the heading from one LatLng to another LatLng.

- `$from` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\]
- `$to` - \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\]

Returns int

```
$response =  \GeometryLibrary\SphericalUtil::computeHeading(
              ['lat' => 25.775, 'lng' => -80.190],
              ['lat' => 21.774, 'lng' => -80.190]));

  echo $response; // -180
```

---

**`computeOffset( $from, $distance, $heading )`** - Returns the LatLng resulting from moving a distance from an origin in the specified heading.

- `$from` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\]
- `$distance` - number, the distance to travel
- `$heading` - number, the heading in degrees clockwise from north

Returns array

```
$response =  \GeometryLibrary\SphericalUtil::computeOffset(['lat' => 25.775, 'lng' => -80.190], 152, 120);

  echo $response; /** array (size=2)
                      'lat' => float 25.774316510639
                      'lng' => float -80.188685385944
                  */
```

---

**`computeOffsetOrigin( $from, $distance, $heading )`** - Returns the location of origin when provided with a LatLng destination, meters travelled and original heading. Headings are expressed in degrees clockwise from North.

- `$from` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\]
- `$distance` - number, the distance to travel
- `$heading` - number, the heading in degrees clockwise from north

Returns array

```
$response =  \GeometryLibrary\SphericalUtil::computeOffsetOrigin(['lat' => 25.775, 'lng' => -80.190], 152, 120);

  echo $response; /** array (size=2)
                        'lat' => float 14.33435503928
                        'lng' => float -263248.24242931
                  */
```

---

**`interpolate( $from, $to, $fraction )`** - Returns the LatLng which lies the given fraction of the way between the origin LatLng and the destination LatLng.

- `$from` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\]
- `$to` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\]
- `$fraction` - number, a fraction of the distance to travel

Returns array

```
$response =  \GeometryLibrary\SphericalUtil::interpolate(['lat' => 25.775, 'lng' => -80.190],
                                                          ['lat' => 26.215, 'lng' => -81.218], 2);

  echo $response; /** array (size=2)
                      'lat' => float 26.647635362403
                      'lng' => float -82.253737943391
                  */
```

---

**`computeDistanceBetween( $from, $to )`** - Returns the distance, in meters, between two LatLngs. You can optionally specify a custom radius. The radius defaults to the radius of the Earth.

- `$from` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\]
- `$to` - \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\]

Returns float

```
$response =  \GeometryLibrary\SphericalUtil::computeDistanceBetween(['lat' => 25.775, 'lng' => -80.190], ['lat' => 26.215, 'lng' => -81.218]);

  echo $response; //float 113797.92421349
```

---

**`computeLength( $path )`** - Returns the length of the given path, in meters, on Earth.

- `$path` - \[ \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\], \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\], \['lat' =&gt; 43.252, 'lng' =&gt; -126.453\] \]

Returns float

```
$response =  \GeometryLibrary\SphericalUtil::computeLength([
                ['lat' => 38.5, 'lng' => -120.2],
                ['lat' => 40.7, 'lng' => -120.95],
                ['lat' => 43.252, 'lng' => -126.453]
              ]);

  echo $response; //float 788906.98459431
```

---

**`computeArea( $path )`** - Returns the area of a closed path.

- `$path` - \[ \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\], \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\], \['lat' =&gt; 43.252, 'lng' =&gt; -126.453\] \]

Returns float

```
$response =  \GeometryLibrary\SphericalUtil::computeArea([
                ['lat' => 38.5, 'lng' => -120.2],
                ['lat' => 40.7, 'lng' => -120.95],
                ['lat' => 43.252, 'lng' => -126.453]
              ]);

  echo $response; //float 44766785529.143
```

---

**`computeSignedArea( $path )`** - Returns the signed area of a closed path.

- `$path` - \[ \['lat' =&gt; 38.5, 'lng' =&gt; -120.2\], \['lat' =&gt; 40.7, 'lng' =&gt; -120.95\], \['lat' =&gt; 43.252, 'lng' =&gt; -126.453\] \]

Returns float

```
$response =  \GeometryLibrary\SphericalUtil::computeSignedArea([
                ['lat' => 38.5, 'lng' => -120.2],
                ['lat' => 40.7, 'lng' => -120.95],
                ['lat' => 43.252, 'lng' => -126.453]
              ]);

  echo $response; //float 44766785529.143
```

---

Support
-------

[](#support)

[Please open an issue on GitHub](https://github.com/alexpechkarev/geometry-library/issues)

License
-------

[](#license)

Geometry Library Google Maps API V3 is released under the MIT License. See the bundled [LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE)file for details.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 60.5% 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 ~711 days

Total

4

Last Release

1766d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a91f7c8918681b974b97f16953e37f2f76aa5495864e8cb7c70d56cdd9a3603f?d=identicon)[Brian-FP](/maintainers/Brian-FP)

---

Top Contributors

[![alexpechkarev](https://avatars.githubusercontent.com/u/5559162?v=4)](https://github.com/alexpechkarev "alexpechkarev (26 commits)")[![brianfp](https://avatars.githubusercontent.com/u/15164262?v=4)](https://github.com/brianfp "brianfp (4 commits)")[![jaska120](https://avatars.githubusercontent.com/u/23189620?v=4)](https://github.com/jaska120 "jaska120 (4 commits)")[![andykarpov](https://avatars.githubusercontent.com/u/147979?v=4)](https://github.com/andykarpov "andykarpov (3 commits)")[![nikugogoi](https://avatars.githubusercontent.com/u/20663182?v=4)](https://github.com/nikugogoi "nikugogoi (2 commits)")[![rseyferth](https://avatars.githubusercontent.com/u/1991813?v=4)](https://github.com/rseyferth "rseyferth (2 commits)")[![gistrec](https://avatars.githubusercontent.com/u/15942162?v=4)](https://github.com/gistrec "gistrec (1 commits)")[![RetaxMaster](https://avatars.githubusercontent.com/u/48848312?v=4)](https://github.com/RetaxMaster "RetaxMaster (1 commits)")

---

Tags

google maps geometry libraryphp geometry libraryspherical geometry utilitiesencoding and decoding polyline paths utilitiescomputations polygons and polylines

### Embed Badge

![Health badge](/badges/brianfp-geometry-library/health.svg)

```
[![Health](https://phpackages.com/badges/brianfp-geometry-library/health.svg)](https://phpackages.com/packages/brianfp-geometry-library)
```

###  Alternatives

[alexpechkarev/geometry-library

PHP Geometry library provides utility functions for the computation of geometric data on the surface of the Earth.

1393.6M6](/packages/alexpechkarev-geometry-library)[amwhalen/noaa

Client library for NOAA's forecast and current weather services

301.5k](/packages/amwhalen-noaa)

PHPackages © 2026

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