-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype3.py
424 lines (350 loc) · 19.3 KB
/
prototype3.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Form implementation generated from reading ui file 'prototype1.ui'
#
# Created by: PyQt6 UI code generator 6.4.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtWidgets
from PyQt6.QtWidgets import QFileDialog, QSplashScreen
from PyQt6.QtCore import QThread, QObject, pyqtSignal, Qt
from PyQt6.uic import loadUi
from PyQt6.QtGui import QPixmap, QIcon
import sys
import os
import rasterio as rast
from osgeo import gdal
import time
if getattr(sys, 'frozen', False):
import pyi_splash
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
datadir = resource_path("data")
class Ui_Compress(object):
def setupUi(self, Compress):
Compress.setObjectName("Compress")
Compress.resize(448, 363)
Compress.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(parent=Compress)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.gridLayout_3 = QtWidgets.QGridLayout()
self.gridLayout_3.setObjectName("gridLayout_3")
self.label_2 = QtWidgets.QLabel(parent=self.centralwidget)
self.label_2.setAutoFillBackground(True)
self.label_2.setObjectName("label_2")
self.gridLayout_3.addWidget(self.label_2, 0, 0, 1, 1)
self.textBrowser = QtWidgets.QTextBrowser(parent=self.centralwidget)
self.textBrowser.setObjectName("textBrowser")
self.gridLayout_3.addWidget(self.textBrowser, 0, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(parent=self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout_3.addWidget(self.pushButton, 0, 2, 1, 1)
self.label = QtWidgets.QLabel(parent=self.centralwidget)
self.label.setAutoFillBackground(True)
self.label.setObjectName("label")
self.gridLayout_3.addWidget(self.label, 1, 0, 1, 1)
self.textBrowser_2 = QtWidgets.QTextBrowser(parent=self.centralwidget)
self.textBrowser_2.setObjectName("textBrowser_2")
self.gridLayout_3.addWidget(self.textBrowser_2, 1, 1, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(parent=self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout_3.addWidget(self.pushButton_2, 1, 2, 1, 1)
self.gridLayout.addLayout(self.gridLayout_3, 0, 0, 1, 2)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.verticalLayout_3 = QtWidgets.QVBoxLayout()
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.label_6 = QtWidgets.QLabel(parent=self.centralwidget)
self.label_6.setAutoFillBackground(True)
self.label_6.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight|QtCore.Qt.AlignmentFlag.AlignTrailing|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.label_6.setObjectName("label_6")
self.verticalLayout_3.addWidget(self.label_6)
self.label_3 = QtWidgets.QLabel(parent=self.centralwidget)
self.label_3.setAutoFillBackground(True)
self.label_3.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight|QtCore.Qt.AlignmentFlag.AlignTrailing|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.label_3.setObjectName("label_3")
self.verticalLayout_3.addWidget(self.label_3)
self.label_4 = QtWidgets.QLabel(parent=self.centralwidget)
self.label_4.setAutoFillBackground(True)
self.label_4.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight|QtCore.Qt.AlignmentFlag.AlignTrailing|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.label_4.setObjectName("label_4")
self.verticalLayout_3.addWidget(self.label_4)
self.horizontalLayout_2.addLayout(self.verticalLayout_3)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalSlider = QtWidgets.QSlider(parent=self.centralwidget)
self.horizontalSlider.setMinimum(0)
self.horizontalSlider.setMaximum(20)
self.horizontalSlider.setSliderPosition(10)
self.horizontalSlider.setOrientation(QtCore.Qt.Orientation.Horizontal)
self.horizontalSlider.setTickPosition(QtWidgets.QSlider.TickPosition.TicksBelow)
self.horizontalSlider.setTickInterval(25)
self.horizontalSlider.setSingleStep(5)
self.horizontalSlider.setObjectName("horizontalSlider")
self.horizontalLayout.addWidget(self.horizontalSlider)
self.label_7 = QtWidgets.QLabel(parent=self.centralwidget)
self.label_7.setAutoFillBackground(True)
self.label_7.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight|QtCore.Qt.AlignmentFlag.AlignTrailing|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.label_7.setObjectName("label_7")
self.horizontalLayout.addWidget(self.label_7)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.comboBox = QtWidgets.QComboBox(parent=self.centralwidget)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.verticalLayout_2.addWidget(self.comboBox)
self.comboBox_2 = QtWidgets.QComboBox(parent=self.centralwidget)
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.verticalLayout_2.addWidget(self.comboBox_2)
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
self.verticalLayout_4 = QtWidgets.QVBoxLayout()
self.verticalLayout_4.setObjectName("verticalLayout_4")
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.verticalLayout_4.addItem(spacerItem)
spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.verticalLayout_4.addItem(spacerItem1)
spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.verticalLayout_4.addItem(spacerItem2)
self.horizontalLayout_2.addLayout(self.verticalLayout_4)
self.gridLayout.addLayout(self.horizontalLayout_2, 1, 0, 1, 1)
self.gridLayout_2 = QtWidgets.QGridLayout()
self.gridLayout_2.setObjectName("gridLayout_2")
spacerItem3 = QtWidgets.QSpacerItem(20, 15, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
self.gridLayout_2.addItem(spacerItem3, 0, 0, 1, 1)
spacerItem4 = QtWidgets.QSpacerItem(20, 15, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
self.gridLayout_2.addItem(spacerItem4, 0, 1, 1, 1)
self.pushButton_4 = QtWidgets.QPushButton(parent=self.centralwidget)
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout_2.addWidget(self.pushButton_4, 1, 0, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(parent=self.centralwidget)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout_2.addWidget(self.pushButton_3, 1, 1, 1, 1)
self.gridLayout.addLayout(self.gridLayout_2, 1, 1, 1, 1)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.line = QtWidgets.QFrame(parent=self.centralwidget)
self.line.setFrameShape(QtWidgets.QFrame.Shape.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Shadow.Sunken)
self.line.setObjectName("line")
self.verticalLayout.addWidget(self.line)
self.progressBar = QtWidgets.QProgressBar(parent=self.centralwidget)
self.progressBar.setProperty("value", 0)
self.progressBar.setAlignment(QtCore.Qt.AlignmentFlag.AlignLeading|QtCore.Qt.AlignmentFlag.AlignLeft|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.progressBar.setObjectName("progressBar")
self.verticalLayout.addWidget(self.progressBar)
self.label_5 = QtWidgets.QLabel(parent=self.centralwidget)
self.label_5.setObjectName("label_5")
self.verticalLayout.addWidget(self.label_5, 0, QtCore.Qt.AlignmentFlag.AlignHCenter)
self.gridLayout.addLayout(self.verticalLayout, 2, 0, 1, 2)
Compress.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(parent=Compress)
self.menubar.setGeometry(QtCore.QRect(0, 0, 448, 22))
self.menubar.setObjectName("menubar")
self.menuMenu = QtWidgets.QMenu(parent=self.menubar)
self.menuMenu.setObjectName("menuMenu")
Compress.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(parent=Compress)
self.statusbar.setObjectName("statusbar")
Compress.setStatusBar(self.statusbar)
self.menubar.addAction(self.menuMenu.menuAction())
if getattr(sys, 'frozen', False):
pyi_splash.close()
self.retranslateUi(Compress)
QtCore.QMetaObject.connectSlotsByName(Compress)
def retranslateUi(self, Compress):
_translate = QtCore.QCoreApplication.translate
Compress.setWindowTitle(_translate("Compress", "Compress & Reduce TIFF"))
self.label_2.setText(_translate("Compress", "Input File:"))
self.textBrowser.setHtml(_translate("Compress", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"hr { height: 1px; border-width: 0; }\n"
"li.unchecked::marker { content: \"\\2610\"; }\n"
"li.checked::marker { content: \"\\2612\"; }\n"
"</style></head><body style=\" font-family:\'Segoe UI\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
self.pushButton.setText(_translate("Compress", "Browse"))
self.label.setText(_translate("Compress", "Destination Folder"))
self.textBrowser_2.setHtml(_translate("Compress", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"hr { height: 1px; border-width: 0; }\n"
"li.unchecked::marker { content: \"\\2610\"; }\n"
"li.checked::marker { content: \"\\2612\"; }\n"
"</style></head><body style=\" font-family:\'Segoe UI\'; font-size:9pt; font-weight:400; font-style:normal;\">\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
self.pushButton_2.setText(_translate("Compress", "Browse"))
self.label_6.setText(_translate("Compress", "Compression Level"))
self.label_3.setText(_translate("Compress", "Compression Types"))
self.label_4.setText(_translate("Compress", "No. of tiles"))
self.label_7.setText(_translate("Compress", "50%"))
self.comboBox.setPlaceholderText(_translate("Compress", "LZW"))
self.comboBox.setItemText(0, _translate("Compress", "JPEG (lossy, small size)"))
self.comboBox.setItemText(1, _translate("Compress", "LZW"))
self.comboBox.setItemText(2, _translate("Compress", "Deflate"))
self.comboBox_2.setPlaceholderText(_translate("Compress", "16"))
self.comboBox_2.setItemText(0, _translate("Compress", "1"))
self.comboBox_2.setItemText(1, _translate("Compress", "4"))
self.comboBox_2.setItemText(2, _translate("Compress", "9"))
self.comboBox_2.setItemText(3, _translate("Compress", "16"))
self.comboBox_2.setItemText(4, _translate("Compress", "25"))
self.pushButton_4.setText(_translate("Compress", "Reset"))
self.pushButton_3.setText(_translate("Compress", "Compress"))
self.label_5.setText(_translate("Compress", "Progress"))
self.menuMenu.setTitle(_translate("Compress", "Menu"))
self.pushButton.clicked.connect(self.pushButton_handler)
self.pushButton_2.clicked.connect(self.pushButton2_handler)
self.comboBox.activated.connect(self.get_compression_type)
self.comboBox_2.activated.connect(self.get_number_of_tiles)
self.pushButton_3.clicked.connect(self.compress_worker)
self.pushButton_4.clicked.connect(self.reset_all)
self.horizontalSlider.valueChanged.connect(self.get_level)
def pushButton_handler(self):
global inputfile
self.textBrowser.clear()
filename = QFileDialog.getOpenFileName()
inputfile = filename[0]
self.textBrowser.append(inputfile)
print(inputfile)
def pushButton2_handler(self):
global destfolder
self.textBrowser_2.clear()
destfolder = QFileDialog.getExistingDirectory(None,"Select a Directory")
self.textBrowser_2.append(destfolder)
print(destfolder)
def get_level(self):
global compression_level
slider_value = self.horizontalSlider.value()*5
if slider_value == 100:
slider_value = 99
elif slider_value == 0:
slider_value = 1
compression_level = str(100-slider_value)
self.label_7.setText(str(slider_value) + "%")
def get_compression_type(self):
global compression_index
compression_index = int(self.comboBox.currentIndex())
print(compression_index)
def get_number_of_tiles(self):
global tiles
tiles = int(self.comboBox_2.currentIndex()) + 1
print(tiles)
def error_check(self):
global inputfile, destfolder
if inputfile == '' or destfolder == '':
self.label_5.setStyleSheet("color: red")
self.label_5.setText("Missing inputs, please make sure an input file and an output folder is selected")
return True
elif inputfile[-3:] != 'tif':
self.label_5.setStyleSheet("color: red")
self.label_5.setText("Invalid file type, please select a tif file")
return True
return False
def compress_worker(self):
#check if input is valid
if self.error_check():
return None
self.label_5.setStyleSheet("color: black")
self.label_5.setText("Compressing...")
self.thread = QThread()
# Step 3: Create a worker object
self.worker = Worker()
# Step 4: Move worker to the thread
self.worker.moveToThread(self.thread)
# Step 5: Connect signals and slots
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.progress.connect(self.reportProgress)
# Step 6: Start the thread
self.thread.start()
self.pushButton_3.setEnabled(False)
self.pushButton_4.setEnabled(False)
self.thread.finished.connect(
lambda: self.pushButton_3.setEnabled(True)
)
self.thread.finished.connect(
lambda: self.pushButton_4.setEnabled(True)
)
self.thread.finished.connect(
lambda: self.label_5.setText("Finished!")
)
def reportProgress(self,progress):
progressValue = int((progress/(tiles*tiles))*100)
self.progressBar.setValue(progressValue)
def reset_all(self):
global inputfile,destfolder,compression_index,tiles
self.textBrowser.clear()
self.textBrowser_2.clear()
self.comboBox.setCurrentIndex(0)
self.comboBox_2.setCurrentIndex(0)
self.progressBar.setValue(0)
self.horizontalSlider.setValue(10)
self.label_5.setText("Progress")
self.label_7.setText("50%")
inputfile = ""
destfolder = ""
compression_index = 1
tiles = 1
class Worker(QObject):
finished = pyqtSignal()
progress = pyqtSignal(int)
def run(self):
compression=["JPEG","LZW","DEFLATE"]
output_filename = destfolder + r"\tile_"
#open file using rasterio to get dimensions
raster = rast.open(inputfile)
#determine the number of files generated (divide by 4 means 4 rows, 4 columns, 16 files total)
tile_size_x = int((raster.width/tiles)+(raster.width%tiles != 0))
tile_size_y = int((raster.height/tiles)+(raster.height%tiles != 0))
ds = gdal.Open(inputfile)
band = ds.GetRasterBand(1)
xsize = band.XSize
ysize = band.YSize
iteration = 0
for i in range(0, xsize, tile_size_x):
for j in range(0, ysize, tile_size_y):
#try delete file if it already exists
try:
os.remove(str(output_filename) + str(i) + "_" + str(j) + ".tif")
except OSError:
pass
#string to pass to GDAL
#com_string = r"gdal_translate -of GTIFF -outsize 50% 50% -co COMPRESS=LZW -srcwin " + str(i)+ " " + str(j) + " " + str(tile_size_x) + " " + str(tile_size_y) + " " + str(input_filename) + " " + str(output_filename) + str(i) + "_" + str(j) + ".tif"
#com_string = r"gdal_translate -of GTIFF -outsize "+compression_level+"% "+compression_level+r"% -co COMPRESS="+compression[compression_index]+" -srcwin " + str(i)+ " " + str(j) + " " + str(tile_size_x) + " " + str(tile_size_y) + " " + str(inputfile) + " " + str(output_filename) + str(i) + "_" + str(j) + ".tif"
#com_string = r"gdal_translate -of GTIFF -co COMPRESS=LERC -co MAX_Z_ERROR = 0.01 " + str(input_filename) + r" C:\Users\User\Desktop\learnpy\outputtest.tif"
#print(com_string)
#os.system(com_string)
dest = str(output_filename) + str(i) + "_" + str(j) + ".tif"
options = gdal.TranslateOptions(format="GTIFF",widthPct=int(compression_level),heightPct=int(compression_level),creationOptions=["COMPRESS="+compression[compression_index]],srcWin=[i,j,tile_size_x,tile_size_y])
gdal.Translate(dest,inputfile,options=options)
iteration = iteration + 1
self.progress.emit(iteration)
self.finished.emit()
if __name__ == "__main__":
print("Python Code Starts Here")
inputfile = ""
destfolder = ""
compression_level = "50"
compression_index = 0
tiles = 1
os.environ['PROJ_LIB'] = os.path.join(datadir,"proj")
app = QtWidgets.QApplication(sys.argv)
app.setWindowIcon(QIcon(os.path.join(datadir, "sat.ico")))
Form = QtWidgets.QMainWindow()
ui = Ui_Compress()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec())