Exemplo n.º 1
0
    def test_getRasterTXTUnionExtent(self):
        padding = 10
        cellsize = 1.0
        tmp = TempPathHelper()
        currdir = path.dirname(path.abspath(__file__))
        gridPath1 = path.join(currdir, 'test', 'assets', 'grids', 'grid1.txt')
        gridPath2 = path.join(currdir, 'test', 'assets', 'grids', 'grid2.txt')
        gridPath3 = path.join(currdir, 'test', 'assets', 'grids', 'grid3.txt')

        theExtent = unionCSVExtents([gridPath1, gridPath2, gridPath3], padding=10)
        self.assertTupleEqual(theExtent, (-10.5, 14.5, -0.5, 23.5))
Exemplo n.º 2
0
    def test_ResampleCSVDEM(self):
        tmp = TempPathHelper()
        padding = 5

        cellSize = 1
        gridPath1 = path.join(path.dirname(path.abspath(__file__)), 'test', 'assets', 'grids', 'realgrid.txt')
        theExtent = unionCSVExtents([gridPath1], padding=padding)
        # Create our grid from CSV file that we can add into the min surface
        rTest1 = Raster(extent=theExtent, cellWidth=cellSize)
        rTest1.loadDEMFromCSV(gridPath1, theExtent, ptCenter=Raster.PointShift.CENTER)
        rTest1filename = path.join(tmp.path, 'realgrid.tif')
        rTest1.write(rTest1filename)

        rLinear = rTest1.ResampleDEM(0.5, 'linear')
        rLinearFilename = path.join(tmp.path, 'realgridLinear.tif')
        rLinear.write(rLinearFilename)

        rBilinear = rTest1.ResampleDEM(0.5, 'bilinear')
        rBilinearFilename = path.join(tmp.path, 'realgridBilinear.tif')
        rBilinear.write(rBilinearFilename)

        rCubic = rTest1.ResampleDEM(0.5, 'cubic')
        rCubicfilename = path.join(tmp.path, 'realgridCubic.tif')
        rCubic.write(rCubicfilename)

        rNearest = rTest1.ResampleDEM(0.5, 'nearest')
        rNearestFilename = path.join(tmp.path, 'realgridNearest.tif')
        rNearest.write(rNearestFilename)

        deleteRaster(rTest1filename)
        deleteRaster(rLinearFilename)
        deleteRaster(rBilinearFilename)
        deleteRaster(rCubicfilename)
        deleteRaster(rNearestFilename)
        tmp.destroy()

        # We have no test for this so it should always fail
        self.assertTrue(False)
Exemplo n.º 3
0
    def test_loadDEMFromCSV(self):
        tmp = TempPathHelper()
        filename = path.join(tmp.path, 'raster1.tif')
        cellSize = 1.0
        padding = 0
        gridPath = path.join(path.dirname(path.abspath(__file__)), 'test', 'assets', 'grids', 'grid1.txt')
        theExtent = unionCSVExtents([gridPath], padding=padding, cellSize=cellSize)
        rTest = Raster(extent=theExtent, cellWidth=cellSize)

        # Here's what we're testing:
        rTest.loadDEMFromCSV(gridPath, theExtent, Raster.PointShift.CENTER)

        testArray = np.ma.masked_array([
            [924.846, 926.17, 901.45, 942.184],
            [904.828, 912.693, np.nan, np.nan],
            [937.801, 928.423, 941.453, 912.141]],
            mask=np.array([
                [0, 0, 0, 0],
                [0, 0, 1, 1],
                [0, 0, 0, 0]]))

        # If the extent is wrong then this test is invalid
        self.assertTupleEqual(theExtent, (-0.5, 3.5, 9.5, 12.5))
        self.assertEqual(theExtent[0], rTest.left)
        self.assertEqual(theExtent[3], rTest.top)

        # Now the real test begins
        self.assertEqual(rTest.cellHeight, -cellSize)
        self.assertEqual(rTest.cellWidth, cellSize)
        self.assertEqual(rTest.rows, 3)
        self.assertEqual(rTest.cols, 4)
        self.assertEqual(rTest.left, -0.5)
        self.assertEqual(rTest.top, 12.5)
        self.assertEqual(rTest.array.shape, (3, 4))
        self.assertTrue((rTest.array == testArray).all())
        rTest.write(filename)
        deleteRaster(filename)
        tmp.destroy()
