-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCellSquare.py
84 lines (67 loc) · 2.32 KB
/
CellSquare.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python2.7
# Geometry of squared cells defined by origin, rotation and cell_size
import math
import geometry
class CellSquare:
# defining constructor
# origin is the upper left corner
# todo rename table_rotation in rotation
def __init__(self, origin, table_rotation, cell_size, cell_id):
self.origin = origin
self.table_rotation = table_rotation
self.cell_size = cell_size
self.cell_id = cell_id
# defining class methods
def get_origin(self):
return self.origin
def get_table_rotation(self):
return self.table_rotation
def get_cell_size(self):
return self.cell_size
def get_cell_id(self):
return self.cell_id
def get_upper_right_corner(self):
return self.get_cell_corner(90)
def get_lower_right_corner(self):
return self.get_cell_corner(135)
def get_lower_left_corner(self):
return self.get_cell_corner(180)
# gets the cell corner as a point with coordinates.
def get_cell_corner(self, angle):
if angle % 90 == 0:
distance = self.get_cell_size()
elif angle % 45 == 0:
distance = self.get_cell_size() * math.sqrt(2)
else:
raise Exception('The angle does not correspond to a corner in a square. Given angle: {}'.format(angle))
return geometry.get_point_in_distance(self.get_origin(), distance, angle, self.get_table_rotation())
# order of coordinates following right hand rule
def get_polygon_coord(self):
return [
[
self.get_origin().x,
self.get_origin().y
],
[
self.get_lower_left_corner().x,
self.get_lower_left_corner().y
],
[
self.get_lower_right_corner().x,
self.get_lower_right_corner().y
],
[
self.get_upper_right_corner().x,
self.get_upper_right_corner().y
],
# coordinates of a polygon need to form a closed linestring
[
self.get_origin().x,
self.get_origin().y
],
]
def get_the_geom(self):
return {
"type": "Polygon",
"coordinates": [self.get_polygon_coord()]
}