vancuren/php-turf

一个类似于 Turf.js 的 PHP 地理空间分析库

v1.0.4 2024-07-15 16:02 UTC

README

PHP Turf 是一个类似于 Turf.js 的 PHP 地理空间分析库。

安装

您可以通过 Composer 安装 PHP Turf。在终端中运行以下命令

composer require vancuren/php-turf

示例

以下是一个基本示例,演示了如何使用 PHP Turf 执行地理空间分析

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

使用

辅助工具

从一个坐标创建一个点要素。

$point1 = new Point([-73.9864, 40.7486]); // New York, NY*
$point2 = new Point([-118.2437, 34.0522]); // Los Angeles, CA*

多边形

从一个坐标数组创建一个多边形要素。

$coords = [[[0, 0],[0, 4],[3, 4],[3, 0],[0, 0]]];

$polygon = new Polygon($coords);

线字符串

创建一个新的线字符串

$points = [[0, 0],[1, 1],[2, 2]];

$lineString = new LineString($points);

要素集合

接受一个或多个要素并创建一个要素集合。

$features = [ 
    new Point([1,2]),
    new Point([1,3]),
    new Point([1,4]),
    new Point([1,5])
];

$featureCollection = new FeatureCollection($features);

测量

面积

返回多边形的面积

$coords = [[[0, 0],[0, 4],[3, 4],[3, 0],[0, 0]]];

$polygon = new Polygon($coords);
$area = Measurement::area($polygon);

方位角

计算两点之间的方位角

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

中心

接受一个要素或要素集合,并返回所有要素的绝对中心点。

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

目的地

从给定的点、距离和方位角计算目的地点

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

距离

计算两点之间的距离

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

中点

计算两点之间的中点

$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 - 需要实现坐标变异...

转换

TODO - 需要实现转换...

要素转换

TODO - 需要实现要素转换...

杂项

最近点

找到参考点最近的点

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

包含点

检查多边形是否包含一个点。

$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

随机

杂项

获取点

返回创建线字符串的点。

$points = [
    [0, 0],
    [1, 1],
    [2, 2]
];

$lineString = new LineString($points);
$result = $lineString->getPoints();

贡献

欢迎贡献!如果您遇到任何问题或对改进有建议,请随时提交拉取请求或打开问题。

支持者

感谢所有支持者!🙏

成为支持者

赞助商

通过成为赞助商来支持此项目。您的标志将在这里显示,并带有链接到您的网站。

成为赞助商

许可证

此项目受 GNU AGPLv3 许可证的许可。有关详细信息,请参阅 LICENSE 文件。