This project has moved. For the latest updates, please go here.

Coordinate transform example

Topics: SharpMap v0.9 / v1.x
Sep 30, 2009 at 9:43 PM


I loaded an ecw map which is recorded in Lambert II etendu.

In mouse move function, I can access to XY positions and World XY positions.

I'd like to get lat and lon of those positions (WGS84).

I've read through FAQ solutions about Transform from ProjNet, I've read ProjNET FAQ too.

But as I am a beginner, all of this doesn't mean much sense for me... I'm a bit lost in all transformation possibilities (OSGeo.GDAL, OSGeo.OGR, OSGeo.OSR)...

Could anybody help me by giving a sample code to get Lat and Long from World or Mouse positions please ?




Oct 1, 2009 at 12:59 PM
Edited Oct 1, 2009 at 2:48 PM



in fact sample code was not in Sharp discussion but in Proj discussion.

So here is a sample code that converts from projection to another (Example is Lambert to WGS84) :


// String of my ecw image : It is SOURCE PROJECTION
string LAMBERTin = "PROJCS[\"NTF (Paris) / Lambert Corse\","
                                + "GEOGCS[\"NTF (Paris)\","
                                + "DATUM[\"Nouvelle_Triangulation_Francaise_Paris\","
                                + "SPHEROID[\"Clarke 1880 (IGN)\",6378249.2,293.4660212936269,"
                                + "AUTHORITY[\"EPSG\",\"7011\"]"
                                + "],"
                                + "TOWGS84[-168,-60,320,0,0,0,0],"
                                + "AUTHORITY[\"EPSG\",\"6807\"]],"
                                + "PRIMEM[\"Paris\",2.33722917,"
                                + "AUTHORITY[\"EPSG\",\"8903\"]],"
                                + "UNIT[\"grad\",0.01570796326794897,"
                                + "AUTHORITY[\"EPSG\",\"9105\"]],"
                                + "AUTHORITY[\"EPSG\",\"4807\"]],"
                                + "PROJECTION[\"Lambert_Conformal_Conic_2SP\"],"
                                + "PARAMETER[\"latitude_of_origin\",47.0],"
                                + "PARAMETER[\"standard_parallel_1\",45.0],"
                                + "PARAMETER[\"standard_parallel_2\",49.0],"
                                + "PARAMETER[\"central_meridian\",0],"
                                + "PARAMETER[\"scale_factor\",0.99994471],"
                                + "PARAMETER[\"false_easting\",234.358],"
                                + "PARAMETER[\"false_northing\",0],"
                                + "UNIT[\"metre\",1,"
                                + "AUTHORITY[\"EPSG\",\"9001\"]],"
                                + "AUTHORITY[\"EPSG\",\"27564\"]"
                                + "]";

// String of my ecw image : It is DESTINATION PROJECTION
string GEO84out = "GEOGCS[\"GCS_WGS_1984\", "
                              + "DATUM[\"D_WGS_1984\", "
                              + "SPHEROID[\"WGS_1984\",6378137,298.257223563]], "
                              + "PRIMEM[\"Greenwich\",0], "
                              + "UNIT[\"Degree\",0.01745329251994328]]";

// Transform From Map(X,Y) to WGS84
            IProjectedCoordinateSystem SCPin = CoordinateSystemWktReader.Parse(LAMBERTin) as IProjectedCoordinateSystem;
            IGeographicCoordinateSystem SCGout = CoordinateSystemWktReader.Parse(GEO84out) as IGeographicCoordinateSystem;

            CoordinateTransformationFactory ctfactory = new CoordinateTransformationFactory();
            ICoordinateTransformation Transformation = ctfactory.CreateFromCoordinateSystems(SCPin, SCGout);

// My two points (X,Y) come from mousemove routine. This is why it is WolrdPos.X and WorldPos.Y
            double[] fromPoint = new double[] { WorldPos.X, WorldPos.Y };

// Transform from Lambert To WGS84 here :
           double[] toPoint = Transformation.MathTransform.Transform(fromPoint);

// Then I put them in tbLat to display Lat and Long
            tbLat.Text = toPoint[0].ToString();
            tbLon.Text = toPoint[1].ToString();


This works fine.