/// <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; }
private bool Multipoint2Grid(MapWinGIS.Shapefile MultipointSf, int FldID, MapWinGIS.Grid Newgrd, MapWinGIS.GridHeader header, MapWinGIS.GridDataType grType, MapWinGIS.ICallback cback) { //count the number of shapes int s, p; MapWinGIS.Shape shp; MapWinGIS.Point pt; ArrayList pixels = new ArrayList(); object val; GridPixel px; int nShps = MultipointSf.NumShapes; int nPts = 0; int perc = 1; int shpsPerc = NumPercentShapes(perc, nShps); //percent of progress for (s = 0; s < nShps; ++s) { //get the shape shp = MultipointSf.get_Shape(s); //exclude shapes which are completely outside of the grid extents if (!IsGridContainsShape(shp, header)) { continue; } //get the shape's value val = GetCellValue(MultipointSf, FldID, s, header.NodataValue); nPts = shp.numPoints; for (p = 0; p < nPts; ++p) { //write pixel values pt = shp.get_Point(p); Newgrd.ProjToCell(pt.x, pt.y, out px.col, out px.row); pixels.Add(px); 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; }
/// <summary> /// convert point shapefile to grid /// </summary> private bool Point2Grid(MapWinGIS.Shapefile PointSf, int FldID, MapWinGIS.Grid Newgrd, MapWinGIS.GridHeader header, MapWinGIS.GridDataType grType, MapWinGIS.ICallback cback) { //count the number of shapes int s; MapWinGIS.Shape shp; MapWinGIS.Point pt = new MapWinGIS.Point(); object val; GridPixel px; int nShps = PointSf.NumShapes; int perc = 1; int shpsPerc = NumPercentShapes(perc, nShps); //percent of progress for (s = 0; s < nShps; ++s) { //get the point shp = PointSf.get_Shape(s); //exclude shapes which are completely outside of the grid extents if (!IsGridContainsShape(shp, header)) { continue; } if (shp.numPoints > 0) { pt = shp.get_Point(0); if (pt != null) { //return the shape's value and write the pixel val = GetCellValue(PointSf, FldID, s, header.NodataValue); Newgrd.ProjToCell(pt.x, pt.y, out px.col, out px.row); writePx(Newgrd, grType, px, val, cback); } } //report the progress if (s >= shpsPerc) { perc = (int)((s * 100) / nShps); shpsPerc = NumPercentShapes(perc + 1, nShps); reportProgress(shpsPerc, nShps, "shapefile to grid", cback); } } return true; }
/// <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; }