/// <summary> /// Save the edition as a specified file. /// </summary> /// <param name="fileName">New file path of raster layer to be saved.</param> /// <param name="activeLayer">Raster layer to be saved.</param> /// <param name="edits">Pixel collection that contains edited pixels.</param> public static void SaveEditsAs(string fileName, IRasterLayer activeLayer, PixelCollection edits) { if (activeLayer == null || edits.Count == 0) { return; } //Random rnd = new Random(); //string tempFile = rnd.Next().ToString(); ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDesktop); // Get the original file // IRasterLayer rasterLayer = activeLayer; IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); //IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(rasterLayer.FilePath), 0); //IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(rasterLayer.FilePath)); // Open the new file save location IWorkspace mWorkspace = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(fileName), 0); // Save the original file to a new file ISaveAs saveAs = (ISaveAs)activeLayer.Raster; IRasterDataset2 mRasterDataset = (IRasterDataset2)saveAs.SaveAs(System.IO.Path.GetFileName(fileName), mWorkspace, Raster.GetFormat(System.IO.Path.GetExtension(fileName))); IRaster mRaster = mRasterDataset.CreateFullRaster(); // Save edits to file Raster.WriteEdits(mRaster, edits); System.Runtime.InteropServices.Marshal.ReleaseComObject(mRaster); }
private void changeColorToolStripMenuItem_Click_1(object sender, EventArgs e) { if (colorDialog.ShowDialog() == DialogResult.OK) { try { valueListBox.Items[changeColorIndex].SubItems[1].BackColor = colorDialog.Color; // Redraw the paint symbol based on new fill color double value = double.Parse(valueListBox.Items[changeColorIndex].Text); PixelCollection pixels = Painter.Paints.FindAll(pixel => pixel.NewValue == value); if (pixels.Count > 0) { IColor color = this.SelectedColor; foreach (Pixel pixel in pixels) { Display.RemoveElement(pixel.GraphicElement); pixel.GraphicElement = Display.DrawBox(pixel.Position, Painter.GetPaintSymbol(color), Painter.ActiveLayer); ArcMap.Document.ActiveView.Refresh(); } } } 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> /// Set the cell values of the selected region /// </summary> /// <param name="tlCorner">Left-top corner</param> /// <param name="brCorner">Right-bottom corner</param> /// <param name="values"></param> public void SetValues(Position tlCorner, Position brCorner, double[,] values) { rasterGridView.SetValues(tlCorner, brCorner, values); PixelCollection editedCellCollection = Editor.Edits.WithIn(tlCorner, brCorner); for (int i = 0; i < editedCellCollection.Count; i++) { int gridViewCol = editedCellCollection[i].Position.Column - tlCorner.Column + 1; int gridViewRow = editedCellCollection[i].Position.Row - tlCorner.Row; rasterGridView[gridViewCol, gridViewRow].Value = editedCellCollection[i].NewValue; rasterGridView[gridViewCol, gridViewRow].Style.Font = new Font(rasterGridView.Font, FontStyle.Bold); } }
/// <summary> /// Save edits to the original file. /// </summary> /// <param name="activeLayer">Raster layer to be saved.</param> /// <param name="edits">Pixel collection that contains edited pixels.</param> public static void SaveEdits(IRasterLayer activeLayer, PixelCollection edits) { if (activeLayer == null || edits.Count == 0) { return; } ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDesktop); // Get the original file //IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); //IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(activeLayer.FilePath), 0); //IRasterDataset2 mRasterDataset = (IRasterDataset2)rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(activeLayer.FilePath)); //IRaster mRaster = mRasterDataset.CreateFullRaster(); Raster.WriteEdits(activeLayer.Raster, edits); //System.Runtime.InteropServices.Marshal.ReleaseComObject(mRaster); ArcMap.Document.ActiveView.Refresh(); }
// Clear all edits within the ROI private void clearAllEditsToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("Are you going to clear all edits within the selected region?", "Confirm", MessageBoxButtons.YesNo); if (result != DialogResult.Yes) { return; } Position tlCorner = new Position(Convert.ToInt32(rasterGridView.Columns[1].HeaderText) - 1, Convert.ToInt32(rasterGridView.Rows[0].Cells[0].Value) - 1); Position brCorner = new Position(Convert.ToInt32(rasterGridView.Columns[rasterGridView.Columns.Count - 1].HeaderText) - 1, Convert.ToInt32(rasterGridView.Rows[rasterGridView.Rows.Count - 1].Cells[0].Value) - 1); PixelCollection editedCellCollection = Editor.Edits.WithIn(tlCorner, brCorner); for (int i = 0; i < editedCellCollection.Count; i++) { Pixel editedCell = editedCellCollection[i]; EditValue(editedCell.NewValue, editedCell.NewValue, editedCell.Position.Column - tlCorner.Column + 1, editedCell.Position.Row - tlCorner.Row); } }
/// <summary> /// Set the cell values of the identified region /// </summary> /// <param name="tlCorner">Left-top corner</param> /// <param name="brCorner">Right-bottom corner</param> /// <param name="values"></param> public void SetValues(Position tlCorner, Position brCorner, double[,] values) { tlPosTextBox.Text = String.Format("({0}, {1})", tlCorner.Column + 1, tlCorner.Row + 1); brPosTextBox.Text = String.Format("({0}, {1})", brCorner.Column + 1, brCorner.Row + 1); rasterGridView.SetValues(tlCorner, brCorner, values); PixelCollection editedCellCollection = Editor.Edits.WithIn(tlCorner, brCorner); for (int i = 0; i < editedCellCollection.Count; i++) { int gridViewCol = editedCellCollection[i].Position.Column - tlCorner.Column + 1; int gridViewRow = editedCellCollection[i].Position.Row - tlCorner.Row; if (editedCellCollection[i].NewValue == rasterGridView.NoDataValue) { rasterGridView[gridViewCol, gridViewRow].Value = editedCellCollection[i].NewValue; } rasterGridView[gridViewCol, gridViewRow].Style.Font = new Font(rasterGridView.Font, FontStyle.Bold); } rasterGridView[0, 0].Selected = false; }
/// <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); }
/// <summary> /// Save the edition as a specified file. /// </summary> /// <param name="fileName">New file path of raster layer to be saved.</param> /// <param name="activeLayer">Raster layer to be saved.</param> /// <param name="edits">Pixel collection that contains edited pixels.</param> public static void SaveEditsAs(string fileName, IRasterLayer activeLayer, PixelCollection edits) { if (activeLayer == null || edits.Count == 0) return; //Random rnd = new Random(); //string tempFile = rnd.Next().ToString(); ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDesktop); // Get the original file // IRasterLayer rasterLayer = activeLayer; IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); //IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(rasterLayer.FilePath), 0); //IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(rasterLayer.FilePath)); // Open the new file save location IWorkspace mWorkspace = workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(fileName), 0); // Save the original file to a new file ISaveAs saveAs = (ISaveAs)activeLayer.Raster; IRasterDataset2 mRasterDataset = (IRasterDataset2)saveAs.SaveAs(System.IO.Path.GetFileName(fileName), mWorkspace, Raster.GetFormat(System.IO.Path.GetExtension(fileName))); IRaster mRaster = mRasterDataset.CreateFullRaster(); // Save edits to file Raster.WriteEdits(mRaster, edits); System.Runtime.InteropServices.Marshal.ReleaseComObject(mRaster); }
/// <summary> /// Save edits to the original file. /// </summary> /// <param name="activeLayer">Raster layer to be saved.</param> /// <param name="edits">Pixel collection that contains edited pixels.</param> public static void SaveEdits(IRasterLayer activeLayer, PixelCollection edits) { if (activeLayer == null || edits.Count == 0) return; ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDesktop); // Get the original file //IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); //IRasterWorkspace rasterWorkspace = (IRasterWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(activeLayer.FilePath), 0); //IRasterDataset2 mRasterDataset = (IRasterDataset2)rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(activeLayer.FilePath)); //IRaster mRaster = mRasterDataset.CreateFullRaster(); Raster.WriteEdits(activeLayer.Raster, edits); //System.Runtime.InteropServices.Marshal.ReleaseComObject(mRaster); ArcMap.Document.ActiveView.Refresh(); }