Пример #1
0
        /// <summary>
        /// converts a Line shapefile to grid using Bresenham algorithm
        /// </summary>
        /// <param name="PolySf">Polygon shapefile object</param>
        /// <param name="FldID">Field index</param>
        /// <param name="Newgrd">New grid object</param>
        /// <param name="header">Grid header</param>
        /// <param name="nodatavalue"></param>
        /// <param name="cback">optional, for reporting progress</param>
        /// <returns>true if successful</returns>
        private bool Line2Grid(MapWinGIS.Shapefile LineSf, int FldID, MapWinGIS.Grid Newgrd,
            MapWinGIS.GridHeader header, MapWinGIS.GridDataType grType, MapWinGIS.ICallback cback)
        {
            int numParts, numPoints;
            int curPartStart, curPartEnd, vertexCol, vertexRow, lastCol, lastRow;
            int nShps = LineSf.NumShapes;
            int perc = 1;
            int shpsPerc = NumPercentShapes(perc, nShps); //percent of progress
            int s = 0; //shape number
            int p = 0; //part number
            int k = 0; //point number
            MapWinGIS.Shape LineShp = new MapWinGIS.Shape();
            MapWinGIS.Point curPoint;
            //Rasterization ras = new Rasterization(); //rasterization class, contains the code of
            //lineToGrid methods independent on MapWinGIS functions
            ArrayList vertices = new ArrayList(); //list of line vertex points
            ArrayList pixels = new ArrayList();   //list of calculated pixels on the line
            GridPixel px;

            object val; //the shape's value

            for (s = 0; s < nShps; ++s)
            {
            LineShp = LineSf.get_Shape(s);
            numParts = LineShp.NumParts;
            numPoints = LineShp.numPoints;

            //exclude shapes which are completely outside of the grid extents
            if (!IsGridContainsShape(LineShp, header))
            {
                continue;
            }

            //get the shape's value
            val = GetCellValue(LineSf, FldID, s, header.NodataValue);

            //process each part of the polyline
            curPartStart = 0;
            for (p = 0; p < numParts; ++p)
            {
                vertices.Clear();
                pixels.Clear();

                curPartStart = LineShp.get_Part(p);

                // check for multi-part lines
                if (p < numParts - 1)
                {
                    curPartEnd = LineShp.get_Part(p + 1) - 1;
                }
                else
                {
                    curPartEnd = numPoints - 1;
                }

                //go to next part if there's zero points
                if (numPoints <= 0)
                {
                    continue;
                }

                // add all points of current part to rasterization list
                // always add the first point of the part (convert its coordinates to
                // grid row and column)
                curPoint = LineShp.get_Point(curPartStart);
                Newgrd.ProjToCell(curPoint.x, curPoint.y,
                        out vertexCol, out vertexRow);
                px.col = vertexCol;
                px.row = vertexRow;
                vertices.Add(px);
                lastCol = vertexCol; lastRow = vertexRow;

                // add all other points with different grid coordinates
                for (k = curPartStart + 1; k <= curPartEnd; ++k)
                {
                    // (check if it has a different row or column than the previous point)
                    curPoint = LineShp.get_Point(k);
                    Newgrd.ProjToCell(curPoint.x, curPoint.y,
                        out vertexCol, out vertexRow);
                    if (vertexCol != lastCol || vertexRow != lastRow)
                    {
                        px.col = vertexCol;
                        px.row = vertexRow;
                        vertices.Add(px);
                        lastCol = vertexCol;
                        lastRow = vertexRow;
                    }
                }

                // convert the polyline and write pixels to grid
                LineBresenham(vertices, pixels);
                writePxList(Newgrd, grType, pixels, val, cback);

            }

            //report the progress
            if (s >= shpsPerc)
            {
                reportProgress(shpsPerc, nShps, "shapefile to grid", cback);
                perc = (int)((s * 100) / nShps);
                shpsPerc = NumPercentShapes(perc + 1, nShps);
            }
            }
            return true;
        }
