Geometries


No articles match the selected filter.

Introduction

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:


Point

Represents a single point in 2D space.

Methods

new(float $x, float $y): Point

Creates a new Point.

use JWare\GeoPHP\Point;

$point = new Point(3, 28);
$point2 = new Point($x=12.3, $y=-9.21);

clone(): Point

Creates a Point’s clone.

$point = new Point(3, 28);
$point2 = $point->clone();

$point2->isEqual($point); // True

getX(): float

X coordinate getter.

$point = new Point(3, 28);
$point->getX(); // 3

setX(float $x): Point

X coordinate setter. Returns the instance for chainable behaviour.

$point = new Point(3, 28);
$point->setX(33)->getX(); // 33

getY(): float

Y coordinate getter.

$point = new Point(3, 28);
$point->getY(); // 28

setY(float $y): Point

Y coordinate setter. Returns the instance for chainable behaviour.

$point = new Point(3, 28);
$point->setY(42)->getY(); // 42

getXandY(): array

X and Y coordinates getter.

$point = new Point(3, 28);
$point->getXandY(); // [3, 28]

isEqual(Point $otherPoint): bool

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

getMagnitude(): float

Computes the magnitude of the point’s vector.

$point = new Point(1, 2);
$point->getMagnitude(); // 2.236067977

fromRadiansToDegrees(): Point

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]

fromDegreesToRadians(): Point

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]

getAngle(Point $otherPoint, bool $inDegrees = true): float

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

dotProduct(Point $otherPoint): float

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

crossProduct(Point $point2, Point $point3): float

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

euclideanDistance(Point $otherPoint): float

Computes the euclidean distance between two points.

$point = new Point(1, 2);
$point2 = new Point(4, 5);

$point->euclideanDistance($point2); // 4.243

intersectsLine(Line $line): bool

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

intersectsPoint(Point $otherPoint): bool

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

intersectsPolygon(Polygon $polygon): bool

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

Line

A line segment made up of exactly two Points.

Methods

new(Point $start, Point $end): Line

Creates a new Line.

use JWare\GeoPHP\Line;
use JWare\GeoPHP\Point;

$line = new Line(new Point(3, 28), new Point(-2, -9));

clone(): Line

Creates a Line’s clone

$line = new Line(new Point(3, 28), new Point(-2, -9));
$line2 = $line->clone();

getStart(): Point

Starting point’s getter of the line.

$line = new Line(new Point(1, -2), new Point(-2, -9));
$line->getStart(); // Point(1, -2)

setStart(Point $start): Line

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)

getEnd(): Point

Ending point’s getter of the line.

$line = new Line(new Point(1, -2), new Point(-2, -9));
$line->getEnd(); // Point(-2, -9)

setEnd(Point $end): Line

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)

determinant(): float

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

dx(): float

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

dy(): float

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

slope(): float

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

getPoints(): array

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

intersectsPoint(Point $point): bool

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

intersectsLine(Line $line): bool

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

intersectsPolygon(Polygon $polygon): bool

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

euclideanDistance(Point $otherPoint): float

Computes the euclidean distance between two points.

$point = new Point(1, 2);
$point2 = new Point(4, 5);

$point->euclideanDistance($point2); // 4.243

intersectsLine(Line $line): bool

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

intersectsPoint(Point $otherPoint): bool

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

intersectsPolygon(Polygon $polygon): bool

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

Polygon

A bounded two-dimensional area.

Methods

new(array $points): Polygon

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";
}

clone(): Polygon

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();

getPoints(): array

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)]

setPoint(int $idx, Point $point): Polygon

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)]

getCentroid(): Point

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)

area(): float

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

intersectsPoint(Point $point): bool

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

intersectsLine(Line $line): bool

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

intersectsPolygon(Polygon $polygon): bool

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

euclideanDistance(Point $otherPoint): float

Computes the euclidean distance between two points.

$point = new Point(1, 2);
$point2 = new Point(4, 5);

$point->euclideanDistance($point2); // 4.243

intersectsLine(Line $line): bool

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

intersectsPoint(Point $otherPoint): bool

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

intersectsPolygon(Polygon $polygon): bool

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

containsPoint(Point $point): bool

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

containsLine(Line $line): bool

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

containsPolygon(Polygon $polygon): bool

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

© Copyright 2020 Made with love ❤

Generated on Jan 28, 2020