Skip to content

Instantly share code, notes, and snippets.

@deangrant
Created July 3, 2023 16:21
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 deangrant/9c2e92bbf82cfc0538305a71437b9ba6 to your computer and use it in GitHub Desktop.
Save deangrant/9c2e92bbf82cfc0538305a71437b9ba6 to your computer and use it in GitHub Desktop.
Create a polygon and define the distance in meters to extend in each direction (bounding box)
import pyproj
from shapely.geometry import (
Polygon
)
# Define the center point of the bounding box in latitude and longitude.
center_latitude = 40.7128
center_longitude = -74.0060
# Define the distance in meters to extend in each direction
distance = 5000
# Define the projection systems
crs_wgs84 = pyproj.CRS(
'EPSG:4326'
)
crs_utm = pyproj.CRS(
'EPSG:32618'
)
# Create a transformer to convert between coordinate systems
transformer = pyproj.Transformer.from_crs(
crs_wgs84,
crs_utm,
always_xy=True
)
# Convert the center point to UTM coordinates
center_x, center_y = transformer.transform(
center_longitude,
center_latitude
)
# Calculate the bounding box coordinates in UTM
min_x = center_x - distance
max_x = center_x + distance
min_y = center_y - distance
max_y = center_y + distance
# Convert the bounding box coordinates back to latitude and longitude
inverse_transformer = pyproj.Transformer.from_crs(
crs_utm,
crs_wgs84,
always_xy=True
)
min_longitude, min_latitude = inverse_transformer.transform(
min_x,
min_y
)
max_longitude, max_latitude = inverse_transformer.transform(
max_x,
max_y
)
# Create the polygon
polygon = Polygon(
[
(
min_longitude,
min_latitude
),
(
min_longitude,
max_latitude
),
(
max_longitude,
max_latitude
),
(
max_longitude,
min_latitude
)
]
)
# Print the polygon's coordinates
print(
polygon
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment