Mercator Projection Demo

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]



Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 


We receive a few cents from each purchase.  Thanks


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only



Problem Description

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?

Mercator Projection:

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.  


bulletThe Load Map button will allow the user to select a new map. Four sample Mercator projection outline maps are included in the downloaded zip file:
bulletMap USA.bmp is the default outline map of the USA. Scaling predefined using San Diego, California and Estcourt Station, Maine.  The location of these points as well as the Cape Alava, WA & Ballast Key, FL pair used for testing have been pre-located with colored dots on the map file.
bulletMap Europe.bmp: Scaling predefined using Lisbon and Stockholm.
bulletMap Mexico.bmp: Not tested. Scaling points have not been established for this map. User must enter Lat/Long coordinates for two points which can be located and clicked on the outline based on geometric features of the outline. I would suggest trying "Tijuana" and "Cancuin".
bulletMap Canada.bmp: Not tested. See comments above for Mexico.
bulletThe Add Location button will display a dialog to add additional points.
bulletThe Rescale button will let user reselect the scaling points if not satisfied with the accuracy of initial scaling clicks.
bulletChange Name or Lat/Long button in the scaling points box allows you to define or redefine Name and Lat/Long coordinates for  the two points used to establish the scaling factors.

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.

Location List:

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.

Programmer's Notes:

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:

  1. Convert LongLat to LongLatXY, degree values when projected to a flat surface.  The math details and derivation of this conversion are from the Wikipedia "Mercator Projection" page.   
  2. Convert LongLatXY degrees to PixelXY distances.   Scale factors (ScaleX, Scaley) are computed as pixels per degree.   So the conversion is a straightforward linear operation: translate to some arbitrary origin (I chose the second scale point), multiply by the scaling factor and add the origin pixel value.  E .g. for a new  Longitude:  Newpixels.x = scalex * (newlongitude - baselongitude) + basepixels.x).  

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.  

Running/Exploring the Program 

bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

Mercator projection code embedded in the program should be moved to a separate TMercator class for easy export to other programs
.AngleToStr function should allow control of the string format by passing an "angle format" string similar to date format string parameter.


Original:  July 25,2008

Modified:  July 29, 2017

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.