3

I have set of lat long values and I want their equivalent in meters using Mercator projection in C#. Can you please post some sample code or api example of some library that does exactly this?

4 Answers 4

6

Here's some Python I wrote, which I am sure can be moved to C# quite easily:

# Ellipsoid model constants (actual values here are for WGS84) 
sm_a = 6378137.0 
sm_b = 6356752.314

def projLatLonToWorldMercator(lat,lon,isDeg=False):
    """
    LatLonToWorldMercator

     Converts a latitude/longitude pair to x and y coordinates in the
     World Mercator projection.

     Inputs:
       lat   - Latitude of the point.
       lon   - Longitude of the point.
       isDeg - Whether the given latitude and longitude are in degrees. If False 
               (default) it is assumed they are in radians.

     Returns:
       x,y - A 2-element tuple with the World Mercator x and y values.

    """     
    lon0 = 0
    if isDeg:
        lat = projDegToRad(lat)
        lon = projDegToRad(lon)

    x = sm_a*(lon-lon0)
    y = sm_a*math.log((math.sin(lat)+1)/math.cos(lat))

    return  x,y 

and you also need:

def projDegToRad(deg):
    return (deg / 180.0 * pi)

def projRadToDeg (rad):
    return (rad / pi * 180.0)
13
  • Does this return answer in meters? Can I use the resulting coordinates to compute area in meter square of a rectangle? Oct 3, 2011 at 6:44
  • World Mercator is based on WGS84 and its Projected Bounds are : 20037508.3428, -15496570.7397, 20037508.3428, 18764656.2314 - So it's going to be a fairly simple formula to calculate the area; bear in mind there are negative values in there as well, so it's best to convert the differences between minx/maxx etc, not to try to use the native numbers, if you are calculating area.
    – Hairy
    Oct 3, 2011 at 6:51
  • What is sm_a in your code? Where are these bounds used in your formula? Again is the answer in meters? Oct 3, 2011 at 6:57
  • Sorry: `# Ellipsoid model constants (actual values here are for WGS84) # sm_a = 6378137.0 sm_b = 6356752.314
    – Hairy
    Oct 3, 2011 at 6:58
  • 1
    Please bear in mind that if you are using the World Mercator for area calculation, you will be getting significant errors as you move away from the equator, as it is not an equal-area projection. You are far better off projecting from Lat / Lon into an equal-area projection that suits, then getting the area.
    – Kelso
    Oct 3, 2011 at 21:52
1

After struggling for 2 days below is the answer for C#. Suppose you got the map coordinates by tapping on the ESRI maps. You can use the below code to convert to latitude and longitude. I am using this for android app.

 // MapPoint[X=-8357774.94648172, Y=4775228.75015334]

MapPoint pointGeometry = new MapPoint(e.Location.X, e.Location.Y, SpatialReferences.WebMercator);

MapPoint tt = (MapPoint)GeometryEngine.Project(pointGeometry, SpatialReferences.Wgs84);

var latitudeVal = tt.X;
var longitude = tt.Y;
0

Here is another Python version with some tests. The top left corner is at (0,0) and the bottom right is at (circumference, circumference), as in Google Maps. Adapted from the Wikipedia Web Mercator article.

# params
radius = 6378137.0 # WGS84 equatorial radius (meters) - used in spherical projection
circumference = 2 * math.pi * radius
latmax = math.degrees(2 * math.atan(math.exp(math.pi)) - math.pi/2) # 85.051128 degrees

def latlngToMeters(lat, lng):
    "Convert lat,lng degrees to meters in Web Mercator projection. 0,0 is at top left corner"
    lat = math.radians(lat)
    lng = math.radians(lng)
    x = radius * (math.pi + lng)
    y = radius * (math.pi - math.log(math.tan(math.pi/4 + lat/2)))
    return x, y

def test_latlngToMeters():
    test(latmax, 85.051128)
    test(latlngToMeters(latmax,-180), (0,0)) # top left corner
    test(latlngToMeters(0,-180), (0,circumference/2)) # left equator
    test(latlngToMeters(0,0), (circumference/2,circumference/2)) # center
    test(latlngToMeters(-latmax,180), (circumference,circumference)) # bottom right corner
1
  • Just a note that this transforms a lat long pair into the Web Mercator coordinate system, but this is not the same as converting it into meters because mercator units have different lengths at different latitudes as a result of the warping required to convert spherical coordinate system into the Web Mercator projection.
    – jonseymour
    Sep 23, 2020 at 3:42
0

Here is an article that explains this really simply:

How to project a point from Geography to Web Mercator in C#

public static MapPoint ProjectPointToWebMercator(MapPoint PointToReproject)
{
            double RadiansPerDegree = Math.PI / 180;
            double Rad = PointToReproject.y * RadiansPerDegree;
            double FSin = Math.Sin(Rad);
            double DegreeEqualsRadians = 0.017453292519943;
            double EarthsRadius = 6378137;

            double y = EarthsRadius / 2.0 * Math.Log((1.0 + FSin) / (1.0 - FSin));
            double x = PointToReproject.x * DegreeEqualsRadians * EarthsRadius;

            return new MapPoint(x, y);
}
2
  • I am sorry but your article doesn't explain anything.
    – blah238
    Mar 16, 2012 at 7:08
  • Yeah, it just has the code to accomplish this. Sorry ... I thought this is what you were looking for.
    – Khattab
    Mar 19, 2012 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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