ZIP It Up!
Before you can start examining the function that determines distance between two ZIP codes, you need to examine the data required. Included with the code for this article is zipfile.savf, a physical file containing five-digit United States ZIP codes, along with their associated state abbreviations and longitude and latitude values. This data is from the 2000 U.S. Census Bureau census. The DDS source for this file is in Figure 1.
|
Figure 1: This is the DDS source for the ZipCodes physical file.
To load this file on your iSeries, you must first create the save file in QGPL using the following command:
CRTSAVF FILE(QGPL/ZIPSSAVF) TEXT('Zip Code Data Save File')
Once you've created the save file, you're ready to load the save file data into this file using FTP, as shown in Figure 2.
|
Figure 2: Load the ZIP code data file onto the iSeries using FTP.
Within this example, replace the IP address shown (192.168.0.3) with the IP address of your iSeries.
Next, you'll have to restore the physical file with the RSTOBJ command:
RSTOBJ OBJ(ZIPCODES) SAVLIB(QGPL) DEV(*SAVF) OBJTYPE(*FILE)
SAVF(QGPL/ZIPSSAVF)
Building the Function
Now that you have the ZIP code data loaded into your iSeries, you're ready to take a look at the SQL function that you'll use with this data. This function calculates the number of air miles, nautical miles, or kilometers between ZIP codes, using the longitude and latitude points associated with each ZIP code. The source for the GetMiles function is shown in Figure 3.
|
Figure 3: Use this source to create the GetMiles function.
To create this function, copy the source in Figure 3 into a source physical file (QSQLSRC, for example). Next, use the RUNSQLSTM command:
RUNSQLSTM SRCFILE(QGPL/QSQLSRC) SRCMBR(GETMILES)
COMMIT(*NONE) NAMING(*SQL)
The formula contained within the GetMiles function uses the circumference of the Earth along with the longitude and latitude values that correspond to each ZIP code supplied. The function accepts three parameters: the five-character origin ZIP, the five-character destination ZIP, and the one-character unit of measure value representing the value to be returned. The value calculated is represented as kilometers by default but is converted to either air miles or nautical miles, based on the third parameter. Use a value of 'M' for air miles or a value of 'N' for nautical miles. Any other value specified will return the resulting distance in kilometers.
Once the function has been created, you can test it using the Interactive SQL console by typing the command STRSQL. From the console, type this SELECT statement:
SELECT QGPL.GETMILES('18222', '90210', 'M')
FROM QSYS2.SYSCOLUMNS
When executed, this statement will return multiple rows showing the air miles distance between these two ZIP codes. In this example, the file specified on the FROM clause is used for example purposes only.
Putting the Function to Use
Once you've successfully built and tested the GetMiles function, you're ready to put it to practical use. The example shown below calculates a shipping charge based on the origin ZIP (ORGZIP) and destination ZIP (DSTZIP) fields from the ORDERS file and joins to the CARRIERS file to get the rate per mile (RTPRMI) value.
SELECT ORGZIP, DSTZIP, QGPL.GETMILES(ORGZIP, DSTZIP, 'M') AS DIST,
QGPL.GETMILES(ORGZIP, DSTZIP, 'M') * RTPRMI AS FRTCST
FROM ORDERS INNER JOIN CARRIERS ON ORDERS.CARR = CARRIERS.CARR
A slightly more complicated example uses the function as part of an ORDER BY clause to return the data based on distance in miles. The statement for this example is shown below.
SELECT STNAME, STADD1, STADD2, STCITY, STSTAT, STZIP,
QGPL.GETMILES('18222', STZIP, 'M')
FROM STORES
ORDER BY QGPL.GETMILES('18222', STZIP, 'M')
This is an example of how you would use the function to display locations closest to a defined location. For instance, you could use it to create Web pages that allow a user to display stores closest to their home (the specified origin ZIP). To achieve that, you would replace the origin ZIP in the example with the value of the variable containing the user's origin ZIP.
This example shows how you can take an otherwise difficult task and make it a simple part of an SQL statement. This function has oodles of uses, a few of which we've examined here, but many more of which you'll discover on your own now that you've got GetMiles.
Mike Faust is MIS Manager for The Lehigh Group in Macungie, Pennsylvania. Mike is also the author of the books The iSeries and AS/400 Programmer's Guide to Cool Things and Active Server Pages Primer from MC Press. You can contact Mike at
LATEST COMMENTS
MC Press Online