Skip to content

Commit 3829af3

Browse files
authored
Merge pull request #116 from minorsecond/develop
Develop
2 parents 2868ca8 + 61efa78 commit 3829af3

File tree

7 files changed

+118
-62
lines changed

7 files changed

+118
-62
lines changed

Jenkinsfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pipeline {
5555
}
5656
post {
5757
success {
58-
archiveArtifacts 'dist/gh.exe'
58+
archiveArtifacts 'dist/gh/**'
5959
}
6060
always {
6161
junit 'results.xml'

gh-debug.spec

+10-8
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@ a = Analysis(['gh.py'],
3030
runtime_hooks=[],
3131
excludes=[],
3232
win_no_prefer_redirects=False,
33-
win_private_assemblies=True,
33+
win_private_assemblies=False,
3434
cipher=block_cipher)
3535
pyz = PYZ(a.pure, a.zipped_data,
3636
cipher=block_cipher)
3737
exe = EXE(pyz,
3838
a.scripts,
39-
a.binaries,
40-
a.zipfiles,
41-
a.datas,
39+
exclude_binaries=True,
4240
name='gh',
4341
debug=True,
4442
strip=False,
4543
upx=False,
46-
upx_exclude=[],
47-
runtime_tmpdir=None,
48-
console=True,
49-
icon='assets\\map.ico')
44+
console=True , icon='assets\\map.ico')
45+
coll = COLLECT(exe,
46+
a.binaries,
47+
a.zipfiles,
48+
a.datas,
49+
strip=False,
50+
upx=False,
51+
name='gh')

gh.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ def __init__(self):
4040
self.shapefileViewGo.clicked.connect(self.display_shapefile)
4141

4242
self.catalogTiffBrowseButton.clicked.connect(self.browse_for_raster)
43+
self.catalogTiffBOutputrowseButton.clicked.\
44+
connect(self.browse_catalog_output)
4345
self.catalogTiffProcess.clicked.connect(self.get_raster_bounds)
4446

4547
self.dmsToDD.setChecked(True)
@@ -103,6 +105,16 @@ def browse_for_raster(self):
103105
"Directory")
104106
self.geoTiffDir1.setText(open_dir)
105107

108+
def browse_catalog_output(self):
109+
"""
110+
Browse for shapefile catalog output
111+
:return:
112+
"""
113+
114+
open_dir = QtWidgets.QFileDialog.\
115+
getExistingDirectory(self, "Select output directory")
116+
self.TiffCatalogOutputEdit.setText(open_dir)
117+
106118
def error_popup_show(self, title, message, info):
107119
"""
108120
Display an error message when an exception occurs.
@@ -225,12 +237,11 @@ def get_raster_bounds(self):
225237
:return: tuple of bounding coordinates
226238
"""
227239

228-
raster_measurements = measurements.RasterMeasurements()
229-
230240
path = self.geoTiffDir1.text()
241+
output_path = self.TiffCatalogOutputEdit.text()
231242

232-
raster_count, raster_dictionary = raster_measurements.\
233-
CalculateRasterBounds(path)
243+
raster_count, raster_dictionary = measurements.\
244+
create_catalog(path, output_path)
234245

235246
output_text = "Finished processing {0} rasters.\n\n".\
236247
format(raster_count)

gisHelperGui.ui

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<bool>false</bool>
4848
</property>
4949
<property name="currentIndex">
50-
<number>0</number>
50+
<number>1</number>
5151
</property>
5252
<widget class="QWidget" name="gisCalc">
5353
<attribute name="title">
@@ -650,7 +650,7 @@
650650
<string>Output Directory: </string>
651651
</property>
652652
</widget>
653-
<widget class="QPushButton" name="catalogTiffBrowseButton_2">
653+
<widget class="QPushButton" name="catalogTiffBOutputrowseButton">
654654
<property name="geometry">
655655
<rect>
656656
<x>652</x>
@@ -738,7 +738,7 @@
738738
</font>
739739
</property>
740740
</widget>
741-
<widget class="QLineEdit" name="lineEdit_5">
741+
<widget class="QLineEdit" name="TiffCatalogOutputEdit">
742742
<property name="geometry">
743743
<rect>
744744
<x>140</x>

gui.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Created by: PyQt5 UI code generator 5.12.3
66
#
77
# WARNING! All changes made in this file will be lost!
8-
# flake8: noqa
8+
99

1010
from PyQt5 import QtCore, QtGui, QtWidgets
1111

