ricklab / location
A library of PHP objects for spatial calculations
Installs: 27 399
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 3
Forks: 5
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
Requires (Dev)
- ext-simplexml: *
- friendsofphp/php-cs-fixer: 3.75.0
- phpbench/phpbench: 1.3.1
- phpunit/phpunit: ^10.5 || ^11.5
- vimeo/psalm: 6.12.0
Suggests
- ext-geospatial: >=0.2.2-dev
- dev-main
- v7.0.0
- v6.0.2
- v6.0.1
- v6.0.0
- v6.0.0-rc.2
- v6.0.0-rc.1
- v5.0.1
- v5.0.0
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.0
- 3.0.x-dev
- v3.0.0
- v3.0.0-rc1
- v2.2.1
- v2.2
- v2.1
- 2.0.x-dev
- 2.0
- 1.2.x-dev
- dev-feature/v7-transformers
- dev-upgrade/php-8.3
- dev-upgrade/php8.2
- dev-refactor/remove-json-wkt-from-classes
- dev-update/tools
- dev-feature/additional-distance-formulae
- dev-refactor/calculators
- dev-feature/geohash
- dev-feature/comparison-methods
- dev-fix/get-geometries-call
- dev-refactor/php7-upgrade
- dev-vincenty
This package is auto-updated.
Last update: 2025-07-07 15:00:21 UTC
README
A library for geospatial calculations in PHP.
Installation
Using composer, run composer require ricklab/location
Usage
A brief example of how this library can be used:
use Ricklab\Location\Calculator\VincentyCalculator; use Ricklab\Location\Converter\DegreesMinutesSeconds; use Ricklab\Location\Converter\Unit; use Ricklab\Location\Geometry\GeometryCollection; use Ricklab\Location\Geometry\Point; use Ricklab\Location\Geometry\LineString; use Ricklab\Location\Geometry\BoundingBox; // Usage of Point $point = new Point(-2.34323, 52.43343); // Numeric-strings are also valid, to reduce floating-point errors $point2 = new Point('-2.50002', '54.343211'); // Calculate the distance using the default calculator (Haversine) in meters $distance = $point->distanceTo($point2); // Calculate the distance between two points in miles using the Vincenty formula $distance = $point->distanceTo($point2, Unit::MILES, new VincentyCalculator()); // Usage of LineString $line = new LineString([$point, $point2]); // Create a point from DMS strings $point3 = Point::fromDms( DegreesMinutesSeconds::fromString('40° 26′ 46.2345″ S'), DegreesMinutesSeconds::fromString('79° 58′ 56.5543″ E'), ); // A geometry collection example $multiGeometry = new GeometryCollection([ $line, $point3, ]); // Usage of bounding box $bbox = BoundingBox::fromCenter($point, 1000); // 1000 meters radius $bbox2 = BoundingBox::fromGeometry($multiGeometry); // Bounding box of the line $contains = $bbox2->contains($point2); // true if the point is within the bounding box // A new GeometryCollection with the new bounding-box polygon $newMultiGeometry = $multiGeometry->withGeometry($bbox->getPolygon());
Transformers
Transformers are used to convert geometries to and from various formats. This library includes a GeoJSON, WKT and WKB transformer.
GeoJsonTransformer
The GeoJsonTransformer
class converts geometries into GeoJSON format.
Usage
Here is an example of how to use the GeoJsonTransformer
class:
use Ricklab\Location\Transformer\GeoJsonTransformer; use Ricklab\Location\Geometry\Point; $geometry = new Point($longitude, $latitude); $geoJsonString = GeoJsonTransformer::encode($geometry); $secondGeometry = GeoJsonTransformer::decode($geoJsonString); echo $geoJsonString;
WktTransformer/WkbTransformer
The WktTransformer
and WkbTransformer
class converts geometries into WKT
format.
Usage
Here is an example of how to use the WktTransformer
class:
use Ricklab\Location\Transformer\WktTransformer; use Ricklab\Location\Geometry\Point; $geometry = new Point($longitude, $latitude); $wktString = WktTransformer::encode($geometry); echo $wktString;
WkbTransformer
works in exactly the same way, but returns a binary string.
Features
The Feature
class represents a single geospatial feature from the GeoJSON spec.
Usage
Here is an example of how to use the Feature
class:
use Ricklab\Location\Feature\Feature; use Ricklab\Location\Geometry\Point; $geometry = new Point($longitude, $latitude); $feature = new Feature(['property-name', 'property-value'], $geometry); $geometry = $feature->getGeometry(); $properties = $feature->getProperties();
FeatureCollection
The FeatureCollection
class represents a collection of geospatial features.
Usage
Here is an example of how to use the FeatureCollection
class:
use Ricklab\Location\Feature\Feature; use Ricklab\Location\Feature\FeatureCollection; use Ricklab\Location\Geometry\Point; use Ricklab\Location\Transformer\GeoJsonTransformer; $feature1 = new Feature(['name' => 'Feature 1'], new Point($longitude1, $latitude1)); $feature2 = new Feature(['name' => 'Feature 1'], new Point($longitude2, $latitude2)); $collection = new FeatureCollection([$feature1, $feature2], true); $features = $collection->getFeatures(); $bbox = $collection->getBbox(); // Returns the bounding box of the collection $featureGeoJson = GeoJsonTransformer::encode($features); // GeoJSON feature collection representation