phpgeo

repository·main·Indexed 23 days ago

https://github.com/mjaschen/phpgeo

A PHP library providing high-precision geographical coordinate abstractions and distance calculations. It supports various ellipsoids (e.g., WGS-84) and geometry types including Point, Line, Polyline, and Polygon. Key features include Haversine and Vincenty distance calculations, bearing angles, geofencing, Ramer–Douglas–Peucker polyline simplification, and formatting for GeoJSON and DMS.

Tokens
20.4K
Snippets
53
Records
116
Agent score
81%

What's inside phpgeo

  1. Overview of phpgeo capabilities

    main

    phpgeo is a PHP library providing abstractions for geographical data and spatial calculations. It supports:

    • Geographical Coordinates: Includes support for different ellipsoids.
    • Polylines: Often referred to as "GPS Tracks".
    • Polygons: For representing enclosed areas.
    • Bounds: For defining geographical boundaries.

    Common use cases include calculating distances between coordinates and determining the length of tracks (polylines).

  2. Overview of phpgeo features

    main

    phpgeo provides abstractions for geographical coordinates and high-precision distance calculations. Key features include:

    • Geometry Abstractions: Support for Coordinate/Point, Line, Polyline/GPS track, and Polygon.
    • Ellipsoid Support: Support for different ellipsoids (e.g., WGS-84).
    • Calculations:
      • Length, distance, and perimeter (using Haversine or Vincenty implementations).
      • Bearing angles (spherical or Vincenty).
      • Destination points based on bearing and distance.
      • Perpendicular distance between a point and a line.
      • Cardinal Distances between two points.
    • Geofencing & Intersections: Check if a point is contained within an area or perform intersection checks between geometries.
    • Formatting: Output geometries as GeoJSON or human-readable strings (e.g., 18° 54′ 41″ -155° 40′ 42″).
    • Geometry Manipulation: Get segments of polylines/polygons or reverse their direction.
  3. Configure precision modes for Intersection::intersects()

    main

    The Intersection::intersects() method accepts a third boolean parameter to control the trade-off between performance and accuracy:

    • false (default): Fast mode. Only checks if the bounding boxes (bounds) of the geometries overlap. This is highly performant but may return true (false positives) if the bounds overlap even if the actual shapes do not.
    • true: Precise mode. Performs a detailed check by calculating segment intersections and checking for polygon containment. This is slower but provides accurate results for supported geometries.
  4. Understand the Bounds concept

    main

    In phpgeo, a Bounds object describes a geographic area defined by its north-eastern and south-western points.

    Most geometry classes (excluding the Coordinate class) implement the GetBoundsTrait, which provides a getBounds() method to retrieve the bounding area of that geometry. The Bounds class also includes a method to calculate its own center point, which is designed to handle edge cases like bounds crossing the dateline (180/-180 degrees longitude) correctly.

  5. Represent geographic locations with the Coordinate class

    main

    The Coordinate class is the fundamental building block of phpgeo. It represents a specific geographic location using three components:

    1. Latitude: A float representing geographic latitude, ranging from -90.0 to 90.0 degrees.
    2. Longitude: A float representing geographic longitude, ranging from -180.0 to 180.0 degrees.
    3. Ellipsoid: An instance of the Ellipsoid class representing the approximated shape of the Earth used for calculations.

    All geometric operations in the library rely on Coordinate objects as their base input.

  6. Compare Spherical vs Ellipsoidal earth models

    main

    When performing bearing and destination calculations, choose your model based on your requirements:

    ModelClassProsCons
    SphericalBearingSphericalVery fast performanceLower precision
    EllipsoidalBearingEllipsoidalHigh precisionSlightly more computation

    Note: In many practical scenarios, the performance difference between the two is negligible as the ellipsoidal iteration exit conditions are often met quickly.

  7. Understand the core geometry types in phpgeo

    main

    The phpgeo library provides several fundamental geometry classes to represent geographic data:

    • Coordinate: Represents a specific geographic location using latitude, longitude, and an associated Ellipsoid.
    • Line: A geometric object consisting of exactly two Coordinate points.
    • Polyline: A sequence of two or more Coordinate points.
    • Polygon: A closed geometric shape built from two or more Coordinate points.
  8. Run tests for phpgeo

    main

    You can run the unit tests using the provided Composer script, running PHPUnit directly, or using Docker to test against specific PHP versions.

    To run the standard test suite via Composer:

    composer ci:tests

    To run PHPUnit directly from the vendor directory:

    ./vendor/bin/phpunit

    To run tests against a specific PHP version (e.g., PHP 8.3) using Docker:

    docker run -it --rm --name phpgeo-phpunit \
        -v "$PWD":/usr/src/phpgeo \
        -w /usr/src/phpgeo php:8.3-cli \
        php vendor/bin/phpunit
  9. Upgrade phpgeo to 6.x

    main

    When upgrading to version 6.x, be aware of the following breaking changes:

    • GeometryLinesInterface: Line, Polygon, and Polyline classes now implement this interface, which provides the getSegments() method. No action is required unless you extend these classes.
    • GeometryInterface: A new getBounds() method has been added. If you implement GeometryInterface in your own classes, you must now implement getBounds().
    • PHP Support: Support for PHP 8.1 has been removed. You must use at least PHP 8.2.
    composer require mjaschen/phpgeo:^6.0
  10. Upgrade from phpgeo 3.x to 4.x

    main

    Upgrading to 4.x requires PHP 7.3 (with full support for PHP 8). This version introduces deprecations for setters in certain classes.

    ### Requirements
    - *phpgeo* 4.x requires at least PHP 7.3 and fully supports PHP 8
    
    ### Update phpgeo
    - run `composer require mjaschen/phpgeo:^4.0` or
    - update the version constraint in your `composer.json` to `^4.0` and run `composer update`