@@ -245,12 +245,12 @@ def setupUi(self, MainWindow):
245245
font.setFamily("Helvetica")
246246
self.tiffCatalogOutputLabel.setFont(font)
247247
self.tiffCatalogOutputLabel.setObjectName("tiffCatalogOutputLabel")
248-
self.catalogTiffBrowseButton_2 = QtWidgets.QPushButton(self.catalogTiff)
249-
self.catalogTiffBrowseButton_2.setGeometry(QtCore.QRect(652, 72, 113, 32))
248+
self.catalogTiffBOutputrowseButton = QtWidgets.QPushButton(self.catalogTiff)
249+
self.catalogTiffBOutputrowseButton.setGeometry(QtCore.QRect(652, 72, 113, 32))
250250
font = QtGui.QFont()
251251
font.setFamily("Helvetica")
252-
self.catalogTiffBrowseButton_2.setFont(font)
253-
self.catalogTiffBrowseButton_2.setObjectName("catalogTiffBrowseButton_2")
252+
self.catalogTiffBOutputrowseButton.setFont(font)
253+
self.catalogTiffBOutputrowseButton.setObjectName("catalogTiffBOutputrowseButton")
254254
self.catalogTiffOutputWindow = QtWidgets.QTextEdit(self.catalogTiff)
255255
self.catalogTiffOutputWindow.setGeometry(QtCore.QRect(10, 192, 755, 325))
256256
font = QtGui.QFont()
@@ -278,12 +278,12 @@ def setupUi(self, MainWindow):
278278
font.setFamily("Helvetica")
279279
self.geoTiffDir1.setFont(font)
280280
self.geoTiffDir1.setObjectName("geoTiffDir1")
281-
self.lineEdit_5 = QtWidgets.QLineEdit(self.catalogTiff)
282-
self.lineEdit_5.setGeometry(QtCore.QRect(140, 76, 481, 21))
281+
self.TiffCatalogOutputEdit = QtWidgets.QLineEdit(self.catalogTiff)
282+
self.TiffCatalogOutputEdit.setGeometry(QtCore.QRect(140, 76, 481, 21))
283283
font = QtGui.QFont()
284284
font.setFamily("Helvetica")
285-
self.lineEdit_5.setFont(font)
286-
self.lineEdit_5.setObjectName("lineEdit_5")
285+
self.TiffCatalogOutputEdit.setFont(font)
286+
self.TiffCatalogOutputEdit.setObjectName("TiffCatalogOutputEdit")
287287
self.catalogTiffProcess = QtWidgets.QPushButton(self.catalogTiff)
288288
self.catalogTiffProcess.setGeometry(QtCore.QRect(652, 118, 113, 32))
289289
font = QtGui.QFont()
@@ -382,7 +382,7 @@ def setupUi(self, MainWindow):
382382
MainWindow.setStatusBar(self.statusbar)
383383

384384
self.retranslateUi(MainWindow)
385-
self.tabContainer.setCurrentIndex(0)
385+
self.tabContainer.setCurrentIndex(1)
386386
QtCore.QMetaObject.connectSlotsByName(MainWindow)
387387

388388
def retranslateUi(self, MainWindow):
@@ -414,7 +414,7 @@ def retranslateUi(self, MainWindow):
414414
self.label_2.setText(_translate("MainWindow", "Geotiff Directory: "))
415415
self.catalogTiffBrowseButton.setText(_translate("MainWindow", "Browse"))
416416
self.tiffCatalogOutputLabel.setText(_translate("MainWindow", "Output Directory: "))
417-
self.catalogTiffBrowseButton_2.setText(_translate("MainWindow", "Browse"))
417+
self.catalogTiffBOutputrowseButton.setText(_translate("MainWindow", "Browse"))
418418
self.checkBox.setText(_translate("MainWindow", "Fan out by resolution"))
419419
self.catalogTiffProcess.setText(_translate("MainWindow", "Process"))
420420
self.tabContainer.setTabText(self.tabContainer.indexOf(self.catalogTiff), _translate("MainWindow", "Catalog Tiffs"))

raster/measurements.py

+75-30
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,94 @@
1-
from os.path import join
1+
from os.path import join, splitext
22
from os import walk
33
import gdal
44
import rasterio as rio
5+
from shapely.geometry import Point, Polygon, mapping
6+
import fiona
57

68
# These are needed for pyinstaller
79
from rasterio import _shim, control, crs, sample, vrt, _features # noqa
810

911

10-
class RasterMeasurements:
12+
def calculate_raster_bounds(rasters):
1113
"""
12-
Contains methods for measuring raster bounds, pixel size, etc.
14+
Gets bounding box of raster image using GDAL bindings
15+
:param rasters: list of raster paths
16+
:return: tuple of bounding coordinates
1317
"""
1418

