2

I'm looking for code in c# that converts coordinates from UTM to WGS1984 (and preferably backwards as well). I need to convert coordinates from only one zone (36N).

I've found this and this, but they throw me 150-200 meters from the real point (The allowed diversion I'm aiming for is 5 meters tops).

EDIT: Found this as well, but did not have an opportunity to check this for accuracy.

thanks in advance.

EDIT: Found this as well, but did not have an opportunity to check this for accuracy.

  • Is the UTM coordinate reference system also based on WGS84? 150-200 m offset could easily be due to different geographic CRS (aka datums). – mkennedy May 22 '13 at 15:47
  • I think it's based on WGS-1984 (90% sure). – jonatr May 22 '13 at 15:59
  • Can you post a sample input/output/expected? Then I could check it via Esri software plus see what a transformation in the area would do. If you can't, try running it through PROJ.4 or the online NGS converter using NAD83. That will at least confirm your algorithm's working correctly. ngs.noaa.gov/cgi-bin/utm_getut.prl – mkennedy May 22 '13 at 16:22
3

if you're willing to use the GDAL/OGR C# bindings, which you can get here assuming you use Windows, you can achieve what you want via the OSGeo.OSR library and proj4 projection expressions from spatialreference.org.

The specifics of finding and installing GDAL/OGR builds for your operating system are covered in other questions and several blog posts. But once you find a build that suits you, make sure you reference the osr_csharp.dll in your project.

Second, import the library with a using OSGeo.OSR; statement.

(Third ..and I'm not sure if this is always necessary, but in IIS, I had to use a 32-bit application pool before this worked. So if you run into issues, it's possible your app pool needs to be 32-bit.)

And finally, craft a method to perform coordinate conversion. The following is a recipe I have used to take X and Y values for ESRI 102697 as the srs projection, note the proj4 link at SpatialReference.org, and convert those projected values to WGS 1984 Long/Lat, i.e. the dst projection, returning the converted point values.

The lesson here is projection agnostic. Meaning you just need to find the appropriate src and dst proj4 expressions for your data, then adapt what you see below to your specific situation. I hope it's helpful. /elrobis

[WebMethod]
public string ReprojectPoint(string x, string y)
{
    // Replace this with your appropriate proj4 expression..
    string esri_102697_proj4 = @"+proj=tmerc +lat_0=35.83333333333334 +lon_0=-92.5 +k=0.9999333333333333 +x_0=500000.0000000002 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs";
    OSGeo.OSR.SpatialReference src = new OSGeo.OSR.SpatialReference("");
    src.ImportFromProj4(esri_102697_proj4);

    // Replace this with your appropriate proj4 expression..
    string epsg_wgs1984_proj4 = @"+proj=latlong +datum=WGS84 +no_defs";
    OSGeo.OSR.SpatialReference dst = new OSGeo.OSR.SpatialReference("");
    dst.ImportFromProj4(epsg_wgs1984_proj4);

    // Init the transformer object.
    OSGeo.OSR.CoordinateTransformation ct = new OSGeo.OSR.CoordinateTransformation(src, dst);


    double[] p = new double[3];
    p[0] = Convert.ToDouble(x);
    p[1] = Convert.ToDouble(y);
    p[2] = 0; // I don't remember if "Z" is required, but I didn't use it.

    ct.TransformPoint(p); // Mutate the point coordinates here..

    return "x:" + Convert.ToString(p[0]) + " y:" + Convert.ToString(p[1]) + " z:" + Convert.ToString(p[2]);
}
2

I made a port from a javascript library to C#, I have tested it and works perfectly, you can take a look at: https://github.com/owaremx/LatLngUTMConverter. It does not need any extra library. Just c# code

1

Some other options:

Altough I'm not sure how well maintained these are

  • I'd second Proj4Net. – Alex Leith Nov 27 '13 at 22:13
0

MapGuide Open Source has an object-oriented .net wrapper API around the CS-Map coordinate system library that can be used outside of a MapGuide web application.

The API is available as NuGet package and can be installed like so

PS> install-package mapguide-api-base-x86

Or for 64-bit

PS> install-package mapguide-api-base-x64

Then you need a copy of the CS-Map dictionary files which you can grab form an installation of MapGuide Open Source, or you can nuget install a compact subset of these dictionaries

PS> install-package cs-map-dictionaries

A simple transformation example using this API would look like this

using OSGeo.MapGuide;

public void Transform(double x, double y, ref double tx, ref double ty)
{
    //Set the location of the dictionary files (only needs to be done once, just illustrated here for example purposes)
    Environment.SetEnvironmentVariable("MENTOR_DICTIONARY_PATH", "C:\\Program Files\\OSGeo\\MapGuide\\CS-Map\\Dictionaries", EnvironmentVariableTarget.Process);

    MgCoordinateSystemFactory csFactory = new MgCoordinateSystemFactory();
    //Create WGS84 coord sys
    MgCoordinateSystem wgs84 = csFactory.CreateFromCode("LL84");
    //Create UTM coord sys
    MgCoordinateSystem utm = csFactory.CreateFromCode("UTM-36N");
    MgCoordinateSystemTransform xform = csFactory.GetTransform(wgs84, utm);

    MgCoordinate transformed = xform.Transform(x, y);

    //MgCoordinate contains your transformed coordinates
    tx = transformed.GetX();
    ty = transformed.GetY();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.