PHP Classes

PHP Geocode Address Formatter: Parse a location string and returns its details

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 247 All time: 7,940 This week: 104Up
Version License PHP version Categories
standard_geocode 1.0.0Public Domain7PHP 5, Text processing, Geography, Pa...
Description 

Author

This class can parse a location string and returns its details.

It takes a string with the address of a given location in many different formats that are supported and returns an object with many details of the location.

The object contains the latitude and longitude coordinates as decimal values or other formats like dms, dmd, dd and Open Location Plus Code.

Innovation Award
PHP Programming Innovation award winner
January 2019
Winner


Prize: One subscription to the PDF edition of the PHP Architect magazine
The location of one place in the world may be expressed in text using many different formats.

When you want to develop an application that allows your users to enter a location in all the selected formats, you need support whatever format the user may want to enter the location in text.

The class can parse a location string in many formats, thus providing flexibility for applications that need to processed locations entered manually by their users.

Manuel Lemos
Picture of Ray Paseur
  Performance   Level  
Name: Ray Paseur is available for providing paid consulting. Contact Ray Paseur .
Classes: 8 packages by
Country: United States United States
Age: 73
All time rank: 2240311 in United States United States
Week rank: 131 Up15 in United States United States Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Example

<?php // classes/demo_Geocode.php
/**
 * Class Geocode Demonstration Script
 */
error_reporting(E_ALL);

// Load the Class
require_once('class_Geocode.php');

// Make sure that php works with UTF-8
mb_internal_encoding('utf-8');
mb_regex_encoding('utf-8');
mb_http_output('utf-8');


// Test Objects
Class TO
{
    public
$name, $geo;
    public function
__construct($name, $geo) {
       
$this->name = $name;
       
$this->geo = $geo;
    }
}


// A collection of curated examples, giving Geocode information in widely varying formats
$testdata=[];

// GOOD examples
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '40.4401766,-79.9526167' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '40.4401766N,79.9526167W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026.4106\'N,7957.157\'W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026\'24.636"N,7957\'9.42"W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&apos;24.636&quot;N,7957&apos;9.42&quot;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&prime;24.636&Prime;N,7957&prime;9.42&Prime;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&rsquo;24.636&rdquo;N,7957&lsquo;9.42&ldquo;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&#8217;24.636&#8220;N,7957&#8217;9.42&#8220;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '87G2C2RW+3X' );

$testdata[] = new TO( 'Seatac Airport (Google data)', '47 26.59&prime; -122 18.42&prime;' );
$testdata[] = new TO( 'Seatac Airport (Google data)', '47.443167, -122.307' );
$testdata[] = new TO( 'Seatac Airport (TFlite data)', 'N47 26.59 W122 18.42' );

$testdata[] = new TO( 'Sydney Opera House', '-33.8567844,151.2152966' );
$testdata[] = new TO( 'Sydney Opera House', '3351&prime;24.4&Prime;S 15112\'55.1"E' );

$testdata[] = new TO( 'The Taj Mahal', ' 27.1750151, 78.0421552' );
$testdata[] = new TO( 'The Taj Mahal', '27 10\'30.05 "North and 78 2\'31.76" East' );
$testdata[] = new TO( 'The Taj Mahal', '27.1750154 78.042155');
$testdata[] = new TO( 'The Taj Mahal', '7JVW52GR+2V');

// BAD examples
$testdata[] = new TO( 'Malformed LNG', 'Lat N48 42.29, Long W122.54.40' ); // Note two decimal points, a not-well-formed number
$testdata[] = new TO( 'Impossible LAT', '138.9304,-77.1481' );
$testdata[] = new TO( 'Impossible LNG', '38.9304,-277.1481' );
$testdata[] = new TO( 'Impossible LAT,LNG', '138.9304,-277.1481' );
$testdata[] = new TO( 'Coordinates not Geocode or OLC', '0' );
$testdata[] = new TO( 'Coordinates missing', '' );
$testdata[] = new TO( 'Coordinates goofy', 'gooseball' );