15-
def CalculateRasterBounds(self, path):
16-
"""
17-
Gets bounding box of raster image using GDAL bindings
18-
:param path: path to raster
19-
:return: tuple of bounding coordinates
20-
"""
19+
raster_dictionary = {}
20+
raster_count = 0
21+
22+
for raster in rasters:
23+
raster_count += 1
24+
image = gdal.Open(raster) # Open the file using gdal
25+
ulx, xres, xskew, uly, yskew, yres = image.GetGeoTransform()
26+
lrx = ulx + (image.RasterXSize * xres)
27+
lry = uly + (image.RasterYSize * yres)
28+
29+
ulx = round(ulx, 3)
30+
uly = round(uly, 3)
31+
lrx = round(lrx, 3)
32+
lry = round(lry, 3)
33+
34+
bounds = [ulx, uly, lrx, lry]
35+
raster_dictionary[raster] = bounds
36+
37+
return raster_count, raster_dictionary
38+
39+
40+
def create_catalog(path, output_dir):
41+
rasters = []
42+
polygons = []
43+
output_path = join(output_dir, 'tif_catalog.shp')
2144

22-
rasters = []
23-
raster_dictionary = {}
24-
raster_count = 0
45+
for dirpath, dirnames, filenames in walk(path):
46+
for file in filenames:
47+
if splitext(file)[1].lower() == '.tif':
48+
rasters.append(join(dirpath, file))
2549

26-
for dirpath, dirnames, filenames in walk(path):
27-
for file in filenames:
28-
if file.endswith('.tif'):
29-
rasters.append(join(dirpath, file))
50+
count, rasters = calculate_raster_bounds(rasters)
3051

31-
for raster in rasters:
32-
raster_count += 1
33-
image = gdal.Open(raster) # Open the file using gdal
34-
ulx, xres, xskew, uly, yskew, yres = image.GetGeoTransform()
35-
lrx = ulx + (image.RasterXSize * xres)
36-
lry = uly + (image.RasterYSize * yres)
52+
print("Creating polygons")
53+
# Create the polygons
54+
for path, bounds in rasters.items():
55+
ulx = bounds[0]
56+
uly = bounds[1]
57+
lrx = bounds[2]
58+
lry = bounds[3]
3759

38-
ulx = round(ulx, 3)
39-
uly = round(uly, 3)
40-
lrx = round(lrx, 3)
41-
lry = round(lry, 3)
60+
p1 = Point(ulx, lry)
61+
p2 = Point(ulx, uly)
62+
p3 = Point(lrx, uly)
63+
p4 = Point(lrx, lry)
4264

43-
bounds = [ulx, uly, lrx, lry]
44-
raster_dictionary[raster] = bounds
65+
pointList = [p1, p2, p3, p4]
4566

46-
return raster_count, raster_dictionary
67+
polygons.append(Polygon([[p.x, p.y] for p in pointList]))
68+
69+
# Write to shp
70+
schema = {
71+
'geometry': 'Polygon',
72+
'properties': {'id': 'int'}
73+
}
74+
75+
id = 0
76+
with fiona.open(output_path, 'w', 'ESRI Shapefile', schema) as c:
77+
for polygon in polygons:
78+
c.write({
79+
'geometry': mapping(polygon),
80+
'properties': {'id': id}
81+
})
82+
83+
id += 1
84+
85+
return count, rasters
86+
87+
88+
class RasterMeasurements:
89+
"""
90+
Contains methods for measuring raster bounds, pixel size, etc.
91+
"""
4792

4893
def getPixelValue(self, raster, in_x, in_y):
4994
"""
@@ -54,7 +99,7 @@ def getPixelValue(self, raster, in_x, in_y):
5499
pixel_val = None
55100

56101
# Remove this - don't want to run this thing twice
57-
raster_count, raster_dictionary = self.CalculateRasterBounds(raster)
102+
raster_count, raster_dictionary = calculate_raster_bounds(raster)
58103

59104
for raster_path, bounds in raster_dictionary.items():
60105
with rio.open(raster_path) as raster_input:

tests/test_calculations.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def test_dms_to_dd():
2020
assert Convert.dms_to_dd("36 7 28.612") == 36.12461444
2121
assert Convert.dms_to_dd("99.5") is None
2222

23-
2423
def test_origin_calculation():
2524
"""
2625
Test origin coordinate calculation.
@@ -35,9 +34,8 @@ def test_raster_bounds():
3534
:return:
3635
"""
3736

38-
path = '.\\tests\\test_data\\'
39-
raster_measurements = measurements.RasterMeasurements()
40-
results = raster_measurements.CalculateRasterBounds(path)
37+
path = ['.\\tests\\test_data\\raster.tif']
38+
results = measurements.calculate_raster_bounds(path)
4139

4240
assert results == (1, {'.\\tests\\test_data\\raster.tif': [-99.632,
4341
31.564,
@@ -47,7 +45,7 @@ def test_raster_bounds():
4745

4846

4947
def test_get_pixel_value():
50-
path = 'tests\\test_data\\'
48+
path = ['.\\tests\\test_data\\raster.tif']
5149
raster_measurements = measurements.RasterMeasurements()
5250
pixel_value = raster_measurements.getPixelValue(raster=path,
5351
in_x=-99.631408,

0 commit comments

Comments
 (0)