13,140,613 members (46,914 online)
alternative version

#### Stats

153.5K views
76 bookmarked
Posted 5 Feb 2006

# Geo-referencing {Map Calibration}

, 5 Feb 2006
 Rate this:
Defining a set of equations that transfers Longitude, Latitude to X, Y coordinate system and vice versa

## 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.

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) `
```// 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) `
```// 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 `
`lat0 = maxLAT().LAT - maxLAT().Y * dY;`

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

 [4] reference longitude `long0= Long1 -x1 * delX `
`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
Y= (lat-lat0)/delY `
```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
Latitude=lat0 + delY * Y `
```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:

```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:

```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

## Share

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.

 Engineer 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.

## You may also be interested in...

 First PrevNext
 Compatability with WLD files krisna_835-Aug-14 19:44 krisna_83 5-Aug-14 19:44
 I am having severe problems with the delX and dely kurrupter28-Dec-09 7:26 kurrupter 28-Dec-09 7:26
 Re: I am having severe problems with the delX and dely Mohammad Riazi4-Feb-10 8:10 Mohammad Riazi 4-Feb-10 8:10
 problem with x piwel and y pixel marti3110-Jun-09 4:12 marti31 10-Jun-09 4:12
 Thumps UP mbaocha4-May-09 16:11 mbaocha 4-May-09 16:11
 Assemblyinfo.cs Member 36049735-Mar-09 22:36 Member 3604973 5-Mar-09 22:36
 Hi Cotamayor27-Jun-08 8:30 Cotamayor 27-Jun-08 8:30
 Re: Hi Cotamayor30-Jun-08 5:30 Cotamayor 30-Jun-08 5:30
 Re: Excellent, you get my 5 H.Riazi5-Jun-08 18:44 H.Riazi 5-Jun-08 18:44
 Tashakkor electronlover28-Feb-08 11:10 electronlover 28-Feb-08 11:10
 what about N/W nokhod20-Nov-07 8:58 nokhod 20-Nov-07 8:58
 Storing latitudes and longitudes in millisecond format (Int32) Pinx19-Jun-07 2:07 Pinx 19-Jun-07 2:07
 I store my latitude and longitude in milliseconds, a normal Int32. I don't know the exact reasons why Microsoft chose to start storing dates, times and timespans in ticks, but it seems like a good idea to me. So the logical thing to me was to use a similar storage format for latitudes and longitudes. This makes calculations faster and easier. No worries about significant bits and stuff. You can even store a coordinate (latitude + longitude) in one single Int64. Can anybody point out the advantage of using double instead of Int32 in this case? ```
