-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh.py
402 lines (335 loc) · 14.5 KB
/
gh.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
"""
Tools to help with various GIS tasks
Robert Ross Wardrup
"""
import sys
import os
from pathlib import Path
from matplotlib import pyplot as plt
from gui import QtWidgets, Ui_MainWindow
from vector import meta
from raster import measurements
from spatial_functions.calculations import Convert, origin_calc
from raster import meta as raster_functions
from shapely.geometry import Polygon
import shutil
from PyQt5.QtWidgets import QHeaderView, QTableWidgetItem
anaconda_dir = os.path.join(str(Path.home()), "anaconda3\\envs\\GIS-Helper")
print("Anaconda3 Dir: {}".format(anaconda_dir))
os.environ['GDAL_DATA'] = os.path.join(anaconda_dir, 'Library\\share\\gdal')
os.environ['PROJ_LIB'] = os.path.join(anaconda_dir, 'Library\\share\\proj')
class GisHelper(QtWidgets.QMainWindow, Ui_MainWindow):
"""
Main user interface class.
"""
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.setFixedSize(self.size())
self.catalogTiffOutputWindow.setColumnCount(5)
self.catalogTiffOutputWindow.setHorizontalHeaderLabels(['Filename',
'ULX', 'ULY',
'LRX', 'LRY'])
self.copyTiffOutputWindow.setColumnCount(2)
self.copyTiffOutputWindow.setColumnWidth(0, 380)
self.copyTiffOutputWindow.setColumnWidth(1, 379)
self.copyTiffOutputWindow.setHorizontalHeaderLabels(["Source",
"Destination"])
self.catalogTiffOutputWindow.horizontalHeader().\
setSectionResizeMode(0, QHeaderView.Stretch)
self.shape_functions = meta.PolygonFunctions()
# GIS Calculator page
self.originCalculateButton.clicked.connect(self.get_origin)
self.originClearButton.clicked.connect(self.clear_origin_fields)
self.convCoordCalc.clicked.connect(self.dd_dms_chooser)
self.convCoordClear.clicked.connect(self.clear_convert_fields)
self.shapefileViewBrowseButton.clicked.connect(self.browse_for_shp)
self.shapefileViewGo.clicked.connect(self.display_shapefile)
self.catalogTiffBrowseButton.clicked.connect(self.browse_for_raster)
self.catalogTiffBOutputrowseButton.clicked.\
connect(self.browse_catalog_output)
self.catalogTiffProcess.clicked.connect(self.get_raster_bounds)
self.dmsToDD.setChecked(True)
# Copy Tiffs page
self.BrowseForTifDir.clicked.connect(self.browse_for_tiff_directory)
self.BrowseForIntShape.\
clicked.connect(self.browse_for_intersecting_shp)
self.BrowseForGeoTiffOutputDir.\
clicked.connect(self.browse_for_output_directory)
self.CopyTiffProcess.clicked.connect(self.handle_tiff_copy)
self.error_popup = QtWidgets.QMessageBox()
plt.rcParams['toolbar'] = 'None'
def browse_for_intersecting_shp(self):
"""
Browse system for files
:return:
"""
openfile = QtWidgets.QFileDialog.getOpenFileName(self, "Open Intersecting Shapefile", None, "shp(*.shp)")[0]
self.intersectingShapefileEdit.setText(openfile)
def browse_for_tiff_directory(self):
"""
Browse for file
:return:
"""
openfile = QtWidgets.QFileDialog.getExistingDirectory(self)
self.TiffDirectory.setText(openfile)
def browse_for_output_directory(self):
"""
Browse for file
:return:
"""
openfile = QtWidgets.QFileDialog.getExistingDirectory(self)
self.geoTiffOutputDirEdit.setText(openfile)
def browse_for_shp(self):
"""
Browse file system for files
:return:
"""
# This now returns a tuple as of Qt5
openfile = QtWidgets.QFileDialog.getOpenFileName(self)[0]
self.shapefileViewPath.setText(openfile)
def browse_for_raster(self):
"""
Browse file system for files
:return:
"""
open_dir = QtWidgets.QFileDialog.getExistingDirectory(self,
"Select Raster "
"Directory")
self.geoTiffDir1.setText(open_dir)
def browse_catalog_output(self):
"""
Browse for shapefile catalog output
:return:
"""
open_dir = QtWidgets.QFileDialog.\
getExistingDirectory(self, "Select output directory")
self.TiffCatalogOutputEdit.setText(open_dir)
def error_popup_show(self, title, message, info):
"""
Display an error message when an exception occurs.
:param title: A string for the window title, should be "Error"
:param message: Quick message about the error.
:param info: How to resolve the error.
:return: Error text displayed in QMessageBox
"""
self.error_popup.setIcon(self.error_popup.Critical)
self.error_popup.setText(message)
self.error_popup.setWindowTitle(title)
self.error_popup.setInformativeText(info)
self.error_popup.setStandardButtons(self.error_popup.Ok)
self.error_popup.show()
def clear_origin_fields(self):
"""
Clear the orgin calculation fields
:return: Blank fields
"""
self.northYEntry.clear()
self.southYEntry.clear()
self.eastXEntry.clear()
self.westXEntry.clear()
self.originOutputBox.clear()
def clear_convert_fields(self):
"""
Clear the coordinate system conversion fields
:return: Blank fields
"""
self.converCoordsEntry.clear()
self.converterOutput.clear()
def dd_dms_chooser(self):
"""
Determine whether to run get_dd_dms or get_dms_dd
"""
coords = self.converCoordsEntry.text()
if len(coords) == 0:
title = "Error"
text = "Missing coordinate input."
info = "Check that coordinate field contains valid value."
self.error_popup_show(title, text, info)
else:
if self.dmsToDD.isChecked():
self.get_dms_dd(coords)
else:
self.get_dd_dms(coords)
def get_dd_dms(self, coords):
"""
Convert decimal degrees to lat/lon
:return: lat/lon value
"""
coords = float(coords)
dd_dms_output_text = self.converterOutput
try:
degrees, minutes, seconds, valid = Convert.dd_to_dms(coords)
if valid:
output = '{0}d, {1}m, {2}s'.format(degrees, minutes,
seconds)
dd_dms_output_text.setText(output)
else:
self.error_popup_show('Error', 'Check input and try again.',
'')
except ValueError:
self.error_popup_show('Error',
'Error converting decimal degrees to '
'lat/lon.',
'Check to ensure coordinate input only'
' contains numbers.')
def get_dms_dd(self, coords):
"""
Convert degrees/minutes/seconds to decimal degrees
:return: a float decimal degree
"""
dms_dd_output_text = self.converterOutput
try:
decimal_degrees = Convert.dms_to_dd(coords)
if decimal_degrees:
output = '{0}'.format(decimal_degrees)
dms_dd_output_text.setText(output)
else:
self.error_popup_show('Error',
'Error converting decimal degrees to '
'lat/lon.',
'Check to ensure coordinate input only'
' contains numbers.')
except ValueError:
self.error_popup_show('Error',
'Error converting decimal degrees to '
'lat/lon.',
'Check to ensure coordinate input only'
' contains numbers.')
def display_shapefile(self):
"""
Linker to run the display_shapefile function iin shape_functions
module. #TODO This is eligible for removal.
:return:
"""
print("calling display_shapefile")
self.shape_functions.display_shapefile(self.shapefileViewPath)
def get_raster_bounds(self):
"""
Gets bounding box of raster image using GDAL bindings
:param path: path to raster
:return: tuple of bounding coordinates
"""
path = self.geoTiffDir1.text()
output_path = self.TiffCatalogOutputEdit.text()
fanout = False
if self.FanOutByRes.isChecked():
fanout = True
print("Fanning out by resolution.")
raster_count, raster_dictionary = measurements.\
create_catalog(path, output_path, fanout)
output_text = "Finished processing {0} rasters.\n\n".\
format(raster_count)
output_text += 'Raster paths and bounds (ulX, ulY, lrX, lrY): \n'
row = 0
self.catalogTiffOutputWindow.setRowCount(len(raster_dictionary))
for filepath, bounds in raster_dictionary.items():
print(f"adding {filepath} to window.")
print(bounds[1])
filename = os.path.basename(filepath)
self.catalogTiffOutputWindow.setItem(row, 0,
QTableWidgetItem(filename))
self.catalogTiffOutputWindow.\
setItem(row, 1, QTableWidgetItem(str(bounds[0])))
self.catalogTiffOutputWindow.\
setItem(row, 2, QTableWidgetItem(str(bounds[1])))
self.catalogTiffOutputWindow.\
setItem(row, 3, QTableWidgetItem(str(bounds[2])))
self.catalogTiffOutputWindow.\
setItem(row, 4, QTableWidgetItem(str(bounds[3])))
row += 1
return raster_count, raster_dictionary
def handle_tiff_copy(self):
"""
Handles code that copies tifs
:return: None
"""
rasters_by_resolution = {}
resolution = None
tiff_directory = self.TiffDirectory.text()
shapefile_path = self.intersectingShapefileEdit.text()
output_directory = self.geoTiffOutputDirEdit.text()
intersecting_rasters = raster_functions.\
intersect_by_shape(tiff_directory, shapefile_path,
output_directory)
self.copyTiffOutputWindow.setRowCount(0)
if self.CopyFanoutByResolution.isChecked():
for raster_path in intersecting_rasters:
row_position = self.copyTiffOutputWindow.rowCount()
self.copyTiffOutputWindow.insertRow(row_position)
resolution = measurements.get_resolution(raster_path)
# Create resolution directory name
resolution = f"{resolution[0]}x{resolution[1]}"
output_basedir = os.path.join(output_directory, resolution)
if not os.path.exists(output_basedir):
os.mkdir(output_basedir)
output_filename = os.path.basename(raster_path)
output_raster_path = os.path.join(output_basedir,
output_filename)
shutil.copy(raster_path, output_raster_path)
self.copyTiffOutputWindow.\
setItem(row_position, 0, QTableWidgetItem(raster_path))
self.copyTiffOutputWindow.\
setItem(row_position, 1,
QTableWidgetItem(output_raster_path))
self.copyTiffOutputWindow.resizeRowsToContents()
else:
for raster_path in intersecting_rasters:
row_position = self.copyTiffOutputWindow.rowCount()
self.copyTiffOutputWindow.insertRow(row_position)
output_path = os.path.join(output_directory,
os.path.basename(raster_path))
shutil.copy(raster_path, output_path)
self.copyTiffOutputWindow.\
setItem(row_position, 0, QTableWidgetItem(raster_path))
self.copyTiffOutputWindow.\
setItem(row_position, 1, QTableWidgetItem(output_path))
self.copyTiffOutputWindow.resizeRowsToContents()
def get_origin(self):
"""
Button function to get origin calculation. Runs the origin_calc()
function which runs the calculation.
:return: Origin
"""
blank_entry = False
output_text = self.originOutputBox
# Grab values from text entry boxes and convert to floats
north_y = self.northYEntry.text()
south_y = self.southYEntry.text()
east_x = self.eastXEntry.text()
west_x = self.westXEntry.text()
# Print error message if entry is blank
for i in north_y, south_y, east_x, west_x:
if len(i) == 0:
blank_entry = True
if blank_entry:
title = "Error"
text = "Missing coordinate(s) input."
info = "Check that all coordinate fields contain valid values."
self.error_popup_show(title, text, info)
else:
north_y = float(north_y)
south_y = float(south_y)
east_x = float(east_x)
west_x = float(west_x)
coordinates = [north_y, south_y, east_x, west_x]
# Get origin from origin_calc
centroid = origin_calc(coordinates)
if centroid: # If a centroid is returned, print to text box.
centroid = "{0}, {1}".format(centroid[0], centroid[1])
output_text.setText(centroid)
# Calculate_origin returned false, indicating invalid input.
# Print error message.
else:
title = "Error"
text = "Error converting coordinates to decimal numbers."
info = "Check to insure coordinate input contains only " \
"numbers."
self.error_popup_show(title, text, info)
if __name__ == "__main__":
# app = QtGui.QGuiApplication.QApplication(sys.argv)
app = QtWidgets.QApplication(sys.argv)
window = GisHelper()
window.show()
sys.exit(app.exec_())