Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

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)
share|improve this answer
    
Does this return answer in meters? Can I use the resulting coordinates to compute area in meter square of a rectangle? –  Hasan Khan Oct 3 '11 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 '11 at 6:51
    
What is sm_a in your code? Where are these bounds used in your formula? Again is the answer in meters? –  Hasan Khan Oct 3 '11 at 6:57
    
Sorry: `# Ellipsoid model constants (actual values here are for WGS84) # sm_a = 6378137.0 sm_b = 6356752.314 –  Hairy Oct 3 '11 at 6:58
    
Hairy you've not yet answered my question whether your function results are in meters. –  Hasan Khan Oct 3 '11 at 7:07
show 8 more comments

Here is an article that explains this really simply:

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

share|improve this answer
    
I am sorry but your article doesn't explain anything. –  blah238 Mar 16 '12 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 '12 at 7:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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