Skip to content

Commit

Permalink
Merge pull request #122 from minorsecond/feature-fanout-by-res
Browse files Browse the repository at this point in the history
This PR brings in raster fanout by resolution
  • Loading branch information
minorsecond committed Aug 31, 2020
2 parents 5553c65 + 669b93b commit 1d0da03
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
7 changes: 6 additions & 1 deletion gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,14 @@ def get_raster_bounds(self):

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)
create_catalog(path, output_path, fanout)

output_text = "Finished processing {0} rasters.\n\n".\
format(raster_count)
Expand Down
2 changes: 1 addition & 1 deletion gisHelperGui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@
<bool>true</bool>
</property>
</widget>
<widget class="QCheckBox" name="checkBox">
<widget class="QCheckBox" name="FanOutByRes">
<property name="geometry">
<rect>
<x>12</x>
Expand Down
10 changes: 5 additions & 5 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ def setupUi(self, MainWindow):
self.catalogTiffOutputWindow.setFont(font)
self.catalogTiffOutputWindow.setReadOnly(True)
self.catalogTiffOutputWindow.setObjectName("catalogTiffOutputWindow")
self.checkBox = QtWidgets.QCheckBox(self.catalogTiff)
self.checkBox.setGeometry(QtCore.QRect(12, 124, 153, 20))
self.FanOutByRes = QtWidgets.QCheckBox(self.catalogTiff)
self.FanOutByRes.setGeometry(QtCore.QRect(12, 124, 153, 20))
font = QtGui.QFont()
font.setFamily("Helvetica")
self.checkBox.setFont(font)
self.checkBox.setObjectName("checkBox")
self.FanOutByRes.setFont(font)
self.FanOutByRes.setObjectName("FanOutByRes")
self.progressBar_2 = QtWidgets.QProgressBar(self.catalogTiff)
self.progressBar_2.setGeometry(QtCore.QRect(16, 158, 745, 23))
font = QtGui.QFont()
Expand Down Expand Up @@ -415,7 +415,7 @@ def retranslateUi(self, MainWindow):
self.catalogTiffBrowseButton.setText(_translate("MainWindow", "Browse"))
self.tiffCatalogOutputLabel.setText(_translate("MainWindow", "Output Directory: "))
self.catalogTiffBOutputrowseButton.setText(_translate("MainWindow", "Browse"))
self.checkBox.setText(_translate("MainWindow", "Fan out by resolution"))
self.FanOutByRes.setText(_translate("MainWindow", "Fan out by resolution"))
self.catalogTiffProcess.setText(_translate("MainWindow", "Process"))
self.tabContainer.setTabText(self.tabContainer.indexOf(self.catalogTiff), _translate("MainWindow", "Catalog Tiffs"))
self.label_9.setText(_translate("MainWindow", "Tif Directory"))
Expand Down
79 changes: 62 additions & 17 deletions raster/measurements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os.path import join, splitext
from os import mkdir
from os import walk
import gdal
import rasterio as rio
Expand Down Expand Up @@ -37,9 +38,12 @@ def calculate_raster_bounds(rasters):
return raster_count, raster_dictionary


def create_catalog(path, output_dir):
def create_catalog(path, output_dir, fanout=False):
rasters_by_resolution = {}
rasters = []
polygons = []
resolution = None

output_path = join(output_dir, 'tif_catalog.shp')

for dirpath, dirnames, filenames in walk(path):
Expand All @@ -50,9 +54,11 @@ def create_catalog(path, output_dir):
# rasters is a dict - path: [ulx, uly, lrx, lry]
count, rasters = calculate_raster_bounds(rasters)

print("Creating polygons")
# Create the polygons
for path, bounds in rasters.items():
resolution = get_resolution(path)
resolution = f"{resolution[0]}x{resolution[1]}"

ulx = bounds[0]
uly = bounds[1]
lrx = bounds[2]
Expand All @@ -67,33 +73,72 @@ def create_catalog(path, output_dir):

poly = Polygon([[p.x, p.y] for p in pointList])
poly.path = path
polygons.append(poly)
poly.resolution = resolution

#polygons.append(Polygon([[p.x, p.y] for p in pointList]))
if fanout: # Build the resolution dictionary
if resolution not in rasters_by_resolution:
rasters_by_resolution[resolution] = [poly]
else:
rasters_by_resolution[resolution].append(poly)
else:
polygons.append(poly)

# Write to shp
schema = {
'geometry': 'Polygon',
'properties': {'id': 'int',
'path': 'str'
}
'path': 'str',
'resolution': 'str'}
}

id = 0
with fiona.open(output_path, 'w', 'ESRI Shapefile', schema) as c:
for polygon in polygons:
c.write({
'geometry': mapping(polygon),
'properties': {'id': id,
'path': path
}
})

id += 1
row_number = 0
if fanout:
for resolution, polygons in rasters_by_resolution.items():
output_basedir = join(output_dir, resolution)
mkdir(output_basedir)
output_filename = resolution + "_raster_catalog.shp"
output_path = join(output_basedir, output_filename)
with fiona.open(output_path, 'w', 'ESRI Shapefile', schema) as c:
for polygon in polygons:
c.write({
'geometry': mapping(polygon),
'properties': {'id': row_number,
'path': path,
'resolution': resolution
}
})

row_number += 1

else:
with fiona.open(output_path, 'w', 'ESRI Shapefile', schema) as c:
for polygon in polygons:
c.write({
'geometry': mapping(polygon),
'properties': {'id': row_number,
'path': path,
'resolution': resolution
}
})

row_number += 1

return count, rasters


def get_resolution(raster_path):
"""
Gets resolution if input raster
:param raster_path: Path to raster
:return: list of integers denoting resolution of raster
"""

with rio.open(raster_path) as raster:
res = raster.res

return res


class RasterMeasurements:
"""
Contains methods for measuring raster bounds, pixel size, etc.
Expand Down

0 comments on commit 1d0da03

Please sign in to comment.