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.
Requires
- php: >=8.1
- illuminate/database: ^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
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_precision
in 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