### Install phpgeo with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/010_Installation.md Use this command to install the latest version of phpgeo compatible with your PHP environment. This is the recommended installation method. ```shell composer require mjaschen/phpgeo ``` -------------------------------- ### Install Dependencies Source: https://github.com/mjaschen/phpgeo/blob/main/CONTRIBUTING.md Install project dependencies using Composer. This should be done after checking out your branch. ```bash composer install ``` -------------------------------- ### Install phpgeo v3.x with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/010_Installation.md Use this command to install version 3.x of phpgeo, suitable for PHP version 7.2. This version is end-of-life. ```shell composer require mjaschen/phpgeo:^3.0 ``` -------------------------------- ### Install phpgeo v1.x with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/010_Installation.md Use this command to install version 1.x of phpgeo, suitable for PHP versions 5.4 to 5.6. This version is end-of-life. ```shell composer require mjaschen/phpgeo:^1.0 ``` -------------------------------- ### Install phpgeo v2.x with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/010_Installation.md Use this command to install version 2.x of phpgeo, suitable for PHP versions 7.0 to 7.1. This version is end-of-life. ```shell composer require mjaschen/phpgeo:^2.0 ``` -------------------------------- ### Install phpgeo v4.x with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/010_Installation.md Use this command to install version 4.x of phpgeo, suitable for PHP versions 7.2 to 8.0. This is a security-only support version. ```shell composer require mjaschen/phpgeo:^4.0 ``` -------------------------------- ### Run Unit Tests Directly with PHPUnit Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Execute the unit tests directly using the PHPUnit executable. Ensure PHPUnit is installed via Composer. ```shell ./vendor/bin/phpunit ``` -------------------------------- ### Require a specific phpgeo version Source: https://github.com/mjaschen/phpgeo/blob/main/README.md To upgrade or install a specific version of phpgeo, use this Composer command. ```shell composer require mjaschen/phpgeo:^6.0 ``` -------------------------------- ### Get Polygon Points and Count Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/140_Polygon.md Retrieve all points within a polygon and the total number of points using getPoints() and getNumberOfPoints(). ```php addPoint(new Coordinate(52.5, 13.5)); $polygon->addPoint(new Coordinate(54.5, 12.5)); $polygon->addPoint(new Coordinate(55.5, 14.5)); printf("The polygon consists of %d points:\n", $polygon->getNumberOfPoints()); foreach ($polygon->getPoints() as $point) { echo $point->format(new DMS()) . PHP_EOL; } ?> ``` -------------------------------- ### Calculate Destination Point (Spherical and Ellipsoidal) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/420_Bearing_and_Destination.md Determines the destination coordinate given a starting point, bearing, and distance. Compares results from both spherical and ellipsoidal models. ```php calculateDestination($berlin, 153, 56100); $destination2 = $bearingEllipsoidal->calculateDestination($berlin, 153, 56100); echo "Spherical: " . $destination1->format(new DecimalDegrees()) . PHP_EOL; echo "Ellipsoidal: " . $destination2->format(new DecimalDegrees()) . PHP_EOL; ``` -------------------------------- ### Get Polygon Segments and Lengths Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/140_Polygon.md Obtain the line segments that form the polygon's perimeter and calculate their lengths using getSegments() and getLength(). ```php addPoint(new Coordinate(52.5, 13.5)); $polygon->addPoint(new Coordinate(54.5, 12.5)); $polygon->addPoint(new Coordinate(55.5, 14.5)); foreach ($polygon->getSegments() as $line) { printf("%0.3f m\n", $line->getLength(new Haversine())); } ?> ``` -------------------------------- ### Get Polyline Segments Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/130_Polyline.md Retrieve an array of Line instances representing the segments of the polyline. Useful for calculating the length of each individual segment. ```php addPoint(new Coordinate(52.5, 13.5)); $track->addPoint(new Coordinate(54.5, 12.5)); $track->addPoint(new Coordinate(55.5, 14.5)); foreach ($track->getSegments() as $segment) { printf( "Segment length: %0.2f kilometers\n", ($segment->getLength(new Haversine()) / 1000) ); } ?> ``` -------------------------------- ### Calculate Final Bearing for a Calculated Destination (Ellipsoidal) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/420_Bearing_and_Destination.md Calculates the final bearing angle to a destination point, given the starting point, initial bearing, and distance, using the ellipsoidal model. ```php calculateDestinationFinalBearing($berlin, 153, 56100); var_dump($finalBearing); ``` -------------------------------- ### Create and Use Ellipsoid Instances Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/300_Ellipsoid.md Demonstrates how to create Ellipsoid objects using default configurations (like WGS-84) and custom parameters (name, a, 1/f). It also shows how to retrieve ellipsoid properties. ```php getName(), $ellipsoid->getA(), $ellipsoid->getB(), $ellipsoid->getF() ); $ellipsoid = new Ellipsoid('GRS-80', 6378137, 298.257222); printf( "%s: a=%f; b=%f; 1/f=%f\n", $ellipsoid->getName(), $ellipsoid->getA(), $ellipsoid->getB(), $ellipsoid->getF() ); ``` -------------------------------- ### Generate Documentation with Makefile Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Generate the complete project documentation using the 'make docs' command. This utilizes Material for MkDocs. ```shell make docs ``` -------------------------------- ### Creating and Testing a Geofence Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/440_Geofence.md Demonstrates how to create a geofence using the Polygon class and test if points are inside or outside of it. Ensure the Location library is imported. ```php 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)); $outsidePoint = new Coordinate(-12.075452, -76.985079); $insidePoint = new Coordinate(-12.092542, -77.021540); echo $geofence->contains($outsidePoint) ? 'Point 1 is located inside the polygon' . PHP_EOL : 'Point 1 is located outside the polygon' . PHP_EOL; echo $geofence->contains($insidePoint) ? 'Point 2 is located inside the polygon' . PHP_EOL : 'Point 2 is located outside the polygon' . PHP_EOL; ``` -------------------------------- ### Run Unit Tests with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Execute the unit tests using the Composer script. This is the recommended way to run tests for easy usage. ```shell composer ci:tests ``` -------------------------------- ### Stage and Commit Changes Source: https://github.com/mjaschen/phpgeo/blob/main/CONTRIBUTING.md Stage all changes and commit them. This prepares your changes for pushing to your fork. ```bash git add -A -- . git commit ``` -------------------------------- ### Run Unit Tests with Docker and PHP 8.3 Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Use Docker to run the unit tests with a specific PHP version (e.g., 8.3). This command mounts the current directory and sets the working directory inside the container. ```shell docker run -it --rm --name phpgeo-phpunit \ -v "$PWD":/usr/src/phpgeo \ -w /usr/src/phpgeo php:8.3-cli \ php vendor/bin/phpunit ``` -------------------------------- ### Run All Tests with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Execute all defined tests and CI tasks at once using the 'composer ci' command. ```shell composer ci ``` -------------------------------- ### Run All CI Tasks with Different PHP Versions Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Iterate through multiple PHP versions (8.2, 8.3, 8.4) using Docker to run all CI tasks sequentially. The loop breaks if any task fails. ```shell for PHP_VERSION in 8.2 8.3 8.4 ; do \ docker run -it --rm -v "$PWD":/phpgeo -w /phpgeo \ ghcr.io/mjaschen/php:${PHP_VERSION}-cli-mj composer ci || break ; \ done ``` -------------------------------- ### Create a Polyline Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/130_Polyline.md Instantiate a new Polyline object and add points using the addPoint() method. Points are instances of the Coordinate class. ```php addPoint(new Coordinate(52.5, 13.5)); $polyline->addPoint(new Coordinate(54.5, 12.5)); $polyline->addPoint(new Coordinate(55.5, 14.5)); ?> ``` -------------------------------- ### Calculate Initial Bearing of a Line Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/120_Line.md Calculates the initial bearing of a line using the BearingEllipsoidal interface. Requires instances of Coordinate and BearingEllipsoidal. ```php getBearing(new BearingEllipsoidal()); printf("The line has a bearing of %.2f degrees\n", $bearing); ``` -------------------------------- ### Calculate Cardinal Distances using Calculator Instance Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/425_Cardinal_Distance.md Use this method when you need to calculate cardinal distances and have separate Coordinate objects and a distance calculator instance. Requires importing relevant classes. ```php getCardinalDirectionDistances($coordinate1, $coordinate2, $calculator); echo 'Cardinal Distances: north=' . $result->getNorth() . ' m; east=' . $result->getEast() . ' m; south=' . $result->getSouth( . ' m; west=' . $result->getWest() . ' m.'; ``` -------------------------------- ### Create Bounds from Center and Distance (PHP) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/200_Bounds.md Use BoundsFactory to create a Bounds object by specifying a center coordinate, a distance to the corners, and a bearing calculation method. This is useful for defining an area around a specific point. ```php addPoint(new Coordinate(52.5, 13.5)); $polygon->addPoint(new Coordinate(54.5, 12.5)); $polygon->addPoint(new Coordinate(55.5, 14.5)); ?> ``` -------------------------------- ### Run PHP Unit Tests Source: https://github.com/mjaschen/phpgeo/blob/main/README.md Executes the unit tests for the PHPGeo library using PHPUnit. Ensure all tests pass before submitting changes. ```shell ./vendor/bin/phpunit ``` -------------------------------- ### Simplify Polyline using Delta-Bearing Algorithm Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/500_Transformations_and_Processing/510_Simplifying_a_Polyline_or_Polygon.md Simplifies a polyline by removing points where the change in bearing angle between consecutive segments is below a specified threshold. The constructor argument is the minimum angle in degrees required to keep a point. ```php addPoint(new Coordinate(10.0, 10.0)); $polyline->addPoint(new Coordinate(20.0, 20.0)); $polyline->addPoint(new Coordinate(30.0, 10.0)); $processor = new SimplifyBearing(90); $simplified = $processor->simplify($polyline); foreach ($simplified->getPoints() as $point) { echo $point->format(new DecimalDegrees()) . PHP_EOL; } ``` -------------------------------- ### Checkout Feature Branch Source: https://github.com/mjaschen/phpgeo/blob/main/CONTRIBUTING.md Create a new branch for your feature or bug fix. Replace 'fix-random-bug' with a descriptive branch name. ```bash git checkout -b fix-random-bug ``` -------------------------------- ### Check Line and Polygon Intersection Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/300_Comparisons/340_Intersections.md Demonstrates how to check for intersections between a line and a polygon using both fast bounds checking and precise segment intersection checks. ```php use Location\Coordinate; use Location\Intersection\Intersection; use Location\Line; use Location\Polygon; $intersection = new Intersection(); $line = new Line( new Coordinate(52.237594, 9.287635), new Coordinate(52.258154, 11.010313) ); $polygon = new Polygon(); $polygon->addPoint(new Coordinate(52.56571, 9.998658)); $polygon->addPoint(new Coordinate(52.369576, 9.560167)); $polygon->addPoint(new Coordinate(52.102252, 9.62397)); $polygon->addPoint(new Coordinate(51.983342, 10.132727)); $polygon->addPoint(new Coordinate(52.083498, 10.699142)); $polygon->addPoint(new Coordinate(52.457087, 10.590607)); $polygon->addPoint(new Coordinate(52.56571, 9.998658)); // Fast check: bounds intersection only (may return false positives). $intersects = $intersection->intersects($line, $polygon, false); // Precise check: segment intersections + containment. $intersectsPrecisely = $intersection->intersects($line, $polygon, true); ``` -------------------------------- ### Clone phpgeo Repository Source: https://github.com/mjaschen/phpgeo/blob/main/CONTRIBUTING.md Clone the phpgeo repository to your local machine. Replace 'yourname' with your GitHub username. ```bash git clone git@github.com:yourname/phpgeo.git ``` -------------------------------- ### Output of Coordinate Instance Cardinal Distance Calculation Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/425_Cardinal_Distance.md The expected output format for cardinal distances and direction calculated using the Coordinate instance method. ```text Cardinal Distances: direction=north-east; north=0 m; east=0 m; south=8768.566 m; west=26969.504 m. ``` -------------------------------- ### Run Static Code Analysis with PHPStan Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Perform static code analysis using PHPStan via a Composer script. This helps identify potential issues in the code. ```shell composer ci:phpstan ``` -------------------------------- ### Run Linting Checks with Composer Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/020_Development.md Execute the lint command via Composer to ensure the sources do not contain any syntax errors. This is part of the static test runners. ```shell composer ci:lint ``` -------------------------------- ### Calculate Cardinal Distances using Coordinate Instance Method Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/425_Cardinal_Distance.md This method is convenient for calculating cardinal distances directly from one Coordinate object to another. It also determines the general direction. ```php getCardinalDirection($point1, $point2); $result = $point1->getCardinalDirectionDistances($point2, new Vincenty()); echo 'Cardinal Distances: direction=' . $direction . '; north=' . $result->getNorth() . ' m; east=' . $result->getEast() . ' m; south=' . $result->getSouth() . ' m; west=' . $result->getWest() . ' m.'; ``` -------------------------------- ### Calculate Distance Between Point and Line Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/435_Distance_Between_Point_and_Line.md Use the PointToLineDistance utility to calculate the distance between a Coordinate and a Line. Requires an instance of a Distance calculator (e.g., Vincenty) to be passed to the constructor. ```php getDistance($point, $line), PHP_EOL ); ``` -------------------------------- ### Calculate Distance using Vincenty's Formula (Direct Calculator) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/410_Distance_and_Length.md Use the Vincenty calculator directly to find the distance between two coordinates. Ensure the Location library and Vincenty class are imported. ```php getDistance($coordinate1, $coordinate2); ``` -------------------------------- ### Calculate Initial and Final Bearing (Spherical) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/420_Bearing_and_Destination.md Calculates the initial and final bearing between two coordinates using the spherical earth model. Useful for quick estimations where high precision is not critical. ```php calculateBearing($berlin, $london)); var_dump($bearingCalculator->calculateFinalBearing($berlin, $london)); $endTime = microtime(true); printf("Time elapsed: %0.6f s\n", ($endTime - $startTime)); ``` -------------------------------- ### Output of Calculator Instance Cardinal Distance Calculation Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/425_Cardinal_Distance.md The expected output format for cardinal distances calculated using the Calculator instance. ```text Cardinal Distances: north=98425.507 m; east=0 m; south=0 m; west=82268.492 m. ``` -------------------------------- ### Format Coordinate to Degrees/Minutes/Seconds (DMS) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/600_Formatting_and_Output/100_Coordinates.md Formats a coordinate into Degrees/Minutes/Seconds. Options include custom separators, cardinal letters, and ASCII units. ```php format($formatter) . PHP_EOL; $formatter->setSeparator(', ') ->useCardinalLetters(true) ->setUnits(DMS::UNITS_ASCII); echo $coordinate->format($formatter) . PHP_EOL; ``` -------------------------------- ### Push to Fork Source: https://github.com/mjaschen/phpgeo/blob/main/CONTRIBUTING.md Push your committed changes to your fork on GitHub. Set the upstream to link your local branch to the remote one. ```bash git push --set-upstream origin fix-random-bug ``` -------------------------------- ### Calculate Distance using Vincenty's Formula (Coordinate Method) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/410_Distance_and_Length.md Calculate the distance between two coordinates by calling the getDistance method on a Coordinate instance, injecting a Vincenty calculator. Requires importing Location classes. ```php getDistance($coordinate2, new Vincenty()); ``` -------------------------------- ### Simplify Polyline using Ramer-Douglas-Peucker Algorithm Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/500_Transformations_and_Processing/510_Simplifying_a_Polyline_or_Polygon.md Simplifies a polyline by removing points that are too close to the line formed by their neighbors. The constructor argument is the maximum distance in meters for points to be removed. ```php addPoint(new Coordinate(10.0, 10.0)); $polyline->addPoint(new Coordinate(20.0, 20.0)); $polyline->addPoint(new Coordinate(30.0, 10.0)); $processor = new SimplifyDouglasPeucker(1500000); $simplified = $processor->simplify($polyline); foreach ($simplified->getPoints() as $point) { echo $point->format(new DecimalDegrees()) . PHP_EOL; } ``` -------------------------------- ### Reverse Polyline Direction Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/130_Polyline.md Obtain a new Polyline instance with the order of points reversed. The original polyline remains unchanged. ```php addPoint(new Coordinate(52.5, 13.5)); $track->addPoint(new Coordinate(54.5, 12.5)); $reversed = $track->getReverse(); print_r($reversed); ?> ``` -------------------------------- ### Calculate Initial and Final Bearing (Ellipsoidal) Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/420_Bearing_and_Destination.md Calculates the initial and final bearing between two coordinates using the more precise ellipsoidal earth model. Suitable for applications requiring higher accuracy. ```php calculateBearing($berlin, $london)); var_dump($bearingCalculator->calculateFinalBearing($berlin, $london)); $endTime = microtime(true); printf("Time elapsed: %0.6f s\n", ($endTime - $startTime)); ``` -------------------------------- ### Reverse Polygon Point Order Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/140_Polygon.md Create a new polygon instance with the order of its points reversed using getReverse(). The original polygon remains unchanged. ```php addPoint(new Coordinate(52.5, 13.5)); $polygon->addPoint(new Coordinate(64.1, - 21.9)); $polygon->addPoint(new Coordinate(40.7, - 74.0)); $polygon->addPoint(new Coordinate(33.9, - 118.4)); $reversed = $polygon->getReverse(); foreach ($reversed->getPoints() as $point) { echo $point->format(new DecimalDegrees(', ')) . PHP_EOL; } ?> ``` -------------------------------- ### Check Direction Between Two Coordinates Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/300_Comparisons/320_Directions.md Use the Direction class to compare two Coordinate objects and determine their relative north/south and east/west positions. Ensure the Location\Coordinate and Location\Direction classes are imported. ```php pointIsNorthOf(point: $helsinki, compareAgainst: $berlin)) { echo 'Helsinki is located north of Berlin.' . PHP_EOL; } else { echo 'Berlin is located north of Helsinki.' . PHP_EOL; } if ($direction->pointIsEastOf(point: $rome, compareAgainst: $berlin)) { echo 'Rome is located east of Berlin.' . PHP_EOL; } else { echo 'Berlin is located east of Rome.' . PHP_EOL; } ``` -------------------------------- ### Comparing Two Points for Same Location Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/300_Comparisons/310_Same_Point_Comparison.md Use `hasSameLocation` to check if two coordinates are identical. This method returns a boolean value. ```php hasSameLocation($coordinate2) ? 'Mauna Kea and Haleakala share the same location.' : 'Mauna Kea and Haleakala have different locations.'; $coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit $coordinate2 = new Coordinate(19.82365, -155.46905); // Gemini North Telescope echo $coordinate1->hasSameLocation($coordinate2, 1000) ? 'Mauna Kea and the Gemini North Telescope are located within the same 1 km-radius.' : 'Mauna Kea and the Gemini North Telescope are located more than 1 km apart.'; ``` -------------------------------- ### Check Point-in-Polygon Intersection Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/300_Comparisons/340_Intersections.md Shows how to perform a precise check to determine if a given point lies inside a polygon. ```php use Location\Coordinate; use Location\Intersection\Intersection; use Location\Polygon; $intersection = new Intersection(); $point = new Coordinate(52.328745, 10.151638); $intersects = $intersection->intersects($point, $polygon, true); ``` -------------------------------- ### Calculate Line Length using Haversine Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/120_Line.md Calculates the length of a line using the Haversine distance formula. Requires instances of Coordinate and Haversine. ```php getLength(new Haversine()); printf("The line has a length of %.3f meters\n", $length); ``` -------------------------------- ### Format Coordinate to Decimal Minutes Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/600_Formatting_and_Output/100_Coordinates.md Formats a coordinate into Decimal Minutes, commonly used in Geocaching. Supports custom separators, cardinal letters, and ASCII units. ```php format($formatter) . PHP_EOL; $formatter->setSeparator(', ') ->useCardinalLetters(true) ->setUnits(DecimalMinutes::UNITS_ASCII); echo $coordinate->format($formatter) . PHP_EOL; ``` -------------------------------- ### Parse Coordinate String Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/700_Parsing_and_Input/110_Coordinates_Parser.md Parses a coordinate string in a variety of formats (e.g., Decimal Degrees, Decimal Minutes) into a Coordinate object. Requires the CoordinateFactory and a Coordinate formatter. ```php use Location\Factory\CoordinateFactory; use Location\Formatter\Coordinate\DecimalDegrees; require_once __DIR__ . '/vendor/autoload.php'; $point = CoordinateFactory::fromString('52° 13.698′ 020° 58.536′'); echo $point->format(new DecimalDegrees()); ``` -------------------------------- ### Format Coordinate as Decimal Degrees Source: https://github.com/mjaschen/phpgeo/blob/main/README.md Formats a coordinate object into a string representation of Decimal Degrees. Requires the DecimalDegrees formatter class. ```php format(new DecimalDegrees()); ``` -------------------------------- ### Calculate Final Bearing of a Line Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/120_Line.md Calculates the final bearing of a line using the BearingEllipsoidal interface. Requires instances of Coordinate and BearingEllipsoidal. ```php getFinalBearing(new BearingEllipsoidal()); printf("The line has a final bearing of %.2f degrees\n", $bearing); ``` -------------------------------- ### Format Coordinate to Decimal Degrees Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/600_Formatting_and_Output/100_Coordinates.md Formats a coordinate into decimal degrees. The separator and number of decimal places can be customized. ```php format(new DecimalDegrees()); ``` ```php format(new DecimalDegrees(', ', 3)); ``` -------------------------------- ### Calculate Polygon Area and Perimeter Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/140_Polygon.md Compute the area in square meters and the perimeter of a polygon. Note that area calculation may have minor inaccuracies for large polygons. ```php addPoint(new Coordinate(0.0000000000, 0.0000000000)); $polygon->addPoint(new Coordinate(0.0000000000, 0.0008983153)); $polygon->addPoint(new Coordinate(0.0009043695, 0.0008983153)); $polygon->addPoint(new Coordinate(0.0009043695, 0.0000000000)); printf( 'Polygon Area = %f m², Perimeter = %f m%s', $polygon->getArea(), $polygon->getPerimeter(new \Location\Distance\Vincenty()), PHP_EOL ); ?> ``` -------------------------------- ### Format Polygon to GeoJSON Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/600_Formatting_and_Output/140_Polygons.md Use the GeoJSON formatter to convert a Polygon object into a GeoJSON string. Ensure the Location\Polygon and Location\Formatter\Polygon\GeoJSON classes are imported. ```php addPoint(new Coordinate(10, 20)); $polygon->addPoint(new Coordinate(20, 40)); $polygon->addPoint(new Coordinate(30, 40)); $polygon->addPoint(new Coordinate(30, 20)); $formatter = new GeoJSON; echo $formatter->format($polygon); ``` ```json {"type":"Polygon","coordinates":[[20,10],[40,20],[40,30],[20,30]]} ``` -------------------------------- ### Format Coordinate to GeoJSON Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/600_Formatting_and_Output/100_Coordinates.md Formats a coordinate into a GeoJSON Point object. Note that float precision is affected by the 'serialize_precision' ini setting. ```php format(new GeoJSON()); ``` -------------------------------- ### Calculate Intermediate Point on a Line Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/120_Line.md Calculates an intermediate point on a line segment at a specified fraction (0.0 to 1.0) along the Great Circle. The result is formatted using DecimalMinutes. ```php getIntermediatePoint(0.25); printf( 'The first quarter of the line ends at %s%s', $result->format(new DecimalMinutes('வைக்')), PHP_EOL ); ``` -------------------------------- ### Format Polyline to GeoJSON Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/600_Formatting_and_Output/130_Polylines.md Use the GeoJSON formatter to convert a Polyline object into a GeoJSON LineString format. Ensure the Location library and its GeoJSON formatter are included. ```php addPoint(new Coordinate(52.5, 13.5)); $polyline->addPoint(new Coordinate(62.5, 14.5)); $formatter = new GeoJSON; echo $formatter->format($polyline); ``` ```json {"type":"LineString","coordinates":[[13.5,52.5],[14.5,62.5]]} ``` -------------------------------- ### Calculate Length of a Polyline Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/410_Distance_and_Length.md Determine the total length of a polyline (a sequence of points) by summing the distances between consecutive points. Requires Coordinate, Polyline, and a distance calculator (e.g., Vincenty). ```php addPoint(new Coordinate(52.5, 13.5)); $track->addPoint(new Coordinate(54.5, 12.5)); echo $track->getLength(new Vincenty()); ``` -------------------------------- ### Calculate Distance using Haversine Formula Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/410_Distance_and_Length.md Compute the distance between two coordinates using the Haversine formula for faster, though less precise, calculations. Import Coordinate and Haversine classes. ```php getDistance($coordinate2, new Haversine()); ``` -------------------------------- ### Format Coordinate as GeoJSON Point Source: https://github.com/mjaschen/phpgeo/blob/main/README.md Formats a coordinate object into a GeoJSON Point geometry string. Note the order of longitude and latitude in the GeoJSON 'coordinates' array. ```php format(new GeoJSON()); // { "type" : "point" , "coordinates" : [ -155.678268, 18.911306 ] } ``` -------------------------------- ### Calculate Perpendicular Distance Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/430_Perpendicular_Distance.md Use the PerpendicularDistance utility to find the shortest distance between a point and a great circle defined by a line. Requires importing Coordinate, Line, and PerpendicularDistance classes. ```php getPerpendicularDistance($point, $line) ); ``` -------------------------------- ### Calculate Perimeter of a Polygon Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/400_Calculations/410_Distance_and_Length.md Compute the perimeter of a polygon by summing the lengths of its segments. This requires defining the polygon's vertices as Coordinate objects and using a distance calculator like Vincenty. ```php addPoint(new Coordinate(10, 10)); $polygon->addPoint(new Coordinate(10, 20)); $polygon->addPoint(new Coordinate(20, 20)); $polygon->addPoint(new Coordinate(20, 10)); echo $polygon->getPerimeter(new Vincenty()); ``` -------------------------------- ### Parsed Coordinate Output Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/700_Parsing_and_Input/110_Coordinates_Parser.md The output format for a parsed coordinate string when using the DecimalDegrees formatter. ```plaintext 52.22830 20.97560 ``` -------------------------------- ### Calculate Line Midpoint Source: https://github.com/mjaschen/phpgeo/blob/main/documentation/docs/100_Geometries/120_Line.md Calculates the midpoint of a line segment along the Great Circle. The midpoint's distance from each endpoint is also calculated using the Haversine formula. ```php getMidpoint(); printf( 'The midpoint of the line is located at %.3f degrees latitude and %.3f degrees longitude.%s', $midpoint->getLat(), $midpoint->getLng(), PHP_EOL ); printf( 'Its distance from the first point is %.1f meters, its distance from the second point is %.1f meters.%s', $line->getPoint1()->getDistance($midpoint, new Haversine()), $line->getPoint2()->getDistance($midpoint, new Haversine()), PHP_EOL ); ``` -------------------------------- ### Check if a Point is Inside a Polygon Source: https://github.com/mjaschen/phpgeo/blob/main/README.md Determines if a given point is contained within a polygon. Note: This method may produce incorrect results for polygons with points on both sides of the 180/-180 degree meridian. ```php 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)); $outsidePoint = new Coordinate(-12.075452, -76.985079); $insidePoint = new Coordinate(-12.092542, -77.021540); var_dump($geofence->contains($outsidePoint)); // returns bool(false) the point is outside the polygon var_dump($geofence->contains($insidePoint)); // returns bool(true) the point is inside the polygon ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.