{"id":13700167,"url":"https://github.com/php-geospatial/geospatial","last_synced_at":"2025-05-04T18:34:31.038Z","repository":{"id":4919267,"uuid":"6075685","full_name":"php-geospatial/geospatial","owner":"php-geospatial","description":"PHP Extension to handle common geospatial functions.","archived":false,"fork":false,"pushed_at":"2024-10-03T14:04:23.000Z","size":177,"stargazers_count":61,"open_issues_count":2,"forks_count":18,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-11-13T06:33:11.555Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-geospatial.png","metadata":{"files":{"readme":"README.rst","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":"2012-10-04T13:28:46.000Z","updated_at":"2024-10-03T14:04:28.000Z","dependencies_parsed_at":"2024-03-05T16:45:50.588Z","dependency_job_id":"c24ae5c6-e2be-46e7-b202-45c489e18459","html_url":"https://github.com/php-geospatial/geospatial","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-geospatial%2Fgeospatial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-geospatial%2Fgeospatial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-geospatial%2Fgeospatial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-geospatial%2Fgeospatial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-geospatial","download_url":"https://codeload.github.com/php-geospatial/geospatial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252383031,"owners_count":21739261,"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":[],"created_at":"2024-08-02T20:00:49.248Z","updated_at":"2025-05-04T18:34:25.993Z","avatar_url":"https://github.com/php-geospatial.png","language":"C","funding_links":[],"categories":["Geospatial Library","地理","PHP"],"sub_categories":["PHP"],"readme":"=====================================\ngeospatial - PHP Geospatial Extension\n=====================================\n.. image:: https://travis-ci.org/php-geospatial/geospatial.svg?branch=master\n    :target: https://travis-ci.org/php-geospatial/geospatial\n.. image:: https://codecov.io/gh/php-geospatial/geospatial/branch/master/graphs/badge.svg?branch=master\n    :target: https://codecov.io/github/php-geospatial/geospatial?branch=master\n\nPHP Extension to handle common geospatial functions. The extension currently\nhas implementations of the Haversine and Vincenty's formulas for calculating\ndistances, an initial bearing calculation function, a Helmert transformation\nfunction to transfer between different supported datums, conversions between\npolar and Cartesian coordinates, conversions between Degree/Minute/Seconds and\ndecimal degrees, a method to simplify linear geometries, as well as a method\nto calculate intermediate points on a LineString.\n\nInstalation\n===========\n\n::\n\n    git clone git@github.com:php-geospatial/geospatial.git\n    cd geospatial\n    phpize\n    ./configure --enable-geospatial\n    make\n    sudo make install\n\nThen add the extension to an ini file e.g. /etc/php.ini::\n\n    extension = geospatial.so\n\nUsage\n=====\n\nThe extension makes use of the GeoJSON standard format for specifying points as\nco-ordinates. One important thing to note about this format is that points are\nspecified longitude **first** i.e. longitude, latitude.\n\ne.g.::\n\n    $greenwichObservatory = array(\n        'type' =\u003e 'Point',\n        'coordinates' =\u003e array( -0.001483 , 51.477917);\n    );\n\n\nHaversine\n---------\n\n::\n\n    $from = array(\n        'type' =\u003e 'Point',\n        'coordinates' =\u003e array( -104.88544, 39.06546 )\n    );\n    $to = array(\n        'type' =\u003e 'Point',\n        'coordinates' =\u003e array( -104.80, 39.06546 )\n    );\n    var_dump(haversine($to, $from));\n    \n\nVincenty's Formula\n------------------\n\nVincenty's formula attempts to provide a more accurate distance between two\npoints than the Haversine formula. Whereas the Haversine formula assumes a\nspherical earth the Vincenty method models the earth as an ellipsoid::\n\n    $flinders = array(\n        'type' =\u003e 'Point',\n        'coordinates' =\u003e array(144.42486788889, -37.951033416667 )\n    );\n    $buninyong = array(\n        'type' =\u003e 'Point',\n        'coordinates' =\u003e array(143.92649552778, -37.652821138889 )\n    );\n    var_dump(vincenty($flinders, $buninyong));\n\n\nHelmert Transformation\n----------------------\n\nThe Helmert transformation allows for the transformation of points between\ndifferent datums. It can for instance be used to convert between the WGS84\nellipsoid (GEO_WGS84) used by GPS systems and OSGB36 (GEO_AIRY_1830) used by\nOrdnance Survey in the UK::\n\n    $greenwichObservatory = array(\n        'type' =\u003e 'Point',\n        'coordinates' =\u003e array(-0.0014833333333333 , 51.477916666667)\n    );\n\n    $greenwichObservatoryWGS84 = transform_datum($greenwichObservatory, GEO_WGS84, GEO_AIRY_1830);\n\n    var_dump($greenwichObservatoryWGS84);\n\nCalculating Initial Bearing\n---------------------------\n\nThe ``initial_bearing`` function calculates the initial bearing to go from the\nfirst to the second point, as expressed in a GeoJSON wrapper::\n\n\t$from = array(\n\t\t'type' =\u003e 'Point',\n\t\t'coordinates' =\u003e array( 2.351, 48.857 )\n\t);\n\t$to = array(\n\t\t'type' =\u003e 'Point',\n\t\t'coordinates' =\u003e array( 0.119, 52.205 )\n\t);\n\tvar_dump(initial_bearing($from, $to));\n\nThe range of the resulting heading is 0° to +360°.\n\nConverting between polar and Cartesian Coordinates\n--------------------------------------------------\n\nThese two functions calculate between Polar and Cartesian Coordinates,\nwith results depending on which ellipsoid you use.\n\nFrom Polar to Cartesian::\n\n\t$lat = 53.38361111111;\n\t$long = 1.4669444444;\n\n\tvar_dump(polar_to_cartesian($lat, $long, GEO_AIRY_1830));\n\nAnd back::\n\n\t$x = 3810891.6734396;\n\t$y = 97591.624686311;\n\t$z = 5095766.3939034;\n\n\t$polar = cartesian_to_polar($x, $y, $z, GEO_AIRY_1830);\n\techo round($polar['lat'], 6), PHP_EOL;\n\techo round($polar['long'], 6), PHP_EOL;\n\techo round($polar['height'], 3), PHP_EOL;\n\nConverting between Degree/Min/Sec and Decimal coordinates\n---------------------------------------------------------\n\nFrom decimal to dms. The second argument is either \"longitude\" or \"latitude\"::\n\n\t$dms = decimal_to_dms(-1.034291666667, 'longitude');\n\tvar_dump($dms);\n\nWhich outputs::\n\n\tarray(4) {\n\t  [\"degrees\"]=\u003e int(1)\n\t  [\"minutes\"]=\u003e int(2)\n\t  [\"seconds\"]=\u003e float(3.4500000011994)\n\t  [\"direction\"]=\u003e string(1) \"W\"\n\t}\n\nAnd back from DMS to decimal, where the fourth argument is either \"N\", \"S\",\n\"E\", or \"W\"::\n\n\t$decimal = dms_to_decimal(0, 6, 9, 'S');\n\nWhich outputs::\n\n\tfloat(-0.1025)\n\nSimplifying LineStrings\n-----------------------\n\nThe ``rdp_simplify``  method implements RDP_ to simplify a LineString\naccording to a certain accuracy (epsilon). As first argument it takes a\nGeoJSON LineString (in PHP variable format), and it outputs a similar\nstructure but then simplified\n\n.. _RDP: https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm\n\nInterpolation along a Greater Circle Line\n-----------------------------------------\n\nThe ``fraction_along_gc_line`` function can be used to calculate intermediate\npoints along a Greater Circle Line. For example if you need to draw lines with\nmore accuracy with for example Leaflet. The function takes the start and end\ncoordinates (as GeoJson Point), and calculates the intermediate point along\nthose line. To calculate the point 25% from the start point to the end point,\nyou would use::\n\n\t$point1 = [ 'type' =\u003e 'Point', 'coordinates' =\u003e [  5, 10 ] ];\n\t$point2 = [ 'type' =\u003e 'Point', 'coordinates' =\u003e [ 15, 10 ] ];\n\n\tvar_dump(fraction_along_gc_line($point1, $point2, 0.25));\n\nInterpolating a GeoJSONLineString\n---------------------------------\n\nThe ``interpolate_linestring`` functions takes a GeoJSONLineString. If the\nPythagorean distance in degrees between two points in the line than the\n``epsilon`` value, it inserts a new point every ``epsilon / distance``\nfraction of the Great Circle Line.\n\nGiven the Linestring::\n\n\t$lineString = [\n\t\t'type' =\u003e 'Linestring',\n\t\t'coordinates' =\u003e [\n\t\t\t[  5,  10 ],\n\t\t\t[ 15,  10 ],\n\t\t\t[  0, -50 ],\n\t\t]\n\t];\n\nThe following will return an array with 26 elements::\n\n\tvar_dump(interpolate_linestring($lineString, 3));\n\nFive for the first pair, at fractions ``0.0``, ``0.3``, ``0.6``, ``0.9`` and\n``1.0``, and then two times, each another ``0.0485`` fraction along the Great\nCircle Line for the second pair.\n\nGeohashing\n----------\n\nThe `geohash_encode` function can be used to convert GeoJSON Point to a\ngeohash of a specific length (in this case, 12)::\n\n\t$point = [ 'type' =\u003e 'Point', 'coordinates' =\u003e [ 16.4, 48.2 ] ];\n\techo geohash_encode( $point, 12 );\n\nWhich outputs::\n    \n\tu2edjnw17enr\n\nSimilarly, a hashed coordinates pair can be decoded using `geohash_decode`\nfunction::\n\n\tvar_dump(geohash_decode('u2edjnw17enr'));\n\tarray(2) {\n\t  [\"type\"]=\u003e\n\t  string(5) \"Point\"\n\t  [\"coordinates\"]=\u003e\n\t  array(2) {\n\t\t[0]=\u003e\n\t\tfloat(16.40000006184)\n\t\t[1]=\u003e\n\t\tfloat(48.199999993667)\n\t  }\n\t}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-geospatial%2Fgeospatial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-geospatial%2Fgeospatial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-geospatial%2Fgeospatial/lists"}