Esempio n. 1
0
        /// <summary>
        /// This function converts a polygon shapefile to grid. It implements the line-scan algorithm.
        /// <param name="PolySf">The polygon shapefile</param>
        /// <param name="FldID">The field index</param>
        /// <param name="Newgrd">File name of the new grid</param>
        /// <param name="header">Header object of the new grid</param>
        /// <param name="writePxDeleg">Delegate function for writing the grid pixels</param>
        /// <param name="nodatavalue">'no data' value</param>
        /// <param name="cback">can be used to report progress (optional)</param>
        /// <returns>true if successful</returns>
        //
        // AUTHOR NAME: Enrico A. Chiaradia
        //       FROM: University of Milan (Italy)
        //     e.mail: [email protected]
        //
        // For details about the line-scan algorithm,
        // please refer to http://www.profc.udec.cl/~gabriel/tutoriales/giswb/vol1/cp3/cp3-6.htm
        //
        // for each shape in PolySf
        // 0) get the shape value (in the shapefile table)
        // 1) load all the borders in a matrix composed by x(startpoint), x(endpoint), slope and share
        // 2) define x of first scan line
        // for each scan lines
        // 3) calculate intersections with polygon border
        // 4) sort intersections from lower Y to the higther
        // 5) convert line space between two consecutive intersection to raster
        private bool Poly2Grid(MapWinGIS.Shapefile PolySf, int FldID, MapWinGIS.Grid Newgrd,
            MapWinGIS.GridHeader header, MapWinGIS.GridDataType grType,
            MapWinGIS.ICallback cback)
        {
            double xMin, xMax;
            double x0, x1, x2, y0, y1, y2, m, q;
            int nShps, nVs, i, s, nP, p, tempn, v, b;
            object val;
            bool flg;
            bord brd; //struct to keep a border
            System.Collections.ArrayList bordi2; //list of borders
            int perc = 1; //percent shapes done
            int shpsPerc;

            MapWinGIS.Shape PolyShp; //the polygon shape

            //count the number of shapes
            nShps = PolySf.NumShapes;
            shpsPerc = NumPercentShapes(perc, nShps); //percent of progress

            for( s = 0; s < nShps; ++s )
            {
                //for each shape, determine the extent and the number of parts
                i = 0;
                PolyShp = PolySf.get_Shape(s);
                nP = PolyShp.NumParts;

                xMin = PolyShp.Extents.xMin;
                //yMin = PolyShp.Extents.yMin;
                xMax = PolyShp.Extents.xMax;
                //yMax = PolyShp.Extents.yMax;

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

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

                //load polygon borders
                nVs = PolyShp.numPoints - 1;
                bordi2 = new System.Collections.ArrayList(nVs + 1);

                b = -1;
                tempn = 0;

                for( p = 0; p < nP; ++p )
                {
                    x0 = PolyShp.get_Point(tempn).x;
                    y0 = PolyShp.get_Point(tempn).y;

                    for( v = tempn; v < nVs; ++v )
                    {
                        x1 = PolyShp.get_Point(v).x;
                        x2 = PolyShp.get_Point(v + 1).x;
                        y1 = PolyShp.get_Point(v).y;
                        y2 = PolyShp.get_Point(v + 1).y;

                        if (x1 != x2) // no vertical lines
                        {
                            m = (y2 - y1) / (x2 - x1);
                            q = y1 - (m * x1);

                            //add border to the list
                            b = b + 1;
                            brd = new bord();
                            brd.x1 = x1;
                            brd.x2 = x2;
                            brd.m = m;
                            brd.q = q;
                            bordi2.Add(brd);
                        }

                        //in case of multi-parts polygon
                        if( (x2 == x0) && (y2 == y0) )
                        {
                            tempn = v + 2;
                            break;
                        }
                    } //Next v
                } // next p

                //define the first line-scan
                double xstart;

                //    20 nov 07 added by Enrico A. Chiaradia
                //necessary if grid extentions differ from shapefile extentions
                if ( xMin >= header.XllCenter )
                {
                    xstart = FirstLineXY(xMin, header.XllCenter, header.dX, 1);
                }
                else
                {
                    xstart = FirstLineXY(xMin, header.XllCenter, header.dX, -1);
                }

                // the list of intersection y-values
                System.Collections.ArrayList y_int = new System.Collections.ArrayList();
                System.Collections.ArrayList pixels = new System.Collections.ArrayList();

                do
                {
                    flg = Interseca(bordi2, y_int, xstart);
                    flg = SortArray(y_int);
                    flg = ScanLine2(y_int, xstart, ref pixels, ref Newgrd, header, val, cback);
                    xstart = xstart + header.dX;
                }
                while ( xstart <= xMax );

                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);
                }

            } // next shape

            reportProgress(100, 100, "shapefile to grid", cback); //100 % shapes done
            return true;
        }