// Process each of the examples
echo '<meta charset="utf-8" />';
echo
'<pre>';
foreach (
$testdata as $to)
{
    echo
"<h3>$to->name: " . '<a target="_blank" href="https://www.google.com/maps?q=' . urlencode($to->geo) . '">' . $to->geo . '</a> </h3>';
   
$geothing = new Geocode($to->geo, $to->name);
    if (empty(
$geothing->error)) {
       
print_r($geothing);
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo) . '">' . $geothing->geo . '</a> computed geocode in D.ddd decimal' . PHP_EOL;
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo_dd) . '">' . $geothing->geo_dd . '</a> computed geocode in D.ddd decimal degrees' . PHP_EOL;
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo_dmd) . '">' . $geothing->geo_dmd . '</a> computed geocode in DM.mmm\' degrees decimal minutes' . PHP_EOL;
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo_dms) . '">' . $geothing->geo_dms . '</a> computed geocode in DM\'S.sss" degrees minutes decimal seconds' . PHP_EOL;
        echo
'<a target="_blank" href="https://plus.codes/' . urlencode($geothing->olc) . '">' . $geothing->olc . '</a> open location code link on plus.codes' . PHP_EOL;
    }
    else {
        echo
$geothing->error . PHP_EOL;
    }
    echo
PHP_EOL;
}


Details

Class Geocode This is a lightweight class that normalizes geocodes (Lat,Lng pairs). It has a single method, the constructor, which receives a geocode string in any of several common and very relaxed formats. See the "demo" file for examples. The constructor attempts to interpret the input string. It returns an instance of the Geocode object with public properties as shown below. If it's impossible to make sense of the input, the "error" property will have some explanation of what went wrong. If "error" is not empty, you should not rely on the other values in the object. This shows the Geocode object for the Post Office at ZIP code 15213 in Pittsburgh: Geocode Object ( [name] => Post Office 15213 in Pittsburgh [lat] => 40.4401766 [lng] => -79.9526167 [geo] => 40.4401766,-79.9526167 [lat_dms] => 4026'24.636"N [lng_dms] => 7957'9.42"W [geo_dms] => 4026'24.636"N,7957'9.42"W [lat_dmd] => 4026.4106'N [lng_dmd] => 7957.157'W [geo_dmd] => 4026.4106'N,7957.157'W [lat_dd] => 40.4401766N [lng_dd] => 79.9526167W [geo_dd] => 40.4401766N,79.9526167W [olc] => 87G2C2RW+3X [input] => 40.4401766,-79.9526167 [error] => ) You must provide a geocode string or an open location code string to use the object. You may provide an optional name that can identify the place the geocode points to. To get this object, create an instance of Geocode like these: require_once('class_Geocode.php'); $geo = new Geocode('40.4401766,-79.9526167', 'Post Office 15213 in Pittsburgh'); To use this object, first check $geo->error for empty() status, then you can get any of the properties that make sense for your application, eg: echo $geo->geo_dms; // prints 4026'24.636"N,7957'9.42"W echo $geo->olc; // prints 87G2C2RW+3X Reconstruction of Lat,Lng pairs is accurate when using explicit geocodes because the geocode points to a place on earth. Accuracy is a bit less when using an open location code. A 10-digit plus code has only about "rooftop" accuracy; the generated geocode will point to the southwest corner of the area defined by the OLC. References: https://journeynorth.org/tm/LongitudeIntro.html https://en.wikipedia.org/wiki/Horizontal_position_representation https://en.wikipedia.org/wiki/Geographic_coordinate_system https://en.wikipedia.org/wiki/Decimal_degrees https://en.wikipedia.org/wiki/Open_Location_Code

  Files folder image Files  
File Role Description
Plain text file class_Geocode.php Class The Geocode Class
Accessible without login Plain text file demo_Geocode.php Example Demonstration Script
Accessible without login Plain text file readme_Geocode.txt Doc. readme text file

 Version Control Unique User Downloads Download Rankings  
 0%
Total:247
This week:0
All time:7,940
This week:104Up