[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
A program is needed to translate place names with known Latitude/Longitude coordinates to the proper point on an outline map. And conversely, determine the Lat/long coordinates for any arbitrary point on the map.
Background & Techniques
A viewer recently asked about our Traveling Salesman Program. Would it be possible to add additional locations to the existing map and would it be possible to load maps other than the default USA map? It is certainly possible, the question is how difficult?
Mapping political or geographic features based on Latitude/Longitude coordinates requires that we convert the point from Lat/Long to screen Pixel (x,y) locations. Since we live on a spherical earth and most maps, including computer screens, are flat, maps must distort the data points in some way. The Mercator projection, developed in the 16th century and is still widely used, is a cylindrical projection with straight equally spaced longitude lines, the ones running vertically through the poles. (X coordinate is proportional to longitude angle). Away from the equator, the map is "stretched" in an east-west and north-south direction so that the latitude lines, (the ones parallel to the equator which get shorter as we move away from the equator), also lie on the surface of a cylinder. (Think of a rubber globe inflated inside a glass cylinder). There is a problem of course near the poles, the scaling (stretching) would have to approach infinity as the length of the latitude lines approached zero. In practice, 70 or 80 degrees North or South is as far as we want to go. This greater stretching at higher latitudes has the effect of maintaining geographic shapes but increasing their area. In a USA map that includes Alaska, for example, its area of about 1/5 of the lower 48, appears to be about 1/2 the lower 48's size.
In order to establish the scaling factors to convert lat/long angle coordinates to pixel x,y coordinates, we need to locate two known points, preferably widely separated in latitude and longitude. If scaling has not been established, it will be necessary to define and click on two scaling points first. After the scaling has been established, you can click on any point on the map to see its coordinates in the location list. The user can also click on any point in the location list (where lat/long are specified) to see its location on the map. Items in the Location list may be added or deleted by the user. Scaling points and location list points are automatically saved and reloaded from run to run in an "initialization" file, Mercator.ini.
The INI file:
Information about scaling points (Name, Latitude/Longitude and Pixel X/Y coordinates) and Location List contents are saved for each map in file Mercator.ini. This is a text file which may be viewed or edited (carefully) outside of the program. Since the program generates content of this file, error checking when retrieved is less stringent than for direct user input.
A list of predefined named locations with Latitude/Longitude coordinates is maintained for each map. The Add Location button will solicit that information from the user and add it to the list. Clicked items on the list to see the point highlighted with a red dot on the map. Select a an entry and press the Delete key to remove it from the list. Points clicked on the map will be added to the list with a name of "Clicked point" and the Lat/Long of the point.
Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now.
A TLoc class holds the Lat/Long and pixel coordinates for a point so to avoid recalculating each time they are referenced. Using fields names defined in TLoc, the conversion from one to the other requires two steps:
Conversions from clicked map pixel points to lat/long run the above steps in reverse.
Note that degrees of longitude correspond to horizontal (X) coordinates and degrees of latitude refer to vertical (Y) coordinates. So even though latitudes usually precede longitudes when coordinates are written, I decided to keep them in a TRealPoint structure with longitude preceding latitude. Thus the "LongLat.. field names.
Most of the rest of the code is to handle user interface stuff, necessary if users are to play with the program, but not nearly as much fun to develop.
Suggestions for Further Explorations
Copyright © 2000-2018, Gary Darby All rights reserved.