phpgeo

Marcus Jaschen Free   Connect to feed
Simple Yet Powerful Geo Library

phpgeo - A Simple Geo Library

phpgeo provides abstractions to geographical coordinates (including support for different ellipsoids) and allows you to calculate geographical distances between coordinates with high precision.

License

License

Starting with version 2.0.0 phpgeo is licensed under the MIT license. Older versions were GPL-licensed.

Examples/Usage

This list is incomplete, please visit the documentation site for the full monty of documentation and examples!

Distance between two coordinates (Vincenty's Formula)

Use the calculator object directly:

var coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
var coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit

var calculator = new Vincenty();

Console.WriteLine(calculator.getDistance(coordinate1, coordinate2)); // returns 128130.850 (meters; ≈128 kilometers)

or call the getDistance() method of a Coordinate object by injecting a calculator object:

var coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
var coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit

Console.WriteLine(coordinate1.getDistance(coordinate2, new Vincenty()); // returns 128130.850 (meters; ≈128 kilometers)

Simplifying a polyline

Polylines can be simplified to save storage space or bandwidth. Simplification is done with the Ramer–Douglas–Peucker algorithm (AKA Douglas-Peucker algorithm).

var polyline = new Polyline();
polyline.addPoint(new Coordinate(10.0, 10.0));
polyline.addPoint(new Coordinate(20.0, 20.0));
polyline.addPoint(new Coordinate(30.0, 10.0));

var processor = new SimplifyBearing(1500000);

// remove all points which perpendicular distance is less
// than 1500 km from the surrounding points.
var simplified = processor.simplify(polyline);

// simplified is the polyline without the second point (which
// perpendicular distance is ~1046 km and therefore below
// the simplification threshold)

Polygon contains a point (e.g. "GPS geofence")

phpgeo has a polygon implementation which can be used to determinate if a point is contained in it or not. A polygon consists of at least three points. Points are instances of the Coordinate class.

Warning: The calculation gives wrong results if the polygons has points on both sides of the 180/-180 degrees meridian.

var geofence = new Polygon();

geofence.addPoint(new Coordinate(-12.085870,-77.016261));
geofence.addPoint(new Coordinate(-12.086373,-77.033813));
geofence.addPoint(new Coordinate(-12.102823,-77.030938));
geofence.addPoint(new Coordinate(-12.098669,-77.006476));

var outsidePoint = new Coordinate(-12.075452, -76.985079);
var insidePoint = new Coordinate(-12.092542, -77.021540);

Console.WriteLine((bool)geofence.contains(outsidePoint)); // returns bool(false) the point is outside the polygon
Console.WriteLine((bool)geofence.contains(insidePoint)); // returns bool(true) the point is inside the polygon

Formatted output of coordinates

You can format a coordinate in different styles.

Decimal Degrees

var coordinate = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit

Console.WriteLine(coordinate.format(new DecimalDegrees()).ToString());

Degrees/Minutes/Seconds (DMS)

var coordinate = new Coordinate(18.911306, -155.678268); // South Point, HI, USA

var formatter = new DMS();

Console.WriteLine(coordinate.format(formatter).ToString()); // 18° 54′ 41″ -155° 40′ 42″

formatter
    .setSeparator(", ")
    .useCardinalLetters(true)
    .setUnits(DMS.UNITS_ASCII);

Console.WriteLine(coordinate.format(formatter).ToString()); // 18° 54' 41" N, 155° 40' 42" W

GeoJSON

var coordinate = new Coordinate(18.911306, -155.678268); // South Point, HI, USA

Console.WriteLine(coordinate.format(new GeoJSON()).ToString()); // { "type" : "point" , "coordinates" : [ -155.678268, 18.911306 ] }

Features

Info: Please visit the documentation site for complete and up-to-date documentation with many examples!

phpgeo provides the following features (follow the links for examples):

Miscellaneous

@clemdesign created a TypeScript port of phpgeo.

Credits

More information

mjaschen.phpgeo
3.2.1
7/21/2021
phpgeo.marcusjaschen.de
licenses.nuget.org/MIT

Share