PHP DevCenter
oreilly.comSafari Books Online.Conferences.


PHP Cookbook

Trip Mapping with PHP

by David Sklar, coauthor of PHP Cookbook

Remember Raiders of the Lost Ark? One of the distinctive images was a thick red line cruising across a map, showing Indiana Jones' routes when crisscrossing the globe and fighting bad guys. I don't think they used PHP for any of the special effects in 1981, but you can use PHP today to create a similar map of the United States.

To draw lines in the right places, you need a few things: data describing the location of U.S. places, a blank map image, and a short PHP program to draw the lines where you want them.


First, you need a way to locate different places in the United States. The Census Bureau has already done this. The Gazetteer data file ( contains information (including longitude and latitude) about over 30,000 US ZIP codes. The Gazetteer page explains the file format:

Columns 1-2:     United States Postal Service State Abbreviation 
Columns 3-66:    Name 
Columns 67-75:   Total Population (2000) 
Columns 76-84:   Total Housing Units (2000) 
Columns 85-98:   Land Area (square meters) 
Columns 99-112:  Water Area (square meters) 
Columns 113-124: Land Area (square miles) 
Columns 125-136: Water Area (square miles) 
Columns 137-146: Latitude (decimal degrees) First character is blank or "-"
                 denoting North or South latitude respectively 
Columns 147-157: Longitude (decimal degrees) First character is blank or "-" 
                 denoting East or West longitude respectively

The Gazetteer file unzips to zcta.txt. A line of the text file looks like this (without the linebreaks):

PA19096 5-Digit ZCTA                                                  
13299     5456       8907922             0    3.439368    
0.000000 39.992345 -75.276248

The first five characters of the Name field are the ZIP code. To draw lines in the right places on your map, you just need the ZIP code, the longitude, and the latitude. Store the data in this zcta table:

	zip CHAR(5) NOT NULL,
	lat DECIMAL(12,2) NOT NULL,
	lon DECIMAL(12,2) NOT NULL,

This short PHP program parses the file and inserts information into the zcta table. You should change the DSN passed to DB::connect() to have the correct configuration settings for your database.

require 'DB.php';
$dbh = DB::connect('mysql://test:@localhost/test');

$fh = fopen('zcta5.txt','r') or die("can't open: $php_errormsg");
while ($s = fgets($fh, 256)) {
    if (preg_match('/^\d{5}$/',$zip = substr($s,2,5))) {
        $q = $dbh->query(
        'INSERT INTO zcta (zip,lat,lon) VALUES (?,?,?)',
        if (DB::isError($q)) {

Some of the entries in the file have some alphabetical characters in the ZIP codes. These seem to be consolidations of metropolitan areas. We want only actual ZIP codes in our table, so we'll use the regular expression /^\d{5}$/ to filter out ZIP codes that aren't just five digits. ZIP codes that match go into the database along with their latitudes and longitudes.

The line:


takes advantage of a shortcut in PHP that an assignment operation also returns the value being assigned. $zip = substr($s,2,5) assigns the five-character substring of the line that holds the ZIP code to $zip. The value of the entire expression "$zip = substr($s,2,5)" is also that substring, so preg_match() thinks its second argument is the value of $zip. If this value matches the regular expression, then preg_match() returns true. You can do the same thing in multiple steps:

$zip = substr($s,2,5);
if (preg_match('/^\d{5}$/',$zip)) {

If the ZIP code passes numeric muster, then it goes into the database:

$q = $dbh->query(

The first argument to the PEAR DB query() method is a SQL statement. Here, ? is a placeholder. When the query is executed, the placeholders are replaced with the values in the array passed as the second argument to query(): the ZIP code, the longitude, and the latitude. substr() extracts the longitude and latitude.

Pages: 1, 2

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career


Enroll today!

Sponsored by: