Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#
Article

Geo-referencing {Map Calibration}

,
Rate me:
Please Sign up or sign in to vote.
3.63/5 (42 votes)
5 Feb 2006CPOL4 min read 193.8K   3.9K   78   59
Defining a set of equations that transfers Longitude, Latitude to X, Y coordinate system and vice versa
Sample screenshot

Introduction

As any GPS software requires background maps in order to see where the action takes place, thus any raster image i.e. JPG or BMP file can be used as the background. However, this requires some calibration in order to have the map fitting its true position.

What Is Required?

You need a *.jpg (or *.jpeg) picture file (from a map or from a satellite picture). For this purpose, you can scan a map, or make a screenshot from, for example, GoogleMap. You also need some reference points in order to calibrate it.

Moreover, you need to determine in which hemisphere you live in, i.e. Northern or Southern and Eastern or Western. The solution might change if you are in a different hemisphere. Since we are in N/E we'll give the solution that fits our hemisphere.

Explanations

As you might already know, formats such as BMP, JPG, TIFF, etc. are called Raster Images. The reason to this is the means by which information is stored in their structures, Pixels. In other words the top left corner of the picture is (0,0) pixel i.e. its x=0 increases in the positive X axis (from left to right) and y=0 increases in the negative Y axis (from top to bottom).
In the N/E hemisphere (our hemisphere), Longitude increases in the positive x direction (left to right) and the Latitude increases in the positive y direction (bottom to top*).
* Note the direction of y has changed here.

Raster Format

Now calibrating the map means transferring from x,y coordinate system to longitude, latitude coordinate system or vice versa. This purpose can be achieved in several ways. The method we chose to go with is calculating the coordinate of the top left corner and the rate of change in both latitude and longitude.

Code Clarification

So we need to define some relationship between the two coordinate systems and the simplest method is knowing the X,Y as well as Lat, Long coordinates of one point (In this case, we take 0,0 pixel i.e. Top Left Corner). Ideally the Latitude along the X-axis should not increase if we keep y=0. For example if point (0,0) has Latitude of 10 degrees then all points in the x direction should have lat = 10 degrees. Similarly the Y-axis should obey the mentioned rule.

Rate In Each Axis

Let’s take a look at another new concept, rate in each axis. If we move 1 pixel in Y direction how many degrees/Pixels does this represent?

We know three points for which the latitude and longitude are available. So what we do is take the point with the minimum longitude (leftmost point) and the corresponding x pixel. Name this Long1 and X1.

Next, take the point with the maximum longitude (rightmost point) and the matching x pixel. Name this Long2 and X2.

Calculate the rate of change in X direction, delX as:

[1] degrees per pixel in x direction
delX= (Long2-Long1)/ (X2-X1)
C#
// calculating the deltax
delX = (maxLONG().LONG - minLONG().LONG) / (maxLONG().X - minLONG().X);

Similarly, take the maximum latitude (topmost point) and the corresponding y pixel. Name it Lat1 and Y1, the minimum latitude and the matching y pixel, Lat2 and Y2. We calculate delY as:

[2] degrees per pixel in y direction
delY= (Lat2-Lat1)/ (Y2-Y1)
C#
// calculating the deltay
delY = (minLAT().LAT - maxLAT().LAT) / (minLAT().Y - maxLAT().Y);

Now we're on with the calculation of the reference point (Top, Left). Note the maximum latitude (uppermost point) as Lat4 and Y4.

[3] reference latitude
lat0= Lat4+y4 * delY
C#
lat0 = maxLAT().LAT - maxLAT().Y * dY;

Also the minimum longitude (leftmost point) Long1, X1:

[4] reference longitude
long0= Long1 -x1 * delX
C#
long0 = minLONG().LONG - minLONG().X * dX;

Lat0 and Long0 are the geo coordinate representation of point (0,0).
Having all the above information and formulas, we can simply use the following equations to calculate the X and Y points of any given Latitude and longitude.

Conversion between Geographic and Pixel Coordinates

To convert from latitude and longitude to X and Y, you use the following equation:

[5a – 6a] X and Y Coordinates
X= (long-long0)/delx<br />Y= (lat-lat0)/delY
C#
public point LatLongtoXY(float Lat, float Long) 
{
    float X = (Long - LONG0) / dX;
        float Y = (Lat - LAT0) / dY;
        point pnt = new point(X, Y, Lat, Long);
        return pnt;
}

To convert to latitude and longitude from any X and Y point:

[5b - 6b] X and Y Coordinates
Longitude= long0 + delX * X<br />Latitude=lat0 + delY * Y
C#
public point XYtoLatLong(float X, float Y)
{
        float Lat = LAT0 + dY * Y;
        float Long = LONG0 + dX * X;
        point pnt = new point(X, Y, Lat, Long);
        return pnt;
}

As a result, equations [1 2 3 4] will only be used once to digitize (Georeference) the map. From there, you only need equations [5a 5b – 6a 6b] to handle the conversions.

Using the Code

In order for the code to work, you need to build the geoReference library and use it in your projects.

geoReference contains a class point, which represents x, y, latitude and longitude of the reference points. We first create an array of points that hold the three reference points:

