def test_getRasterFileCrs(self): rast_crs = getCRSfromRasterFile(os.path.realpath( this_dir + r"/data/rasters/area1_rgbi_jan_50cm_84sutm54.tif")) self.assertEqual(rast_crs.epsg_number, None) self.assertEqual(rast_crs.crs_wkt, None) rast_crs = getCRSfromRasterFile(os.path.normpath(self.singletif)) self.assertEqual(rast_crs.epsg_number, 28354) self.assertEqual(rast_crs.crs_wkt, rasterio.crs.CRS.from_epsg(28354).wkt)
def test11_rescaleNormaliseRaster(self): in_file = self.gridextract_files[-1] rast_crs = crs.getCRSfromRasterFile(in_file) with rasterio.open(os.path.normpath(in_file)) as src: rescaled = rescale(src, 0, 255) rescaled2 = rescale(src, 0, 5) norm = normalise(src) out_meta = src.meta.copy() out_meta['crs'] = rast_crs.crs_wkt out_meta['count'] = 1 # contains only one band out_meta['dtype'] = np.float32 out_rescale = os.path.join(TmpDir, os.path.basename(in_file).replace('.tif', '_rescale0-255.tif')) with rasterio.open(os.path.normpath(out_rescale), 'w', **out_meta) as out: out.write_band(1, rescaled) out_normalised = os.path.join(TmpDir, os.path.basename(in_file).replace('.tif', '_normalised.tif')) with rasterio.open(os.path.normpath(out_normalised), 'w', **out_meta) as out: out.write_band(1, rescaled2) self.assertAlmostEqual(2.0000722408294678, float(np.nanmax(norm)), 4) self.assertAlmostEqual(-2.266947031021118, float(np.nanmin(norm)), 4) self.assertEqual(0, np.nanmin(rescaled) ) self.assertEqual(255, np.nanmax(rescaled)) self.assertEqual(0, np.nanmin(rescaled2)) self.assertEqual(5, np.nanmax(rescaled2))
def test08_randomPixelSelection(self): global rand_gdf, rand_crs out_randompts = os.path.join(TmpDir, os.path.basename(fileBlockTif).replace('.tif', '_randpts.shp')) rast_crs = crs.getCRSfromRasterFile(fileBlockTif) with rasterio.open(os.path.normpath(fileBlockTif)) as raster: rand_gdf, rand_crs = random_pixel_selection(raster, rast_crs, 50, out_shapefile=out_randompts) self.assertEqual(len(rand_gdf), 50) self.assertTrue(os.path.exists(out_randompts)) self.assertEqual(rast_crs.epsg, rand_gdf.crs)
def accept(self, *args, **kwargs): if not self.validate(): return False try: QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor)) write_setting(PLUGIN_NAME + "/" + self.toolKey + "/LastSize", self.dsbSize.value()) LOGGER.info('{st}\nProcessing {}'.format(self.windowTitle(), st='*' * 50)) self.iface.mainWindow().statusBar().showMessage('Processing {}'.format(self.windowTitle())) # Add settings to log settingsStr = 'Parameters:---------------------------------------' settingsStr += '\n {:30}\t{}'.format('Sample Size:', self.dsbSize.value()) settingsStr += '\n {:30}\t{}'.format('Layer:', self.mcboTargetLayer.currentLayer().name()) settingsStr += '\n {:30}\t{}'.format('Output Shapefile:', self.lneSaveFile.text()) LOGGER.info(settingsStr) lyrTarget = self.mcboTargetLayer.currentLayer() removeFileFromQGIS(self.lneSaveFile.text()) raster_file = lyrTarget.source() rasterCRS = pyprecag_crs.getCRSfromRasterFile(raster_file) if rasterCRS.epsg is None: rasterCRS.getFromEPSG(lyrTarget.crs().authid()) with rasterio.open(os.path.normpath(raster_file)) as src: processing.random_pixel_selection(src, rasterCRS, int(self.dsbSize.value()), self.lneSaveFile.text()) lyrPts = addVectorFileToQGIS(self.lneSaveFile.text(), atTop=True, layer_name=os.path.splitext(os.path.basename(self.lneSaveFile.text()))[0]) QtGui.qApp.restoreOverrideCursor() self.iface.mainWindow().statusBar().clearMessage() return super(RandomPixelSelectionDialog, self).accept(*args, **kwargs) except Exception as err: QtGui.qApp.restoreOverrideCursor() self.iface.mainWindow().statusBar().clearMessage() self.cleanMessageBars(True) self.send_to_messagebar(str(err), level=QgsMessageBar.CRITICAL, duration=0, addToLog=True, exc_info=sys.exc_info()) return False # leave dialog open