Image_GIS is a PEAR package for PHP that allows developers
to add digital maps to their Web sites using free downloadable GIS files, which
contain latitude and longitude information about certain geographic areas. In
this article I’ll demonstrate how to use the Image_GIS
package to add mapping functionality to a Web site.
In order to use the Image_GIS
package, you have to install the package through PEAR. The Image_GIS
package accepts E00 format input via an .e00 file through the addDataFile()
method. When you add the file, the information within the file is parsed;
parsing involves reading each of the lines in the input file, deciphering the
data, and storing the information locally. Once the information is parsed, you
can then use the showImage() method to render the data as a PNG image. The data
consists of latitude and longitude point-to-point information for drawing lines
that create the final image.
The first step to creating this solution is to install the Image_GIS package. Once you’ve installed Image_GIS and its dependencies, you need to locate a file
containing GIS data in ARC/INFO (.e00) format. Fortunately, the U.S. Census
Bureau’s Web site hosts downloads of GIS
data in ARC/INFO format. In this solution, I’ll be using county boundary data
for the state in which I currently reside, Kentucky.
One obstacle is that the format of the data in these files
is double-precision data, and the Image_GIS package
can only parse single-precision data. The spectacular thing about PHP though is
that the source code is located in the package, and in order to accept
double-precision data, a slight coding change is necessary. Follow these steps:
- Locate
the E00.php file underneath the installation directories of the Image_GIS package. - Open
this file and find the following regular expression search pattern:([ -][0-9]\.[0-9]{7}E[-+][0-9]{2})
- Replace
the 7-digit restriction on the search pattern with a 7- to 14-digit restriction,
like this:([ -][0-9]\.[0-9]{7,14}E[-+][0-9]{2})
Another problem is that the Image_GIS
package expects the point-to-point information to end when the file ends. In
the U.S. Census Bureau data, however, this is not the case; the point-to-point
information ends when the line-set identifier changes to -1. In the input file,
label information follows this particular line. The only things we have to do
are search for this pattern within the input file and discontinue parsing when
we reach this line:
else if ($numRecords == 0 &&
preg_match("#^\s+(-1)\s+([-0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0
-9]+)\s+([0-9]+)\s+([0-9]+)#", $line, $a)) {
if ($this->debug) {
echo $line . '<br />';
}
break;
}
Once we make these changes, we can use the package. The
documentation on this package is quite limited, but there is an example at the
beginning of the GIS.php file. This example maps out
the hometown of one of the Image_GIS package
developers. Using this example, I can create my own code to generate the PNG
image of Kentucky:
<?php
require_once "Image/GIS.php";
$map = new Image_GIS(
array(
'width' => 1200,
'height' => 600,
'debug' => 0
)
);
$map->addDataFile('X:\path_to_file\co21_d00.e00', 'black');
$map->showImage();
?>
The first thing I do is require the Image_GIS
package. Then I create an instance of the Image_GIS
class with constructor information. In this example, I set the width and height
of the resulting image and turn off debugging because debugging simply outputs
verbose information as the data is parsed and rendered. Once the instance is
created, the data file is added through the addDataFile() method. The
lines are rendered in the color specified in the second parameter of the method
call. Finally, the showImage() method outputs the image as a PNG image to the response
using the underlying PHP imaging’s imagepng() method.
You can add more than one GIS file and specify different colors to map more
regions within the same image.
Once you get a feel for coding this solution, crack open the
GIS file and look at the contents of it. You will see that each set represents
a series of interconnected lines. Each line of data represents a line on the
resulting image in lat/long coordinates. When you understand this file, you can
create your own GIS file to add to your image. This can be a useful device if
you have an underlying satellite or map image bounded by the lat/long
coordinates. You can overlay the map image with the new image created by the Image_GIS package, thus giving you a resultant image
showing the locations of your mapped regions.
To view a working example, download the source code.
Keep your developer skills sharp by automatically signing up for TechRepublic’s free Web Development Zone newsletter, delivered each Tuesday.