-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmz_processor.py
184 lines (161 loc) · 7.01 KB
/
kmz_processor.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import glob, csv
import pandas as pd
from lxml import html
from zipfile import ZipFile
from PIL import Image
from math import sqrt
ZIP_KML_DOC = "doc.kml"
CSV_KML_DOC = "kml_index.csv"
CSV_COORDS = "csv_coords.csv"
ZIP_KMZ_IMG_FOLDER = "files"
KMZ_GLOBAL_IMAGE = "map.jpg"
DF_COLUMNS = ["index", "image", "draw_order", "north", "south", "east", "west", "rotation"]
PRECISION = 1
COLORS = [
(255, 255, 255), #0 white
(220, 190, 250), #1 light purple / pink
(240, 110, 250), #2 purple
(230, 0, 0), #3 red
(255, 85, 10), #4 orange
(255, 255, 0), #5 yellow
(50, 140, 0), #6 green
(0, 150, 210), #7 light blue
(10, 75, 180), #8 blue
(0, 35, 115), #9 dark blue
(128, 128, 128), #10 light grey
(77, 77, 77), #11 dark grey
(0, 0, 0), #12 black
]
pd.options.mode.chained_assignment = None
class KMZ:
def __init__(self, csv=False) -> None:
self.kmz_zip = ZipFile(glob.glob("*.kmz")[0], "r")
self.kml_file = self.kmz_zip.open(ZIP_KML_DOC, "r").read()
self.globe_matrix = []
if csv:
self._load_df(to_csv=csv)
else:
self._load_df()
self._arrange_df()
def _load_data(self, ):
kml_content = html.fromstring(self.kml_file)
temp = []
for item in kml_content.cssselect("Document GroundOverlay"):
image = item.cssselect("name")[0].text_content()
index = image[23:-4]
draw_order = item.cssselect("drawOrder")[0].text_content()
coords = item.cssselect("LatLonBox")[0]
north = float(coords.cssselect("north")[0].text_content())
south = float(coords.cssselect("south")[0].text_content())
east = float(coords.cssselect("east")[0].text_content())
west = float(coords.cssselect("west")[0].text_content())
rotation = coords.cssselect("rotation")[0].text_content()
temp.append([index, image, draw_order, north, south, east, west, rotation])
return temp
def _load_df(self, to_csv=False) -> None:
if to_csv:
self._data_to_csv(self._load_data())
self.df = pd.read_csv(CSV_KML_DOC)
else:
self.df = pd.DataFrame(self._load_data(), columns=DF_COLUMNS)
self.df.sort_values(by='north', ascending=False, inplace = True)
def _data_to_csv(self, data: list) -> None:
with open(CSV_KML_DOC, "w", newline="") as kml_csv:
kml_csv_writer = csv.writer(kml_csv)
kml_csv_writer.writerow(DF_COLUMNS)
for item in data:
kml_csv_writer.writerow(item)
def _arrange_df(self, ) -> None:
for i, row in self.df.iterrows():
sub_df = self.df.loc[(self.df['north'] == row['north']) & (self.df['south'] == row['south'])]
if not sub_df.empty:
sub_df.sort_values(by='west', inplace = True)
self.globe_matrix.append(sub_df)
self.df.drop(sub_df.index, inplace = True)
def _generate_image(self, images: list, fullvh=False, vertical=False, horizontal=False):
if horizontal:
widths, heights = zip(*(img.size for img in images))
total_width = sum(widths)
max_height = max(heights)
new_image = Image.new('RGB', (total_width, max_height))
x_offset = 0
for img in images:
new_image.paste(img, (x_offset,0))
x_offset += img.size[0]
elif vertical:
widths, heights = zip(*(img.size for img in images))
max_width = max(widths)
total_height = sum(heights)
new_image = Image.new('RGB', (max_width, total_height))
y_offset = 0
for img in images:
new_image.paste(img, (0,y_offset))
y_offset += img.size[1]
elif fullvh:
vertical_set = [self._generate_image(image, horizontal=True) for image in images]
new_image = self._generate_image(vertical_set, vertical=True)
return new_image
def _closest_color(self, rgb: list) -> tuple:
r, g, b = rgb
color_diffs = []
for color in COLORS:
cr, cg, cb = color
color_diff = sqrt(abs(r - cr) ** 2 + abs(g - cg) ** 2 + abs(b - cb) ** 2)
color_diffs.append((color_diff, color))
return min(color_diffs)[1]
def coords_item(self, coords: list) -> list:
if coords[0] > 0: # first 10
gset = [None, -7]
if coords[1] > 0: # last 21
sset = [22, None]
else:
sset = [None, -21]
else: # last 7
gset = [10, None]
if coords[1] > 0: # last 21
sset = [22, None]
else:
sset = [None, -21]
for item in self.globe_matrix[gset[0]:gset[1]]:
for i, row in item.iloc[sset[0]:sset[1]].iterrows():
if (row['north'] >= coords[0] >= row['south']) and (row['west'] <= coords[1] <= row['east']):
return row.tolist()
def load_images(self, images, single=False, neighbours=False) -> list:
if single:
if neighbours:
f = [images[:-7], images[-4:]]
c = int(images[:-4][-3:])
images = [
[c+42 , c+43, c+44],
[c-1 , c , c+1],
[c-44, c-43, c-42],
]
edges = [0, 0, 0, 0]
for item in self.globe_matrix:
for i, row in item.iterrows():
row = row.tolist()
if int(row[0]) == images[0][0]:
edges[0] = row[3]
edges[3] = row[6]
elif int(row[0]) == images[2][2]:
edges[1] = row[4]
edges[2] = row[5]
for i, s in enumerate(images):
for j, g in enumerate(s):
images[i][j] = f[0]+str(g)+f[1]
return edges, self._generate_image(self.load_images(images), fullvh=True)
else:
return Image.open(self.kmz_zip.open(ZIP_KMZ_IMG_FOLDER+"/"+images))
else:
if images:
if type(images[0]) == list:
kmz_imgs = [[Image.open(self.kmz_zip.open(ZIP_KMZ_IMG_FOLDER+"/"+image)) for image in image_set] for image_set in images]
else:
kmz_imgs = [Image.open(self.kmz_zip.open(ZIP_KMZ_IMG_FOLDER+"/"+image)) for image in images]
else:
kmz_imgs = [Image.open(self.kmz_zip.open(image)) for image in self.kmz_zip.namelist() if image.split("/")[0] == ZIP_KMZ_IMG_FOLDER]
return kmz_imgs
def global_imager(self, images=[]) -> None:
if images == []:
images = [self.load_images(matrix["image"].tolist()) for matrix in self.globe_matrix]
self._generate_image(images, fullvh=True).save(KMZ_GLOBAL_IMAGE)