-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
69 lines (52 loc) · 2.18 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
from shapely.geometry import Polygon
# Given bounding box in (lon, lat) pairs
poly = np.array(
[
[-74.35067099968856, 40.95038628235707],
[-74.35134665517963, 40.49961892079759],
[-73.64880132727227, 40.50053835401126],
[-73.65109221855438, 40.94965116545227],
[-74.35067099968856, 40.95038628235707],
]
)
# Convert to a Shapely Polygon
polygon = Polygon(poly)
# Determine the bounding box width and height in meters
bounds_gdf = gpd.GeoDataFrame(geometry=[polygon], crs="EPSG:4326")
bounds_gdf = bounds_gdf.to_crs("EPSG:3395") # Project to a metric CRS
print(bounds_gdf)
minx, miny, maxx, maxy = bounds_gdf.total_bounds
width = maxx - minx
height = maxy - miny
# Define the size of the squares
square_size = 300 # in meters
# Calculate the number of squares in each dimension
num_squares_x = int(np.ceil(width / square_size))
num_squares_y = int(np.ceil(height / square_size))
# Create a list to store the bounding boxes
bounding_boxes = []
# Create a grid of points based on the number of squares
for i in range(num_squares_x):
for j in range(num_squares_y):
x = minx + i * square_size
y = miny + j * square_size
# Create a bounding box for each grid point
box = Polygon([(x, y), (x + square_size, y), (x + square_size, y + square_size), (x, y + square_size)])
# Append the bounding box to the list
bounding_boxes.append(box)
# Convert the bounding boxes back to (lon, lat) coordinates
bounding_boxes_lonlat = gpd.GeoDataFrame(geometry=bounding_boxes, crs="EPSG:3395")
bounding_boxes_lonlat = bounding_boxes_lonlat.to_crs("EPSG:4326") # Project back to WGS84
# Convert the bounding boxes to a list of (lon, lat) pairs
bounding_boxes_lonlat_list = np.array(
[(box.exterior.xy[0].tolist(), box.exterior.xy[1].tolist()) for box in bounding_boxes_lonlat.geometry]
)
print(bounding_boxes_lonlat_list.shape)
# Now, bounding_boxes_lonlat_list contains the list of bounding boxes in (lon, lat) pairs
plt.plot(poly[:, 0], poly[:, 1], c="green")
for box in bounding_boxes_lonlat_list:
plt.plot(box[0], box[1], c="red", alpha=0.5)
plt.show()