Code, Code, Revolution!
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.
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.
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).
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.
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)
With this blog I try to provide useful tips and solutions for programming .NET, Objective-C and more. My name is Björn Sållarp, and I love writing code.
Otto Dandenell
June 2nd, 2009 at 12:18 pm
Hej,
Strålande bibliotek, precis vad jag letade efter. Och hittade det bara dagen efter du lagt ut det! Google regerar!
Jag jobbar som systemarkitekt på Svenska Fotbollförbundet. Vi ska precis börja koordinatsätta alla våra anläggningar och kommer kunna använda detta till att presentera koordinaterna i valfritt standardformat.
En liten bugg hittade jag på rad 164 (metoden ConvToDmString) i WGS84Position.cs. Formateringssträngen förväntar sig ett fjärde argument, men det finns bara tre.
Bort:
return string.Format(“{0} {1}º {3}’”, value >= 0 ? positiveValue : negativeValue, degrees, (Math.Floor(minutes * 10000) / 10000));
In:
return string.Format(“{0} {1}º {2}’”, value >= 0 ? positiveValue : negativeValue, degrees, (Math.Floor(minutes * 10000) / 10000));
Otto Dandenell
June 2nd, 2009 at 2:21 pm
Och så var det en till liten bugg.
Denna gång i RT90Position.cs, rad 132, metoden ToString().
Ut:
return string.Format(“X: {1} Y: {2} Projection: {3}”, Latitude, Longitude, ProjectionString);
In:
return string.Format(“X: {0} Y: {1} Projection: {2}”, Latitude, Longitude, ProjectionString);
Björn Sållarp
June 2nd, 2009 at 3:58 pm
Thanks for your feedback Otto,
I’ve updated both the compiled binary and the source. Good luck with your project!
Mathias Åhsberg
September 7th, 2009 at 9:32 pm
Hi. Thanks for an excellent library, it helped me a lot.
Because I couldn’t use it as it was (c#) i needed to port it to java instead.
If you or anyone else need the java version you can find it on:
http://github.com/goober
Hope it is ok that I release my java version of your library.
Björn Sållarp
September 8th, 2009 at 6:29 am
Hey Mathias,
Great job! If you haven’t yet, update wikipedia with a link to your library for RT90 etc. I put my url there and lots of people find it that way.
Johan S
October 12th, 2009 at 2:27 pm
To bad it isn’t this version of the CC license: http://creativecommons.org/licenses/by/3.0/
Björn Sållarp
October 13th, 2009 at 8:17 am
Johan,
If it was up to me I would distribute this without a licence. Because the calculations are based on the work by Arnold Andreasson I want to respect his licence. Contact Arnold Andreasson (http://latlong.mellifica.se/) and ask him to change his licence, that way I can change mine.
Domidimi
November 21st, 2009 at 12:51 pm
Arnold Andreasson has no copyright for the calculations. The algorithm used is Gauß-Krüger projection. Formulas are published here:
http://www.lantmateriet.se/upload/filer/kartor/geodesi_gps_och_detaljmatning/geodesi/Formelsamling/Gauss_Conformal_Projection.pdf
So as far as you have not copied the code from Arnold you can use whichever license you want for your work.
Mats Blomqvist
April 15th, 2010 at 6:39 pm
Hej Johan
är det inte SWEREF 99 TM du menar istället för bara SWEREF99?
Komma igång med Silverlight och Bing Maps | Victor Stodell
April 23rd, 2010 at 6:33 pm
[...] .NET-bibliotek med möjlighet att konvertera mellan dessa format. Björn Sållarp har snickrat ihop MightyLittleGeodesy. Eftersom det är ett vanligt .NET-bibliotek var jag tvungen att ladda ner källkoden och bygga [...]
RP
May 26th, 2010 at 3:45 pm
Helt underbart, tack så mycket! Kommer följa din blogg framöver.