{"id":16749624,"url":"https://github.com/alexpechkarev/geometry-library","last_synced_at":"2025-04-13T00:42:11.417Z","repository":{"id":49724750,"uuid":"42305250","full_name":"alexpechkarev/geometry-library","owner":"alexpechkarev","description":"PHP Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth. Code ported from Google Maps Android API.","archived":false,"fork":false,"pushed_at":"2024-04-11T17:18:58.000Z","size":91,"stargazers_count":141,"open_issues_count":0,"forks_count":35,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-13T00:41:51.951Z","etag":null,"topics":["distance","geometry-library","google-maps-api","latlng","php","polyline"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexpechkarev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-11T11:42:16.000Z","updated_at":"2025-04-02T06:43:55.000Z","dependencies_parsed_at":"2024-06-18T12:37:58.474Z","dependency_job_id":null,"html_url":"https://github.com/alexpechkarev/geometry-library","commit_stats":{"total_commits":41,"total_committers":8,"mean_commits":5.125,"dds":0.3414634146341463,"last_synced_commit":"35839ed841805c8a0bc2fd8e4d5b5f600cb1416a"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpechkarev%2Fgeometry-library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpechkarev%2Fgeometry-library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpechkarev%2Fgeometry-library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpechkarev%2Fgeometry-library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexpechkarev","download_url":"https://codeload.github.com/alexpechkarev/geometry-library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650417,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distance","geometry-library","google-maps-api","latlng","php","polyline"],"created_at":"2024-10-13T02:25:25.619Z","updated_at":"2025-04-13T00:42:11.390Z","avatar_url":"https://github.com/alexpechkarev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Geometry Library Google Maps API V3 \nPHP Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth. Code ported from Google [Maps Android API](https://github.com/googlemaps/android-maps-utils/).\n\n\nFeatures\n------------\n* [Spherical](https://developers.google.com/maps/documentation/javascript/reference#spherical) contains spherical geometry utilities allowing you to compute angles, distances and areas from latitudes and longitudes.\n* [Poly](https://developers.google.com/maps/documentation/javascript/reference#poly) utility functions for computations involving polygons and polylines.\n* [Encoding](https://developers.google.com/maps/documentation/javascript/reference#encoding) utilities for polyline encoding and decoding.\n\nDependency\n------------\n* [PHP 5](http://php.net/)\n\n\n\nInstallation\n------------\n\nIssue following command:\n\n```php\ncomposer require alexpechkarev/geometry-library:1.0.5\n```\n\nAlternatively  edit composer.json by adding following line and run **`composer update`**\n```php\n\"require\": { \n\t\t....,\n\t\t\"alexpechkarev/geometry-library\":\"1.0.5\",\n\t\n\t},\n```\n\nUsage\n------------\n\nHere is an example of using GeometryLibrary:\n```php\n$response =  \\GeometryLibrary\\SphericalUtil::computeHeading(\n                ['lat' =\u003e 25.775, 'lng' =\u003e -80.190], // from array [lat, lng]\n                ['lat' =\u003e 21.774, 'lng' =\u003e -80.190]); // to array [lat, lng]\n  echo $response; // -180\n  \n$response = \\GeometryLibrary\\SphericalUtil::computeDistanceBetween(\n              ['lat' =\u003e 25.775, 'lng' =\u003e -80.190], //from array [lat, lng]\n              ['lat' =\u003e 21.774, 'lng' =\u003e -80.190]); // to array [lat, lng]\n              \n  echo $response; // 444891.52998049          \n  \n  \n$response =  \\GeometryLibrary\\PolyUtil::isLocationOnEdge(\n              ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], // point array [lat, lng]\n              [ // poligon arrays of [lat, lng]\n                ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], \n                ['lat' =\u003e 18.466, 'lng' =\u003e -66.118], \n                ['lat' =\u003e 32.321, 'lng' =\u003e -64.757]\n              ])  ;\n              \n  echo $response; // true\n  \n  \n  \n$response =  \\GeometryLibrary\\PolyUtil::isLocationOnPath(\n              ['lat' =\u003e 25.771, 'lng' =\u003e -80.190], // point array [lat, lng]\n             [ // poligon arrays of [lat, lng]\n              ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], \n              ['lat' =\u003e 18.466, 'lng' =\u003e -66.118], \n              ['lat' =\u003e 32.321, 'lng' =\u003e -64.757]\n             ]);  \n             \n  echo $response; // false  \n  \n$response =  \\GeometryLibrary\\PolyUtil::containsLocation(\n              ['lat' =\u003e 23.886, 'lng' =\u003e -65.269], // point array [lat, lng]\n             [ // poligon arrays of [lat, lng]\n                ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], \n                ['lat' =\u003e 18.466, 'lng' =\u003e -66.118], \n                ['lat' =\u003e 32.321, 'lng' =\u003e -64.757]\n             ]);  \n             \n  echo $response; // false    \n  \n$response =  \\GeometryLibrary\\PolyUtil::distanceToLine(\n              ['lat' =\u003e 61.387002, 'lng' =\u003e 23.890636], // point array [lat, lng]\n              ['lat' =\u003e 61.487002, 'lng' =\u003e 23.790636], // line startpoint array [lat, lng]\n              ['lat' =\u003e 60.48047, 'lng' =\u003e 22.052754] // line endpoint array [lat, lng]\n             );  \n             \n  echo $response; // 12325.124046196 in meters\n  \n$response =  \\GeometryLibrary\\PolyUtil::encode(\n              [ \n                ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], \n                ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], \n                ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]\n              ]);\n              \n  echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'\n  \n  \n$response =  \\GeometryLibrary\\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@');  \n\n  echo $response; /** array (size=3)\n                        0 =\u003e \n                          array (size=2)\n                            'lat' =\u003e float 38.5\n                            'lng' =\u003e float -120.2\n                        1 =\u003e \n                          array (size=2)\n                            'lat' =\u003e float 40.7\n                            'lng' =\u003e float -120.95\n                        2 =\u003e \n                          array (size=2)\n                            'lat' =\u003e float 43.252\n                            'lng' =\u003e float -126.453\n                   */\n  \n```\n\nAvailable methods\n------------\n## PolyUtil class\n* [`containsLocation($point, $polygon, $geodesic = false)`](#containsLocation)\n* [`isLocationOnEdge($point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true)`](#isLocationOnEdge)\n* [`isLocationOnPath($point, $polyline, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true)`](#isLocationOnPath)\n* [`distanceToLine($p, $start, $end)`](#distanceToLine)\n* [`decode($encodedPath)`](#decode)\n* [`encode($path)`](#encode)\n\n## SphericalUtil class\n* [`computeHeading($from, $to)`](#computeHeading)\n* [`computeOffset($from, $distance, $heading)`](#computeOffset)\n* [`computeOffsetOrigin($to, $distance,  $heading)`](#computeOffsetOrigin)\n* [`interpolate($from, $to, $fraction)`](#interpolate)\n* [`computeDistanceBetween( $from, $to)`](#computeDistanceBetween)\n* [`computeLength($path)`](#computeLength)\n* [`computeArea($path)`](#computeArea)\n* [`computeSignedArea($path)`](#computeSignedArea)\n\n\n---\n\n\u003ca name=\"containsLocation\"\u003e\u003c/a\u003e\n**`containsLocation( $point, $polygon, $geodesic = false )`** - To find whether a given point falls within a polygon\n\n* `$point` -  ['lat' =\u003e 38.5, 'lng' =\u003e -120.2 ]\n* `$polygon` - [ ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]]\n* `$geodesic` - boolean\n\nReturns boolean\n\n```php\n\n$response =  \\GeometryLibrary\\PolyUtil::containsLocation(\n              ['lat' =\u003e 23.886, 'lng' =\u003e -65.269], // point array [lat, lng]\n             [ // poligon arrays of [lat, lng]\n                ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], \n                ['lat' =\u003e 18.466, 'lng' =\u003e -66.118], \n                ['lat' =\u003e 32.321, 'lng' =\u003e -64.757]\n             ]);  \n             \n  echo $response; // false\n\n```\n---\n\n\n\u003ca name=\"isLocationOnEdge\"\u003e\u003c/a\u003e\n**`isLocationOnEdge( $point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true )`** - To determine whether a point falls on or near a polyline, or on or near the edge of a polygon, within a specified tolerance in meters.\n\n* `$point` -  ['lat' =\u003e 25.774, 'lng' =\u003e -80.190 ]\n* `$polygon` -  [ ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]]\n* `$tolerance` -  tolerance value in degrees\n* `$geodesic` - boolean\n\nReturns boolean\n\n```php\n\n$response =  \\GeometryLibrary\\PolyUtil::isLocationOnEdge(\n              ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], // point array [lat, lng]\n              [ // poligon arrays of [lat, lng]\n                ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], \n                ['lat' =\u003e 18.466, 'lng' =\u003e -66.118], \n                ['lat' =\u003e 32.321, 'lng' =\u003e -64.757]\n              ])  ;\n              \n  echo $response; // true\n\n```\n---\n\n\u003ca name=\"isLocationOnPath\"\u003e\u003c/a\u003e\n**`isLocationOnPath( $point, $polygon, $tolerance = self::DEFAULT_TOLERANCE, $geodesic = true )`** - To determine whether a point falls on or near a polyline, within a specified tolerance in meters\n\n* `$point` -  ['lat' =\u003e 25.774, 'lng' =\u003e -80.190 ]\n* `$polygon` -  [ ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]]\n* `$tolerance` -  tolerance value in degrees\n* `$geodesic` - boolean\n\nReturns boolean\n\n```php\n\n$response =  \\GeometryLibrary\\PolyUtil::isLocationOnPath(\n              ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], // point array [lat, lng]\n              [ // poligon arrays of [lat, lng]\n                ['lat' =\u003e 25.774, 'lng' =\u003e -80.190], \n                ['lat' =\u003e 18.466, 'lng' =\u003e -66.118], \n                ['lat' =\u003e 32.321, 'lng' =\u003e -64.757]\n              ])  ;\n              \n  echo $response; // true\n\n```\n---\n\n\u003ca name=\"distanceToLine\"\u003e\u003c/a\u003e\n**`distanceToLine( $p, $start, $end )`** - To calculate distance from a point to line start-\u003eend on sphere.\n\n* `$p` -  ['lat' =\u003e 61.387002, 'lng' =\u003e 23.890636]\n* `$start` -  ['lat' =\u003e 61.487002, 'lng' =\u003e 23.790636]\n* `$end` -  ['lat' =\u003e 60.48047, 'lng' =\u003e 22.052754]\n\nReturns distance from a point to line\n\n```php\n\n$response =  \\GeometryLibrary\\PolyUtil::distanceToLine(\n              ['lat' =\u003e 61.387002, 'lng' =\u003e 23.890636], // point array [lat, lng]\n              ['lat' =\u003e 61.487002, 'lng' =\u003e 23.790636], // line startpoint array [lat, lng]\n              ['lat' =\u003e 60.48047, 'lng' =\u003e 22.052754] // line endpoint array [lat, lng]\n             );  \n             \n  echo $response; // 12325.124046196 in meters\n\n```\n---\n\n\u003ca name=\"decode\"\u003e\u003c/a\u003e\n**`decode( $encodedPath )`** - Decodes an encoded path string into a sequence of LatLngs.\n\n* `$encodedPath` - string '_p~iF~ps|U_ulLnnqC_mqNvxq`@'\n\nReturns array\n\n```php\n\n$response =  \\GeometryLibrary\\PolyUtil::decode('_p~iF~ps|U_ulLnnqC_mqNvxq`@');  \n\n  echo $response; /** array (size=3)\n                        0 =\u003e \n                          array (size=2)\n                            'lat' =\u003e float 38.5\n                            'lng' =\u003e float -120.2\n                        1 =\u003e \n                          array (size=2)\n                            'lat' =\u003e float 40.7\n                            'lng' =\u003e float -120.95\n                        2 =\u003e \n                          array (size=2)\n                            'lat' =\u003e float 43.252\n                            'lng' =\u003e float -126.453\n                   */\n\n```\n---\n\n\u003ca name=\"encode\"\u003e\u003c/a\u003e\n**`encode( $path )`** - Encodes a sequence of LatLngs into an encoded path string.\n\n* `$path` -  [ ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], ['lat' =\u003e 43.252, 'lng' =\u003e -126.453] ]\n\nReturns string\n\n```php\n\n$response =  \\GeometryLibrary\\PolyUtil::encode(\n              [ \n                ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], \n                ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], \n                ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]\n              ]);\n              \n  echo $response; // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'\n\n```\n---\n\n\u003ca name=\"computeHeading\"\u003e\u003c/a\u003e\n**`computeHeading( $from, $to )`** - Returns the heading from one LatLng to another LatLng.\n\n* `$from` -  ['lat' =\u003e 38.5, 'lng' =\u003e -120.2]\n* `$to` -  ['lat' =\u003e 40.7, 'lng' =\u003e -120.95]\n\nReturns int\n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::computeHeading(\n              ['lat' =\u003e 25.775, 'lng' =\u003e -80.190], \n              ['lat' =\u003e 21.774, 'lng' =\u003e -80.190]));\n              \n  echo $response; // -180\n\n```\n---\n\n\u003ca name=\"computeOffset\"\u003e\u003c/a\u003e\n**`computeOffset( $from, $distance, $heading )`** - Returns the LatLng resulting from moving a distance from an origin in the specified heading.\n\n* `$from` -  ['lat' =\u003e 38.5, 'lng' =\u003e -120.2]\n* `$distance` - number, the distance to travel\n* `$heading` - number, the heading in degrees clockwise from north\n\nReturns array\n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::computeOffset(['lat' =\u003e 25.775, 'lng' =\u003e -80.190], 152, 120);\n              \n  echo $response; /** array (size=2)\n                      'lat' =\u003e float 25.774316510639\n                      'lng' =\u003e float -80.188685385944\n                  */\n\n```\n---\n\n\u003ca name=\"computeOffsetOrigin\"\u003e\u003c/a\u003e\n**`computeOffsetOrigin( $from, $distance, $heading )`** - Returns the location of origin when provided with a LatLng destination, meters travelled and original heading. Headings are expressed in degrees clockwise from North. \n\n* `$from` -  ['lat' =\u003e 38.5, 'lng' =\u003e -120.2]\n* `$distance` - number, the distance to travel\n* `$heading` - number, the heading in degrees clockwise from north\n\nReturns array \n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::computeOffsetOrigin(['lat' =\u003e 25.775, 'lng' =\u003e -80.190], 152, 120);\n              \n  echo $response; /** array (size=2)\n                        'lat' =\u003e float 14.33435503928\n                        'lng' =\u003e float -263248.24242931\n                  */\n\n```\n---\n\n\u003ca name=\"interpolate\"\u003e\u003c/a\u003e\n**`interpolate( $from, $to, $fraction )`** - Returns the LatLng which lies the given fraction of the way between the origin LatLng and the destination LatLng.\n\n* `$from` -  ['lat' =\u003e 38.5, 'lng' =\u003e -120.2]\n* `$to` -  ['lat' =\u003e 38.5, 'lng' =\u003e -120.2]\n* `$fraction` - number, a fraction of the distance to travel\n\nReturns array\n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::interpolate(['lat' =\u003e 25.775, 'lng' =\u003e -80.190], \n                                                          ['lat' =\u003e 26.215, 'lng' =\u003e -81.218], 2);\n              \n  echo $response; /** array (size=2)\n                      'lat' =\u003e float 26.647635362403\n                      'lng' =\u003e float -82.253737943391\n                  */\n\n```\n---\n\n\u003ca name=\"computeDistanceBetween\"\u003e\u003c/a\u003e\n**`computeDistanceBetween( $from, $to )`** - Returns the distance, in meters, between two LatLngs. You can optionally specify a custom radius. The radius defaults to the radius of the Earth.\n\n* `$from` - ['lat' =\u003e 38.5, 'lng' =\u003e -120.2]\n* `$to` - ['lat' =\u003e 38.5, 'lng' =\u003e -120.2]\n\nReturns float\n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::computeDistanceBetween(['lat' =\u003e 25.775, 'lng' =\u003e -80.190], ['lat' =\u003e 26.215, 'lng' =\u003e -81.218]);\n              \n  echo $response; //float 113797.92421349\n\n```\n---\n\n\u003ca name=\"computeLength\"\u003e\u003c/a\u003e\n**`computeLength( $path )`** - Returns the length of the given path, in meters, on Earth.\n\n* `$path` - [ ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], ['lat' =\u003e 43.252, 'lng' =\u003e -126.453] ]\n\nReturns float\n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::computeLength([ \n                ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], \n                ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], \n                ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]\n              ]);\n              \n  echo $response; //float 788906.98459431\n\n```\n---\n\n\u003ca name=\"computeArea\"\u003e\u003c/a\u003e\n**`computeArea( $path )`** - Returns the area of a closed path.\n\n* `$path` - [ ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], ['lat' =\u003e 43.252, 'lng' =\u003e -126.453] ]\n\nReturns float\n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::computeArea([ \n                ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], \n                ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], \n                ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]\n              ]);\n              \n  echo $response; //float 44766785529.143\n\n```\n---\n\n\u003ca name=\"computeSignedArea\"\u003e\u003c/a\u003e\n**`computeSignedArea( $path )`** - Returns the signed area of a closed path.\n\n* `$path` - [ ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], ['lat' =\u003e 43.252, 'lng' =\u003e -126.453] ]\n\nReturns float\n\n```php\n\n$response =  \\GeometryLibrary\\SphericalUtil::computeSignedArea([ \n                ['lat' =\u003e 38.5, 'lng' =\u003e -120.2], \n                ['lat' =\u003e 40.7, 'lng' =\u003e -120.95], \n                ['lat' =\u003e 43.252, 'lng' =\u003e -126.453]\n              ]);\n              \n  echo $response; //float 44766785529.143\n\n```\n---\n\nSupport\n-------\n\n[Please open an issue on GitHub](https://github.com/alexpechkarev/geometry-library/issues)\n\n\nLicense\n-------\n\nGeometry Library Google Maps API V3 is released under the MIT License. See the bundled\n[LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE)\nfile for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpechkarev%2Fgeometry-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexpechkarev%2Fgeometry-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpechkarev%2Fgeometry-library/lists"}