Quantcast
Channel: DotSpatial
Viewing all articles
Browse latest Browse all 3973

New Post: Convert shapefile to raster

$
0
0
Maybe you can do a work around by coupling a raster conversion with a clip raster method

First you convert your shapes to raster, then clip those rasters with a feature of desired extent.

To clip a raster with a polygon I found this old sub I made a long ago (maybe there is one already implemented in dotspatial)

I don't think it is very optimized and also I don't remember what I was doing exactly, I guess that if you are clipping a raster with a larger polygon it adds nodata value on the outer border...but check it

if you want to have a look, if I remember it was pretty slow

    Public Shared Function ClipGrid2Featureset(inputRast As IRaster, inputFS As Envelope, cellWidth As Double, cellHeight As Double, Filename As String, cancelProgressHandler As ICancelProgressHandler) As IRaster
        'Validates the input and output data
        If inputRast Is Nothing OrElse inputFS Is Nothing Then
            Return Nothing
        End If

        'Calculate the Intersect Envelope
        Dim envelope1 As IEnvelope = inputRast.Bounds.Extent.ToEnvelope
        Dim envelope2 As IEnvelope = inputFS
        envelope1 = envelope1.Intersection(envelope2)
        Dim envelopr2use As IEnvelope = envelope1

        Dim noOfRow As Integer = Convert.ToInt32(envelope2.Height / cellHeight)
        Dim noOfCol As Integer = Convert.ToInt32(envelope2.Width / cellWidth)

        'create output raster
        Dim output As IRaster
        output = Raster.Create(Filename, inputRast.DriverCode, noOfCol, noOfRow, 1, inputRast.DataType, New String() {""})
        output.NoDataValue = inputRast.NoDataValue
        Dim extOLD As Extent = envelope2.ToExtent

        Dim extNEW As Extent
        Dim dx As Double = ((extOLD.Width) / 2) / noOfCol
        Dim dy As Double = ((extOLD.Height) / 2) / noOfRow
        extNEW = New Extent(extOLD.MinX + dx, extOLD.MinY - dy, extOLD.MaxX + dx, extOLD.MaxY - dy)
        Dim bound As New RasterBounds(noOfRow, noOfCol, extNEW)

        output.Bounds = bound

        output.NoDataValue = inputRast.NoDataValue
        Dim v1 As RcIndex

        For i As Integer = 0 To output.Bounds.NumRows - 1
            For j As Integer = 0 To output.Bounds.NumColumns - 1
                output.Value(i, j) = output.NoDataValue
            Next
        Next

        Dim imin, imax, jmin, jmax As Integer
        Dim xmin, xmax, ymin, ymax As Double
        Dim x1, y1 As Double

        Dim coord2 As New Coordinate
        Dim coord3 As New Coordinate
        Dim i1, j1, i2, j2 As Integer
        Dim rc1i, rc2i As RcIndex
        Dim sizemax, sizeX1, sizeY1, sizemin As Integer
        Dim startX, startY As Integer
        If envelopr2use.IsNull = False Then
            coord2 = envelopr2use.TopLeft
            coord3 = envelopr2use.BottomRight
            rc1i = inputRast.ProjToCell(coord2)
            If rc1i.Column < 0 Then rc1i.Column = 1
            If rc1i.Row < 0 Then rc1i.Row = 1

            rc2i = inputRast.ProjToCell(coord3)
            sizeX1 = rc2i.Column - rc1i.Column + 1
            sizeY1 = rc2i.Row - rc1i.Row + 1

            startX = rc1i.Column
            startY = rc1i.Row
        Else
            startX = 1
            startY = 1
            sizeX1 = 1
            sizeY1 = 1

        End If
        Dim sizemin2 As Integer
        Dim s1_x, s2_y
        sizemin2 = Math.Min(sizeX1, sizeY1)
        sizemin = Math.Min(sizemin2, 1000)
        sizemax = Math.Max(sizeX1, sizeY1)

        For s2 As Integer = startY To startY + sizeY1 - 1 Step sizemin
            For s1 As Integer = startX To startX + sizeX1 - 1 Step sizemin

                s1_x = s1
                s2_y = s2

                Dim inputrast2 As IRaster
                Dim inputrast3 As IRaster

 
                If inputRast.IsInRam = False Then
                    s1_x = Math.Min(s1, inputRast.NumColumns - 1000 - 6)
                    s2_y = Math.Min(s2, inputRast.NumRows - 1000 - 6)
                    inputrast2 = inputRast.ReadBlock(s1_x, s2_y, sizemin + 5, sizemin + 5)
                Else
                    inputrast2 = inputRast
                End If

                xmin = inputrast2.Bounds.Extent.MinX 'inputrast2.Bounds.Extent.MinX
                ymin = inputrast2.Bounds.Extent.MinY
                xmax = inputrast2.Bounds.Extent.MaxX
                ymax = inputrast2.Bounds.Extent.MaxY



                Dim coord1 As New Coordinate

                imin = 0
                For i As Integer = 0 To output.Bounds.NumRows - 1
                    coord1 = output.CellToProj(i, 1)
                    If ymax > coord1.Y Then
                        imin = i
                        Exit For
                    End If
                Next

                imax = -1
                For i As Integer = output.Bounds.NumRows - 1 To 0 Step -1
                    coord1 = output.CellToProj(i, 1)
                    If ymin < coord1.Y Then
                        imax = i
                        Exit For
                    End If
                Next

                jmin = 0
                For j As Integer = 0 To output.Bounds.NumColumns - 1
                    coord1 = output.CellToProj(1, j)
                    If xmin < coord1.X Then
                        jmin = j
                        Exit For
                    End If
                Next

                jmax = -1
                For j As Integer = output.Bounds.NumColumns - 1 To 0 Step -1
                    coord1 = output.CellToProj(1, j)
                    If xmax > coord1.X Then
                        jmax = j
                        Exit For
                    End If
                Next

                Dim previous As Integer = 0
                Dim cellcenter As Coordinate
                Dim max As Integer = (output.Bounds.NumRows + 1)
                For i As Integer = imin To imax
                    For j As Integer = jmin To jmax
                        cellcenter = output.CellToProj(i, j)

                        If output.Value(i, j) = output.NoDataValue Then
                            output.Value(i, j) = inputrast2.GetNearestValue(cellcenter)

                        End If

                    Next

                Next
            Next
        Next

        output.GetStatistics()
        output.Save()
        Return output
    End Function
Oscar

Viewing all articles
Browse latest Browse all 3973

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>