Select to view content in your preferred language

Lat/Long to Web Mercator

12968
3
Jump to solution
03-03-2014 07:06 AM
TonySmith3
Emerging Contributor
Can someone enlighten me with the following problem?

I have an x,y lat/long values and want to convert them to the equivalent web mercator coordinates in meters, what's my best approach?  I can't seem to find anything in arcpy that will take 2 lat/long and give me 2 web mercator x,y.  Thought?  Thanks in advance!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
I don't believe anything is built in that has the functionality. You can pass geometry objects and use project to change coordinate systems. Otherwise it is a fairly simple and well known formula.

I have a function that works, but I am not sure where it came from.

def geographic_to_web_mercator(x_lon, y_lat):     if abs(x_lon) <= 180 and abs(y_lat) < 90:          num = x_lon * 0.017453292519943295         x = 6378137.0 * num         a = y_lat * 0.017453292519943295          x_mercator = x         y_mercator = 3189068.5 * math.log((1.0 + math.sin(a)) / (1.0 - math.sin(a)))         return x_mercator, y_mercator      else:         print('Invalid coordinate values for conversion')      

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Honored Contributor
I don't believe anything is built in that has the functionality. You can pass geometry objects and use project to change coordinate systems. Otherwise it is a fairly simple and well known formula.

I have a function that works, but I am not sure where it came from.

def geographic_to_web_mercator(x_lon, y_lat):     if abs(x_lon) <= 180 and abs(y_lat) < 90:          num = x_lon * 0.017453292519943295         x = 6378137.0 * num         a = y_lat * 0.017453292519943295          x_mercator = x         y_mercator = 3189068.5 * math.log((1.0 + math.sin(a)) / (1.0 - math.sin(a)))         return x_mercator, y_mercator      else:         print('Invalid coordinate values for conversion')      
0 Kudos
NeilAyres
MVP Alum
Matt,
will save that bit of code for later reference....

But surely the Arc way to do it is via the spatial reference of the data frame. Load the GCS data, change the df SR to web Mercator, then export the data into a new dataset, or calculate new XY field values using the coord sys of the data frame.

In python using either an update or insert cursor use the .projectAs(arcpy.SpatialReference(3857)) method on the geometry object.
3857 is the WKID of the web Mercator aux sphere projection.

Or just use the Project tool in the tool box.

Cheers,
Neil
0 Kudos
MathewCoyle
Honored Contributor

But surely the Arc way to do it ...


Yes, you are right Neil. I had forgotten about the projectAs method on geometry objects. I haven't done much testing with it and am not sure about how efficient it is, but I imagine that it would garner the same results the OP is after. At that point the only advantage to mine is that it does not require an arc license.
0 Kudos