栅格重分类方法很多,在AE中有多种方式可以实现,使用地图代数(在RasterModel中实现),或者IReclassOp,或者Geoprocessor的方式都可以,甚至可以遍历栅格来实现,这是最原始的方式,不过也可能是最实用的。

这里使用的是最原始的遍历栅格的方式。

 

 

 Public Function reclass(ByVal pRaster As IRaster, ByVal extent As ArrayList, ByVal value As ArrayList, ByVal NoData As Single) As IRaster


        '得倒栅格的IRasterProps和IRawPixels
        Dim pRasterBands As IRasterBandCollection
        pRasterBands = pRaster

        Dim pRasterBand As IRasterBand
        pRasterBand = pRasterBands.Item(0)

        Dim pRawRixels As IRawPixels
        pRawRixels = pRasterBands.Item(0)

        Dim pRasterPro As IRasterProps
        pRasterPro = pRasterBand

        '设置栅格数据起始点
        Dim pBlockSize As IPnt
        pBlockSize = New Pnt()

        pBlockSize.SetCoords(pRasterPro.Width, pRasterPro.Height)

        '在栅格上创建pPixBlock,用来存放栅格数据
        Dim pPixelBlock As IPixelBlock
        pPixelBlock = pRaster.CreatePixelBlock(pBlockSize)

        '左上点坐标
        Dim tlp As IPnt = New Pnt()
        tlp.SetCoords(0, 0)

        '读入栅格
        pRawRixels.Read(tlp, pPixelBlock)

        '将PixBlock的值组成数组
        Dim pSafeArray As System.Array
        pSafeArray = pPixelBlock.SafeArray(0)


        '遍历每一个栅格,进行分类设置
        For y As Long = 0 To pRasterPro.Height - 1
            For x As Long = 0 To pRasterPro.Width - 1

                '判断每个栅格的所属类,确定其属于哪一类,然后进行赋值,即重分类
                For i As Integer = 0 To extent.Count - 1

                    '如果值< 0,说明该处为空值(Nodata),赋给这个栅格相应的值
                    If CType(pSafeArray.GetValue(x, y), Single) < 0 Then
                        pSafeArray.SetValue(NoData, x, y)
                        Exit For
                    End If

                    '判断栅格属于哪一类,进行赋值
                    If CType(pSafeArray.GetValue(x, y), Single) <= CType(extent(i), Single) Then
                        pSafeArray.SetValue(value(i), x, y)
                        Exit For
                    End If
                Next
            Next
        Next


        '将数组赋给PixBlock
        pPixelBlock.SafeArray(0) = CType(pSafeArray, System.Array)

        '编辑raster,将更新的值写入raster中
        Dim rasterEdit As IRasterEdit

        rasterEdit = pRaster
        rasterEdit.Write(tlp, pPixelBlock)
        rasterEdit.Refresh()

        Return pRaster

    End Function


 

 

posted on 2008-08-10 18:46  张进洁  阅读(4044)  评论(1编辑  收藏  举报