Working with maps and geographic data is becoming more popular and important by the hour, much due to Google Maps extremely simple and powerful API. If you found this page through a search engine you’re probably facing the same problem I’ve faced multiple times in my work, you posses geographic data in one format and need to convert it to another format. Chances are you need to convert your coordinates in your own application/code in real time, not manually converting them on Lantmäteriets web site.

Background

I’m from Sweden and like most countries around the world we have separate coordinate grid for Sweden. Most geographic data from Swedish government agencies, like Lantmäteriet, use a two dimensional grid system called RT90 (Rikets Triangelnät). Around 1999 SWEREF99 (SWEdish REference Frame 1999) was introduced to align with EUREF (European Reference Frame) and in 2007 Lantmäteriet replaced RT with SWEREF99.  RT90 is still by far the most used grid system for maps and POI in Sweden, big Swedish search and map services Hitta and Eniro are based on RT90.

So you have all this great information you want to visualize on Google Maps and/or query using the new awesome spatial functionality in SQL Server 2008. Problem is, they want WGS84 formatted GPS-positions. There’s plenty of open resources and algorithms available on how to translate coordinates from one format to another, but that doesn’t mean it’s easy to do. Therefor I’ve decided to share my .NET library for translating Swedish coordinates between RT90, SWEREF99 and WGS84 which i call MightyLittleGeodesy. The transformation calculations are entirely based on the excellent JavaScript library by Arnold Andreasson found here.

Using the library

Here’s an example of how to translate from RT90 to WGS84:

RT90Position position = new RT90Position(6583052, 1627548);
WGS84Position wgsPos = position.ToWGS84();

From WGS84 to SWEREF99

WGS84Position wgsPos = new WGS84Position();
wgsPos.SetLatitudeFromString("N 59º 58' 55.23\"", WGS84Position.WGS84Format.DegreesMinutesSeconds);
wgsPos.SetLongitudeFromString("E 017º 50' 06.12\"", WGS84Position.WGS84Format.DegreesMinutesSeconds);
 
SWEREF99Position rtPos = new SWEREF99Position(wgsPos, SWEREF99Position.SWEREFProjection.sweref_99_tm);

From SWEREF99 to WGS84:

SWEREF99Position swePos = new SWEREF99Position(6652797.165, 658185.201);
WGS84Position wgsPos = swePos.ToWGS84();

From WGS84 to RT90:

WGS84Position wgsPos = new WGS84Position("N 59º 58' 55.23\" E 017º 50' 06.12\"", WGS84Position.WGS84Format.DegreesMinutesSeconds);
RT90Position rtPos = new RT90Position(wgsPos, RT90Position.RT90Projection.rt90_2_5_gon_v);

From RT90 to SWEREF99:

RT90Position rt90Pos = new RT90Position(6583052, 1627548);
SWEREF99Position sweRef = new SWEREF99Position(rt90Pos.ToWGS84(), SWEREF99Position.SWEREFProjection.sweref_99_tm);

You can translate both ways between all three coordinate systems as both RT90 and SWEREF99 can be translated to WGS84 and WGS84 can be translated into both RT90 and SWEREF99. The WGS84 object can parse out positions from strings in DMS (degrees, minutes seconds, and DM (degrees, minutes) format. All in all it’s a pretty easy to use and powerful library. I’ve added some unit-tests to the project which verifies the calculations against other sources (Lantmäteriet, Hitta and Eniro).

Creative Commons

The calculations in this library is based on the excellent javascript library by Arnold Andreasson which is published under the Creative Commons license. Therefor my library is also published under the Creative Commons license.

Download

As usual the project is in VS2008 format but the code can be compiled for all .NET versions.
Download source (VS2008 format)
Download compiled binary (.NET 2.0)

Related posts