Exemplo n.º 4
0
    def test_MergeMinSurface(self):
        cellSize = 1
        gridPath1 = path.join(path.dirname(path.abspath(__file__)), 'test', 'assets', 'grids', 'grid1.txt')
        gridPath2 = path.join(path.dirname(path.abspath(__file__)), 'test', 'assets', 'grids', 'grid2.txt')

        theExtent = unionCSVExtents([gridPath1,gridPath2], padding=0)
        # Create our two grids from CSV files that we can add into the min surface
        rTest1 = Raster(extent=theExtent, cellWidth=cellSize)
        rTest1.loadDEMFromCSV(gridPath1, theExtent, ptCenter=Raster.PointShift.CENTER)
        rTest2 = Raster(extent=theExtent, cellWidth=cellSize)
        rTest2.loadDEMFromCSV(gridPath2, theExtent, ptCenter=Raster.PointShift.CENTER)

        # Now let's create a min surface we can use to test things
        rMinSrf = Raster(extent=theExtent, cellWidth=cellSize)
        rMinSrf.setArray(np.nan * np.empty((rTest1.rows, rTest1.cols)))

        rMinSrf.MergeMinSurface(rTest1)
        testArray = np.ma.masked_array([
            [924.846, 926.17, 901.45, 942.184],
            [904.828, 912.693, np.nan, np.nan],
            [937.801, 928.423, 941.453, 912.141]],
            mask=np.array([
                [0, 0, 0, 0],
                [0, 0, 1, 1],
                [0, 0, 0, 0]]))
        self.assertTrue((rMinSrf.array == testArray).all())

        rMinSrf.MergeMinSurface(rTest2)
        testArray2 = np.ma.masked_array([
            [911.511, 922.517, 901.45, 923.232],
            [904.828, 911.819, 907.663, 931.264],
            [919.413, 920.007, 941.453, 912.141]],
            mask=np.array([
                [0, 0, 0, 0],
                [0, 0, 0, 0],
                [0, 0, 0, 0]]))
        self.assertTrue((rMinSrf.array == testArray2).all())
    def GenerateDEMRasters(self, dirSurveyFolder, fCSVCellSize, fCellSize,
                           resampleMethod, nEPSG, bReUseRasters):
        """
        :param dirSurveyFolder:
        :param fCSVCellSize:
        :param fCellSize:
        :param resampleMethod:
        :param theExtent:
        :param nEPSG:
        :param bReUseRasters:
        :return:
        """
        demFolder = os.path.join(dirSurveyFolder, "DEMs_Unclipped")
        if not os.path.exists(demFolder):
            os.makedirs(demFolder)

        # Make sure we're clean and typed
        fCSVCellSize = float(fCSVCellSize)
        fCellSize = float(fCellSize)

        # Retrieve the union of all TXT files for this site
        csvFiles = [
            aDate.txtPointsPath for idx, aDate in self.surveyDates.items()
        ]
        theExtent = unionCSVExtents(csvFiles,
                                    cellSize=fCSVCellSize,
                                    padding=10.0)
        self.log.info("Site {0}: Unioned extent for {1} surveys is {2}".format(
            self.siteCode5, len(self.surveyDates), theExtent))

        # Create a temporary template raster object we can resample
        tempRaster = Raster(proj=nEPSG,
                            extent=theExtent,
                            cellWidth=fCSVCellSize)
        self.log.info(
            "Site {0}: Generating {1} rasters with {2} cols, {3} rows at {4}m cell size..."
            .format(self.siteCode5, len(self.surveyDates), tempRaster.cols,
                    tempRaster.rows, fCellSize))

        # Initialize the Minimum Surface Raster and give it an array of appropriate size
        self.MinimumSurfacePath = os.path.join(
            dirSurveyFolder, "{0}_min_surface.tif".format(self.siteCode5))
        self.MinimumSurface = Raster(proj=nEPSG,
                                     extent=theExtent,
                                     cellWidth=fCellSize)
        self.MinimumSurface.setArray(np.nan * np.empty(
            (self.MinimumSurface.rows, self.MinimumSurface.cols)))

        for SurveyID, aDate in self.surveyDates.items():

            aDate.DEMPath = os.path.join(
                demFolder,
                "{0}_{1:%Y%m%d}_dem.tif".format(self.siteCode5,
                                                aDate.SurveyDate))

            # option to skip that speeds up debugging
            if os.path.isfile(aDate.DEMPath) and bReUseRasters:
                continue

            # Create a raster object that will represent the raw CSV
            rDEM = Raster(proj=nEPSG, extent=theExtent, cellWidth=fCSVCellSize)
            # This function will add the array in-place to the raster object
            rDEM.loadDEMFromCSV(aDate.txtPointsPath, theExtent)

            if fCSVCellSize != fCellSize:
                # This method resamples the array and returns a new raster object
                newDEM = rDEM.ResampleDEM(fCellSize, resampleMethod)

                # Only incorporate the DEM into the analysis if required
                if aDate.IsMinSurface:
                    self.MinimumSurface.MergeMinSurface(newDEM)

                newDEM.write(aDate.DEMPath)
            else:
                # No resample necessary.

                # Only incorporate the DEM into the analysis if required
                if aDate.IsMinSurface:
                    self.MinimumSurface.MergeMinSurface(rDEM)

                # Write the raw DEM object
                rDEM.write(aDate.DEMPath)

            assert os.path.isfile(
                aDate.DEMPath
            ), "Failed to generate raster for site {0} at {1}".format(
                self.siteCode5, aDate.DEMPath)

        ## write the minimum surface raster to file
        if not bReUseRasters:
            assert self.MinimumSurface is not None, "Error generating minimum surface raster for site {0}".format(
                self.siteCode5)
            self.MinimumSurface.write(self.MinimumSurfacePath)

        assert os.path.isfile(
            self.MinimumSurfacePath
        ), "Minimum surface raster is missing for site {0} at {1}".format(
            self.siteCode5, self.MinimumSurfacePath)