mjaschen/phpgeo
PHPGeo is a lightweight geospatial library for PHP. Model geographic coordinates (with ellipsoid support) and compute high‑precision distances and related calculations between points. Compatible with modern PHP versions (8.2+ for latest).
Point objects) and integrates with packages like spatie/laravel-geotools for database-backed geospatial queries.Coordinate and Line objects reduce side effects but may require refactoring existing mutable code (e.g., Eloquent attribute mutators).GeometryLinesInterface). Risk mitigated by:
^6.0 and audit usage of Line, Polygon, or Polyline classes.getBounds() or getSegments() methods.Haversine calculator for performance.Polygon with 10K points).GIST).NaN coordinates, empty polylines).location column or dedicated coordinates table.mapbox-gl, leaflet).geospatial-service calculating distances for multiple apps).phpgeo in a single feature (e.g., "Delivery Route Optimizer").Coordinate objects in models/services with phpgeo\Coordinate.Vincenty or Haversine calculators.Polygon::contains() for "exclusive zone" checks).distance_meters).geometry column (PostgreSQL geometry type preferred).composer.json to align with phpgeo’s active support.spatie/laravel-permission).composer require mjaschen/phpgeo.use Location\Coordinate).Vincenty::getDistance().getDistanceTo()).isWithinPolygon()).phpgeo for breaking changes (e.g., via GitHub watch or Dependabot).^6.0) to avoid surprises.phpunit to test Eloquent model interactions.phpgeo usage patterns (e.g., "How to calculate distances in the Delivery module").phpgeo’s detailed error messages (e.g., invalid coordinates).Coordinate cannot be modified after creation).users_geo_index).CLUSTER on geometry columns).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Invalid coordinate input (e.g., NaN) | Silent failures or incorrect results | Validate inputs with isFinite() or custom validators. |
| Database spatial index missing | Slow geofence queries | Ensure PostgreSQL GIST index on geometry columns. |
| Vincenty calculation timeout | API timeouts | Fallback to Haversine for approximate results or use async processing. |
| Cross-dateline polygon | False geofence results | Reject or auto-split polygons crossing 180°/-180°. |
| High memory usage (large polygons) | Worker crashes | Limit polygon point count or use simplification algorithms. |
phpgeo cheat sheet (e.g., "5 Common Use Cases").phpgeo-specific linting (e.g., Psalm rules forHow can I help you explore Laravel packages today?