/// <summary> /// Executes the Erase Opaeration tool programaticaly /// </summary> /// <param name="input1">The input raster</param> /// <param name="input2">The input raster</param> /// <param name="output">The output raster</param> /// <param name="cancelProgressHandler">The progress handler</param> /// <returns></returns> public bool Execute(IRaster input1, IRaster input2, IRaster output, MapWindow.Tools.ICancelProgressHandler cancelProgressHandler) { //Validates the input and output data if (input1 == null || input2 == null || output == null) { return false; } IEnvelope envelope = new Envelope() as IEnvelope; envelope = UnionEnvelope(input1, input2); int noOfCol; int noOfRow; //Figures out which raster has smaller cells IRaster smallestCellRaster; if (input1.CellWidth < input2.CellWidth) smallestCellRaster = input1; else smallestCellRaster = input2; //Given the envelope of the two rasters we calculate the number of columns / rows noOfCol = Convert.ToInt32(Math.Abs(envelope.Width / smallestCellRaster.CellWidth)); noOfRow = Convert.ToInt32(Math.Abs(envelope.Height / smallestCellRaster.CellHeight)); //Create the new raster with the appropriate dimensions Raster Temp = new Raster(); //Temp.CreateNew(output.Filename, noOfRow, noOfCol, input1.DataType); Temp.CreateNew(output.Filename, "", noOfCol, noOfRow, 1, input1.DataType, new string[] { "" }); Temp.CellWidth = smallestCellRaster.CellWidth; Temp.CellHeight = smallestCellRaster.CellHeight; Temp.Xllcenter = envelope.Minimum.X + (Temp.CellWidth / 2); Temp.Yllcenter = envelope.Minimum.Y + (Temp.CellHeight / 2); MapWindow.Geometries.ICoordinate I1 = new MapWindow.Geometries.Coordinate() as MapWindow.Geometries.ICoordinate; RcIndex v1 = new RcIndex(); RcIndex v2 = new RcIndex(); double val1; double val2; int previous=0; int current=0; int max = (Temp.Bounds.NumRows + 1); for (int i = 0; i < Temp.Bounds.NumRows; i++) { for (int j = 0; j < Temp.Bounds.NumColumns; j++) { I1 = Temp.CellToProj(i, j); v1 = input1.ProjToCell(I1); if (v1.Row <= input1.EndRow && v1.Column <= input1.EndColumn && v1.Row > -1 && v1.Column > -1) val1 = input1.Value[v1.Row, v1.Column]; else val1 = input1.NoDataValue; v2 = input2.ProjToCell(I1); if (v2.Row <= input2.EndRow && v2.Column <= input2.EndColumn && v2.Row > -1 && v2.Column > -1) val2 = input2.Value[v2.Row, v2.Column]; else val2 = input2.NoDataValue; if (val1 == input1.NoDataValue && val2 == input2.NoDataValue) { Temp.Value[i, j] = Temp.NoDataValue; } else if (val1 != input1.NoDataValue && val2 == input2.NoDataValue) { Temp.Value[i, j] = val1; } else if (val1 == input1.NoDataValue && val2 != input2.NoDataValue) { Temp.Value[i, j] = val2; } else { Temp.Value[i, j] = val1; } if (cancelProgressHandler.Cancel == true) return false; } current = Convert.ToInt32(Math.Round(i * 100D / max)); //only update when increment in persentage if(current>previous) cancelProgressHandler.Progress("", current, current.ToString() + "% progress completed"); previous = current; } output = Temp; output.Save(); return true; }
/// <summary> /// Executes the Erase Opaeration tool programaticaly. /// </summary> /// <param name="input1">The input raster.</param> /// <param name="input2">The input raster.</param> /// <param name="output">The output raster.</param> /// <param name="cancelProgressHandler">The progress handler.</param> /// <returns></returns> public bool Execute(IRaster input1, IFeatureSet input2, IRaster output, MapWindow.Tools.ICancelProgressHandler cancelProgressHandler) { //Validates the input and output data if (input1 == null || input2 == null || output == null) { return false; } int noOfCol = input1.NumColumns; int noOfRow = input1.NumRows; //Calculate the Intersect Envelope IEnvelope envelope1 = new Envelope() as IEnvelope; IEnvelope envelope2 = new Envelope() as IEnvelope; envelope1 = input1.Bounds.Envelope; envelope2 = input2.Envelope; envelope1 = envelope1.Intersection(envelope2); //Create the new raster with the appropriate dimensions IRaster Temp = Raster.Create(output.Filename, "", noOfCol, noOfRow, 1, input1.DataType, new string[] { }); //Temp.CreateNew(output.Filename, noOfRow, noOfCol, input1.DataType); Temp.CellWidth = input1.CellWidth; Temp.CellHeight = input1.CellHeight; Temp.Xllcenter = envelope1.Minimum.X + (Temp.CellWidth / 2); Temp.Yllcenter = envelope1.Minimum.Y + (Temp.CellHeight / 2); MapWindow.Geometries.ICoordinate I1 = new MapWindow.Geometries.Coordinate() as MapWindow.Geometries.ICoordinate; RcIndex v1 = new RcIndex(); // MapWindow.Analysis.Topology.Algorithm.PointLocator pointLocator1 = new MapWindow.Analysis.Topology.Algorithm.PointLocator(); double val1; int previous = 0; int current = 0; int max = (Temp.Bounds.NumRows + 1); for (int i = 0; i < Temp.Bounds.NumRows; i++) { for (int j = 0; j < Temp.Bounds.NumColumns; j++) { I1 = Temp.CellToProj(i, j); v1 = input1.ProjToCell(I1); if (v1.Row <= input1.EndRow && v1.Column <= input1.EndColumn && v1.Row > -1 && v1.Column > -1) val1 = input1.Value[v1.Row, v1.Column]; else val1 = Temp.NoDataValue; //test if the coordinate is inside of the polygon bool isInside = false; IFeature pointFeat = new Feature(new Point(I1)) as IFeature; foreach (IFeature f in input2.Features) { if (f.Contains(pointFeat)) { Temp.Value[i, j] = val1; isInside = true; break; } } if (isInside == false) { Temp.Value[i, j] = Temp.NoDataValue; } if (cancelProgressHandler.Cancel == true) return false; } current = Convert.ToInt32(Math.Round(i * 100D / max)); //only update when increment in percentage if (current > previous) cancelProgressHandler.Progress("", current, current.ToString() + "% progress completed"); previous = current; } output = Temp; output.Save(); return true; }
/// <summary> /// Extends the IRaster interface to return the coordinate of the center of a row column position. /// </summary> /// <param name="raster">The raster interface to extend</param> /// <param name="position">The zero based integer index of the row and column of the cell to locate</param> /// <returns>The geographic location of the center of the specified cell</returns> public static Coordinate CellToProj(this IRaster raster, RcIndex position) { if (raster == null) return null; if (raster.Bounds == null) return null; return raster.Bounds.CellCenter_ToProj(position.Row, position.Column); }