opensource-labs-gh/laravel-geo-utils
Composer 安装命令:
composer require opensource-labs-gh/laravel-geo-utils
包简介
A Laravel utility package for geographic polygon point checks with support for both PHP algorithms and MySQL spatial functions.
README 文档
README
A comprehensive Laravel utility package for geographic operations, including polygon point checks, distance calculations, and spatial data manipulation. This package provides both pure PHP implementations and MySQL spatial function integration.
Features
- 🎯 Point-in-Polygon Detection: Ray-casting algorithm implementation
- 🚀 Optimized Performance: Bounding box pre-filtering for faster checks
- 🗄️ MySQL Spatial Support: Leverage MySQL's built-in spatial functions
- 📏 Distance Calculations: Haversine formula for accurate distance measurement
- 🔄 WKT Conversion: Convert between array and Well-Known Text formats
- ⚡ Laravel Integration: Service provider, facade, and Artisan commands
- 🧪 Comprehensive Testing: Full test coverage with PHPUnit
- 🛠️ CLI Tools: Command-line testing and validation tools
Installation
Install the package via Composer:
composer require opensource-labs-gh/laravel-geo-utils
The package will automatically register its service provider and facade.
Publishing Configuration
Optionally, you can publish the configuration file:
php artisan vendor:publish --provider="OpensourceLabsGh\GeoUtils\GeoServiceProvider" --tag="geo-utils-config"
Usage
Basic Point-in-Polygon Check
use OpensourceLabsGh\GeoUtils\GeoHelper; // Or use the facade use OpensourceLabsGh\GeoUtils\Facades\GeoHelper; $polygon = [ ['lat' => 5.6037, 'lng' => -0.1870], ['lat' => 5.6037, 'lng' => -0.1700], ['lat' => 5.5800, 'lng' => -0.1700], ['lat' => 5.5800, 'lng' => -0.1870], ]; $point = ['lat' => 5.5919, 'lng' => -0.1785]; $isInside = GeoHelper::isPointInPolygon($polygon, $point); // Returns: true
Optimized Point-in-Polygon Check
For better performance with large polygons, use the optimized version that includes bounding box pre-filtering:
$isInside = GeoHelper::isPointInPolygonOptimized($polygon, $point);
MySQL Spatial Functions
Use MySQL's built-in spatial functions for potentially better performance:
$polygonWkt = GeoHelper::arrayToWktPolygon($polygon); $isInside = GeoHelper::isPointInPolygonMySQL($polygonWkt, 5.5919, -0.1785);
Distance Calculations
Calculate distances between two points using the Haversine formula:
$point1 = ['lat' => 37.7749, 'lng' => -122.4194]; // San Francisco $point2 = ['lat' => 40.7128, 'lng' => -74.0060]; // New York $distanceKm = GeoHelper::calculateDistance($point1, $point2, 'km'); $distanceMiles = GeoHelper::calculateDistance($point1, $point2, 'miles'); $distanceMeters = GeoHelper::calculateDistance($point1, $point2, 'meters'); // Results: // $distanceKm ≈ 4135.46 // $distanceMiles ≈ 2569.46 // $distanceMeters ≈ 4135458.97
Bounding Box Operations
Get the bounding box of a polygon:
$boundingBox = GeoHelper::getBoundingBox($polygon); // Returns: // [ // 'min_lat' => 5.5800, // 'max_lat' => 5.6037, // 'min_lng' => -0.1870, // 'max_lng' => -0.1700 // ] // Quick check if point is within bounding box $isInBounds = GeoHelper::isPointInBoundingBox($boundingBox, $point);
WKT Conversion
Convert array coordinates to Well-Known Text format:
$wktPolygon = GeoHelper::arrayToWktPolygon($polygon); // Returns: "POLYGON((-0.187000 5.603700, -0.170000 5.603700, -0.170000 5.580000, -0.187000 5.580000, -0.187000 5.603700))"
Artisan Commands
The package includes a command-line tool for testing and validation:
Basic Point-in-Polygon Test
php artisan geo-utils:test
Custom Polygon and Point Test
php artisan geo-utils:test \ --polygon='[{"lat":5.6037,"lng":-0.1870},{"lat":5.6037,"lng":-0.1700},{"lat":5.5800,"lng":-0.1700},{"lat":5.5800,"lng":-0.1870}]' \ --point='{"lat":5.5919,"lng":-0.1785}'
Distance Calculation Test
php artisan geo-utils:test --distance
Bounding Box Test
php artisan geo-utils:test --bbox
Configuration
The package includes a configuration file with the following options:
return [ 'default_distance_unit' => 'km', 'mysql_spatial_enabled' => true, 'coordinate_precision' => 6, 'validation' => [ 'strict_coordinates' => true, 'min_polygon_points' => 3, ], 'performance' => [ 'use_bounding_box_optimization' => true, 'cache_bounding_boxes' => false, ], ];
API Reference
GeoHelper Methods
| Method | Parameters | Return | Description |
|---|---|---|---|
isPointInPolygon() |
array $polygon, array $point |
bool |
Check if point is inside polygon using ray-casting |
isPointInPolygonOptimized() |
array $polygon, array $point |
bool |
Optimized point-in-polygon with bounding box pre-check |
isPointInPolygonMySQL() |
string $polygonWkt, float $lat, float $lng |
bool |
MySQL spatial function point-in-polygon check |
calculateDistance() |
array $point1, array $point2, string $unit |
float |
Calculate distance between two points |
getBoundingBox() |
array $polygon |
array |
Get polygon bounding box |
isPointInBoundingBox() |
array $boundingBox, array $point |
bool |
Check if point is in bounding box |
arrayToWktPolygon() |
array $polygon |
string |
Convert array coordinates to WKT format |
Data Formats
Point Format:
['lat' => float, 'lng' => float]
Polygon Format:
[
['lat' => float, 'lng' => float],
['lat' => float, 'lng' => float],
['lat' => float, 'lng' => float],
// ... minimum 3 points required
]
Bounding Box Format:
[
'min_lat' => float,
'max_lat' => float,
'min_lng' => float,
'max_lng' => float
]
Performance Considerations
-
Bounding Box Optimization: For large polygons, use
isPointInPolygonOptimized()which performs a quick bounding box check before the full polygon test. -
MySQL Spatial Functions: When working with large datasets, consider using
isPointInPolygonMySQL()which leverages MySQL's optimized spatial functions. -
Coordinate Precision: Adjust
coordinate_precisionin config based on your needs - higher precision means more accuracy but potentially slower performance.
Error Handling
The package validates input data and throws InvalidArgumentException for:
- Polygons with fewer than 3 points
- Invalid coordinate values (latitude not between -90 and 90, longitude not between -180 and 180)
- Missing required array keys ('lat', 'lng')
- Invalid distance units
- Empty WKT strings
Testing
Run the package tests:
composer test
Or run tests with coverage:
composer test-coverage
Contributing
Please see CONTRIBUTING.md for details on how to contribute to this project.
Security
If you discover any security related issues, please email mawulikofiagbenyo@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Changelog
Please see CHANGELOG.md for more information about recent changes.
Support
- 📧 Email: mawulikofiagbenyo@gmail.com
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
Made with ❤️ by Opensource Labs Ghana
opensource-labs-gh/laravel-geo-utils 适用场景与选型建议
opensource-labs-gh/laravel-geo-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「geo」 「geometry」 「laravel」 「utilities」 「coordinates」 「gis」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 opensource-labs-gh/laravel-geo-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 opensource-labs-gh/laravel-geo-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 opensource-labs-gh/laravel-geo-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Show geo marker on a mapbox map
Show geo shape on a mapbox map with drawing capabilities
Geo-related tools PHP 7.3+ library
PHP Countries and Currencies
Add a weather widget to Flarum
Laravel Geocode by address library
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-19