def test06_prepareForVesperKrig(self): csv_desc = CsvDescribe(fileTrimmed) df_csv = csv_desc.open_pandas_dataframe() global file_control sub_file = os.path.splitext(os.path.basename(file_csv))[0] file_control = sub_file + '_control_' + data_col + '.txt' vc = VesperControl() vc.update(xside=30, yside=30) if not os.path.exists(file_control): bat_file, file_control = prepare_for_vesper_krige(df_csv, data_col, file_block_txt, TmpDir, control_textfile=file_control, coord_columns=[], epsg=epsg, control_options=vc) self.assertTrue(os.path.exists(bat_file)) self.assertTrue(os.path.exists(file_control)) self.assertTrue(os.path.exists(os.path.join(TmpDir, r'Vesper', sub_file + '_vesperdata_' + data_col + '.csv'))) df_csv = pd.read_csv(os.path.join(TmpDir, r'Vesper', sub_file + '_vesperdata_' + data_col + '.csv')) x_column, y_column = predictCoordinateColumnNames(df_csv.columns) self.assertEqual(x_column.upper(), 'EASTING') self.assertEqual(y_column.upper(), 'NORTHING') print('Running Vesper, Please wait....') run_vesper(file_control)
def test1_prepareForVesperKrig_filesExist(self): gdfPoints, gdfPtsCrs = convert.convert_csv_to_points( file, coord_columns_epsg=4326, out_epsg=28354) outGDF, outCRS = clean_trim_points(gdfPoints, gdfPtsCrs, data_col, fileSubName + '_trimmed.csv', poly, thin_dist_m=2.5) self.assertTrue(os.path.exists(fileSubName + '_trimmed.csv')) self.assertEqual(outGDF.crs, {'init': 'epsg:28354', 'no_defs': True}) self.assertEqual(len(outGDF), 554) processing.block_grid(in_shapefilename=poly, pixel_size=5, out_rasterfilename=block_tif, out_vesperfilename=fileSubName + '_block_v.txt', snap=True, overwrite=True) global file_ctrl file_bat, file_ctrl = prepare_for_vesper_krige( outGDF, data_col, fileSubName + '_block_v.txt', TmpDir, control_textfile=os.path.basename(fileSubName) + '_control.txt', block_size=30, coord_columns=[], epsg=28354) self.assertTrue( os.path.exists(os.path.join(TmpDir, r'Vesper\Do_Vesper.bat'))) self.assertTrue( os.path.exists( os.path.join(TmpDir, r'Vesper', os.path.basename(fileSubName) + '_control.txt'))) self.assertTrue( os.path.exists( os.path.join(TmpDir, r'Vesper', os.path.basename(fileSubName) + '_vesperdata.csv'))) dfCSV = pd.read_csv( os.path.join(TmpDir, r'Vesper', os.path.basename(fileSubName) + '_vesperdata.csv')) x_column, y_column = predictCoordinateColumnNames(dfCSV.columns) self.assertEqual(x_column.upper(), 'EASTING') self.assertEqual(y_column.upper(), 'NORTHING')
def test1_CreateControlHighDensity_VesperControlClass(self): # check using VesperControl class file_csv = os.path.realpath(this_dir + "/area2_high_trimmed.csv") grid_file = os.path.realpath(this_dir + "/rasters/area2_5m_blockgrid_v.txt") data_col = r'Yld Mass(Dry)(tonne/ha)' csv_desc = CsvDescribe(file_csv) df_csv = csv_desc.open_pandas_dataframe() vc = VesperControl() vc.update(xside=30, yside=30) global g_ctrl_file file_bat, g_ctrl_file = prepare_for_vesper_krige( df_csv, data_col, grid_file, TEMPDIR, control_textfile='test_high_5m_control.txt', coord_columns=[], epsg=28354, control_options=vc) if os.path.exists(kriging_ops.vesper_exe): self.assertTrue( os.path.exists(os.path.join(TEMPDIR, 'Vesper/Do_Vesper.bat'))) else: self.assertEqual('', file_bat) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_control.txt'))) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_vesperdata.csv'))) src_df = pd.read_csv( os.path.realpath(this_dir + '/VESPER/high_5m_vesperdata.csv')) test_df = pd.read_csv( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_vesperdata.csv')) pd.testing.assert_frame_equal(src_df, test_df) with open(os.path.realpath(this_dir + '/VESPER/high_5m_control.txt')) as src_file,\ open(g_ctrl_file) as test_file: self.assertEqual(src_file.readlines()[11:], test_file.readlines()[11:])
def test1a_CreateControlHighDensity_block_size(self): file_csv = os.path.realpath(this_dir + "/area2_high_trimmed.csv") grid_file = os.path.realpath(this_dir + "/rasters/area2_5m_blockgrid_v.txt") data_col = r'Yld Mass(Dry)(tonne/ha)' csv_desc = CsvDescribe(file_csv) df_csv = csv_desc.open_pandas_dataframe() # check using block_size argument - backwards compatible file_bat, file_ctrl = prepare_for_vesper_krige( df_csv, data_col, grid_file, TEMPDIR, control_textfile='test_high_5m_control.txt', block_size=30, coord_columns=[], epsg=28354) if os.path.exists(kriging_ops.vesper_exe): self.assertTrue( os.path.exists(os.path.join(TEMPDIR, 'Vesper/Do_Vesper.bat'))) else: self.assertEqual('', file_bat) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_control.txt'))) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_vesperdata.csv'))) src_df = pd.read_csv( os.path.realpath(this_dir + '/VESPER/high_5m_vesperdata.csv')) test_df = pd.read_csv( os.path.join(TEMPDIR, 'Vesper', 'test_high_5m_vesperdata.csv')) pd.testing.assert_frame_equal(src_df, test_df)
def accept(self, *args, **kwargs): if not self.validate(): return False try: self.cleanMessageBars(True) message = '- and run VESPER' if self.gbRunVesper.isChecked() else '' LOGGER.info('{st}\nProcessing {} {}'.format( self.windowTitle(), message, st='*' * 50)) # Add settings to log settingsStr = 'Parameters:---------------------------------------' settingsStr += '\n {:30}\t{}'.format('Data File:', self.lneInCSVFile.text()) settingsStr += '\n {:30}\t{}'.format('Krige Column:', self.cboKrigColumn.currentText()) settingsStr += '\n {:30}\t{}'.format('Grid File:', self.lneInGridFile.text()) settingsStr += '\n {:30}\t{}'.format('Output Vesper Folder:', self.lneVesperFold.text()) settingsStr += '\n {:30}\t{}'.format(self.cboMethod.currentText(), '') if self.cboMethod.currentText() == 'High Density Kriging': settingsStr += '\n {:30}\t{}'.format('Block Kriging Size:', int(self.dsbBlockKrigSize.value())) else: settingsStr += '\n {:30}\t{}'.format('Variogram File:', self.lneVariogramFile.text()) settingsStr += '\n {:30}\t{}'.format('Min Point Number:', self.lneMinPoint.text()) settingsStr += '\n {:30}\t{}'.format( 'Display Vesper Graphics:', self.chkDisplayGraphics.isChecked()) settingsStr += '\n {:30}\t{}'.format( 'Run Vesper Now:', self.gbRunVesper.isChecked()) if self.gbRunVesper.isChecked(): settingsStr += '\n {:30}\t{}'.format('Import Vesper Files to Rasters:', self.chkVesper2Raster.isChecked()) if self.chkVesper2Raster.isChecked(): settingsStr += '\n {:30}\t{}'.format( 'Vesper Files Coordinate System:', self.lblInCRS.text()) LOGGER.info(settingsStr) # get a fresh dataframe for the input csv file self.dfCSV = pd.read_csv(self.lneInCSVFile.text()) vc = VesperControl() if self.cboMethod.currentText() == 'High Density Kriging': vc.update(xside=int(self.dsbBlockKrigSize.value()), yside=int(self.dsbBlockKrigSize.value())) else: # from the variogram text file find and update the control file keys vario = self.parse_variogram_file() vesp_keys = {key: val for key, val in vario.items() if key in vc} vc.update(vesp_keys) # apply the other keys. vc.update({'jpntkrg': 1, 'jlockrg': 0, 'minpts': int(self.lneMinPoint.text()), 'maxpts': len(self.dfCSV), 'jcomvar': 0, }) epsg = int(self.in_qgscrs.authid().replace('EPSG:', '')) bat_file, ctrl_file = prepare_for_vesper_krige(self.dfCSV, self.cboKrigColumn.currentText(), self.lneInGridFile.text(), self.lneVesperFold.text(), control_textfile=self.lneCtrlFile.text(), coord_columns=[], epsg=epsg, display_graphics=self.chkDisplayGraphics.isChecked(), control_options=vc) epsg = 0 if self.in_qgscrs is not None and self.chkVesper2Raster.isChecked(): epsg = int(self.in_qgscrs.authid().replace('EPSG:', '')) if self.gbRunVesper.isChecked(): # Add to vesper queue self.vesp_dict = {'control_file': ctrl_file, 'epsg': epsg} else: message = 'Successfully created files for Vesper kriging. ' \ 'The control file is {}'.format(ctrl_file) self.send_to_messagebar(message, level=QgsMessageBar.SUCCESS, duration=0, addToLog=True, core_QGIS=True) LOGGER.info('Successfully created files for Vesper kriging') QtGui.qApp.restoreOverrideCursor() return super(PreVesperDialog, self).accept(*args, **kwargs) except Exception as err: QtGui.qApp.restoreOverrideCursor() self.cleanMessageBars(True) self.send_to_messagebar(str(err), level=QgsMessageBar.CRITICAL, duration=0, addToLog=True, showLogPanel=True, exc_info=sys.exc_info()) return False
def test2_prepareForVesperKrig_LowDensity(self): file_csv = os.path.realpath(this_dir + '/area2_lowdensity_points.csv') grid_file = os.path.realpath(this_dir + "/rasters/area2_5m_blockgrid_v.txt") data_col = 'Hand_Sample' csv_desc = CsvDescribe(file_csv) ctrl_para = VesperControl() ctrl_para.update({ 'jpntkrg': 1, 'jlockrg': 0, 'minpts': csv_desc.row_count - 2, 'maxpts': csv_desc.row_count, 'jcomvar': 0, 'modtyp': 'Spherical', 'iwei': 'no_pairs/variance', 'CO': 92.71, 'C1': 277.9, 'A1': 116.0 }) file_bat, file_ctrl = prepare_for_vesper_krige( csv_desc.open_pandas_dataframe(), data_col, grid_file, TEMPDIR, control_textfile='test_low_control.txt', control_options=ctrl_para, coord_columns=[], epsg=28354) if os.path.exists(kriging_ops.vesper_exe): self.assertTrue( os.path.exists(os.path.join(TEMPDIR, 'Vesper/Do_Vesper.bat'))) else: self.assertEqual('', file_bat) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_low_control.txt'))) self.assertTrue( os.path.exists( os.path.join(TEMPDIR, 'Vesper', 'test_low_vesperdata.csv'))) df_csv = pd.read_csv( os.path.join(TEMPDIR, 'Vesper', 'test_low_vesperdata.csv')) x_column, y_column = predictCoordinateColumnNames(df_csv.columns) self.assertEqual('EASTING', x_column.upper()) self.assertEqual('NORTHING', y_column.upper()) with open(file_ctrl) as r_file: data = r_file.read() self.assertIn("outfil='test_low_kriged.txt'", data) self.assertIn("outdir=''", data) self.assertIn("jpntkrg=1", data) self.assertIn("jlockrg=0", data) self.assertIn("minpts=198", data) self.assertIn("maxpts=200", data) self.assertIn("modtyp=1", data) self.assertIn("jcomvar=0", data) self.assertIn("iwei=3", data) self.assertIn("CO=92.71", data) del data src_df = pd.read_csv( os.path.realpath(this_dir + '/VESPER/low_vesperdata.csv')) test_df = pd.read_csv( os.path.join(TEMPDIR, 'Vesper', 'test_low_vesperdata.csv')) pd.testing.assert_frame_equal(src_df, test_df) with open(os.path.realpath(this_dir + '/VESPER/low_control.txt')) as src_file,\ open(os.path.join(TEMPDIR, 'Vesper', 'test_low_control.txt')) as test_file: self.assertEqual(src_file.readlines()[11:], test_file.readlines()[11:])