Skip to content

Instantly share code, notes, and snippets.

@manuelep
Created May 26, 2023 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelep/f2e786c2c83ef8502b8e036739196e16 to your computer and use it in GitHub Desktop.
Save manuelep/f2e786c2c83ef8502b8e036739196e16 to your computer and use it in GitHub Desktop.
square bbox
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