Public ReadOnly Property Degrees() As Integer
Get
Return (_MilliSeconds / 1000) \ SecondsPerDegree
End Get
End Property
Public ReadOnly Property Minutes() As Integer
Get
Return ((_MilliSeconds / 1000) - Me.Degrees * SecondsPerDegree) \ SecondsPerMinute
End Get
End Property
Public ReadOnly Property Seconds() As Integer
Get
Return ((_MilliSeconds / 1000) - Me.Degrees * SecondsPerDegree - Me.Minutes * SecondsPerMinute)
End Get
End Property
Public ReadOnly Property TotalSeconds() As Integer
Get
Return (_MilliSeconds / 1000)
End Get
End Property
Public ReadOnly Property MilliSeconds() As Integer
Get
Return (_MilliSeconds)
End Get
End Property
Public ReadOnly Property TotalDegrees() As Double
Get
Return (_MilliSeconds / 1000) / SecondsPerDegree
End Get
End Property``` Fast approximation of distance: ```Public Shared Function Distance(ByVal Latitude1 As LatLong, ByVal Latitude2 As LatLong, ByVal Longitude1 As LatLong, ByVal Longitude2 As LatLong) As Long
'Approximation of the distance in meters between two points
Return Math.Sqrt( _
(30.9 * (Latitude1.TotalSeconds - Latitude2.TotalSeconds)) ^ 2 + _
(18.9 * (Longitude1.TotalSeconds - Longitude2.TotalSeconds)) ^ 2)
End Function
``` Readable representations: ```Public Overrides Function ToString() As String
Return String.Format("{0}{1}{2}{3}{4}{5}", New Object() {Me.Degrees, Me.DegreeSymbol, Me.Minutes, Me.MinuteSymbol, Me.Seconds, Me.SecondSymbol})
End Function
Public Function ToShortString() As String
Return String.Format("{0:+000;-000}{1:00}{2:00}", Me.Degrees, Me.Minutes, Me.Seconds)
End Function
Public Function ToDouble() As Double
Return Me.Degrees + Me.Minutes / 60 + Me.Seconds / 3600
End Function
``` -- modified at 8:06 Tuesday 19th June, 2007
 Converting code to work with NW hemisphere Bubba Hawkins14-Jun-07 7:00 Bubba Hawkins 14-Jun-07 7:00
 How to add size info miltash3-Jan-07 8:47 miltash 3-Jan-07 8:47
 Mistakes !!! Mantasg22-Nov-06 20:45 Mantasg 22-Nov-06 20:45
 you are not taking into account unrectified images bbja3-Nov-06 3:48 bbja 3-Nov-06 3:48
 Re: you are not taking into account unrectified images H.Riazi3-Nov-06 4:38 H.Riazi 3-Nov-06 4:38
 Re: you are not taking into account unrectified images [modified] bbja3-Nov-06 5:41 bbja 3-Nov-06 5:41
 Re: one more question about georeference H.Riazi30-Oct-06 16:33 H.Riazi 30-Oct-06 16:33
 where should the reference points be placed? fadfooood29-Oct-06 15:01 fadfooood 29-Oct-06 15:01
 Re: where should the reference points be placed? H.Riazi29-Oct-06 19:35 H.Riazi 29-Oct-06 19:35
 Re: where should the reference points be placed? fadfooood30-Oct-06 11:19 fadfooood 30-Oct-06 11:19
 about the second converter ash4_ashish@yahoo.com23-Jun-06 10:13 ash4_ashish@yahoo.com 23-Jun-06 10:13
 Usage permission? Bubba Hawkins6-Jun-06 16:02 Bubba Hawkins 6-Jun-06 16:02
 Re: Usage permission? H.Riazi6-Jun-06 18:40 H.Riazi 6-Jun-06 18:40
 Re: Usage permission? H.Riazi6-Jun-06 18:43 H.Riazi 6-Jun-06 18:43
 Re: Usage permission? Bubba Hawkins7-Jun-06 17:44 Bubba Hawkins 7-Jun-06 17:44
 its excellent work but still i have prob with it ejaz13-Jun-06 0:50 ejaz1 3-Jun-06 0:50
 Re: its excellent work but still i have prob with it H.Riazi3-Jun-06 6:57 H.Riazi 3-Jun-06 6:57
 Re: its excellent work but still i have prob with it ejaz14-Jun-06 7:18 ejaz1 4-Jun-06 7:18
 Re: its excellent work but still i have prob with it Ahsaan2-Mar-07 2:44 Ahsaan 2-Mar-07 2:44
 Reference points ?? ejaz113-May-06 2:33 ejaz1 13-May-06 2:33
 Least-squares Morten on GIS6-Feb-06 1:02 Morten on GIS 6-Feb-06 1:02
 Won't work for map projections... Willem Fourie5-Feb-06 23:46 Willem Fourie 5-Feb-06 23:46
 Re: Won't work for map projections... H.Riazi6-Feb-06 0:23 H.Riazi 6-Feb-06 0:23
 Re: Won't work for map projections... Jun Du6-Feb-06 5:33 Jun Du 6-Feb-06 5:33
 Re: Won't work for map projections... Mohammad Riazi6-Feb-06 5:40 Mohammad Riazi 6-Feb-06 5:40
 Last Visit: 31-Dec-99 18:00     Last Update: 20-Sep-17 2:46 Refresh 12 Next »