/// <summary> /// Validate the input value. /// </summary> /// <param name="value">The input value.</param> /// <param name="validValue">If validation successes, return the validated value. If not, return null.</param> /// <returns>A value indicating whether the validationg is successful.</returns> private bool ValueValidate(string value, out object validValue) { IRasterLayer rasterLayer = (IRasterLayer)Editor.ActiveLayer; IRasterProps rasterProp = (IRasterProps)rasterLayer.Raster; return(Raster.CSharpValue2PixelValue(value, rasterProp.PixelType, out validValue)); }
// After editing the cell value, the current value is recorded. void rasterGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { Position pos = new Position(Convert.ToInt32(rasterGridView.SelectedCells[0].OwningColumn.HeaderText) - 1, Convert.ToInt32(rasterGridView.SelectedCells[0].OwningRow.Cells[0].Value) - 1); IRasterLayer rasterLayer = (IRasterLayer)Editor.ActiveLayer; IRasterProps rasterProps = (IRasterProps)rasterLayer.Raster; object newValue = null; if (rasterGridView[e.ColumnIndex, e.RowIndex].Value == null) { if (oldValue != rasterGridView.NoDataValue) { EditValue(rasterGridView.NoDataValue, oldValue, e.ColumnIndex, e.RowIndex); } } else { if (Raster.CSharpValue2PixelValue(rasterGridView[e.ColumnIndex, e.RowIndex].Value, rasterProps.PixelType, out newValue)) { double newDValue = Convert.ToDouble(newValue); EditValue(newDValue, oldValue, e.ColumnIndex, e.RowIndex); } else { MessageBox.Show(string.Format("Invalid pixel value!\n\nValid pixel type: {0}", rasterProps.PixelType.ToString()), "Error"); if (oldValue != rasterGridView.NoDataValue) { rasterGridView[e.ColumnIndex, e.RowIndex].Value = oldValue; } } } ShowCellStatus(); } catch (Exception ex) { MessageBox.Show(string.Format("Unfortunately, the application meets an error.\n\nSource: {0}\nSite: {1}\nMessage: {2}", ex.Source, ex.TargetSite, ex.Message), "Error"); } }
/// <summary> /// Write edits to the input raster. /// </summary> /// <param name="raster">Raster of raster layer to be edited.</param> /// <param name="edits">Pixel collection that contains edited pixels.</param> private static void WriteEdits(IRaster raster, PixelCollection edits) { IRasterProps rasterProps = (IRasterProps)raster; int minRow = rasterProps.Height - 1; int maxRow = 0; int minCol = rasterProps.Width - 1; int maxCol = 0; for (int i = 0; i < edits.Count; i++) { #region Get the extent of the edition region Position cellPos = edits[i].Position; if (cellPos.Row > maxRow) { maxRow = cellPos.Row; } if (cellPos.Row < minRow) { minRow = cellPos.Row; } if (cellPos.Column > maxCol) { maxCol = cellPos.Column; } if (cellPos.Column < minCol) { minCol = cellPos.Column; } #endregion } IPnt pos = new PntClass(); pos.SetCoords(maxCol - minCol + 1, maxRow - minRow + 1); IPixelBlock pixelBlock = raster.CreatePixelBlock(pos); pos.SetCoords(minCol, minRow); raster.Read(pos, pixelBlock); // Set new values IPixelBlock3 pixelBlock3 = (IPixelBlock3)pixelBlock; Array pixels = (Array)pixelBlock3.get_PixelData(0); for (int i = 0; i < edits.Count; i++) { object value = null; Raster.CSharpValue2PixelValue(edits[i].NewValue, rasterProps.PixelType, out value); pixels.SetValue(value, edits[i].Position.Column - minCol, edits[i].Position.Row - minRow); } pixelBlock3.set_PixelData(0, (System.Object)pixels); IRasterEdit rasterEdit = (IRasterEdit)raster; rasterEdit.Write(pos, (IPixelBlock)pixelBlock3); }