Last active
February 28, 2022 01:38
-
-
Save ciscorn/11b459835b565a390dba14403f576e01 to your computer and use it in GitHub Desktop.
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
import math | |
import numpy as np | |
from matplotlib import pyplot as plt | |
def webMercatorToLngLat(mx: float, my: float) -> tuple[float, float]: | |
lng = (mx * 2 - 1) * 180.0 | |
lat = (my * 2 - 1) * 180.0 | |
lat = ( | |
-180 | |
/ np.pi | |
* (2 * np.arctan(np.exp(lat * np.pi / 180.0)) - np.pi / 2.0) | |
) | |
return lng, lat | |
def make_rect(x1, y1, x2, y2): | |
x_top = np.linspace(x1, x2, 20) | |
y_top = np.linspace(y1, y1, 20) | |
x_right = np.linspace(x2, x2, 20) | |
y_right = np.linspace(y1, y2, 20) | |
x_bottom = np.linspace(x2, x1, 20) | |
y_bottom = np.linspace(y2, y2, 20) | |
x_left = np.linspace(x1, x1, 20) | |
y_left = np.linspace(y2, y1, 20) | |
x_diag = np.linspace(x1, x2, 20) | |
y_diag = np.linspace(y1, y2, 20) | |
theta = np.linspace(0, 2 * np.pi, 200) | |
x_circle = x1 + (x2 - x1) * (np.cos(theta) / 2 + 0.5) | |
y_circle = y1 + (x2 - x1) * (np.sin(theta) / 2 + 0.5) | |
x = np.concatenate((x_top, x_right, x_bottom, x_left, x_diag)) | |
y = np.concatenate((y_top, y_right, y_bottom, y_left, y_diag)) | |
mx, my = webMercatorToLngLat(x, y) | |
mx_circle, my_circle = webMercatorToLngLat(x_circle, y_circle) | |
fig = plt.figure() | |
ax1 = fig.add_subplot(1, 2, 1) | |
ax1.plot(x, y, 'b.-') | |
ax1.plot(x_circle, y_circle, 'r.-') | |
ax1.set_title("Web Mercator") | |
ax1.invert_yaxis() | |
ax2 = fig.add_subplot(1, 2, 2) | |
ax2.plot(mx, my, 'b.-') | |
ax2.plot(mx_circle, my_circle, 'r.-') | |
ax2.set_title("WGS84") | |
fig.tight_layout() | |
plt.show() | |
print(make_rect(0.85, 0.30, 0.87, 0.32)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment