GeoPHP offers some structures to work with geospatial data called Geometries . More will be added as needed by the community. At the moment they are available:
GeoPHP offers some structures to work with geospatial data called Geometries . More will be added as needed by the community. At the moment they are available:
Represents a single point in 2D space.
Creates a new Point.
use JWare\GeoPHP\Point;
$point = new Point(3, 28);
$point2 = new Point($x=12.3, $y=-9.21);
Creates a Point’s clone.
$point = new Point(3, 28);
$point2 = $point->clone();
$point2->isEqual($point); // True
X coordinate getter.
$point = new Point(3, 28);
$point->getX(); // 3
X coordinate setter. Returns the instance for chainable behaviour.
$point = new Point(3, 28);
$point->setX(33)->getX(); // 33
Y coordinate getter.
$point = new Point(3, 28);
$point->getY(); // 28
Y coordinate setter. Returns the instance for chainable behaviour.
$point = new Point(3, 28);
$point->setY(42)->getY(); // 42
X and Y coordinates getter.
$point = new Point(3, 28);
$point->getXandY(); // [3, 28]
Checks if two point have the same coordinates.
$point = new Point(3, 28);
$point2 = new Point(3, 28);
$point3 = new Point(23, 12);
$point->isEqual($point2); // True
$point->isEqual($point3); // False
Computes the magnitude of the point’s vector.
$point = new Point(1, 2);
$point->getMagnitude(); // 2.236067977
Converts x and y components of the Point from radians to degrees. Returns a Point with the new components.
$point = new Point(2, 3);
$pointConverted = $point->fromRadiansToDegrees();
$pointConverted->getXAndY(); // [114.5916, 171.8873]
Converts x and y components of the Point from degrees to radians. Returns a Point with the new components.
$point = new Point(57.29578, 0);
$pointConverted = $point->fromDegreesToRadians();
$pointConverted->getXAndY(); // [1, 0]
Computes the angle between two points. If $inDegrees
is true the result is returned in degrees, in radians otherwise.
$point1 = new Point(2, 0);
$point2 = new Point(2, 2);
$point1->getAngle($point2); // 45
Computes the dot product of the two points: dot = x1 * x2 + y1 * y2.
$point1 = new Point(2, 4.5);
$point2 = new Point(1.5, 0.5);
$point1->dotProduct($point2); // 5.25s
Computes the cross product of the three points. A positive value implies $this → #point2 → $point3 is counter-clockwise, negative implies clockwise.
$pointA = new Point(1, 2);
$pointB = new Point(3, 5);
$pointC = new Point(7, 12);
$pointA->crossProduct($pointB, $pointC); // 2
Computes the euclidean distance between two points.
$point = new Point(1, 2);
$point2 = new Point(4, 5);
$point->euclideanDistance($point2); // 4.243
Checks whether the point intersects a line.
$line = new Line(
new Point(1, 1),
new Point(5, 5)
);
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point->intersectsLine($line); // True
$point2->intersectsLine($line); // False
Checks whether the point intersects with another point. Two points intersect if they have the same X and Y coordinates (same as isEqual
method).
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point3 = new Point(3, 4);
$point->intersectsPoint($point2); // False
$point2->intersectsPoint($point3); // True
Checks whether the point intersects with a polygon.
$polygon = new Polygon([
new Point(1, 1),
new Point(3, 3),
new Point(2, 7),
new Point(4, 5),
new Point(7, 5),
new Point(1, 1)
]);
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point->intersectsPolygon($Polygon); // True
$point2->intersectsPolygon($Polygon); // False
A line segment made up of exactly two Points.
Creates a new Line.
use JWare\GeoPHP\Line;
use JWare\GeoPHP\Point;
$line = new Line(new Point(3, 28), new Point(-2, -9));
Creates a Line’s clone
$line = new Line(new Point(3, 28), new Point(-2, -9));
$line2 = $line->clone();
Starting point’s getter of the line.
$line = new Line(new Point(1, -2), new Point(-2, -9));
$line->getStart(); // Point(1, -2)
Starting point’s setter. Returns the instance for chainable behaviour.
$line = new Line(new Point(1, -2), new Point(-2, -9));
$line->setStart(new Point(9, 9))->getStart(); // Point(9, 9)
Ending point’s getter of the line.
$line = new Line(new Point(1, -2), new Point(-2, -9));
$line->getEnd(); // Point(-2, -9)
Ending point’s setter. Returns the instance for chainable behaviour.
$line = new Line(new Point(1, -2), new Point(-2, -9));
$line->setEnd(new Point(92, 19))->getEnd(); // Point(92, 19)
Computes the determinant of the line. Determinant = line.start.x * line.end.y - line.start.y * line.end.x
$line = new Line(
new Point(1.34, 2.87),
new Point(3.23, 4.5)
);
$line->determinant(); // -3.2401
Computes the difference in ‘x’ components (Δx): line.end.x - line.start.x.
$line = new Line(
new Point(2.77, 4.23),
new Point(2.77, 7.23)
);
$line->dx(); // 0
Computes the difference in ‘y’ components (Δy): line.end.y - line.start.y.
$line = new Line(
new Point(2.77, 14.32),
new Point(3.77, 9.55)
);
$line->dy(); // -4.77
Computes the slope of a line: line.dy() / line.dx()
$line = new Line(
new Point(3.45, 11.3),
new Point(0.87, 34.243)
);
$line->slope(); // -8.8926
Getter for starting and ending points.
$line = new Line(
new Point(2, 34),
new Point(2, 11)
);
$points = $line->getPoints(); // [Point(2, 34), Point(2, 11)]
$points[0]->isEquals($points[1]); // False
Checks whether the line intersects a point.
$line = new Line(
new Point(1, 1),
new Point(5, 5)
);
$point1 = new Point(3, 3);
$point2 = new Point(-1, 1);
$line->intersectsPoint($point1); // True
$line->intersectsPoint($point2); // False
Checks whether the line intersects another line.
$line1 = new Line(
new Point(1, 1),
new Point(5, 5)
);
$line2 = new Line(
new Point(3, 3),
new Point(5, 1)
);
$line3 = new Line(
new Point(-1, 1),
new Point(-5, -10)
);
$line1->intersectsLine($line2); // True
$line1->intersectsLine($line3); // False
Checks whether the line intersects a polygon.
$line = new Line(
new Point(1, 1),
new Point(5, 5)
);
$polygon = new Polygon([
new Point(2, 4),
new Point(4, 4),
new Point(4, 2),
new Point(3, 1),
new Point(2.5, 3),
new Point(2, 4)
]);
$line->intersectsPolygon($polygon); // True
Computes the euclidean distance between two points.
$point = new Point(1, 2);
$point2 = new Point(4, 5);
$point->euclideanDistance($point2); // 4.243
Checks whether the point intersects a line.
$line = new Line(
new Point(1, 1),
new Point(5, 5)
);
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point->intersectsLine($line); // True
$point2->intersectsLine($line); // False
Checks whether the point intersects with another point. Two points intersect if they have the same X and Y coordinates (same as isEqual
method).
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point3 = new Point(3, 4);
$point->intersectsPoint($point2); // False
$point2->intersectsPoint($point3); // True
Checks whether the point intersects with a polygon.
$polygon = new Polygon([
new Point(1, 1),
new Point(3, 3),
new Point(2, 7),
new Point(4, 5),
new Point(7, 5),
new Point(1, 1)
]);
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point->intersectsPolygon($Polygon); // True
$point2->intersectsPolygon($Polygon); // False
A bounded two-dimensional area.
Creates a new Polygon. The polygon has to have at least three diferent points, and first and last point must have the same coordinates! Otherwise it will throw \JWare\GeoPHP\Exceptions\NotEnoughPointsException
or \JWare\GeoPHP\Exceptions\FirstAndLastPointNotEqualException
exceptions respectively.
use JWare\GeoPHP\Poing;
use JWare\GeoPHP\Polygon;
use \JWare\GeoPHP\Exceptions\FirstAndLastPointNotEqualException;
$polygon = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(4, 4),
new Point(0, 4),
new Point(0, 0),
]);
// Invalid polygons
try {
// Has only 2 different points!
$invalidPolygon = new Polygon([
new Point(0, 1),
new Point(3, 1),
new Point(1, 1)
]);
} catch (NotEnoughPointsException $e) {
echo 'Opps! Exception: ', $e->getMessage(), "\n";
}
try {
$invalidPolygon = new Polygon([
new Point(0, 1),
new Point(3, 1),
new Point(4, 2),
new Point(1, 1) // Not equals to first point
]);
} catch (FirstAndLastPointNotEqualException $e) {
echo 'Opps! Exception: ', $e->getMessage(), "\n";
}
Creates a Polygon’s clone
$polygon = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(4, 4),
new Point(0, 4),
new Point(0, 0),
]);
$clone = $polygon->clone();
All the polygon’s points’ getter.
$polygon = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(2, 4),
new Point(0, 0)
]);
$polygon->getPoints(); // [Point(0, 0), Point(4, 0), Point(2, 4), Point(0, 0)]
Setter for one point of the polygon. $idx
is the index in the array (starting from 0) of the polygon to replace and $point
Point to put in the specified position. $idx
can’t be the first nor the last Point of the Polygon, otherwise it will throw an \JWare\GeoPHP\Exceptions\SettingPointException
exception.
$polygon = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(2, 4),
new Point(0, 0)
]);
$polygon->setPoint(2, new Point(5, 6));
$polygon->getPoints(); // [Point(0, 0), Point(4, 0), Point(5, 6), Point(0, 0)]
Computes the polygon’s centroid.
$polygon = new Polygon([
new Point(-81, 41),
new Point(-88, 36),
new Point(-84, 31),
new Point(-80, 33),
new Point(-77, 39),
new Point(-81, 41)
]);
$centroid = $polygon->getCentroid(); // Point(-82, 36)
Get the Polygon’s area. Uses the Shoelace Formula.
$polygon = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(4, 4),
new Point(0, 4),
new Point(0, 0),
]);
$polygon1->area(); // 16
Checks whether the polygon intersects a point. A Polygon intersects a Point if at least one of its lines intersects the Point.
$polygon = new Polygon([
new Point(2, 4),
new Point(4, 4),
new Point(5, 2),
new Point(3, 1),
new Point(2.5, 3),
new Point(2, 4)
]);
$polygon->intersectsPoint(new Point(3, 4); // True
$polygon->intersectsPoint(new Point(5, 4); // False
Checks whether the polygon intersects a line.
$polygon = new Polygon([
new Point(2, 4),
new Point(4, 4),
new Point(4, 2),
new Point(3, 1),
new Point(2.5, 3),
new Point(2, 4)
]);
$line1 = new Line(
new Point(1, 1),
new Point(5, 5)
);
$line2 = new Line(
new Point(-1, 1),
new Point(-5, -10)
);
$polygon->intersectsLine($line1); // True
$polygon->intersectsLine($line2); // False
Checks whether the polygon intersects another polygon.
$polygon1 = new Polygon([
new Point(2, 4),
new Point(4, 4),
new Point(5, 2),
new Point(3, 1),
new Point(2.5, 3),
new Point(2, 4)
]);
$polygon2 = new Polygon([
new Point(-3, -4),
new Point(-1, -5),
new Point(-2, -6),
new Point(-3, -4)
]);
$polygon3 = new Polygon([
new Point(4, 3),
new Point(6, 2.5),
new Point(6, 1.5),
new Point(4.5, 1.25),
new Point(4, 3)
]);
$polygon1->intersectsPolygon($polygon3); // True
$polygon1->intersectsPolygon($polygon2); // False
Computes the euclidean distance between two points.
$point = new Point(1, 2);
$point2 = new Point(4, 5);
$point->euclideanDistance($point2); // 4.243
Checks whether the point intersects a line.
$line = new Line(
new Point(1, 1),
new Point(5, 5)
);
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point->intersectsLine($line); // True
$point2->intersectsLine($line); // False
Checks whether the point intersects with another point. Two points intersect if they have the same X and Y coordinates (same as isEqual
method).
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point3 = new Point(3, 4);
$point->intersectsPoint($point2); // False
$point2->intersectsPoint($point3); // True
Checks whether the point intersects with a polygon.
$polygon = new Polygon([
new Point(1, 1),
new Point(3, 3),
new Point(2, 7),
new Point(4, 5),
new Point(7, 5),
new Point(1, 1)
]);
$point = new Point(3, 3);
$point2 = new Point(3, 4);
$point->intersectsPolygon($Polygon); // True
$point2->intersectsPolygon($Polygon); // False
Checks whether a point is inside a polygon.
$polygon = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(4, 4),
new Point(0, 4),
new Point(0, 0)
]);
$polygon->containsPoint(new Point(2, 2)); // True
$polygon->containsPoint(new Point(10, 12)); // False
Checks whether the polygon contains a line.
$polygon = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(4, 4),
new Point(0, 4),
new Point(0, 0)
]);
$line1 = new Line(
new Point(2, 2),
new Point(3, 3)
);
$line2 = new Line(
new Point(-1, -1),
new Point(2, 3)
);
$polygon->containsLine($line1); // True
$polygon->containsLine($line2); // False
Checks whether the polygon contains another polygon.
$polygon1 = new Polygon([
new Point(0, 0),
new Point(4, 0),
new Point(4, 4),
new Point(0, 4),
new Point(0, 0)
]);
$polygon2 = new Polygon([
new Point(4, 0),
new Point(8, 0),
new Point(8, 4),
new Point(4, 4),
new Point(4, 0)
]);
$polygon3 = new Polygon([
new Point(1, 2),
new Point(2, 2),
new Point(2, 3),
new Point(1, 2)
]);
$polygon1->containsPolygon($polygon3) // True
$polygon1->containsPolygon($polygon2) // False