-
Notifications
You must be signed in to change notification settings - Fork 0
/
Labelizer.py
97 lines (67 loc) · 3.15 KB
/
Labelizer.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
85
86
87
88
89
90
91
92
93
94
95
96
97
from subprocess import run, PIPE
from sys import exit
from ArcLabel import ArcLabel
BLACKBOX_PATH = "../area_labeling/standalone_lib/build/bin/labeling"
LOG_FILE = open("labelizer_log.txt", "a+")
class Labelizer:
def __init__(self):
self.output_dic = {}
def labeling(self, geometries, label_names_dict):
for geoIndex, geometry in geometries.items():
if not geoIndex in label_names_dict:
continue
outer_coordinates = []
inner_coordinates = []
# Geometry is a polygon
if geometry['type'] == 'Polygon':
# line_rings = geometry['coordinates']
outer_coordinates = geometry['coordinates'][0]
if len(geometry['coordinates']) > 1:
inner_coordinates = geometry['coordinates'][1:]
self.blackbox(geoIndex, outer_coordinates, inner_coordinates, label_names_dict[geoIndex])
# Geometry is a multi polygon
elif geometry['type'] == 'MultiPolygon':
current = None
for polygon in geometry['coordinates']:
if current == None or len(current[0]) < len(polygon[0]):
current = polygon
# polygon = geometry['coordinates'][0]
outer_coordinates = current[0]
if len(current) > 1:
inner_coordinates = current[1:]
self.blackbox(geoIndex, outer_coordinates, inner_coordinates, label_names_dict[geoIndex])
else:
print(f"Other geometry: {geometry['type']}")
# raise Exception("Invalid geometry type")
return self.output_dic
def blackbox(self, geoIndex, outer, inner, label_name):
# Estimate Height/Length
aspect = str(1 / (len(label_name) * 0.61))
input_string = aspect + "\n"
for coordinate in outer:
input_string += str(coordinate[0]) + " " + str(coordinate[1]) + " "
input_string += "\n"
if len(inner) > 0:
for hole in inner:
for coordinate in hole:
input_string += str(coordinate[0]) + " " + str(coordinate[1]) + " "
input_string += "\n"
# Logging
LOG_FILE.write(f"Label {label_name}:")
LOG_FILE.write(input_string)
LOG_FILE.write("\n\n\n")
# run black box
label_process = run([BLACKBOX_PATH, "-s"], stdout=PIPE, input=input_string,
encoding='ascii')
if label_process.returncode is not 0:
print("labeling command failed.")
self.output_dic[geoIndex] = None
label_output = label_process.stdout
result = label_output.split()
if len(result) < 6:
print(f"Fail for Poly with {len(inner)} holes :-(")
return
print(f"Subber with {len(inner)} holes")
center = [float(result[0]), float(result[1])]
self.output_dic[geoIndex] = ArcLabel(label_name, center, float(result[4]), float(result[5]), float(result[2]),
float(result[3]))