C#
geoReference.point[] p = new geoReference.point[] 
{
        new geoReference.point(3.2f,3.1f,29.77f,52.7f),
        new geoReference.point(9.8f,10.2f,29.52f,52.97f),
        new geoReference.point(18.3f,8.6f,29.57f,53.31f)
};

Then we use the geoReference’s methods to convert from x,y to lat/long and vice versa:

C#
geoReference gr = new geoReference(p);
//converting from Lat or Long to X,Y
geoReference.point pt = gr.LatLongtoXY(29.27f, 53.22f);

float x = pt.X;
float y = pt.Y;

Conclusion

You could now load a raster map in a picture box and use the above code to calibrate it. As an outcome if you pass the x and y coordinates to the mouse move event you could see the corresponding geo coordinates.

Special thanks …

To Diego Mijelshon for his excellent article ObjectComparer. It helped us with part of the code implementation.

History

  • 5th February, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager AltaML
Canada Canada
I was born in Shiraz, Iran. A beautiful city that's famous for its weather, poets, ancient culture.
Got my high school diploma from QHS - Quebec High School.
Studied Computer Eng. at the University of New Brunswick (Canada) & Continued at Shiraz University.
Graduated from the University, school of Engineering: 2004.
Masters program, IT, e-Commerce at the electronic University of Shiraz.
Masters in IT Management at UTM, Malaysia.
Masters in Computer Science (Machine Learning) at University of Alberta
Currently in Edmonton, AB Canada!

Written By
Engineer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I was born in Shiraz (Iran). The city of popular poets and flowers.
Studied more than 8 years in Canada and was at UNB (University of New Brunswick) for a year.
Graduated from Shiraz University in field of Computer Eng.
MBA - Management at Khazar University
Love to play Soccer and write C# code.

Comments and Discussions

 
QuestionCompatability with WLD files Pin
krisna_835-Aug-14 19:44
krisna_835-Aug-14 19:44 
GeneralI am having severe problems with the delX and dely Pin
kurrupter28-Dec-09 7:26
kurrupter28-Dec-09 7:26 
GeneralRe: I am having severe problems with the delX and dely Pin
Mohammad Riazi4-Feb-10 8:10
Mohammad Riazi4-Feb-10 8:10 
Questionproblem with x piwel and y pixel Pin
marti3110-Jun-09 4:12
marti3110-Jun-09 4:12 
GeneralThumps UP Pin
mbaocha4-May-09 16:11
mbaocha4-May-09 16:11 
GeneralRe: Thumps UP Pin
Mohammad Riazi5-May-09 1:19
Mohammad Riazi5-May-09 1:19 
GeneralAssemblyinfo.cs Pin
Member 36049735-Mar-09 22:36
Member 36049735-Mar-09 22:36 
hi i tried to built the project. but it gave me Assemblyinfo.cs missing error :/
GeneralHi Pin
Cotamayor27-Jun-08 8:30
Cotamayor27-Jun-08 8:30 
GeneralRe: Hi Pin
Mohammad Riazi27-Jun-08 18:45
Mohammad Riazi27-Jun-08 18:45 
GeneralRe: Hi Pin
Cotamayor30-Jun-08 5:30
Cotamayor30-Jun-08 5:30 
GeneralRe: Hi Pin
Mohammad Riazi30-Jun-08 19:39
Mohammad Riazi30-Jun-08 19:39 
GeneralExcellent, you get my 5 Pin
jadothebest5-Jun-08 8:41
jadothebest5-Jun-08 8:41 
GeneralRe: Excellent, you get my 5 Pin
HRiazi5-Jun-08 18:44
HRiazi5-Jun-08 18:44 
GeneralTashakkor Pin
electronlover28-Feb-08 11:10
electronlover28-Feb-08 11:10 
GeneralRe: Tashakkor Pin
Mohammad Riazi28-Feb-08 18:06
Mohammad Riazi28-Feb-08 18:06 
Questionwhat about N/W Pin
nokhod20-Nov-07 8:58
nokhod20-Nov-07 8:58 
GeneralThe algorithm is wrong Pin
Advantis6-Oct-07 11:33
Advantis6-Oct-07 11:33 
QuestionRequest Pin
padamraj16-Aug-07 14:00
padamraj16-Aug-07 14:00 
GeneralStoring latitudes and longitudes in millisecond format (Int32) Pin
pinx19-Jun-07 2:07
pinx19-Jun-07 2:07 
QuestionConverting code to work with NW hemisphere Pin
Bubba Hawkins14-Jun-07 7:00
Bubba Hawkins14-Jun-07 7:00 
QuestionHow to add size info Pin
miltash3-Jan-07 8:47
miltash3-Jan-07 8:47 
GeneralMistakes !!! Pin
Mantasg22-Nov-06 20:45
Mantasg22-Nov-06 20:45 
Generalyou are not taking into account unrectified images Pin
bbja3-Nov-06 3:48
bbja3-Nov-06 3:48 
GeneralRe: you are not taking into account unrectified images Pin
HRiazi3-Nov-06 4:38
HRiazi3-Nov-06 4:38 
GeneralRe: you are not taking into account unrectified images [modified] Pin
bbja3-Nov-06 5:41
bbja3-Nov-06 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.