PHP Turf is a PHP library for geospatial analysis similar to Turf.js.
You can install PHP Turf via Composer. Run the following command in your terminal:
composer require vancuren/php-turf
Here's a basic example demonstrating how to use PHP Turf to perform geospatial analysis:
<?php
*require* 'vendor/autoload.php';
use Vancuren\PhpTurf\Point;
*// Create some points*
$point1 = new Point([-73.9864, 40.7486]); *// New York, NY*
$point2 = new Point([-118.2437, 34.0522]); *// Los Angeles, CA*
*// Calculate distance between points*
$distance = Measurement::distance($point1, $point2);
echo "Distance between New York and Los Angeles: " . $distance . " kilometers\n";
Creates a Point feature from a coordinate.
$point1 = new Point([-73.9864, 40.7486]); // New York, NY*
$point2 = new Point([-118.2437, 34.0522]); // Los Angeles, CA*
Creates a Polygon feature from an array of coordinates.
$coords = [[[0, 0],[0, 4],[3, 4],[3, 0],[0, 0]]];
$polygon = new Polygon($coords);
Creates a new line string
$points = [[0, 0],[1, 1],[2, 2]];
$lineString = new LineString($points);
Takes one or more Features and creates a FeatureCollection.
$features = [
new Point([1,2]),
new Point([1,3]),
new Point([1,4]),
new Point([1,5])
];
$featureCollection = new FeatureCollection($features);
Returns the polygon’s area
$coords = [[[0, 0],[0, 4],[3, 4],[3, 0],[0, 0]]];
$polygon = new Polygon($coords);
$area = Measurement::area($polygon);
Calculate the bearing between two points
$point1 = new Point([-73.9864, 40.7486]); *// New York, NY*
$point2 = new Point([-118.2437, 34.0522]); *// Los Angeles, CA*
$bearing = Measurement::bearing($point1, $point2);
Takes a Feature or FeatureCollection and returns the absolute center point of all features.
$points = [
new Point([-73.9864, 40.7486]), // New York, NY
new Point([-118.2437, 34.0522]), // Los Angeles, CA
new Point([-87.6298, 41.8781]), // Chicago, IL
new Point([-95.3698, 29.7604]) // Houston, TX
];
$featureCollection = new FeatureCollection($points);
$center = Measurement::center($featureCollection);
Calculate the destination point from a given point, distance, and bearing
$point = new Point([-73.9864, 40.7486]); *// New York, NY*
$distance = 3944; *// Roughly the distance to Los Angeles, CA*
$bearing = 273; *// Bearing to Los Angeles, CA*
$destination = Measurement::destination($point, $distance, $bearing);
Calculate the distance between two points
$point1 = new Point([-73.9864, 40.7486]); *// New York, NY*
$point2 = new Point([-118.2437, 34.0522]); *// Los Angeles, CA*
$distance = Measurement::distance($point1, $point2);
Calculate the midpoint between two points
$point1 = new Point([-73.9864, 40.7486]); *// New York, NY*
$point2 = new Point([-118.2437, 34.0522]); *// Los Angeles, CA*
$midpoint = Measurement::midpoint($point1, $point2);
TODO - Need to implement Coordinate Mutation ...
TODO - Need to implement Transformation ...
TODO - Need to implement Feature Conversion ...
Find the nearest point to a reference point
$referencePoint = new Point([-73.9864, 40.7486]); *// New York, NY*
$points = [
new Point([-118.2437, 34.0522]), *// Los Angeles, CA*
new Point([-87.6298, 41.8781]), *// Chicago, IL*
new Point([-95.3698, 29.7604]) *// Houston, TX*
];
$nearestPoint = Measurement::nearestPoint($referencePoint, $points);
Checks to see if a polygon contains a point.
$vertices = [[[0, 0],[0, 4],[3, 4],[3, 0],[0, 0]]];
$polygon = new Polygon($vertices);
$insidePoint = new Point([2, 2]);
$outsidePoint = new Point([5, 5]);
$isPointInside = $polygon->containsPoint($insidePoint); // TRUE
$isPointInside = $polygon->containsPoint($outsidePoint); // FALSE
Returns the points that create the line string.
$points = [
[0, 0],
[1, 1],
[2, 2]
];
$lineString = new LineString($points);
$result = $lineString->getPoints();
Contributions are welcome! Please feel free to submit pull requests or open issues if you encounter any problems or have suggestions for improvement.
Thank you to all our backers! 🙏
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
This project is licensed under the GNU AGPLv3 License. See the LICENSE file for details.