예제 #1
0
    def test_get_slope_aspect(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")
        mask = os.path.join(input_folder, "test_mask.shp")
        slope_expect = os.path.join(reference_folder, "expect_s_r")
        aspect_expect = os.path.join(reference_folder, "expect_a_r")

        from bal import get_slope_aspect

        output_folder = os.path.join(testdata_folder, 'output')

        slope, aspect = get_slope_aspect(dem, output_folder, mask)

        compare_result_slope = os.path.join(output_folder,
                                            "compare_slope_r.txt")
        compare_result_aspect = os.path.join(output_folder,
                                             "compare_aspect_r.txt")

        arcpy.RasterCompare_management(slope, slope_expect, '',
                                       'Pyramids Exist; Pixel Value', '',
                                       compare_result_slope)
        if '"true"' not in open(compare_result_slope).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        arcpy.RasterCompare_management(aspect, aspect_expect, '',
                                       'Pyramids Exist', '',
                                       compare_result_aspect)
        if '"true"' not in open(compare_result_aspect).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        if arcpy.Exists(slope):
            arcpy.Delete_management(slope)

        if arcpy.Exists(aspect):
            arcpy.Delete_management(aspect)

        os.remove(compare_result_slope)
        os.remove(os.path.join(output_folder, "compare_slope_r.xml"))
        os.remove(compare_result_aspect)
        os.remove(os.path.join(output_folder, "compare_aspect_r.xml"))
예제 #2
0
    def test_extract_by_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')
        input_folder = os.path.join(testdata_folder, 'input')
        output_folder = os.path.join(testdata_folder, 'output')
        refer_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)

        from utilities.sa_tools import extract_by_mask

        arcpy.env.overwriteOutput = True

        raster = os.path.join(input_folder, "vege_mga.img")
        result = os.path.join(output_folder, "extracted_raster.img")

        extent_list = [
            os.path.join(input_folder, "mask_extent_small.shp"),
            os.path.join(input_folder, "mask_extent_large.shp")
        ]

        expec_list = [
            os.path.join(refer_folder, "expect_extracted_small.img"),
            os.path.join(refer_folder, "expect_extracted_large.img")
        ]

        index = 0
        for extent in extent_list:

            extract_by_mask(raster, extent, result)

            compare_result = os.path.join(output_folder, "compare_result.txt")

            arcpy.RasterCompare_management(result, expec_list[index], '',
                                           'Pixel Type', '', compare_result,
                                           '', '', '')

            if '"true"' not in open(compare_result).read():
                self.assertEqual(1, 1, 'No errors')
            else:
                self.assertEqual(1, 0, 'Has errors')

            if arcpy.Exists(result):
                arcpy.Delete_management(result)

            os.remove(compare_result)
            os.remove(os.path.join(output_folder, "compare_result.xml"))

            index += 1