Esempio n. 2
0
        /// <summary>
        /// This function converts a polygon shapefile to grid. It implements the line-scan algorithm.
        /// <param name="PolySf">The polygon shapefile</param>
        /// <param name="FldID">The field index</param>
        /// <param name="Newgrd">File name of the new grid</param>
        /// <param name="header">Header object of the new grid</param>
        /// <param name="writePxDeleg">Delegate function for writing the grid pixels</param>
        /// <param name="nodatavalue">'no data' value</param>
        /// <param name="cback">can be used to report progress (optional)</param>
        /// <returns>true if successful</returns>
        //
        // AUTHOR NAME: Enrico A. Chiaradia
        //       FROM: University of Milan (Italy)
        //     e.mail: [email protected]
        //
        // For details about the line-scan algorithm,
        // please refer to http://www.profc.udec.cl/~gabriel/tutoriales/giswb/vol1/cp3/cp3-6.htm
        //
        // for each shape in PolySf
        // 0) get the shape value (in the shapefile table)
        // 1) load all the borders in a matrix composed by x(startpoint), x(endpoint), slope and share
        // 2) define x of first scan line
        // for each scan lines
        // 3) calculate intersections with polygon border
        // 4) sort intersections from lower Y to the higther
        // 5) convert line space between two consecutive intersection to raster

        private bool Poly2Grid(MapWinGIS.Shapefile PolySf, int FldID, MapWinGIS.Grid Newgrd,
                               MapWinGIS.GridHeader header, MapWinGIS.GridDataType grType,
                               MapWinGIS.ICallback cback)
        {
            double xMin, xMax;
            double x0, x1, x2, y0, y1, y2, m, q;
            int    nShps, nVs, i, s, nP, p, tempn, v, b;
            object val;
            bool   flg;
            bord   brd;                          //struct to keep a border

            System.Collections.ArrayList bordi2; //list of borders
            int perc = 1;                        //percent shapes done
            int shpsPerc;

            MapWinGIS.Shape PolyShp; //the polygon shape

            //count the number of shapes
            nShps    = PolySf.NumShapes;
            shpsPerc = NumPercentShapes(perc, nShps); //percent of progress

            for (s = 0; s < nShps; ++s)
            {
                //for each shape, determine the extent and the number of parts
                i       = 0;
                PolyShp = PolySf.get_Shape(s);
                nP      = PolyShp.NumParts;

                xMin = PolyShp.Extents.xMin;
                //yMin = PolyShp.Extents.yMin;
                xMax = PolyShp.Extents.xMax;
                //yMax = PolyShp.Extents.yMax;

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

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

                //load polygon borders
                nVs    = PolyShp.numPoints - 1;
                bordi2 = new System.Collections.ArrayList(nVs + 1);

                b     = -1;
                tempn = 0;

                for (p = 0; p < nP; ++p)
                {
                    x0 = PolyShp.get_Point(tempn).x;
                    y0 = PolyShp.get_Point(tempn).y;

                    for (v = tempn; v < nVs; ++v)
                    {
                        x1 = PolyShp.get_Point(v).x;
                        x2 = PolyShp.get_Point(v + 1).x;
                        y1 = PolyShp.get_Point(v).y;
                        y2 = PolyShp.get_Point(v + 1).y;

                        if (x1 != x2) // no vertical lines
                        {
                            m = (y2 - y1) / (x2 - x1);
                            q = y1 - (m * x1);

                            //add border to the list
                            b      = b + 1;
                            brd    = new bord();
                            brd.x1 = x1;
                            brd.x2 = x2;
                            brd.m  = m;
                            brd.q  = q;
                            bordi2.Add(brd);
                        }

                        //in case of multi-parts polygon
                        if ((x2 == x0) && (y2 == y0))
                        {
                            tempn = v + 2;
                            break;
                        }
                    } //Next v
                }     // next p

                //define the first line-scan
                double xstart;

                //    20 nov 07 added by Enrico A. Chiaradia
                //necessary if grid extentions differ from shapefile extentions
                if (xMin >= header.XllCenter)
                {
                    xstart = FirstLineXY(xMin, header.XllCenter, header.dX, 1);
                }
                else
                {
                    xstart = FirstLineXY(xMin, header.XllCenter, header.dX, -1);
                }

                // the list of intersection y-values
                System.Collections.ArrayList y_int  = new System.Collections.ArrayList();
                System.Collections.ArrayList pixels = new System.Collections.ArrayList();

                do
                {
                    flg    = Interseca(bordi2, y_int, xstart);
                    flg    = SortArray(y_int);
                    flg    = ScanLine2(y_int, xstart, ref pixels, ref Newgrd, header, val, cback);
                    xstart = xstart + header.dX;
                }while (xstart <= xMax);

                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);
                }
            } // next shape

            reportProgress(100, 100, "shapefile to grid", cback); //100 % shapes done
            return(true);
        }