Пример #2
0
/// <summary>
        /// converts a Line shapefile to grid using Bresenham algorithm
        /// </summary>
        /// <param name="PolySf">Polygon shapefile object</param>
        /// <param name="FldID">Field index</param>
        /// <param name="Newgrd">New grid object</param>
        /// <param name="header">Grid header</param>
        /// <param name="nodatavalue"></param>
        /// <param name="cback">optional, for reporting progress</param>
        /// <returns>true if successful</returns>

        private bool Line2Grid(MapWinGIS.Shapefile LineSf, int FldID, MapWinGIS.Grid Newgrd,
                               MapWinGIS.GridHeader header, MapWinGIS.GridDataType grType, MapWinGIS.ICallback cback)
        {
            int numParts, numPoints;
            int curPartStart, curPartEnd, vertexCol, vertexRow, lastCol, lastRow;
            int nShps    = LineSf.NumShapes;
            int perc     = 1;
            int shpsPerc = NumPercentShapes(perc, nShps); //percent of progress
            int s        = 0;                             //shape number
            int p        = 0;                             //part number
            int k        = 0;                             //point number

            MapWinGIS.Shape LineShp = new MapWinGIS.Shape();
            MapWinGIS.Point curPoint;
            //Rasterization ras = new Rasterization(); //rasterization class, contains the code of
            //lineToGrid methods independent on MapWinGIS functions
            ArrayList vertices = new ArrayList(); //list of line vertex points
            ArrayList pixels   = new ArrayList(); //list of calculated pixels on the line
            GridPixel px;

            object val; //the shape's value


            for (s = 0; s < nShps; ++s)
            {
                LineShp   = LineSf.get_Shape(s);
                numParts  = LineShp.NumParts;
                numPoints = LineShp.numPoints;

                //exclude shapes which are completely outside of the grid extents
                if (!IsGridContainsShape(LineShp, header))
                {
                    continue;
                }

                //get the shape's value
                val = GetCellValue(LineSf, FldID, s, header.NodataValue);

                //process each part of the polyline
                curPartStart = 0;
                for (p = 0; p < numParts; ++p)
                {
                    vertices.Clear();
                    pixels.Clear();

                    curPartStart = LineShp.get_Part(p);

                    // check for multi-part lines
                    if (p < numParts - 1)
                    {
                        curPartEnd = LineShp.get_Part(p + 1) - 1;
                    }
                    else
                    {
                        curPartEnd = numPoints - 1;
                    }

                    //go to next part if there's zero points
                    if (numPoints <= 0)
                    {
                        continue;
                    }

                    // add all points of current part to rasterization list
                    // always add the first point of the part (convert its coordinates to
                    // grid row and column)
                    curPoint = LineShp.get_Point(curPartStart);
                    Newgrd.ProjToCell(curPoint.x, curPoint.y,
                                      out vertexCol, out vertexRow);
                    px.col = vertexCol;
                    px.row = vertexRow;
                    vertices.Add(px);
                    lastCol = vertexCol; lastRow = vertexRow;

                    // add all other points with different grid coordinates
                    for (k = curPartStart + 1; k <= curPartEnd; ++k)
                    {
                        // (check if it has a different row or column than the previous point)
                        curPoint = LineShp.get_Point(k);
                        Newgrd.ProjToCell(curPoint.x, curPoint.y,
                                          out vertexCol, out vertexRow);
                        if (vertexCol != lastCol || vertexRow != lastRow)
                        {
                            px.col = vertexCol;
                            px.row = vertexRow;
                            vertices.Add(px);
                            lastCol = vertexCol;
                            lastRow = vertexRow;
                        }
                    }

                    // convert the polyline and write pixels to grid
                    LineBresenham(vertices, pixels);
                    writePxList(Newgrd, grType, pixels, val, cback);
                }

                //report the progress
                if (s >= shpsPerc)
                {
                    reportProgress(shpsPerc, nShps, "shapefile to grid", cback);
                    perc     = (int)((s * 100) / nShps);
                    shpsPerc = NumPercentShapes(perc + 1, nShps);
                }
            }
            return(true);
        }