예제 #3
0
    def test_reclass_veg(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")
        mask = os.path.join(input_folder, "test_mask.shp")
        veg_class_expect = os.path.join(reference_folder, "expect_v_r")

        input_folder = os.path.dirname(veg)
        arcpy.env.workspace = input_folder

        from bal import reclass_veg

        output_folder = os.path.join(testdata_folder, 'output')

        remap = "1 3;2 2;3 3;4 5 7;6 8 1;9 2;10 3;11 12 1;13 1;14 3;\
                 15 6;16 7;17 3;18 19 2;20 7;21 3;22 2; 23 24 7;25 27 5;\
                 28 2;29 30 7;31 3;32 33 1;34 7;35 3;36 37 7;38 4;39 7;40 1;\
                 41 2;42 7;43 2;44 7;45 4;46 7;47 NODATA;97 2;98 99 1"

        veg_class = reclass_veg(veg, dem, output_folder, remap, mask)

        compare_result = os.path.join(output_folder, "compare_veg_r.txt")

        arcpy.RasterCompare_management(veg_class, veg_class_expect, '', '', '',
                                       compare_result)

        if '"true"' not in open(compare_result).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        # delete output files
        if arcpy.Exists(veg_class):
            arcpy.Delete_management(veg_class)

        os.remove(compare_result)
        os.remove(os.path.join(output_folder, "compare_veg_r.xml"))
예제 #4
0
 def test__clipRasterToArea(self):
     '''
     Compare result of _clipRasterToArea result to known, good comparison dataset
     '''
     runToolMessage = ".....VisibilityUtilityTestCase.test__clipRasterToArea"
     arcpy.AddMessage(runToolMessage)
     Configuration.Logger.info(runToolMessage)
     expectedOutput = os.path.join(Configuration.militaryResultsGDB,
                                   "ExpectedOutputclipRasterToArea")
     resultClippedRaster = os.path.join(Configuration.militaryScratchGDB,
                                        "resultClippedRaster")
     resultClippedRaster = VisibilityUtilities._clipRasterToArea(
         self.inputSurface, self.inputArea, resultClippedRaster)
     deleteIntermediateData.append(resultClippedRaster)
     result = arcpy.RasterCompare_management(expectedOutput,
                                             resultClippedRaster,
                                             "RASTER_DATASET").getOutput(1)
     self.assertEqual(result, "true",
                      "Raster Compare failed: \n %s" % arcpy.GetMessages())
예제 #5
0
    def test_clipRasterToArea(self):
        '''
        Compare result of _clipRasterToArea result to known, good comparison dataset
        '''
        Configuration.Logger.info(
            ".....VisibilityUtilityTestCase.test_clipRasterToArea")

        expectedOutput = os.path.join(Configuration.militaryResultsGDB,
                                      "ExpectedOutputclipRasterToArea")
        resultClippedRaster = os.path.join(Configuration.militaryScratchGDB,
                                           "resultClippedRaster")
        resultClippedRaster = VisibilityUtilities._clipRasterToArea(
            self.inputSurface, self.inputArea, resultClippedRaster)
        deleteIntermediateData.append(resultClippedRaster)
        result = arcpy.RasterCompare_management(
            expectedOutput, resultClippedRaster, "RASTER_DATASET",
            "Columns And Rows;NoData;Pixel Value;Raster Attribute Table", "",
            "", "All 1 Fraction", "", "").getOutput(1)
        self.assertEqual(result, "true",
                         "Raster Compare failed: \n %s" % arcpy.GetMessages())
예제 #6
0
    def test_raster(self, ref_file, in_file, rep_file):
        """
        Logs differences in raster files.

        Parameters:
            ref_file -- reference raster file
            in_file  -- input raster file
            rep_file -- report file

        Return value:
            Logs resulting difference in raster files, error statement otherwise.
        """

        try:
            logging.info('')
            logging.info('          Executing: EGSUtility.test_raster')
            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.CalculateStatistics_management(ref_file, "", "", "",\
                                     "SKIP_EXISTING")
            arcpy.CalculateStatistics_management(in_file, "", "", "",\
                                     "SKIP_EXISTING")
            arcpy.RasterCompare_management(ref_file,in_file,"RASTER_DATASET",\
                          "","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_raster(): {:s}'.format(e))
예제 #7
0
    def test_cal_slope_aspect(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')
        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

        dem = os.path.join(input_folder, "dem.img")
        slope_result = os.path.join(output_folder, "result_s")
        aspect_result = os.path.join(output_folder, "result_a")
        slope_expect = os.path.join(reference_folder, "expect_s")
        aspect_expect = os.path.join(reference_folder, "expect_a")

        from utilities.sa_tools import cal_slope_aspect

        cal_slope_aspect(dem, slope_result, aspect_result)

        compare_result_slope = os.path.join(output_folder, "compare_slope.txt")
        compare_result_aspect = os.path.join(output_folder,
                                             "compare_aspect.txt")

        arcpy.RasterCompare_management(
            slope_result, slope_expect, '',
            'Pyramids Exist; Pixel Value; Statistics; Compression Type', '',
            compare_result_slope)
        if '"true"' not in open(compare_result_slope).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        arcpy.RasterCompare_management(
            aspect_result, aspect_expect, '',
            'Pyramids Exist; Pixel Value; Statistics; Compression Type', '',
            compare_result_aspect)
        if '"true"' not in open(compare_result_aspect).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        # compare pixel value
        nodata_value = -99
        slope_arr = arcpy.RasterToNumPyArray(slope_result,
                                             nodata_to_value=nodata_value)
        slope_arr_exp = arcpy.RasterToNumPyArray(slope_expect,
                                                 nodata_to_value=nodata_value)

        numpy.testing.assert_array_almost_equal(slope_arr,
                                                slope_arr_exp,
                                                decimal=4)

        aspec_arr = arcpy.RasterToNumPyArray(aspect_result,
                                             nodata_to_value=nodata_value)
        aspec_arr_exp = arcpy.RasterToNumPyArray(aspect_expect,
                                                 nodata_to_value=nodata_value)

        numpy.testing.assert_array_almost_equal(aspec_arr,
                                                aspec_arr_exp,
                                                decimal=4)

        if arcpy.Exists(slope_result):
            arcpy.Delete_management(slope_result)

        if arcpy.Exists(aspect_result):
            arcpy.Delete_management(aspect_result)

        try:
            os.remove(compare_result_slope)
            os.remove(os.path.join(output_folder, "compare_slope_r.xml"))
            os.remove(compare_result_aspect)
            os.remove(os.path.join(output_folder, "compare_aspect_r.xml"))
        except OSError:
            pass
예제 #8
0
    def test_bal_cal(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')
        output_folder = os.path.join(testdata_folder, 'output')
        reference_folder = os.path.join(testdata_folder, 'reference')

        veg_class = os.path.join(output_folder, "veg_c")
        slope = os.path.join(output_folder, "slope_c")
        aspect = os.path.join(output_folder, "aspect_c")
        fdi = 80

        if not arcpy.Exists(slope):
            arcpy.CopyRaster_management(os.path.join(input_folder, "slope_c"),
                                        slope)

        if not arcpy.Exists(aspect):
            arcpy.CopyRaster_management(os.path.join(input_folder, "aspect_c"),
                                        aspect)

        if not arcpy.Exists(veg_class):
            arcpy.CopyRaster_management(os.path.join(input_folder, "veg_c"),
                                        veg_class)

        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

        bal_max_expect = os.path.join(reference_folder, "expect_bal_max.img")

        from calculate_bal import bal_cal

        bal_cal(veg_class, slope, aspect, fdi)

        compare_result = os.path.join(output_folder, "compare_bal.txt")

        arcpy.RasterCompare_management('bal_max.img', bal_max_expect, '',
                                       'Pixel Value', '', compare_result)
        if '"true"' not in open(compare_result).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        ds1 = os.path.join(output_folder, "bal_max.img")
        if ds1 is None:
            print 'Could not open bal_max.img'
            sys.exit(1)
        bal_data = arcpy.RasterToNumPyArray(ds1, nodata_to_value=-99)

        ds2 = bal_max_expect
        if ds2 is None:
            print 'Could not open expect_bal_max.img'
            sys.exit(1)
        bal_expect = arcpy.RasterToNumPyArray(ds2, nodata_to_value=-99)

        assert_array_equal(bal_data, bal_expect)

        os.remove(compare_result)
        os.remove(os.path.join(output_folder, "compare_bal.xml"))

        for a_dir in ['w', 'e', 'n', 's', 'nw', 'ne', 'se', 'sw', 'max']:
            output_bal = os.path.join(output_folder, 'bal_' + a_dir + '.img')
            if arcpy.Exists(output_bal):
                arcpy.Delete_management(output_bal)
예제 #9
0
    def test_reclassify(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
        arcpy.env.workspace = input_folder

        output_folder = os.path.join(testdata_folder, 'output')
        reclassify_result = os.path.join(output_folder, 'reclassi')

        from utilities.sa_tools import reclassify

        # test 1st case with vegetation
        veg = os.path.join(input_folder, "vege.img")
        reclassify_expect = os.path.join(reference_folder, "expect_r_1")

        remap = "1 3;2 2;3 3;4 5 7;6 8 1;9 2;10 3;11 12 1;13 1;14 3;\
                 15 6;16 7;17 3;18 19 2;20 7;21 3;22 2; 23 24 7;25 27 5;\
                 28 2;29 30 7;31 3;32 33 1;34 7;35 3;36 37 7;38 4;39 7;40 1;\
                 41 2;42 7;43 2;44 7;45 4;46 7;47 NODATA;97 2;98 99 1"

        reclassify(veg, remap, reclassify_result)

        compare_result = os.path.join(output_folder, "compare_reclassify.txt")

        arcpy.RasterCompare_management(reclassify_result, reclassify_expect,
                                       '', '', '', compare_result)

        if '"true"' not in open(compare_result).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        # delete output files
        if arcpy.Exists(reclassify_result):
            arcpy.Delete_management(reclassify_result)

        os.remove(compare_result)
        os.remove(os.path.join(output_folder, "compare_reclassify.xml"))

        # test 2nd case with slope
        slope = os.path.join(input_folder, "slope_in")
        reclassify_expect = os.path.join(reference_folder, "expect_r_2")

        remap = "0 0 1;0.0001 5 2;5.0001 10 3;10.0001 15 4;\
                15.0001 20 5;20.0001 20 6"

        reclassify(slope, remap, reclassify_result)

        compare_result = os.path.join(output_folder, "compare_reclassify.txt")

        arcpy.RasterCompare_management(reclassify_result, reclassify_expect,
                                       '', '', '', compare_result)

        if '"true"' not in open(compare_result).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        # delete output files
        if arcpy.Exists(reclassify_result):
            arcpy.Delete_management(reclassify_result)

        os.remove(compare_result)
        os.remove(os.path.join(output_folder, "compare_reclassify.xml"))

        # test 3rd case with aspect
        aspect = os.path.join(input_folder, "aspect_in")
        reclassify_expect = os.path.join(reference_folder, "expect_r_3")

        remap = "-1 0 9;0 22.5 1;22.5 67.5 2;67.5 112.5 3;\
               112.5 157.5 4;157.5 202.5 5;202.5 247.5 6;247.5 292.5 7;\
               292.5 337.5 8;337.5 360 1"

        reclassify(aspect, remap, reclassify_result)

        compare_result = os.path.join(output_folder, "compare_reclassify.txt")

        arcpy.RasterCompare_management(reclassify_result, reclassify_expect,
                                       '', '', '', compare_result)

        if '"true"' not in open(compare_result).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        # delete output files
        if arcpy.Exists(reclassify_result):
            arcpy.Delete_management(reclassify_result)

        os.remove(compare_result)
        os.remove(os.path.join(output_folder, "compare_reclassify.xml"))
예제 #10
0
    def test_find_common_area(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')
        output_folder = os.path.join(testdata_folder, 'output')
        reference_folder = os.path.join(testdata_folder, 'reference')

        veg_r = os.path.join(output_folder, "veg_r")
        slope_r = os.path.join(output_folder, "slope_r")
        aspect_r = os.path.join(output_folder, "aspect_r")

        if not arcpy.Exists(slope_r):
            arcpy.CopyRaster_management(os.path.join(input_folder, "slope_r"),
                                        slope_r)

        if not arcpy.Exists(aspect_r):
            arcpy.CopyRaster_management(os.path.join(input_folder, "aspect_r"),
                                        aspect_r)

        if not arcpy.Exists(veg_r):
            arcpy.CopyRaster_management(os.path.join(input_folder, "veg_r"),
                                        veg_r)

        parent = os.path.abspath(os.path.join(cmd_folder, os.pardir))

        if parent not in sys.path:
            sys.path.insert(0, parent)

        veg_expect = os.path.join(reference_folder, "expect_v_c")
        slope_expect = os.path.join(reference_folder, "expect_s_c")
        aspect_expect = os.path.join(reference_folder, "expect_a_c")

        from bal import find_common_area

        veg_c, slope_c, aspect_c = find_common_area(veg_r, slope_r, aspect_r)

        compare_result_veg = os.path.join(output_folder, "compare_veg_c.txt")
        compare_result_slope = os.path.join(output_folder,
                                            "compare_slope_c.txt")
        compare_result_aspect = os.path.join(output_folder,
                                             "compare_aspect_c.txt")

        arcpy.RasterCompare_management(veg_c, veg_expect, '', 'Pyramids Exist',
                                       '', compare_result_veg)
        if '"true"' not in open(compare_result_veg).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        arcpy.RasterCompare_management(slope_c, slope_expect, '', '', '',
                                       compare_result_slope)
        if '"true"' not in open(compare_result_slope).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        arcpy.RasterCompare_management(aspect_c, aspect_expect, '', '', '',
                                       compare_result_aspect)
        if '"true"' not in open(compare_result_aspect).read():
            self.assertEqual(1, 1, 'No errors')
        else:
            self.assertEqual(1, 0, 'Has errors')

        if arcpy.Exists(veg_c):
            arcpy.Delete_management(veg_c)
        if arcpy.Exists(slope_c):
            arcpy.Delete_management(slope_c)
        if arcpy.Exists(aspect_c):
            arcpy.Delete_management(aspect_c)

        os.remove(compare_result_veg)
        os.remove(compare_result_slope)
        os.remove(compare_result_aspect)
        os.remove(os.path.join(output_folder, "compare_veg_c.xml"))
        os.remove(os.path.join(output_folder, "compare_slope_c.xml"))
        os.remove(os.path.join(output_folder, "compare_aspect_c.xml"))