alexpechkarev / geometry-library
PHP 地理形状库提供用于计算地球表面上几何数据的实用函数。
README
PHP 地理形状库提供用于计算地球表面上几何数据的实用函数。代码移植自 Google Maps Android API。
功能
依赖关系
安装
运行以下命令
composer require alexpechkarev/geometry-library:1.0.5
或者通过编辑 composer.json 添加以下行并运行 composer update
"require": { ...., "alexpechkarev/geometry-library":"1.0.5", },
用法
以下是如何使用 GeometryLibrary 的示例
$response = \GeometryLibrary\SphericalUtil::computeHeading( ['lat' => 25.775, 'lng' => -80.190], // from array [lat, lng] ['lat' => 21.774, 'lng' => -80.190]); // to array [lat, lng] echo $response; // -180 $response = \GeometryLibrary\SphericalUtil::computeDistanceBetween( ['lat' => 25.775, 'lng' => -80.190], //from array [lat, lng] ['lat' => 21.774, 'lng' => -80.190]); // to array [lat, lng] echo $response; // 444891.52998049 $response = \GeometryLibrary\PolyUtil::isLocationOnEdge( ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng] [ // poligon arrays of [lat, lng] ['lat' => 25.774, 'lng' => -80.190], ['lat' => 18.466, 'lng' => -66.118], ['lat' => 32.321, 'lng' => -64.757] ]) ; echo $response; // true $response = \GeometryLibrary\PolyUtil::isLocationOnPath( ['lat' => 25.771, 'lng' => -80.190], // point array [lat, lng] [ // poligon arrays of [lat, lng] ['lat' => 25.774, 'lng' => -80.190], ['lat' => 18.466, 'lng' => -66.118], ['lat' => 32.321, 'lng' => -64.757] ]); echo $response; // false $response = \GeometryLibrary\PolyUtil::containsLocation( ['lat' => 23.886, 'lng' => -65.269], // point array [lat, lng] [ // poligon arrays of [lat, lng] ['lat' => 25.774, 'lng' => -80.190], ['lat' => 18.466, 'lng' => -66.118], ['lat' => 32.321, 'lng' => -64.757] ]); echo $response; // false $response = \GeometryLibrary\PolyUtil::distanceToLine( ['lat' => 61.387002, 'lng' => 23.890636], // point array [lat, lng] ['lat' => 61.487002, 'lng' => 23.790636], // line startpoint array [lat, lng] ['lat' => 60.48047, 'lng' => 22.052754] // line endpoint array [lat, lng] ); echo $response; // 12325.124046196 in meters $response = \GeometryLibrary\PolyUtil::encode( [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]); echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@' $response = \GeometryLibrary\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); echo $response; /** array (size=3) 0 => array (size=2) 'lat' => float 38.5 'lng' => float -120.2 1 => array (size=2) 'lat' => float 40.7 'lng' => float -120.95 2 => array (size=2) 'lat' => float 43.252 'lng' => float -126.453 */
可用方法
PolyUtil 类
- containsLocation($point, $polygon, $geodesic = false)
- isLocationOnEdge($point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true)
- isLocationOnPath($point, $polyline, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true)
- distanceToLine($p, $start, $end)
- decode($encodedPath)
- encode($path)
SphericalUtil 类
- computeHeading($from, $to)
- computeOffset($from, $distance, $heading)
- computeOffsetOrigin($to, $distance, $heading)
- interpolate($from, $to, $fraction)
- computeDistanceBetween( $from, $to)
- computeLength($path)
- computeArea($path)
- computeSignedArea($path)
 containsLocation( $point, $polygon, $geodesic = false ) - 判断一个给定点是否在多边形内部
- $point- ['lat' => 38.5, 'lng' => -120.2 ]
- $polygon- [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453]]
- $geodesic- boolean
返回 boolean
$response = \GeometryLibrary\PolyUtil::containsLocation( ['lat' => 23.886, 'lng' => -65.269], // point array [lat, lng] [ // poligon arrays of [lat, lng] ['lat' => 25.774, 'lng' => -80.190], ['lat' => 18.466, 'lng' => -66.118], ['lat' => 32.321, 'lng' => -64.757] ]); echo $response; // false
 isLocationOnEdge( $point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true ) - 判断一个点是否在折线附近或多边形的边缘附近,在指定公差内(以米为单位)。
- $point- ['lat' => 25.774, 'lng' => -80.190 ]
- $polygon- [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453]]
- $tolerance- 公差值(度)
- $geodesic- boolean
返回 boolean
$response = \GeometryLibrary\PolyUtil::isLocationOnEdge( ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng] [ // poligon arrays of [lat, lng] ['lat' => 25.774, 'lng' => -80.190], ['lat' => 18.466, 'lng' => -66.118], ['lat' => 32.321, 'lng' => -64.757] ]) ; echo $response; // true
 isLocationOnPath( $point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true ) - 判断一个点是否在折线附近,在指定公差内(以米为单位)。
- $point- ['lat' => 25.774, 'lng' => -80.190 ]
- $polygon- [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453]]
- $tolerance- 公差值(度)
- $geodesic- boolean
返回 boolean
$response = \GeometryLibrary\PolyUtil::isLocationOnPath( ['lat' => 25.774, 'lng' => -80.190], // point array [lat, lng] [ // poligon arrays of [lat, lng] ['lat' => 25.774, 'lng' => -80.190], ['lat' => 18.466, 'lng' => -66.118], ['lat' => 32.321, 'lng' => -64.757] ]) ; echo $response; // true
 distanceToLine( $p, $start, $end ) - 计算从点到球面上线段起点的距离。
- $p- ['lat' => 61.387002, 'lng' => 23.890636]
- $start- ['lat' => 61.487002, 'lng' => 23.790636]
- $end- ['lat' => 60.48047, 'lng' => 22.052754]
返回点到线的距离
$response = \GeometryLibrary\PolyUtil::distanceToLine( ['lat' => 61.387002, 'lng' => 23.890636], // point array [lat, lng] ['lat' => 61.487002, 'lng' => 23.790636], // line startpoint array [lat, lng] ['lat' => 60.48047, 'lng' => 22.052754] // line endpoint array [lat, lng] ); echo $response; // 12325.124046196 in meters
 decode( $encodedPath ) - 将编码的路径字符串解码成一系列的经纬度。
- $encodedPath- 字符串 '_p- iFps|U_ulLnnqC_mqNvxq`@'
返回数组
$response = \GeometryLibrary\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@'); echo $response; /** array (size=3) 0 => array (size=2) 'lat' => float 38.5 'lng' => float -120.2 1 => array (size=2) 'lat' => float 40.7 'lng' => float -120.95 2 => array (size=2) 'lat' => float 43.252 'lng' => float -126.453 */
 encode( $path ) - 将一系列的经纬度编码成路径字符串。
- $path- [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]
返回字符串
$response = \GeometryLibrary\PolyUtil::encode( [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]); echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
 computeHeading( $from, $to ) - 返回从一个经纬度到另一个经纬度的航向。
- $from- ['lat' => 38.5, 'lng' => -120.2]
- $to- ['lat' => 40.7, 'lng' => -120.95]
返回整数
$response = \GeometryLibrary\SphericalUtil::computeHeading( ['lat' => 25.775, 'lng' => -80.190], ['lat' => 21.774, 'lng' => -80.190])); echo $response; // -180
 computeOffset( $from, $distance, $heading ) - 返回从起点沿指定航向移动指定距离后的经纬度。
- $from- ['lat' => 38.5, 'lng' => -120.2]
- $distance- 数字,移动的距离
- $heading- 数字,相对于北顺时针的航向度数
返回数组
$response = \GeometryLibrary\SphericalUtil::computeOffset(['lat' => 25.775, 'lng' => -80.190], 152, 120); echo $response; /** array (size=2) 'lat' => float 25.774316510639 'lng' => float -80.188685385944 */
 computeOffsetOrigin( $from, $distance, $heading ) - 当提供经纬度目标点、移动距离和原始航向时,返回起点位置。航向以从北开始顺时针度数表示。
- $from- ['lat' => 38.5, 'lng' => -120.2]
- $distance- 数字,移动的距离
- $heading- 数字,相对于北顺时针的航向度数
返回数组
$response = \GeometryLibrary\SphericalUtil::computeOffsetOrigin(['lat' => 25.775, 'lng' => -80.190], 152, 120); echo $response; /** array (size=2) 'lat' => float 14.33435503928 'lng' => float -263248.24242931 */
 interpolate( $from, $to, $fraction ) - 返回位于起点经纬度和目标经纬度之间给定分数距离的经纬度。
- $from- ['lat' => 38.5, 'lng' => -120.2]
- $to- ['lat' => 38.5, 'lng' => -120.2]
- $fraction- 数字,移动距离的分数
返回数组
$response = \GeometryLibrary\SphericalUtil::interpolate(['lat' => 25.775, 'lng' => -80.190], ['lat' => 26.215, 'lng' => -81.218], 2); echo $response; /** array (size=2) 'lat' => float 26.647635362403 'lng' => float -82.253737943391 */
 computeDistanceBetween( $from, $to ) - 返回两个经纬度之间的距离,单位为米。你可以选择指定自定义半径。默认半径为地球半径。
- $from- ['lat' => 38.5, 'lng' => -120.2]
- $to- ['lat' => 38.5, 'lng' => -120.2]
返回浮点数
$response = \GeometryLibrary\SphericalUtil::computeDistanceBetween(['lat' => 25.775, 'lng' => -80.190], ['lat' => 26.215, 'lng' => -81.218]); echo $response; //float 113797.92421349
 computeLength( $path ) - 返回给定路径在地球上的长度,单位为米。
- $path- [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]
返回浮点数
$response = \GeometryLibrary\SphericalUtil::computeLength([ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]); echo $response; //float 788906.98459431
 computeArea( $path ) - 返回闭合路径的面积。
- $path- [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]
返回浮点数
$response = \GeometryLibrary\SphericalUtil::computeArea([ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]); echo $response; //float 44766785529.143
 computeSignedArea( $path ) - 返回闭合路径的符号面积。
- $path- [ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]
返回浮点数
$response = \GeometryLibrary\SphericalUtil::computeSignedArea([ ['lat' => 38.5, 'lng' => -120.2], ['lat' => 40.7, 'lng' => -120.95], ['lat' => 43.252, 'lng' => -126.453] ]); echo $response; //float 44766785529.143
支持
许可协议
Google Maps API V3的几何库在MIT许可下发布。有关详细信息,请参阅捆绑的LICENSE文件。