Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Geo-Toolkit for PHP.
PHP
branch: master

README.md

Geokit

Geokit is a PHP toolkit to solve geo-related tasks like:

  • Distance calculations.
  • Heading, midpoint and endpoint calculations.
  • Rectangular bounds calculations.

Build Status Coverage Status

Installation

Install the latest version with Composer.

composer require geokit/geokit

Check the Packagist page for all available versions.

Reference

LatLng

A LatLng instance represents a geographical point in latitude/longitude coordinates.

  • Latitude ranges between -90 and 90 degrees, inclusive. Latitudes above 90 or below -90 are capped, not wrapped. For example, 100 will be capped to 90 degrees.
  • Longitude ranges between -180 and 180 degrees, inclusive. Longitudes above 180 or below -180 are wrapped. For example, 480, 840 and 1200 will all be wrapped to 120 degrees.
$latLng = new Geokit\LatLng(1, 2);

$latitude = $latLng->getLatitude();
// or
$latitude = $latLng['latitude'];

$longitude = $latLng->getLongitude();
// or
$longitude = $latLng['longitude'];

Alternatively, you can create a LatLng instance through its static normalize method. This method takes anything which looks like a coordinate and generates a LatLng object from it.

$latLng = Geokit\LatLng::normalize('1 2');
$latLng = Geokit\LatLng::normalize('1, 2');
$latLng = Geokit\LatLng::normalize(array('latitude' => 1, 'longitude' => 2));
$latLng = Geokit\LatLng::normalize(array('lat' => 1, 'lng' => 2));
$latLng = Geokit\LatLng::normalize(array('lat' => 1, 'lon' => 2));
$latLng = Geokit\LatLng::normalize(array(1, 2));

Bounds

A Bounds instance represents a rectangle in geographical coordinates, including one that crosses the 180 degrees longitudinal meridian.

It is constructed from its left/bottom (south-west) and right-top (north-east) corner points.

$southWest = new Geokit\LatLng(1, 2);
$northEast = new Geokit\LatLng(1, 2);

$bounds = new Geokit\Bounds($southWest, $northEast);

$southWestLatLng = $bounds->getSouthWest();
// or
$southWestLatLng = $bounds['south_west'];

$northEastLatLng = $bounds->getNorthEast();
// or
$northEastLatLng = $bounds['north_east'];

$centerLatLng = $bounds->getCenter();
// or
$centerLatLng = $bounds['center'];

$spanLatLng = $bounds->getSpan();
// or
$spanLatLng = $bounds['span'];

$boolean = $bounds->contains($latLng);

$newBounds = $bounds->extend($latLng);
$newBounds = $bounds->union($otherBounds);

Alternatively, you can create a Bounds instance through its static normalize method. This method takes anything which looks like bounds and generates a Bounds object from it.

$bounds = Geokit\Bounds::normalize('1 2 3 4');
$bounds = Geokit\Bounds::normalize('1 2, 3 4');
$bounds = Geokit\Bounds::normalize('1, 2, 3, 4');
$bounds = Geokit\Bounds::normalize(array('south_west' => $southWestLatLng, 'north_east' => $northEastLatLng));
$bounds = Geokit\Bounds::normalize(array('south_west' => array(1, 2), 'north_east' => array(3, 4)));
$bounds = Geokit\Bounds::normalize(array('southwest' => $southWestLatLng, 'northeast' => $northEastLatLng));
$bounds = Geokit\Bounds::normalize(array('southWest' => $southWestLatLng, 'northEast' => $northEastLatLng));
$bounds = Geokit\Bounds::normalize(array($southWestLatLng, $northEastLatLng));

Distance

A Distance instance allows for a convenient representation of a distance unit of measure.

$distance = new Geokit\Distance(1000);
// or
$distance = new Geokit\Distance(1, Geokit\Distance::UNIT_KILOMETERS);

$meters = $distance->meters();
$kilometers = $distance->kilometers();
$miles = $distance->miles();
$feet = $distance->feet();
$nauticalMiles = $distance->nautical();

Alternatively, you can create a Distance instance through its static normalize method. This method takes anything which looks like distance and generates a Distance object from it.

$distance = Geokit\Distance::normalize(1000); // Defaults to meters
$distance = Geokit\Distance::normalize('1000m');
$distance = Geokit\Distance::normalize('1km');
$distance = Geokit\Distance::normalize('100 miles');
$distance = Geokit\Distance::normalize('1 foot');
$distance = Geokit\Distance::normalize('234nm');

License

Geokit is released under the MIT License.

Credits

Geokit has been inspired and/or contains ported code from the following libraries:

Something went wrong with that request. Please try again.