hular369 / geo-shapify
这是一个PHP库,允许检查一个坐标(纬度,经度)是否在圆形或多边形内部或外部,计算多边形/圆形所占的面积,以及从给定点找到多边形的最近顶点。
dev-main
2024-09-17 11:31 UTC
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-09-17 11:56:24 UTC
README
此包使您能够检查一个坐标是否在多边形定义的边界内。它还提供了一个功能,可以计算由多边形坐标(纬度,经度)定义的给定边界的面积。
功能
-
多边形边界:
- 检查一个坐标是否在多边形内。
- 计算多边形的面积。
-
圆形边界:
- 检查一个坐标是否在由中心和半径定义的圆内。
- 计算圆的面积。
用法
对于多边形
- 定义多边形坐标。
- 使用
ShapeFactory创建多边形形状。 - 使用
contains检查坐标是否在多边形内。 - 使用
area计算多边形的面积。
对于圆形
- 使用带坐标和半径的
ShapeFactory创建圆形形状。 - 使用
contains检查坐标是否在圆内。 - 使用
area计算圆的面积。此包使您能够检查一个坐标是否在多边形定义的边界内。它还提供了一个功能,可以计算由多边形坐标(纬度,经度)定义的给定边界的面积。
要创建和使用多边形,您可以按照以下方式使用 ShapeFactory 类
<?php // Define the coordinates of the polygon vertices $egpt = [ [31.597741, 25.112545], [22.152357, 24.958816], [22.081148, 36.719048], [31.269823, 31.338550] ]; // Create a ShapeFactory instance $shape = new ShapeFactory(); // Create a polygon shape $drawShape = $shape::create('polygon', $egpt); // Check if a point is inside the polygon $isInside = $drawShape->contains(27.701853, 85.319418); // Calculate the area of the polygon $area = $drawShape->area(); // get nearest vertex of the polygon $nearestVertex = $drawShape->nearestVertex(27.701853, 85.319418) // Output the results echo "Nearest vertex of the polygon: " . json_encode($nearestVertex) . PHP_EOL; echo "Is inside: " . ($isInside ? "Yes" : "No") . PHP_EOL; echo "Area: " . $area . PHP_EOL; ?> For circles: <?php // Create a ShapeFactory instance $shape = new ShapeFactory(); // Create a circle shape with center coordinates and radius $drawShape = $shape::create('circle', null, 27.710258, 85.279664, 10); // radius=10 in km // Check if a point is inside the circle $isInside = $drawShape->contains(27.710258, 85.279664); // Calculate the area of the circle $area = $drawShape->area(); // area in sq. km // Output the results echo "Is inside: " . ($isInside ? "Yes" : "No") . PHP_EOL; echo "Area: " . $area . PHP_EOL; ?>;