Created
May 26, 2023 10:59
-
-
Save manuelep/f2e786c2c83ef8502b8e036739196e16 to your computer and use it in GitHub Desktop.
square bbox
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
from pyproj import Transformer | |
import geojson | |
to_metric = Transformer.from_crs(4326, 3857) | |
to_latlon = Transformer.from_crs(3857, 4326) | |
def square(lon:float, lat:float, dim:float, unit:str='area'): | |
""" """ | |
if unit == 'area': | |
length = (dim**.5)/2 | |
else: | |
length = dim/2 | |
xc, yc = to_metric.transform(lon, lat) | |
x_min = xc - length | |
x_max = xc + length | |
y_min = yc - length | |
y_max = yc + length | |
lon_min, lat_min = to_latlon.transform(x_min, y_min) | |
lon_max, lat_max = to_latlon.transform(x_max, y_max) | |
feature = geojson.Feature( | |
properties = {}, | |
geometry = geojson.Polygon([[ | |
(lon_min, lat_min,), | |
(lon_max, lat_min,), | |
(lon_max, lat_max,), | |
(lon_min, lat_max,), | |
(lon_min, lat_min,), | |
]]) | |
) | |
return feature | |
if __name__ == '__main__': | |
feature = square(0, 0, 100) | |
print(feature) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment