-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmax_rect.py
More file actions
137 lines (122 loc) · 4.55 KB
/
Copy pathmax_rect.py
File metadata and controls
137 lines (122 loc) · 4.55 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
import cvxpy
# from shapely.geometry import Polygon
import shapely
from dem_utils import get_lonlat_geometry
# def rect2poly(ll, ur):
# """
# Convert rectangle defined by lower left/upper right
# to a closed polygon representation.
# """
# x0, y0 = ll
# x1, y1 = ur
# return [
# [x0, y0],
# [x0, y1],
# [x1, y1],
# [x1, y0],
# [x0, y0]
# ]
# def get_intersection(coords):
# """Given an input list of coordinates, find the intersection
# section of corner coordinates. Returns geojson of the
# interesection polygon.
# """
# ipoly = None
# for coord in coords:
# if ipoly is None:
# ipoly = shapely.geometry.Polygon(coord)
# else:
# tmp = shapely.geometry.Polygon(coord)
# ipoly = ipoly.intersection(tmp)
# # close the polygon loop by adding the first coordinate again
# first_x = ipoly.exterior.coords.xy[0][0]
# first_y = ipoly.exterior.coords.xy[1][0]
# ipoly.exterior.coords.xy[0].append(first_x)
# ipoly.exterior.coords.xy[1].append(first_y)
# inter_coords = zip(
# ipoly.exterior.coords.xy[0], ipoly.exterior.coords.xy[1])
# inter_gj = {"geometry":
# {"coordinates": [inter_coords],
# "type": "Polygon"},
# "properties": {}, "type": "Feature"}
# return inter_gj, inter_coords
def two_pts_to_line(pt1, pt2):
"""
Create a line from two points in form of
a1(x) + a2(y) = b
"""
pt1 = [float(p) for p in pt1]
pt2 = [float(p) for p in pt2]
try:
slp = (pt2[1] - pt1[1]) / (pt2[0] - pt1[0])
except ZeroDivisionError:
slp = 1e5 * (pt2[1] - pt1[1])
a1 = -slp
a2 = 1.
b = -slp * pt1[0] + pt1[1]
return a1, a2, b
def pts_to_leq(coords):
"""
Converts a set of points to form Ax = b, but since
x is of length 2 this is like A1(x1) + A2(x2) = B.
returns A1, A2, B
"""
A1 = []
A2 = []
B = []
for i in range(len(coords) - 1):
pt1 = coords[i]
pt2 = coords[i + 1]
a1, a2, b = two_pts_to_line(pt1, pt2)
A1.append(a1)
A2.append(a2)
B.append(b)
return A1, A2, B
def get_maximal_rectangle(polygon:shapely.geometry.Polygon,buffer_val=0.001):
"""
Find the largest, inscribed, axis-aligned rectangle.
:param coordinates:
A list of of [x, y] pairs describing a closed, convex polygon.
Obtained from https://github.com/planetlabs/maxrect
and edited with https://stackoverflow.com/questions/70586297/cvxpy-solvers-produce-solutions-of-different-shapes
"""
#Get lon/lat from shapely polygon
lon,lat = get_lonlat_geometry(polygon,append_nan=False)
coordinates = [[ln,lt] for ln,lt in zip(lon,lat)]
coordinates = np.array(coordinates)
x_range = np.max(coordinates, axis=0)[0]-np.min(coordinates, axis=0)[0]
y_range = np.max(coordinates, axis=0)[1]-np.min(coordinates, axis=0)[1]
scale = np.array([x_range, y_range])
sc_coordinates = coordinates/scale
poly = shapely.geometry.Polygon(sc_coordinates)
inside_pt = (poly.representative_point().x,
poly.representative_point().y)
A1, A2, B = pts_to_leq(sc_coordinates)
bl = cvxpy.Variable(2)
tr = cvxpy.Variable(2)
br = cvxpy.Variable(2)
tl = cvxpy.Variable(2)
obj = cvxpy.Maximize(cvxpy.log(tr[0] - bl[0]) + cvxpy.log(tr[1] - bl[1]))
constraints = [bl[0] == tl[0],
br[0] == tr[0],
tl[1] == tr[1],
bl[1] == br[1],
]
for i in range(len(B)):
if inside_pt[0] * A1[i] + inside_pt[1] * A2[i] <= B[i]:
constraints.append(bl[0] * A1[i] + bl[1] * A2[i] <= B[i])
constraints.append(tr[0] * A1[i] + tr[1] * A2[i] <= B[i])
constraints.append(br[0] * A1[i] + br[1] * A2[i] <= B[i])
constraints.append(tl[0] * A1[i] + tl[1] * A2[i] <= B[i])
else:
constraints.append(bl[0] * A1[i] + bl[1] * A2[i] >= B[i])
constraints.append(tr[0] * A1[i] + tr[1] * A2[i] >= B[i])
constraints.append(br[0] * A1[i] + br[1] * A2[i] >= B[i])
constraints.append(tl[0] * A1[i] + tl[1] * A2[i] >= B[i])
prob = cvxpy.Problem(obj, constraints)
# prob.solve(solver=cvxpy.CVXOPT, verbose=False, max_iters=1000, reltol=1e-9)
prob.solve()
bottom_left = np.array(bl.value).T * scale
top_right = np.array(tr.value).T * scale
return bottom_left[0]+buffer_val,bottom_left[1]+buffer_val,top_right[0]-buffer_val,top_right[1]-buffer_val