def test_point_data(self): arcpy.AddToolbox(self.toolbox_path) # Create an output location in scratch that uses a common prefix and random suffix # This helps prevent conflicts if things don't get cleaned up. temp_output = path.join( "%scratchGDB%", "test_point_data_{}".format(random.randint(0, 9999))) # Execute the tool arcpy.TestableBufferTool_TT(self.point_path, temp_output) # Build the location of the answer answer_output = path.join(self.database_path, "test_point_data_answer") # Compare the temp_output to the known answer. result = arcpy.FeatureCompare_management( in_base_features=answer_output, in_test_features=temp_output, sort_field=["Value"], continue_compare=True) # If it returns false, something has gone wrong, so fail the test. if result.getOutput(1) != 'true': print(result.getMessages()) self.assertEqual(result.getOutput(1), 'true') # Clean up after ourselves arcpy.Delete_management(temp_output)
def compareGeometry(): for key, value in fileMatches.iteritems(): shapeOne = os.path.join(value[0], key) shapeTwo = os.path.join(value[1], key) fieldList = arcpy.ListFields(shapeOne) for field in fieldList: print("{0} is a type of {1} with a length of {2}".format( field.name, field.type, field.length)) '''fieldList = arcpy.ListFields(shapeTwo) for field in fieldList: try: fieldName = str(field.name) #print("{0} is a type of {1} with a length of {2}".format(field.name, field.type, field.length)) if fieldName not in fieldList: fieldList.append(fieldName) except: pass''' #print shapeOne #print shapeTwo sort_field = 'FID' compare_type = "ALL" compare_result = arcpy.FeatureCompare_management( shapeOne, shapeTwo, sort_field, compare_type) print 'Compared results:', compare_result[0]
def test_table_to_lineofbearing_desktop(self): '''Test Table To Line Of Bearing for ArcGIS Desktop''' runToolMessage = ".....TableToLineOfBearingTestCase.test_table_to_lineofbearing_desktop" arcpy.ImportToolbox(Configuration.military_DesktopToolboxPath, "mt") arcpy.AddMessage(runToolMessage) Configuration.Logger.info(runToolMessage) arcpy.TableToLineOfBearing_mt(self.inputTable, "DD_2", "x", "y", "DEGREES", "Orientation", "METERS", "Distance", self.outputLineOfBearing, "GEODESIC", None) self.assertTrue(arcpy.Exists(self.outputLineOfBearing), "Output features do not exist or were not created") featureCount = int( arcpy.GetCount_management(self.outputLineOfBearing).getOutput(0)) expectedFeatures = int(23) self.assertEqual( featureCount, expectedFeatures, "Expected %s features, but got %s" % (str(expectedFeatures), str(featureCount))) compareFeatures = arcpy.FeatureCompare_management( self.BaseFC, self.outputLineOfBearing, "OID") # identical = 'true' means that there are no differences between the base and the output feature class identical = compareFeatures.getOutput(1) self.assertEqual(identical, "true", "Feature compare failed: \n %s" % arcpy.GetMessages()) return
def assertFeatureClassEqual(self, first, second, sort_field, message=None, compare_type=None, ignore_options=None, xy_tolerance=None, m_tolerance=None, z_tolerance=None, attribute_tolerances=None, omit_field=None): """ Compares the second feature class to the first, and reports any issues. Detailed issues are printed to the console if found. :param first: :param second: :param sort_field: :param message: :param compare_type: :param ignore_options: :param xy_tolerance: :param m_tolerance: :param z_tolerance: :param attribute_tolerances: :param omit_field: :return: """ # Make a place to store the compare file compare_file = tempfile.mkstemp(".txt") os.close(compare_file[0]) result = arcpy.FeatureCompare_management( first, second, sort_field, compare_type, ignore_options, xy_tolerance, m_tolerance, z_tolerance, attribute_tolerances, omit_field, continue_compare=True, out_compare_file=compare_file[1]) if 'true' == result.getOutput(1): # delete the compare file os.remove(compare_file[1]) return # read the compare file and print it out print(compare_file[1]) with open(compare_file[1]) as f: [print(l.rstrip()) for l in f.readlines()] os.remove(compare_file[1]) # set the assertion message. msg = message if message is not None else "Feature class {} is not equal to {}".format( second, first) raise AssertionError(msg)
def test_find_aoi(self): cmd_folder = os.path.realpath( os.path.abspath(os.path.split(getfile(currentframe()))[0])) testdata_folder = os.path.join(cmd_folder, 'test_data') input_folder = os.path.join(testdata_folder, 'input') reference_folder = os.path.join(testdata_folder, 'reference') parent = os.path.abspath(os.path.join(cmd_folder, os.pardir)) if parent not in sys.path: sys.path.insert(0, parent) arcpy.env.overwriteOutput = True dem = os.path.join(input_folder, "dem.img") veg = os.path.join(input_folder, "vege.img") from bal import find_aoi extent_list = [ 'DEFAULT', 'MAXOF', 'MINOF', '343000 5850000 347000 5852000 343000 5850000 \ 347000 5852000' ] mask_expect_list = [ os.path.join(reference_folder, "expect_mask_others.shp"), os.path.join(reference_folder, "expect_mask_others.shp"), os.path.join(reference_folder, "expect_mask_others.shp"), os.path.join(reference_folder, "expect_mask.shp") ] index = 0 for extent in extent_list: mask = find_aoi(extent, dem, veg) compare_result_mask = os.path.join( input_folder, "compare_mask_" + extent + ".txt") arcpy.FeatureCompare_management(mask, mask_expect_list[index], 'Id', '', '', '', '', '', '', '', '', compare_result_mask) if '"true"' not in open(compare_result_mask).read(): self.assertEqual(1, 1, 'No errors') else: self.assertEqual(1, 0, 'Has errors') os.remove(compare_result_mask) os.remove( os.path.join(input_folder, "compare_mask_" + extent + ".xml")) if arcpy.Exists(mask): arcpy.Delete_management(mask) index += 1
def test_table_to_point_desktop_MGRS(self): '''Test Table To Point for ArcGIS Desktop_MGRS''' runToolMessage = ".....TableToPointTestCase.test_table_to_point_desktop_MGRS" arcpy.ImportToolbox(Configuration.military_DesktopToolboxPath, "mt") arcpy.AddMessage(runToolMessage) Configuration.Logger.info(runToolMessage) arcpy.TableToPoint_mt(self.inputSingleTable, "MGRS", "MGRS", None, self.outputPoints) self.assertTrue(arcpy.Exists(self.outputPoints), "Output features do not exist or were not created") pointCount = int(arcpy.GetCount_management(self.outputPoints).getOutput(0)) expectedFeatures = int(288) self.assertEqual(pointCount, expectedFeatures, "Expected %s features, but got %s" % (str(expectedFeatures), str(pointCount))) compareFeatures = arcpy.FeatureCompare_management(self.BaseFC, self.outputPoints, "OID") # identical = 'true' means that there are no differences between the baseFC and the output feature class identical = compareFeatures.getOutput(1) self.assertEqual(identical, "true", "Feature compare failed: \n %s" % arcpy.GetMessages()) return
def test_table_to_ellipse_desktop_GEOREF(self): '''test_table_to_ellipse_desktop_GEOREF ''' runToolMessage = ".....TableToEllipseTestCase.test_table_to_ellipse_desktop_GEOREF" arcpy.ImportToolbox(Configuration.military_DesktopToolboxPath, "mt") arcpy.AddMessage(runToolMessage) Configuration.Logger.info(runToolMessage) arcpy.TableToEllipse_mt(self.inputSingleTable, "GEOREF", "GEOREF", None, "Major", "Minor", "KILOMETERS", self.outputEllipses) self.assertTrue(arcpy.Exists(self.outputEllipses), "Output dataset does not exist.") ellipseCount = int(arcpy.GetCount_management(self.outputEllipses).getOutput(0)) expectedFeatures = int(23) self.assertEqual(ellipseCount, expectedFeatures, "Expected %s features but got %s" % (str(expectedFeatures),str(ellipseCount))) compareFeatures = arcpy.FeatureCompare_management(self.baseFC, self.outputEllipses, "OBJECTID") # identical = 'true' means that there are no differences between the base and the output feature class identical = compareFeatures.getOutput(1) self.assertEqual(identical, "true", "Feature Compare failed: \n %s" % arcpy.GetMessages()) return
def test_table_to_polyline_pro(self): '''Test Table To Polyline for ArcGIS Pro''' runToolMessage = ".....TableToPolylineTestCase.test_table_to_polyline_pro" arcpy.ImportToolbox(Configuration.military_ProToolboxPath, "mt") arcpy.AddMessage(runToolMessage) Configuration.Logger.info(runToolMessage) arcpy.TableToPolyline_mt(self.inputTable, "DD_2", "POINT_X", "POINT_Y", self.outputPolylines) self.assertTrue(arcpy.Exists(self.outputPolylines), "Output features do not exist or were not created") polylineCount = int(arcpy.GetCount_management(self.outputPolylines).getOutput(0)) expectedFeatures = int(1) self.assertEqual(polylineCount, expectedFeatures, "Expected %s features, but got %s" % (str(expectedFeatures), str(polylineCount))) compareFeatures = arcpy.FeatureCompare_management(self.BaseFC, self.outputPolylines, "Shape_Length") # identical = 'true' means that there are no differences between the base and the output feature class identical = compareFeatures.getOutput(1) self.assertEqual(identical, "true", "Feature compare failed: \n %s" % arcpy.GetMessages()) return
def test_get_footprint(self): cmd_folder = os.path.realpath(os.path.abspath( os.path.split(getfile( currentframe()))[0])) testdata_folder = os.path.join(cmd_folder, 'test_data') input_folder = os.path.join(testdata_folder, 'input') reference_folder = os.path.join(testdata_folder, 'reference') parent = os.path.abspath(os.path.join(cmd_folder, os.pardir)) if parent not in sys.path: sys.path.insert(0, parent) arcpy.env.overwriteOutput = True veg = os.path.join(input_folder, "vege.img") from bal import get_footprint footprint_expect = os.path.join(reference_folder, "expect_footprint.shp") footprint = 'footprint.shp' get_footprint(veg, footprint) compare_result_footprint = os.path.join(input_folder, "compare_footprint.txt") arcpy.FeatureCompare_management(footprint, footprint_expect, 'Id', '', '', '', '', '', '', '', '', compare_result_footprint) if '"true"' not in open(compare_result_footprint).read(): self.assertEqual(1, 1, 'No errors') else: self.assertEqual(1, 0, 'Has errors') os.remove(compare_result_footprint) os.remove(os.path.join(input_folder, "compare_footprint.xml")) if arcpy.Exists(footprint): arcpy.Delete_management(footprint)
def test_findLocalPeaks(self): ''' test_findLocalPeaks with input 10 peaks to find ''' runToolMessage = ".....VisibilityUtilityTestCase.test_findLocalPeaks" arcpy.AddMessage(runToolMessage) Configuration.Logger.info(runToolMessage) resultPoints = os.path.join(Configuration.militaryScratchGDB, "findLocalPeaks") numPoints = 16 resultPoints = VisibilityUtilities.findLocalPeaks( self.inputArea, numPoints, self.inputSurface, resultPoints) deleteIntermediateData.append(resultPoints) expectedLocalPeaks = os.path.join(Configuration.militaryResultsGDB, "ExpectedOutputFindLocalPeaks") compareResults = arcpy.FeatureCompare_management( resultPoints, expectedLocalPeaks, "OBJECTID").getOutput(1) self.assertEqual(compareResults, "true", "Feature Compare failed: \n %s" % arcpy.GetMessages())
def test_hi_lowPointByArea_highest(self): ''' test hi_lowPointByArea for MAXIMUM (highest) setting. ''' runToolMessage = ".....VisibilityUtilityTestCase.test_hi_lowPointByArea_highest" arcpy.AddMessage(runToolMessage) Configuration.Logger.info(runToolMessage) hi_low_Switch = "MAXIMUM" resultPoints = os.path.join(Configuration.militaryScratchGDB, "highestPoints") resultPoints = VisibilityUtilities.hi_lowPointByArea( self.inputArea, self.inputSurface, hi_low_Switch, resultPoints) deleteIntermediateData.append(resultPoints) expectedHighest = os.path.join(Configuration.militaryResultsGDB, "ExpectedOutputHighestPt") compareResults = arcpy.FeatureCompare_management( resultPoints, expectedHighest, "OBJECTID").getOutput(1) self.assertEqual(compareResults, "true", "Feature Compare failed: \n %s" % arcpy.GetMessages())
def test_get_extent_mask(self): cmd_folder = os.path.realpath(os.path.abspath( os.path.split(getfile( currentframe()))[0])) testdata_folder = os.path.join(cmd_folder, 'test_data') output_folder = os.path.join(testdata_folder, 'output') reference_folder = os.path.join(testdata_folder, 'reference') parent = os.path.abspath(os.path.join(cmd_folder, os.pardir)) if parent not in sys.path: sys.path.insert(0, parent) arcpy.env.overwriteOutput = True mask_expect = os.path.join(reference_folder, "expect_extent_mask.shp") from bal import get_extent_mask extent = '343000 5850000 347000 5852000 343000 5850000 347000 5852000' mask = os.path.join(output_folder, "mask.shp") get_extent_mask(extent, mask) compare_result_mask = os.path.join(output_folder, "compare_mask.txt") arcpy.FeatureCompare_management(mask, mask_expect, 'Id', '', '', '', '', '', '', '', '', compare_result_mask) if '"true"' not in open(compare_result_mask).read(): self.assertEqual(1, 1, 'No errors') else: self.assertEqual(1, 0, 'Has errors') if arcpy.Exists(mask): arcpy.Delete_management(mask) os.remove(compare_result_mask) os.remove(os.path.join(output_folder, "compare_mask.xml"))
def test_vector(self, ref_file, in_file, rep_file): """ Logs differences in vector files. Parameters: ref_file -- reference vector file in_file -- input vector file rep_file -- report file Return value: Logs resulting difference in vector files, error statement otherwise. """ from pci.api import datasource as ds from pci.pcimod import pcimod from pci.exceptions import PCIException ## from pci.his import * from pci.nspio import Report, enableDefaultReport from osgeo import gdal, ogr, osr ## from gdalconst import * import numpy import arcpy try: logging.info('') logging.info(' Executing: EGSUtility.test_vector') logging.info(' Compare ref: ' + ref_file) logging.info(' Compare product: ' + in_file) logging.info(' Report file: ' + rep_file) if os.path.exists(rep_file): os.remove(rep_file) arcpy.FeatureCompare_management(ref_file, in_file, 'FID', 'ALL', '', '', 0, 0,\ '', '', 'CONTINUE_COMPARE', rep_file) # open results file, output results to log file with open(rep_file) as infile: for line in infile: logging.info(line) infile.close() except ValueError as e: self.error('test_vector(): {:s}'.format(e))
# Set local variables base_features = str(os.path.join(bseGdb, k)) test_features = str(os.path.join(tgtGdb, k)) sort_field = sortField compare_type = "All" ignore_option = "IGNORE_M;IGNORE_Z" xy_tolerance = "0.001 METERS" m_tolerance = 0 z_tolerance = 0 attribute_tolerance = "Shape_Length 0.001" omit_field = "#" continue_compare = "CONTINUE_COMPARE" # Process: FeatureCompare compare_result = arcpy.FeatureCompare_management( base_features, test_features, sort_field, compare_type, ignore_option, xy_tolerance, m_tolerance, z_tolerance, attribute_tolerance, omit_field, continue_compare, compare_file) fn.write("\n---------------\n") fn.write("For Feature class name :" + base_features + "\n") fn.write("\n---------------\n") fn.write(arcpy.GetMessages()) fn.write("\n---------------\n") f = open(compare_file) f1 = open(outCsv, 'a') for x in f.readlines(): ss = str(x) #print "going for Next value" #print ss checker1 = None #if 'RelationshipClass relationship class ID is different' in ss: #checker1 = True
def compare_data(data): """TODO: add description""" max_severity = 0 data_paths = [d['FILE PATH'] for d in data if os.path.exists(d['FILE PATH'])] if len(data_paths) == 1: return 1 elif len(data_paths) == 0: return 1 common_file_exts = ['.xyz', '.grd', '.txt', '.tab', '.ecw', '.lyr'] common_raster_exts = ['.tif', '.jpg', '.img', '.png'] common_gis_exts = ['.shp'] common_cad_exts = ['.dxf'] common_fds_exts = ['.sdc'] common_workspace_exts = ['.gdb', '.mdb'] ext = os.path.splitext(data_paths[0])[1].lower() if ext in common_file_exts: data_type = 'file' elif ext in common_raster_exts: data_type = 'rasterdataset' elif ext in common_gis_exts: data_type = 'shapefile' elif ext in common_cad_exts: data_type = 'caddrawingdataset' elif ext in common_workspace_exts: data_type = 'workspace' elif ext in common_fds_exts: data_type = 'featuredataset' elif ext in ['.mxd']: data_type = 'mapdocument' else: # Determine type of data data_type = arcpy.Describe(data_paths[0]).dataType # Create combinations of items combs = {} combs[data_paths[0]] = data_paths[1:] for item in combs.values()[0]: try: if data_type.lower() == 'shapefile': sort_field = get_sort_field([item, data_paths[0]]) if sort_field: result = arcpy.FeatureCompare_management(item, data_paths[0], sort_field, continue_compare=False) max_severity = result.maxSeverity if max_severity > 0: break elif data_type.lower() == 'rasterdataset': # result = arcpy.RasterCompare_management(item, data_paths[0], continue_compare=False) # max_severity = result.maxSeverity max_serverity = raster_compare(item, data_paths[0]) if max_severity > 0: break elif data_type.lower() in ('arcinfotable', 'dbasetable'): sort_field = get_sort_field([item, data_paths[0]]) if sort_field: result = arcpy.TableCompare_management(item, data_paths[0], sort_field, continue_compare=False) max_severity = result.maxSeverity if max_severity > 0: break elif data_type.lower() in ('file', 'textfile', 'prjfile', 'toolbox', 'layer'): if is_ascii(item): result = arcpy.FileCompare_management(item, data_paths[0], "ASCII", False) max_severity = result.maxSeverity if max_severity > 0: break else: result = arcpy.FileCompare_management(item, data_paths[0], "BINARY", False) max_severity = result.maxSeverity if max_severity > 0: break elif data_type.lower() == 'mapdocument': mxdA = arcpy.mapping.MapDocument(data_paths[0]) data_framesA = arcpy.mapping.ListDataFrames(mxdA) mxdB = arcpy.mapping.MapDocument(item) data_framesB = arcpy.mapping.ListDataFrames(mxdB) # Compare dataframes if not sorted([dfA.name for dfA in data_framesA]) == sorted([dfB for dfB in data_framesB]): max_severity = 1 break else: # Compare layers in each dataframe layersA = [] layersB = [] for dframe in data_framesA: lyrs = arcpy.mapping.ListLayers(mxdA, data_frame=dframe) if lyrs: for lA in lyrs: layersA.append(lA.name) for dframe in data_framesB: lyrs = arcpy.mapping.ListLayers(mxdB, data_frame=dframe) if lyrs: for lB in lyrs: layersB.append(lB.name) if not sorted(layersA) == sorted(layersB): max_severity = 1 break elif data_type.lower() == 'workspace': max_severity = compare_geodatabases([data_paths[0], item]) if max_severity > 0: break elif data_type.lower() in ('featuredataset', 'caddrawingdataset'): arcpy.env.workspace = data_paths[0] fcsA = sorted([fc.lower() for fc in arcpy.ListFeatureClasses()]) arcpy.env.workspace = item fcsB = sorted([fc.lower() for fc in arcpy.ListFeatureClasses()]) if not len(fcsA) == len(fcsB): max_severity = 1 break else: for i, fc in enumerate(fcsA): sort_field = get_sort_field([os.path.join(data_paths[0], fc), os.path.join(item, fcsB[i])]) if sort_field: result = arcpy.FeatureCompare_management(os.path.join(data_paths[0], fc), os.path.join(item, fcsB[i]), sort_field, continue_compare=False) max_severity = result.maxSeverity if max_severity > 0: break except arcpy.ExecuteError: status_writer.send_status(arcpy.GetMessages()) max_severity = 0 break return max_severity