def testPercentileMakesImage(self): with TempDir() as d: out_file = os.path.join(d, 'contact_sheet_per.png') scale_comparison.main(config.bathy_raster, 'percentile', self.percentile, self.min_nbhs, self.max_nbhs, None, out_file) self.assertTrue(os.path.exists(out_file))
def testStdRun(self): with TempDir() as d: in_raster = os.path.join(d, 'test_bpi.tif') std_raster = os.path.join(d, 'test_std_bpi.tif') # was encountering this: ERROR 000875: Output raster: # c:\Users\shau7031\AppData\Local\Temp\tmp8co8nk\FocalSt_bath1's # workspace is an invalid workspace. Force workspace to temp: arcpy.env.scratchWorkspace = d bpi.main(bathy=config.bathy_raster, inner_radius=10, outer_radius=30, out_raster=in_raster, bpi_type='broad') self.assertTrue(os.path.exists(in_raster)) standardize_bpi_grids.main(bpi_raster=in_raster, out_raster=std_raster) self.assertTrue(os.path.exists(std_raster)) self.assertAlmostEqual(su.raster_properties(std_raster, "MEAN"), 0.671608391608) self.assertAlmostEqual(su.raster_properties(std_raster, "STD"), 99.655593923183)
def testDepthStatisticsRun(self): with TempDir() as d: neighborhood = 3 # 3x3 neighborhood arcpy.env.scratchWorkspace = d out_workspace = d stats = "'Mean Depth';Variance;"\ "'Standard Deviation';'Terrain Ruggedness (VRM)';"\ "'Interquartile Range';Kurtosis" depth_statistics.main(config.bathy_raster, neighborhood, out_workspace, stats) # mean of depth summary rasters mean_depths = { 'mean': -20.56248074571827, 'sdev': 0.2946229406453136, 'var': 0.1281792675921596, 'iqr': 0.45498055403516, 'kurt': -0.90668194852357 } for (prefix, expected_value) in mean_depths.items(): raster_path = os.path.join( d, "{0}_{1}_{2:03d}.tif".format(self.base, prefix, neighborhood)) self.assertTrue(os.path.exists(raster_path)) self.assertAlmostEqual( su.raster_properties(raster_path, 'MEAN'), expected_value)
def testSlopeRun(self): with TempDir() as d: slope_raster = os.path.join(d, 'test_slope.tif') arcpy.env.scratchWorkspace = d slope.main(bathy=config.bathy_raster, out_raster=slope_raster) self.assertTrue(os.path.exists(slope_raster)) self.assertAlmostEqual(su.raster_properties(slope_raster, "MEAN"), 3.802105241105673)
def testToolboxRunWithLayers(self): """ Test running the tool against the toolbox version using Layer files.""" with TempDir() as d: broad_std_raster = os.path.join(d, 'test_broad_std_bpi.tif') fine_std_raster = os.path.join(d, 'test_fine_std_bpi.tif') arcpy.env.scratchWorkspace = d arcpy.ImportToolbox(config.pyt_file) (install_dir, arcgis_version, product) = su.arcgis_platform() if product == 'Desktop': mxd = arcpy.mapping.MapDocument(config.bpi_grids_mxd) df = arcpy.mapping.ListDataFrames(mxd)[0] layers = arcpy.mapping.ListLayers(df) else: aprx = arcpy.mp.ArcGISProject(config.bpi_grids_aprx) mdoc = aprx.listMaps()[0] layers = mdoc.listLayers() layer_names = [l.name for l in layers] broad_lyr = layers[layer_names.index('broad_bpi')] fine_lyr = layers[layer_names.index('fine_bpi')] arcpy.standardizebpi_btm(broad_lyr.dataSource, "0", "0", broad_std_raster, fine_lyr.dataSource, "0", "0", fine_std_raster) self.assertTrue(os.path.exists(broad_std_raster)) self.assertTrue(os.path.exists(fine_std_raster)) # Compare to known rasters self.assertAlmostEqual( su.raster_properties(broad_std_raster, "MEAN"), su.raster_properties(config.broad_std_raster, "MEAN")) self.assertAlmostEqual( su.raster_properties(broad_std_raster, "STD"), su.raster_properties(config.broad_std_raster, "STD")) self.assertAlmostEqual( su.raster_properties(fine_std_raster, "MEAN"), su.raster_properties(config.fine_std_raster, "MEAN")) self.assertAlmostEqual( su.raster_properties(fine_std_raster, "STD"), su.raster_properties(config.fine_std_raster, "STD")) if product == 'Desktop': del layers, df, mxd else: del layers, mdoc, aprx
def testVrmRun(self): with TempDir() as d: neighborhood = 3 # 3x3 neighborhood vrm_raster = os.path.join(d, 'test_vrm.tif') arcpy.env.scratchWorkspace = d ruggedness.main(config.bathy_raster, neighborhood, vrm_raster) self.assertTrue(os.path.exists(vrm_raster)) self.assertAlmostEqual(su.raster_properties(vrm_raster, "MEAN"), 0.00062628513039036) self.assertAlmostEqual(su.raster_properties(vrm_raster, "STD"), 0.00087457748556755)
def testBpiRun(self): with TempDir() as d: raster_fn = 'test_run_bpi.tif' bpi_raster = os.path.join(d, raster_fn) bpi.main(bathy=config.bathy_raster, inner_radius=10, outer_radius=30, out_raster=bpi_raster, bpi_type='broad') self.assertTrue(raster_fn in os.listdir(d)) self.assertAlmostEqual(su.raster_properties(bpi_raster, "MEAN"), 0.295664335664) self.assertAlmostEqual(su.raster_properties(bpi_raster, "STD"), 1.65611606614)
def testModelExecuteWithXml(self): with TempDir() as d: model_output = os.path.join(d, 'output_zones.tif') arcpy.env.scratchWorkspace = d btm_model.main(d, config.bathy_raster, self.broad_inner_rad, self.broad_outer_rad, self.fine_inner_rad, self.fine_outer_rad, config.base_xml, model_output) self.assertTrue(os.path.exists(model_output)) self.xml_mean = su.raster_properties(model_output, "MEAN") self.assertAlmostEqual(self.xml_mean, self.true_mean) # count up the number of cells in the first class self.assertEqual(self.sumFirstClass(model_output), 88)
def testLZWCompression(self): with TempDir() as d: arcpy.ImportToolbox(config.pyt_file) arcpy.multiplescales_btm(self.in_raster, self.nbh_sizes, self.metrics, d) rast_names = [ 'bathy5m_clip_mean_003.tif', 'bathy5m_clip_sdev_003.tif', 'bathy5m_clip_var_003.tif', 'bathy5m_clip_vrm_003.tif', 'bathy5m_clip_mean_013.tif', 'bathy5m_clip_sdev_013.tif', 'bathy5m_clip_var_013.tif', 'bathy5m_clip_vrm_013.tif', 'bathy5m_clip_iqr_003.tif', 'bathy5m_clip_kurt_003.tif', 'bathy5m_clip_iqr_013.tif', 'bathy5m_clip_kurt_013.tif' ] for each in rast_names: file_name = os.path.join(d, each) self.assertEqual(str(Raster(file_name).compressionType), 'LZW')
def testClassifyWithFgdbLocation(self): with TempDir() as d: fgdb_name = "classify.gdb" fgdb_workspace = os.path.join(d, fgdb_name) # create a temporary File Geodatabase location arcpy.CreateFileGDB_management(d, fgdb_name) self.assertTrue(os.path.exists(fgdb_workspace)) # TODO: this currently hacks up the path to be a valid name, # but should probably instead throw an error and let the user # correct the output name. classify_raster = os.path.join(fgdb_workspace, 'output_in_fgdb') classify.main(config.base_xml, config.broad_std_raster, config.fine_std_raster, config.slope_raster, config.bathy_raster, classify_raster) # resulting 'fixed' name mean = su.raster_properties(classify_raster, "MEAN") self.assertAlmostEqual(mean, 5.78153846153846)
def testSinglePartResults(self): with TempDir() as d: arcpy.ImportToolbox(config.pyt_file) testaoi = os.path.join(d, 'testaoi.shp') arcpy.CopyFeatures_management(self.aoi, testaoi) arcpy.arcchordratio_btm(self.in_raster, testaoi, True, d) planarTIN = os.path.join(d, 'bathy5m_clip_planartin') elevTIN = os.path.join(d, 'bathy5m_clip_elevationtin') self.assertTrue(os.path.exists(planarTIN)) self.assertTrue(os.path.exists(elevTIN)) self.assertEqual(len(arcpy.Describe(testaoi).fields), 8) with arcpy.da.SearchCursor(testaoi, '*') as cursor: expected = (0, (358083.9308262255, 4678265.0709908875), 0, 19972.5978495, 19917.1775893, 1.00278253583, 2.32867335011, 246.842790079) result = cursor.next() for x in range(2, len(expected)): self.assertAlmostEqual(result[x], expected[x], places=2)
def testModelExecuteWithCsvFromToolbox(self): with TempDir() as d: model_output = os.path.join(d, 'output_zones.tif') arcpy.env.scratchWorkspace = d arcpy.ImportToolbox(config.pyt_file) arcpy.runfullmodel_btm(d, config.bathy_raster, self.broad_inner_rad, self.broad_outer_rad, self.fine_inner_rad, self.fine_outer_rad, config.base_csv, model_output) self.assertTrue(os.path.exists(model_output)) self.csv_mean = su.raster_properties(model_output, "MEAN") self.assertAlmostEqual(self.csv_mean, self.true_mean) # count up the number of cells in the first class self.assertEqual(self.sumFirstClass(model_output), 88)
def testAspectRun(self): with TempDir() as d: aspect_sin_raster = os.path.join(d, 'test_aspect_sin.tif') aspect_cos_raster = os.path.join(d, 'test_aspect_cos.tif') arcpy.env.scratchWorkspace = d aspect.main(bathy=config.bathy_raster, out_sin_raster=aspect_sin_raster, out_cos_raster=aspect_cos_raster) self.assertTrue(os.path.exists(aspect_sin_raster)) self.assertTrue(os.path.exists(aspect_cos_raster)) self.assertAlmostEqual( su.raster_properties(aspect_sin_raster, "MEAN"), -0.06153140691827335) self.assertAlmostEqual( su.raster_properties(aspect_cos_raster, "MEAN"), 0.02073092622177259)
def testModelExecuteWithExcel(self): with TempDir() as d: zones_output = os.path.join(d, 'output_zones.tif') arcpy.env.scratchWorkspace = d btm_model.main(d, config.bathy_raster, self.broad_inner_rad, self.broad_outer_rad, self.fine_inner_rad, self.fine_outer_rad, config.zones_excel, zones_output) self.assertTrue(os.path.exists(zones_output)) arcpy.BuildRasterAttributeTable_management(zones_output) self.excel_mean = su.raster_properties(zones_output, "MEAN") self.excel_zones = zones_output # some asserts here for validation rat = self.extractRasterAttributeTable(self.excel_zones) for (value, count) in self.expected.items(): self.assertEqual(count, rat[value])
def testDepthStatisticsOnlyDiffToMean(self): """test just difference to mean.""" with TempDir() as d: neighborhood = 3 # 3x3 neighborhood arcpy.env.scratchWorkspace = d out_workspace = d stats = "'Difference to Mean'" depth_statistics.main(config.bathy_raster, neighborhood, out_workspace, stats) prefix = 'mean_diff' expected_value = -0.0055500285014563 raster_path = os.path.join( d, "{0}_{1}_{2:03d}.tif".format(self.base, prefix, neighborhood)) self.assertTrue(os.path.exists(raster_path)) self.assertAlmostEqual(su.raster_properties(raster_path, 'MEAN'), expected_value)
def testMultipartResults(self): with TempDir() as d: arcpy.ImportToolbox(config.pyt_file) testaoi_mp = os.path.join(d, 'testaoi_mp.shp') arcpy.CopyFeatures_management(self.aoi_multipart, testaoi_mp) arcpy.arcchordratio_btm(self.in_raster, testaoi_mp, True, d) planarTIN = os.path.join(d, 'bathy5m_clip_planartin_4') self.assertTrue(os.path.exists(planarTIN)) rows = int(arcpy.GetCount_management(testaoi_mp).getOutput(0)) self.assertEqual(rows, 4) self.assertEqual(len(arcpy.Describe(testaoi_mp).fields), 9) with arcpy.da.SearchCursor(testaoi_mp, '*') as cursor: expected = (0, (358124.62825836154, 4678229.791685243), 0, 0, 1238.64248438, 1236.28701996, 1.00190527311, 5.3937886264, 225.726428217) result = cursor.next() for x in range(2, len(expected)): self.assertAlmostEqual(result[x], expected[x], places=2)
def testSaPaRunNoCorrection(self): with TempDir() as d: ratio_raster = os.path.join(d, 'test_sapa_ratio.tif') surf_raster = os.path.join(d, 'test_surface_area.tif') arcpy.env.scratchWorkspace = d surface_area_to_planar_area.main(config.bathy_raster, ratio_raster, False, surf_raster) self.assertTrue(os.path.exists(ratio_raster)) self.assertTrue(os.path.exists(surf_raster)) self.assertAlmostEqual(su.raster_properties(ratio_raster, "MEAN"), 1.0042422342676) self.assertAlmostEqual(su.raster_properties(ratio_raster, "STD"), 0.0058175502835692) self.assertAlmostEqual(su.raster_properties(surf_raster, "MEAN"), 25.119343091857) self.assertAlmostEqual(su.raster_properties(surf_raster, "STD"), 0.14551573347447)
def testSaPaRunCorrection(self): with TempDir() as d: ratio_raster = os.path.join(d, 'test_sapa_ratio.tif') surf_raster = os.path.join(d, 'test_surface_area.tif') arcpy.env.scratchWorkspace = d surface_area_to_planar_area.main(config.bathy_raster, ratio_raster, True, surf_raster) self.assertTrue(os.path.exists(ratio_raster)) self.assertTrue(os.path.exists(surf_raster)) self.assertAlmostEqual(su.raster_properties(ratio_raster, "MEAN"), 1.000616701751028) self.assertAlmostEqual(su.raster_properties(ratio_raster, "STD"), 0.0008445980914179108) self.assertAlmostEqual(su.raster_properties(surf_raster, "MEAN"), 25.119343091857) self.assertAlmostEqual(su.raster_properties(surf_raster, "STD"), 0.14551570953239)
def testResultRastersProduced(self): with TempDir() as d: arcpy.env.scratchWorkspace = d arcpy.ImportToolbox(config.pyt_file) arcpy.multiplescales_btm(self.in_raster, self.nbh_sizes, self.metrics, d) depth_stats = { 'mean': -20.56248074571827, 'sdev': 0.2946229406453136, 'var': 0.1281792675921596, 'iqr': 0.45498055403516, 'kurt': -0.90668194852357 } for (prefix, expected_value) in depth_stats.items(): raster_path = os.path.join( d, "bathy5m_clip_{0}_{1:03d}.tif".format(prefix, 3)) self.assertAlmostEqual( su.raster_properties(raster_path, 'MEAN'), expected_value) vrm_raster = os.path.join(d, 'bathy5m_clip_vrm_003.tif') self.assertAlmostEqual(su.raster_properties(vrm_raster, "MEAN"), 0.00062628513039036) self.assertAlmostEqual(su.raster_properties(vrm_raster, "STD"), 0.00087457748556755) rast_names = [ 'bathy5m_clip_mean_003.tif', 'bathy5m_clip_sdev_003.tif', 'bathy5m_clip_var_003.tif', 'bathy5m_clip_vrm_003.tif', 'bathy5m_clip_mean_013.tif', 'bathy5m_clip_sdev_013.tif', 'bathy5m_clip_var_013.tif', 'bathy5m_clip_vrm_013.tif', 'bathy5m_clip_iqr_003.tif', 'bathy5m_clip_kurt_003.tif', 'bathy5m_clip_iqr_013.tif', 'bathy5m_clip_kurt_013.tif' ] for each in rast_names: file_name = os.path.join(d, each) self.assertTrue(os.path.exists(file_name))
def testSetWorkspaceFileIO(self): """Set the default workspace.""" with TempDir() as d: arcpy.ImportToolbox(config.pyt_file) path = os.path.abspath( os.path.join(config.local_path, '..', 'Install', 'toolbox')) wsfile = os.path.join(path, 'workspace.json') wsfile_save = os.path.join(path, 'workspace_orig.json') original = False if os.path.exists(wsfile_save): os.remove(wsfile_save) if os.path.exists(wsfile): os.rename(wsfile, wsfile_save) original = True w = su.Workspace() arcpy.setworkspace_btm(d) self.assertTrue(os.path.exists(wsfile)) self.assertEqual(w.path.lower(), d.lower()) os.remove(wsfile) if original: os.rename(wsfile_save, wsfile)
def testSaPaRunWithFgdbLocation(self): with TempDir() as d: fgdb_name = "sapa.gdb" fgdb_workspace = os.path.join(d, fgdb_name) # create a temporary File Geodatabase location arcpy.CreateFileGDB_management(d, fgdb_name) self.assertTrue(os.path.exists(fgdb_workspace)) ratio_raster = os.path.join(fgdb_workspace, 'test_sapa_ratio') surf_raster = os.path.join(fgdb_workspace, 'test_sapa_area') arcpy.env.scratchWorkspace = d surface_area_to_planar_area.main(config.bathy_raster, ratio_raster, False, surf_raster) self.assertAlmostEqual(su.raster_properties(ratio_raster, "MEAN"), 1.0042422342676) self.assertAlmostEqual(su.raster_properties(ratio_raster, "STD"), 0.0058175502835692) self.assertAlmostEqual(su.raster_properties(surf_raster, "MEAN"), 25.119343091857) self.assertAlmostEqual(su.raster_properties(surf_raster, "STD"), 0.14551573347447)
def testDepthStatisticsKurtosisExact(self): """Check for an exact raster match for kurtosis raster.""" with TempDir() as d: neighborhood = 3 # 3x3 neighborhood arcpy.env.scratchWorkspace = d out_workspace = d stats = "Kurtosis" depth_statistics.main(config.bathy_raster, neighborhood, out_workspace, stats) raster_path = os.path.join( d, "{}_kurt_{:03d}.tif".format(self.base, neighborhood)) self.assertTrue(os.path.exists(raster_path)) # convert output raster to a NumPy array kurt_computed = arcpy.RasterToNumPyArray(raster_path) # load the comparison array kurt_known = np.load(config.kurtosis_npy) # seeing differences on the order of 1e-7, turn down tolerance self.assertTrue(np.allclose(kurt_computed, kurt_known, atol=1e-6))
def testModelExecuteWithMalformedCsv(self): with TempDir() as d: # raises a ValueError when the malformed line is encountered. classify.main(config.malformed_csv, 'null.tif', 'null.tif', 'null.tif', config.bathy_raster, os.path.join(d, 'null.tif'))