Geolocation Library

In development of my generic geosocial media engine, part of the task was to develop a Geolocaiton library for geospatial calculations. This has since been developed and released at https://github.com/rickogden/Location. This contains a variety of geospatial classes: Point, Line, Multipoint Line, Polygon. On top of these it has a Distance class which is used to output the units of measurement for the distance calculations.

It is very simple to use, you can install it through composer:

 require: "ricklab/location" : "dev-master" 

Once installed it can be used simply as follows:

use Ricklab\Location;

$pointOne = new Location\Point(53.234321, -2.34321);
$pointTwo = $pointOne->getRelativePoint(5, 30, 'miles'); //returns a Point object which is 5 miles away with the bearing of 30 degrees.
$distanceObject = $pointOne->distanceTo($pointTwo);
echo $distanceObject->to('km');

$line = new Location\Line($pointOne, $pointTwo);
echo $line->getBearing(); //returns the bearing of the line
echo $line->getMidPoint(); //returns the mid point of the line
echo $line->length()->to('miles'); //length works identically to distanceTo from a Point object

There are also MultipointLine (for paths rather than straight lines) and Polygon classes. They also have a length method which allows for a total length or perimeter measurement.

The MBR class takes in a Point and a radius around that point. This is useful for when doing SQL geospatial searches as it returns the minimum and maximum latitude and longitude values.

Finally they all implement a toSql() method which converts the SQL to a WKT syntax and implement jsonSerializable. This can be plugged straight into an SQL statement and used to query SQL databases (tested on MySQL but should work also on PostGIS). The jsonSerializable converts the geometry into GeoJSON and can be used with databases like MongoDB.

echo json_encode($pointOne);

Please feel free to use it, and let me know of any bugs which you discover!

Leave a Reply