Created
August 9, 2017 20:12
-
-
Save lukecampbell/495002fd829ebcdb4b0c400e062099d0 to your computer and use it in GitHub Desktop.
Converting web mercator projections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scale_factor = np.pi/128. | |
inverse_scale_factor = 1/scale_factor | |
def lam(x, z): | |
return np.rad2deg(x / np.power(2, zoom) * scale_factor - np.pi) | |
def phi(y, z): | |
angle = np.pi - scale_factor * y/(np.power(2, z)) | |
return np.rad2deg(2 * np.arctan(np.exp(angle)) - np.pi/2) | |
def easting(lam, z): | |
return inverse_scale_factor * np.power(2, z) * (lam + np.pi) | |
def northing(phi, z): | |
angle = np.pi - np.log(np.tan(np.pi/4 + phi/2)) | |
return inverse_scale_factor * np.power(2, z) * angle | |
def convert2latlon(x, z): | |
return np.array([ | |
lam(x[0], z), | |
phi(x[1], z) | |
]) | |
def convert2px(x, z): | |
return np.array([ | |
easting(np.deg2rad(x[0]), z), | |
northing(np.deg2rad(x[1]), z) | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment