private Image _stencil; // draw features to the stencil #endregion #region Constructors /// <summary> /// Creates a new raster layer from the specified fileName /// </summary> /// <param name="fileName"></param> /// <param name="symbolizer"></param> public MapRasterLayer(string fileName, IRasterSymbolizer symbolizer) : base(fileName, symbolizer) { base.LegendText = Path.GetFileNameWithoutExtension(fileName); if (DataSet.NumRows * DataSet.NumColumns > 8000) { string pyrFile = Path.ChangeExtension(fileName, ".mwi"); if (File.Exists(pyrFile) && File.Exists(Path.ChangeExtension(pyrFile, ".mwh"))) { BitmapGetter = new PyramidImage(pyrFile); } else { BitmapGetter = CreatePyramidImage(pyrFile, DataManager.DefaultDataManager.ProgressHandler); } } else { Bitmap bmp = new Bitmap(DataSet.NumColumns, DataSet.NumRows); symbolizer.Raster = DataSet; DataSet.DrawToBitmap(symbolizer, bmp, null); InRamImage id = new InRamImage(bmp); id.Bounds = DataSet.Bounds; BitmapGetter = id; } }
/// <summary> /// Creates a bitmap using only the colorscheme, even if a hillshade was specified. /// </summary> /// <param name="raster">The Raster containing values that need to be drawn to the bitmap as a color scheme.</param> /// <param name="rasterSymbolizer">The raster symbolizer to use.</param> /// <param name="bitmap">The bitmap to edit. Ensure that this has been created and saved at least once.</param> /// <param name="progressHandler">An IProgressHandler implementation to receive progress updates.</param> /// <exception cref="ArgumentNullException">rasterSymbolizer cannot be null.</exception> public static void PaintColorSchemeToBitmap(this IRaster raster, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) { if (raster.DataType == typeof(int)) { PaintColorSchemeToBitmapT(raster.ToRaster <int>(), rasterSymbolizer, bitmap, progressHandler); } else if (raster.DataType == typeof(float)) { PaintColorSchemeToBitmapT(raster.ToRaster <float>(), rasterSymbolizer, bitmap, progressHandler); } else if (raster.DataType == typeof(short)) { PaintColorSchemeToBitmapT(raster.ToRaster <short>(), rasterSymbolizer, bitmap, progressHandler); } else if (raster.DataType == typeof(byte)) { PaintColorSchemeToBitmapT(raster.ToRaster <byte>(), rasterSymbolizer, bitmap, progressHandler); } else if (raster.DataType == typeof(double)) { PaintColorSchemeToBitmapT(raster.ToRaster <double>(), rasterSymbolizer, bitmap, progressHandler); } else { PaintColorSchemeToBitmapT(raster, raster.NoDataValue, (row, col) => raster.Value[row, col], rasterSymbolizer, bitmap, progressHandler); } }
/// <summary> /// Creates a bitmap from this raster using the specified rasterSymbolizer. /// </summary> /// <param name="raster">The raster to draw to a bitmap.</param> /// <param name="rasterSymbolizer">The raster symbolizer to use for assigning colors.</param> /// <param name="rgbData">Byte values representing the ARGB image bytes.</param> /// <param name="stride">The stride</param> /// <param name="pm">The progress meter to use.</param> public static void DrawToBitmap(this IRaster raster, IRasterSymbolizer rasterSymbolizer, byte[] rgbData, int stride, ProgressMeter pm) { if (raster.DataType == typeof(int)) { DrawToBitmapT(raster.ToRaster <int>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(float)) { DrawToBitmapT(raster.ToRaster <float>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(short)) { DrawToBitmapT(raster.ToRaster <short>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(byte)) { DrawToBitmapT(raster.ToRaster <byte>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(double)) { DrawToBitmapT(raster.ToRaster <double>(), rasterSymbolizer, rgbData, stride, pm); } else { DrawToBitmapT(raster, raster.NoDataValue, (row, col) => raster.Value[row, col], i => rgbData[i], (i, b) => rgbData[i] = b, rasterSymbolizer, stride, pm); if (rasterSymbolizer.IsSmoothed) { var mySmoother = new Smoother(stride, raster.NumColumns, raster.NumRows, rgbData, pm.ProgressHandler); mySmoother.Smooth(); } } }
/// <summary> /// Initializes a new instance of the <see cref="MapRasterLayer"/> class from the specified fileName. /// </summary> /// <param name="fileName">Filename of the corresponding raster file.</param> /// <param name="symbolizer">Symbolizer used for drawing the raster data.</param> public MapRasterLayer(string fileName, IRasterSymbolizer symbolizer) : base(fileName, symbolizer) { LegendText = Path.GetFileNameWithoutExtension(fileName); // CGX if (File.Exists(fileName)) { if ((long)DataSet.NumRows * DataSet.NumColumns > MaxCellsInMemory) { string pyrFile = Path.ChangeExtension(fileName, ".mwi"); BitmapGetter = File.Exists(pyrFile) && File.Exists(Path.ChangeExtension(pyrFile, ".mwh")) ? new PyramidImage(pyrFile) : CreatePyramidImage(pyrFile, DataManager.DefaultDataManager.ProgressHandler); } else { Bitmap bmp = new Bitmap(DataSet.NumColumns, DataSet.NumRows); symbolizer.Raster = DataSet; DataSet.DrawToBitmap(symbolizer, bmp); var id = new InRamImage(bmp) { Bounds = DataSet.Bounds }; BitmapGetter = id; } } }
private static void DrawToBitmap(IRaster raster, IRasterSymbolizer rasterSymbolizer, IntPtr rgbData, int stride, ProgressMeter pm) { if (raster.DataType == typeof(int)) { DrawToBitmapT(raster.ToRaster <int>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(float)) { DrawToBitmapT(raster.ToRaster <float>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(short)) { DrawToBitmapT(raster.ToRaster <short>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(byte)) { DrawToBitmapT(raster.ToRaster <byte>(), rasterSymbolizer, rgbData, stride, pm); } else if (raster.DataType == typeof(double)) { DrawToBitmapT(raster.ToRaster <double>(), rasterSymbolizer, rgbData, stride, pm); } else { DrawToBitmapT(raster, raster.NoDataValue, (row, col) => raster.Value[row, col], i => Marshal.ReadByte(rgbData, i), (i, b) => Marshal.WriteByte(rgbData, i, b), rasterSymbolizer, stride, pm); if (rasterSymbolizer.IsSmoothed) { var mySmoother = new Smoother(stride, raster.NumColumns, raster.NumRows, rgbData, pm.ProgressHandler); mySmoother.Smooth(); } } }
private Image _stencil; // draw features to the stencil #endregion #region Constructors /// <summary> /// Creates a new raster layer from the specified fileName /// </summary> /// <param name="fileName"></param> /// <param name="symbolizer"></param> public MapRasterLayer(string fileName, IRasterSymbolizer symbolizer) : base(fileName, symbolizer) { base.LegendText = Path.GetFileNameWithoutExtension(fileName); if (DataSet.NumRows * DataSet.NumColumns > 8000) { string pyrFile = Path.ChangeExtension(fileName, ".mwi"); if (File.Exists(pyrFile) && File.Exists(Path.ChangeExtension(pyrFile, ".mwh"))) { BitmapGetter = new PyramidImage(pyrFile); } else { BitmapGetter = CreatePyramidImage(pyrFile, DataManager.DefaultDataManager.ProgressHandler); } } else { Bitmap bmp = new Bitmap(DataSet.NumColumns, DataSet.NumRows); symbolizer.Raster = DataSet; DataSet.DrawToBitmap(symbolizer, bmp, null); InRamImage id = new InRamImage(bmp); id.Bounds = DataSet.Bounds; BitmapGetter = id; } }
//Execute private void button2_Click(object sender, EventArgs e) { string OutputString = this.textBox1.Text.Trim(); string InputString = this.comboBox1.Text.Trim(); //The input is null if ((InputString == "") || (OutputString == "")) { MessageBox.Show("The parameters are not complete.", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (File.Exists(OutputString)) { MessageBox.Show("The output file has existed.", "Admin", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int selectIndex = this.comboBox1.SelectedIndex; if ((selectIndex < 0) && (InputString != "")) //Input data by selecting from folder { RasterSymbolizer RasterSymbol = new RasterSymbolizer(); IRasterSymbolizer IRasterSymbol = RasterSymbol; MapRasterLayer RasterLayer = new MapRasterLayer(InputString, RasterSymbol); //Open the raster layer IRaster Raster = RasterLayer.DataSet; } else if (selectIndex >= 0) //Input data by selecting from combox { IMapRasterLayer[] RasterArr = MainMap.GetRasterLayers(); IMapRasterLayer RasterLayer = RasterArr[selectIndex]; IRaster Raster = RasterLayer.DataSet; } }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { // Grid is if (disposing) { _symbolizer = null; } }
/// <summary> /// Sets up the Table to work with the specified layer. /// </summary> /// <param name="layer">The layer.</param> public void Initialize(IRasterLayer layer) { _originalLayer = layer; _newLayer = layer.Copy(); _symbolizer = layer.Symbolizer; _newScheme = _symbolizer.Scheme; _originalScheme = (IColorScheme)_symbolizer.Scheme.Clone(); _raster = layer.DataSet; GetSettings(); }
/// <summary> /// Opens the specified fileName using the layer manager. /// </summary> /// <param name="fileName"></param> /// <param name="symbolizer"></param> public RasterLayer(string fileName, IRasterSymbolizer symbolizer) { IRaster r = DataManager.DefaultDataManager.OpenRaster(fileName); DataSet = r; _symbolizer = symbolizer; _symbolizer.ParentLayer = this; _symbolizer.Scheme.SetParentItem(this); symbolizer.ColorSchemeUpdated += _symbolizer_SymbologyUpdated; }
/// <summary> /// Opens the specified fileName using the layer manager. /// </summary> /// <param name="fileName"></param> /// <param name="symbolizer"></param> public RasterLayer(string fileName, IRasterSymbolizer symbolizer) { IRaster r = DataManager.DefaultDataManager.OpenRaster(fileName); DataSet = r; _symbolizer = symbolizer; _symbolizer.ParentLayer = this; _symbolizer.Scheme.SetParentItem(this); symbolizer.ColorSchemeUpdated += _symbolizer_SymbologyUpdated; }
/// <summary> /// Creates a bitmap from this raster using the specified rasterSymbolizer. /// </summary> /// <param name="raster">The raster to draw to a bitmap</param> /// <typeparam name="T">Type of the raster.</typeparam> /// <param name="rasterSymbolizer">The raster symbolizer to use for assigning colors</param> /// <param name="rgbData">Byte values representing the ARGB image bytes</param> /// <param name="stride">The stride</param> /// <param name="pm">The progress meter to use.</param> /// <exception cref="ArgumentNullException">rasterSymbolizer cannot be null</exception> public static void DrawToBitmapT <T>(Raster <T> raster, IRasterSymbolizer rasterSymbolizer, byte[] rgbData, int stride, ProgressMeter pm) where T : struct, IEquatable <T>, IComparable <T> { DrawToBitmapT(raster, GetNoData(raster), (row, col) => raster.Data[row][col], i => rgbData[i], (i, b) => rgbData[i] = b, rasterSymbolizer, stride, pm); if (rasterSymbolizer.IsSmoothed) { var mySmoother = new Smoother(stride, raster.NumColumns, raster.NumRows, rgbData, pm.ProgressHandler); mySmoother.Smooth(); } }
private static void DrawToBitmapT <T>(Raster <T> raster, IRasterSymbolizer rasterSymbolizer, IntPtr rgbData, int stride, ProgressMeter pm) where T : struct, IEquatable <T>, IComparable <T> { DrawToBitmapT(raster, GetNoData(raster), (row, col) => raster.Data[row][col], i => Marshal.ReadByte(rgbData, i), (i, b) => Marshal.WriteByte(rgbData, i, b), rasterSymbolizer, stride, pm); if (rasterSymbolizer.IsSmoothed) { var mySmoother = new Smoother(stride, raster.NumColumns, raster.NumRows, rgbData, pm.ProgressHandler); mySmoother.Smooth(); } }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing) { BitmapGetter = null; RasterLayerActions = null; Symbolizer = null; } base.Dispose(disposing); }
/// <summary> /// This should launch a frmRasterSymbolizer /// </summary> /// <param name="context">ITypeDescriptorContext context</param> /// <param name="provider">IServiceProvider provider</param> /// <param name="value">object value</param> /// <returns>A new RasterSymbolizer</returns> public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IRasterSymbolizer rs = value as IRasterSymbolizer ?? new RasterSymbolizer(); IWindowsFormsEditorService dialogProvider = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); IRasterLayer parent = rs.ParentLayer; LayerDialog frm = new LayerDialog(parent, new RasterCategoryControl()); dialogProvider.ShowDialog(frm); return(parent.Symbolizer); }
private void btnCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; _rasterSymbolizer = _backupCopy; Close(); }
private void Configure() { _backupCopy = _rasterSymbolizer.Copy(); InitializeComponent(); csvEditor.IntializeControl(_rasterSymbolizer.ColorBreaks); csvEditor.LayerType = LayerTypes.Raster; txtDecimalCount.Text = "0"; cmbNumberFormat.SelectedIndex = 4; cmbGradientStyle.SelectedIndex = 0; }
/// <summary> /// Creates a new ColorbreakEditor form /// </summary> public RasterSymbolizerDialog() { _rasterSymbolizer = new RasterSymbolizer(); // a new symbolizer to hold values Configure(); }
/// <summary> /// Creates a bitmap based on the specified RasterSymbolizer /// </summary> /// <returns></returns> /// <exception cref="MapWindow.Main.NullLogException">rasterSymbolizer cannot be null</exception> public virtual void DrawToBitmap(IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) { Debug.WriteLine("IntRaster Calculated Bitmap"); BitmapData bmpData; if (Data == null) return; if (rasterSymbolizer == null) throw new NullLogException("rasterSymbolizer"); if (rasterSymbolizer.Scheme == null || rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) return; // Create a new Bitmap and use LockBits combined with Marshal.Copy to get an array of bytes to work with. Rectangle rect = new Rectangle(0, 0, NumColumns, NumRows); try { bmpData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); } catch (ArgumentException ex) { if (ex.ParamName == "format") throw new BitmapFormatException(); LogManager.DefaultLogManager.Exception(ex); throw; } int numBytes = bmpData.Stride*bmpData.Height; byte[] rgbData = new byte[numBytes]; Marshal.Copy(bmpData.Scan0, rgbData, 0, numBytes); bool useHillShade = false; float[][] hillshade = rasterSymbolizer.HillShade; if (rasterSymbolizer.ShadedRelief.IsUsed) { hillshade = rasterSymbolizer.HillShade; useHillShade = true; } Color pixelColor; ProgressMeter pm = new ProgressMeter(progressHandler, MessageStrings.CreatingTexture, NumRows); if (NumRows*NumColumns < 100000) pm.StepPercent = 50; if (NumRows*NumColumns < 500000) pm.StepPercent = 10; if (NumRows*NumColumns < 1000000) pm.StepPercent = 5; for (int row = 0; row < NumRows; row++) { for (int col = 0; col < NumColumns; col++) { // use the colorbreaks to calculate the colors pixelColor = rasterSymbolizer.GetColor(Data[row][col]); // control transparency here int alpha = Convert.ToInt32(rasterSymbolizer.Opacity*255); if (alpha > 255) alpha = 255; if (alpha < 0) alpha = 0; byte a = (byte) alpha; byte r, g, b; if (useHillShade && hillshade != null) { if (hillshade[row][col] == -1) { pixelColor = rasterSymbolizer.NoDataColor; r = pixelColor.R; g = pixelColor.G; b = pixelColor.B; } else { int red = Convert.ToInt32(pixelColor.R*hillshade[row][col]); int green = Convert.ToInt32(pixelColor.G*hillshade[row][col]); int blue = Convert.ToInt32(pixelColor.B*hillshade[row][col]); if (red > 255) red = 255; if (green > 255) green = 255; if (blue > 255) blue = 255; if (red < 0) red = 0; if (green < 0) green = 0; if (blue < 0) blue = 0; b = (byte) blue; r = (byte) red; g = (byte) green; } } else { r = pixelColor.R; g = pixelColor.G; b = pixelColor.B; } int offset = row*bmpData.Stride + col*4; rgbData[offset] = b; rgbData[offset + 1] = g; rgbData[offset + 2] = r; rgbData[offset + 3] = a; } pm.CurrentValue = row; } pm.Reset(); if (rasterSymbolizer.IsSmoothed) { Smoother sm = new Smoother(bmpData, rgbData, progressHandler); rgbData = sm.Smooth(); } // Copy the values back into the bitmap Marshal.Copy(rgbData, 0, bmpData.Scan0, numBytes); bitmap.UnlockBits(bmpData); rasterSymbolizer.ColorSchemeHasUpdated = true; return; }
/// <summary> /// Creates a bitmap from this raster using the specified rasterSymbolizer. /// </summary> /// <param name="raster">The raster to draw to a bitmap.</param> /// <param name="rasterSymbolizer">The raster symbolizer to use for assigning colors.</param> /// <param name="bitmap">This must be an Format32bbpArgb bitmap that has already been saved to a file so that it exists.</param> /// <param name="progressHandler">The progress handler to use.</param> /// <exception cref="ArgumentNullException">rasterSymbolizer cannot be null</exception> public static void DrawToBitmap(this IRaster raster, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler = null) { if (raster == null) { throw new ArgumentNullException(nameof(raster)); } if (rasterSymbolizer == null) { throw new ArgumentNullException(nameof(rasterSymbolizer)); } if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap)); } if (rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) { return; } BitmapData bmpData; var rect = new Rectangle(0, 0, raster.NumColumns, raster.NumRows); try { bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch (Exception) { // if they have not saved the bitmap yet, it can cause an exception var ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Bmp); ms.Position = 0; bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } var numRows = raster.NumRows; var numColumns = raster.NumColumns; // Prepare progress meter if (progressHandler == null) { progressHandler = raster.ProgressHandler; } var pm = new ProgressMeter(progressHandler, "Drawing to Bitmap", numRows); if (numRows * numColumns < 100000) { pm.StepPercent = 50; } if (numRows * numColumns < 500000) { pm.StepPercent = 10; } if (numRows * numColumns < 1000000) { pm.StepPercent = 5; } DrawToBitmap(raster, rasterSymbolizer, bmpData.Scan0, bmpData.Stride, pm); bitmap.UnlockBits(bmpData); rasterSymbolizer.ColorSchemeHasUpdated = true; }
private static void DrawToBitmapT <T>(IRaster raster, T noData, Func <int, int, T> getValue, Func <int, byte> getByte, Action <int, byte> setByte, IRasterSymbolizer rasterSymbolizer, int stride, ProgressMeter pm) where T : struct, IEquatable <T>, IComparable <T> { if (raster == null) { throw new ArgumentNullException(nameof(raster)); } if (rasterSymbolizer == null) { throw new ArgumentNullException(nameof(rasterSymbolizer)); } if (rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) { return; } float[][] hillshade = null; if (rasterSymbolizer.ShadedRelief.IsUsed) { pm.BaseMessage = "Calculating Shaded Relief"; hillshade = rasterSymbolizer.HillShade ?? raster.CreateHillShadeT(getValue, rasterSymbolizer.ShadedRelief, pm); } pm.BaseMessage = "Calculating Colors"; var sets = GetColorSets <T>(rasterSymbolizer.Scheme.Categories); var noDataColor = Argb.FromColor(rasterSymbolizer.NoDataColor); for (int row = 0; row < raster.NumRows; row++) { for (int col = 0; col < raster.NumColumns; col++) { var value = getValue(row, col); Argb argb; if (value.Equals(noData)) { argb = noDataColor; } else { // Usually values are not random, so check neighboring previous cells for same color int?srcOffset = null; if (col > 0) { if (value.Equals(getValue(row, col - 1))) { srcOffset = Offset(row, col - 1, stride); } } if (srcOffset == null && row > 0) { if (value.Equals(getValue(row - 1, col))) { srcOffset = Offset(row - 1, col, stride); } } if (srcOffset != null) { argb = new Argb(getByte((int)srcOffset + 3), getByte((int)srcOffset + 2), getByte((int)srcOffset + 1), getByte((int)srcOffset)); } else { argb = GetColor(sets, value); } } if (hillshade != null) { if (hillshade[row][col] == -1 || float.IsNaN(hillshade[row][col])) { argb = new Argb(argb.A, noDataColor.R, noDataColor.G, noDataColor.B); } else { var red = (int)(argb.R * hillshade[row][col]); var green = (int)(argb.G * hillshade[row][col]); var blue = (int)(argb.B * hillshade[row][col]); argb = new Argb(argb.A, red, green, blue); } } var offset = Offset(row, col, stride); setByte(offset, argb.B); setByte(offset + 1, argb.G); setByte(offset + 2, argb.R); setByte(offset + 3, argb.A); } pm.Next(); } }
/// <summary> /// Creates a bitmap using only the colorscheme, even if a hillshade was specified /// </summary> /// <param name="bitmap">The bitmap to edit. Ensure that this has been created and saved at least once</param> /// <param name="progressHandler">An IProgressHandler</param> /// <param name="rasterSymbolizer">The raster symbolizer to use</param> /// <exception cref="MapWindow.Main.NullLogException">rasterSymbolizer cannot be null</exception> public virtual void PaintColorSchemeToBitmap(IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) { Debug.WriteLine("IntRaster - PaintColorSchemeToBitamp"); BitmapData bmpData; if (Data == null) return; if (rasterSymbolizer == null) throw new NullLogException("rasterSymbolizer"); if (rasterSymbolizer.Scheme == null || rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) return; // Create a new Bitmap and use LockBits combined with Marshal.Copy to get an array of bytes to work with. Rectangle rect = new Rectangle(0, 0, NumColumns, NumRows); try { bmpData = bitmap.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); } catch (ArgumentException ex) { if (ex.ParamName == "format") throw new BitmapFormatException(); LogManager.DefaultLogManager.Exception(ex); throw; } int numBytes = bmpData.Stride*bmpData.Height; byte[] rgbData = new byte[numBytes]; Marshal.Copy(bmpData.Scan0, rgbData, 0, numBytes); Color pixelColor; ProgressMeter pm = new ProgressMeter(progressHandler, MessageStrings.PaintingColorScheme, NumRows); if (NumRows*NumColumns < 100000) pm.StepPercent = 50; if (NumRows*NumColumns < 500000) pm.StepPercent = 10; if (NumRows*NumColumns < 1000000) pm.StepPercent = 5; for (int row = 0; row < NumRows; row++) { for (int col = 0; col < NumColumns; col++) { // use the colorbreaks to calculate the colors pixelColor = rasterSymbolizer.GetColor(Data[row][col]); // control transparency here int alpha = Convert.ToInt32(rasterSymbolizer.Opacity*255); if (alpha > 255) alpha = 255; if (alpha < 0) alpha = 0; byte a = (byte) alpha; byte r = pixelColor.R; byte g = pixelColor.G; byte b = pixelColor.B; int offset = row*bmpData.Stride + col*4; rgbData[offset] = b; rgbData[offset + 1] = g; rgbData[offset + 2] = r; rgbData[offset + 3] = a; } pm.CurrentValue = row; } pm.Reset(); // Copy the values back into the bitmap Marshal.Copy(rgbData, 0, bmpData.Scan0, numBytes); bitmap.UnlockBits(bmpData); rasterSymbolizer.ColorSchemeHasUpdated = true; return; }
/// <summary> /// Loads this form with an existing rasterSymbolizer /// </summary> /// <param name="rasterSymbolizer">A Raster Symbolizer</param> public RasterSymbolizerDialog(IRasterSymbolizer rasterSymbolizer) { _rasterSymbolizer = rasterSymbolizer; Configure(); if ((int) _rasterSymbolizer.NumberFormat < 6) cmbNumberFormat.SelectedIndex = (int) _rasterSymbolizer.NumberFormat; }
/// <summary> /// Assigns the values form the symbolizer to the various combo-boxes and controls on this form. /// </summary> private void DisplaySymbolizerValues() { if (_rasterSymbolizer == null) _rasterSymbolizer = new RasterSymbolizer(); if (_rasterSymbolizer.ShadedRelief == null) _rasterSymbolizer.ShadedRelief = new ShadedRelief(); if (cmbGradientStyle.Items.Contains(_rasterSymbolizer.GradientModel.ToString())) cmbGradientStyle.SelectedItem = _rasterSymbolizer.GradientModel; else cmbGradientStyle.SelectedItem = 0; chkComputeHillshade.Checked = _rasterSymbolizer.ShadedRelief.IsUsed; csvEditor.ColorBreaks = _rasterSymbolizer.ColorBreaks; }
/// <summary> /// Initializes a new instance of the <see cref="RasterLayer"/> class. /// Opens the specified fileName using the layer manager. /// </summary> /// <param name="fileName">The string fileName to use in order to open the file.</param> /// <param name="symbolizer">The symbolizer to use for this layer.</param> public RasterLayer(string fileName, IRasterSymbolizer symbolizer) { DataSet = DataManager.DefaultDataManager.OpenRaster(fileName); Symbolizer = symbolizer; }
/// <summary> /// Creates a bitmap using only the colorscheme, even if a hillshade was specified /// </summary> /// <param name="raster">The Raster containing values that need to be drawn to the bitmap as a color scheme.</param> /// <param name="bitmap">The bitmap to edit. Ensure that this has been created and saved at least once</param> /// <param name="progressHandler">An IProgressHandler</param> /// <param name="rasterSymbolizer">The raster symbolizer to use</param> /// <exception cref="MapWindow.Main.NullLogException">rasterSymbolizer cannot be null</exception> public static void PaintColorSchemeToBitmap(this IRaster raster, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) { System.Drawing.Imaging.BitmapData bmpData; int numRows = raster.NumRows; int numColumns = raster.NumColumns; if (rasterSymbolizer == null) { throw new NullLogException("rasterSymbolizer"); } if (rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) return; // Create a new Bitmap and use LockBits combined with Marshal.Copy to get an array of bytes to work with. Rectangle rect = new Rectangle(0, 0, numColumns, numRows); try { bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); } catch { System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.Position = 0; bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); } int numBytes = bmpData.Stride * bmpData.Height; byte[] rgbData = new byte[numBytes]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, rgbData, 0, numBytes); Color pixelColor; ProgressMeter pm = new ProgressMeter(progressHandler, MessageStrings.PaintingColorScheme, raster.NumRows); if (numRows * numColumns < 100000) pm.StepPercent = 50; if (numRows * numColumns < 500000) pm.StepPercent = 10; if (numRows * numColumns < 1000000) pm.StepPercent = 5; for (int row = 0; row < numRows; row++) { for (int col = 0; col < numColumns; col++) { // use the colorbreaks to calculate the colors pixelColor = rasterSymbolizer.GetColor(raster.Value[row, col]); // control transparency here int alpha = Convert.ToInt32(rasterSymbolizer.Opacity * 255); if (alpha > 255) alpha = 255; if (alpha < 0) alpha = 0; byte a = (byte)alpha; byte r = pixelColor.R; byte g = pixelColor.G; byte b = pixelColor.B; int offset = row * bmpData.Stride + col * 4; rgbData[offset] = b; rgbData[offset + 1] = g; rgbData[offset + 2] = r; rgbData[offset + 3] = a; } pm.CurrentValue = row; } pm.Reset(); if (rasterSymbolizer.IsSmoothed) { Smoother mySmoother = new Smoother(bmpData, rgbData, progressHandler); rgbData = mySmoother.Smooth(); } // Copy the values back into the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbData, 0, bmpData.Scan0, numBytes); bitmap.UnlockBits(bmpData); rasterSymbolizer.ColorSchemeHasUpdated = true; return; }
/// <summary> /// Creates a bitmap from this raster using the specified rasterSymbolizer /// </summary> /// <param name="raster">The raster to draw to a bitmap</param> /// <param name="rasterSymbolizer">The raster symbolizer to use for assigning colors</param> /// <param name="bitmap">This must be an Format32bbpArgb bitmap that has already been saved to a file so that it exists.</param> /// <param name="progressHandler">The progress handler to use.</param> /// <returns>A System.Drawing.Bitmap if the operation was successful or null.</returns> /// <exception cref="MapWindow.Main.NullLogException">rasterSymbolizer cannot be null</exception> public static void DrawToBitmap(this IRaster raster, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) { System.Drawing.Imaging.BitmapData bmpData; if (rasterSymbolizer == null) { throw new NullLogException("rasterSymbolizer"); } if (rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) return; Rectangle rect = new Rectangle(0, 0, raster.NumColumns, raster.NumRows); try { bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); } catch(Exception ex) { string originalError = ex.ToString(); // if they have not saved the bitmap yet, it can cause an exception System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); ms.Position = 0; bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // any further exceptions should simply throw exceptions to allow easy debugging } int numBytes = bmpData.Stride * bmpData.Height; byte[] rgbData = new byte[numBytes]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, rgbData, 0, numBytes); bool useHillShade = false; float[][] hillshade = rasterSymbolizer.HillShade; if (rasterSymbolizer.ShadedRelief.IsUsed) { hillshade = rasterSymbolizer.HillShade; useHillShade = true; } Color pixelColor; ProgressMeter pm = new ProgressMeter(progressHandler, "Recreating Bitmap", raster.NumRows); try { for (int row = 0; row < raster.NumRows; row++) { for (int col = 0; col < raster.NumColumns; col++) { // use the colorbreaks to calculate the colors pixelColor = rasterSymbolizer.GetColor(raster.Value[row, col]); // control transparency here float alpha = rasterSymbolizer.Opacity * 255f; if (alpha > 255f) alpha = 255f; if (alpha < 0f) alpha = 0f; byte a = Convert.ToByte(alpha); byte g; byte r; byte b; if (useHillShade && hillshade != null) { if (hillshade[row][col] == -1 || float.IsNaN(hillshade[row][col])) { pixelColor = rasterSymbolizer.NoDataColor; r = pixelColor.R; g = pixelColor.G; b = pixelColor.B; } else { float red = pixelColor.R * hillshade[row][col]; float green = pixelColor.G * hillshade[row][col]; float blue = pixelColor.B * hillshade[row][col]; if (red > 255f) red = 255f; if (green > 255f) green = 255f; if (blue > 255f) blue = 255f; if (red < 0f) red = 0f; if (green < 0f) green = 0f; if (blue < 0f) blue = 0f; b = Convert.ToByte(blue); r = Convert.ToByte(red); g = Convert.ToByte(green); } } else { r = pixelColor.R; g = pixelColor.G; b = pixelColor.B; } int offset = row * bmpData.Stride + col * 4; rgbData[offset] = b; rgbData[offset + 1] = g; rgbData[offset + 2] = r; rgbData[offset + 3] = a; } pm.CurrentValue = row; } } catch { System.Diagnostics.Debug.WriteLine(" Unable to write data to raster."); } pm.Reset(); if (rasterSymbolizer.IsSmoothed) { Smoother mySmoother = new Smoother(bmpData, rgbData, progressHandler); rgbData = mySmoother.Smooth(); } // Copy the values back into the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbData, 0, bmpData.Scan0, numBytes); bitmap.UnlockBits(bmpData); rasterSymbolizer.ColorSchemeHasUpdated = true; return; }
/// <summary> /// Creates a bitmap from this raster using the specified rasterSymbolizer /// </summary> /// <param name="raster">The raster to draw to the bitmap based on the layout specified in the rasterSymbolizer</param> /// <param name="rasterSymbolizer">The raster symbolizer to use for assigning colors</param> /// <param name="bitmap">This must be an Format32bbpArgb bitmap that has already been saved to a file so that it exists.</param> /// <returns>A System.Drawing.Bitmap if the operation was successful or null.</returns> /// <exception cref="MapWindow.Main.NullLogException">rasterSymbolizer cannot be null</exception> public static void DrawToBitmap(this IRaster raster, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap) { DrawToBitmap(raster, rasterSymbolizer, bitmap, raster.ProgressHandler); }
/// <summary> /// Creates a bitmap using only the colorscheme, even if a hillshade was specified. /// </summary> /// <param name="raster">The Raster containing values that need to be drawn to the bitmap as a color scheme.</param> /// <typeparam name="T">Type of the raster.</typeparam> /// <param name="rasterSymbolizer">The raster symbolizer to use.</param> /// <param name="bitmap">The bitmap to edit. Ensure that this has been created and saved at least once.</param> /// <param name="progressHandler">An IProgressHandler implementation to receive progress updates.</param> /// <exception cref="ArgumentNullException"><paramref name="rasterSymbolizer"/> cannot be null, <paramref name="raster"/> cannot be null, <paramref name="bitmap"/> cannot be null</exception> public static void PaintColorSchemeToBitmapT <T>(this Raster <T> raster, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) where T : struct, IEquatable <T>, IComparable <T> { PaintColorSchemeToBitmapT(raster, GetNoData(raster), (row, col) => raster.Data[row][col], rasterSymbolizer, bitmap, progressHandler); }
/// <summary> /// Opens the specified fileName using the layer manager. /// </summary> /// <param name="fileName"></param> /// <param name="symbolizer"></param> public RasterLayer(string fileName, IRasterSymbolizer symbolizer) { DataSet = DataManager.DefaultDataManager.OpenRaster(fileName); Symbolizer = symbolizer; }
private void chkComputeHillshade_CheckedChanged(object sender, EventArgs e) { // Only enable the ShadedRelief button if we are using a hillshade btnShadedRelief.Enabled = chkComputeHillshade.Checked; if (_rasterSymbolizer == null) _rasterSymbolizer = new RasterSymbolizer(); if (_rasterSymbolizer.ShadedRelief == null) _rasterSymbolizer.ShadedRelief = new ShadedRelief(); _rasterSymbolizer.ShadedRelief.IsUsed = chkComputeHillshade.Checked; OnDataChanged(); }
private void Configure(IRaster raster) { DataSet = raster; _symbolizer = new RasterSymbolizer(this); _symbolizer.ColorSchemeUpdated += _symbolizer_SymbologyUpdated; }
private static void PaintColorSchemeToBitmapT <T>(this IRaster raster, T noData, Func <int, int, T> getValue, IRasterSymbolizer rasterSymbolizer, Bitmap bitmap, IProgressHandler progressHandler) where T : struct, IEquatable <T>, IComparable <T> { if (raster == null) { throw new ArgumentNullException(nameof(raster)); } if (rasterSymbolizer == null) { throw new ArgumentNullException(nameof(rasterSymbolizer)); } if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap)); } if (rasterSymbolizer.Scheme.Categories == null || rasterSymbolizer.Scheme.Categories.Count == 0) { return; } BitmapData bmpData; var numRows = raster.NumRows; var numColumns = raster.NumColumns; var rect = new Rectangle(0, 0, numColumns, numRows); try { bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } catch { var ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.MemoryBmp); ms.Position = 0; bmpData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); } // Prepare progress meter var pm = new ProgressMeter(progressHandler, SymbologyMessageStrings.DesktopRasterExt_PaintingColorScheme, numRows); if (numRows * numColumns < 100000) { pm.StepPercent = 50; } if (numRows * numColumns < 500000) { pm.StepPercent = 10; } if (numRows * numColumns < 1000000) { pm.StepPercent = 5; } var sets = GetColorSets <T>(rasterSymbolizer.Scheme.Categories); var noDataColor = Argb.FromColor(rasterSymbolizer.NoDataColor); var alpha = Argb.ByteRange(Convert.ToInt32(rasterSymbolizer.Opacity * 255)); var ptr = bmpData.Scan0; for (var row = 0; row < numRows; row++) { for (var col = 0; col < numColumns; col++) { var val = getValue(row, col); Argb argb; if (val.Equals(noData)) { argb = noDataColor; } else { // Usually values are not random, so check neighboring previous cells for same color int?srcOffset = null; if (col > 0) { if (val.Equals(getValue(row, col - 1))) { srcOffset = Offset(row, col - 1, bmpData.Stride); } } if (srcOffset == null && row > 0) { if (val.Equals(getValue(row - 1, col))) { srcOffset = Offset(row - 1, col, bmpData.Stride); } } if (srcOffset != null) { argb = new Argb(Marshal.ReadByte(ptr, (int)srcOffset + 3), Marshal.ReadByte(ptr, (int)srcOffset + 2), Marshal.ReadByte(ptr, (int)srcOffset + 1), Marshal.ReadByte(ptr, (int)srcOffset)); } else { var color = GetColor(sets, val); argb = new Argb(alpha, color.R, color.G, color.B); } } var offset = Offset(row, col, bmpData.Stride); Marshal.WriteByte(ptr, offset, argb.B); Marshal.WriteByte(ptr, offset + 1, argb.G); Marshal.WriteByte(ptr, offset + 2, argb.R); Marshal.WriteByte(ptr, offset + 3, argb.A); } pm.CurrentValue = row; } pm.Reset(); if (rasterSymbolizer.IsSmoothed) { var mySmoother = new Smoother(bmpData.Stride, bmpData.Width, bmpData.Height, bmpData.Scan0, progressHandler); mySmoother.Smooth(); } bitmap.UnlockBits(bmpData); rasterSymbolizer.ColorSchemeHasUpdated = true; }
private void mnuColorRamp_Click(object sender, EventArgs e) { IRasterSymbolizer temp = _rasterSymbolizer.Copy(); RampColorDialog rmpcolor = new RampColorDialog(temp); if (rmpcolor.ShowDialog(this) != DialogResult.OK) return; _rasterSymbolizer = temp; csvEditor.Invalidate(); }
private void Configure(IRaster raster) { DataSet = raster; _symbolizer = new RasterSymbolizer(this); _symbolizer.ColorSchemeUpdated += _symbolizer_SymbologyUpdated; }
/// <summary> /// Creates a new instance of RasterSymbolizerArgs /// </summary> public RasterSymbolizerArgs(IRasterSymbolizer rasterSymbolizer) { _rasterSymbolizer = rasterSymbolizer; }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { // Grid is if (disposing) { _symbolizer = null; } }
private void ApplyChanges() { if (_rasterSymbolizer.ShadedRelief.IsUsed) { if (_rasterSymbolizer.ShadedRelief.HasChanged || _rasterSymbolizer.HillShade == null) _rasterSymbolizer.CreateHillShade(mwStatusStrip1); } if (_rasterSymbolizer.ColorSchemeHasChanged) { _rasterSymbolizer.ParentLayer.WriteBitmap(mwStatusStrip1); } _backupCopy = _rasterSymbolizer.Copy(); OnChangesApplied(); btnApply.Enabled = false; }
/// <summary> /// Sets up the Table to work with the specified layer /// </summary> /// <param name="layer"></param> public void Initialize(IRasterLayer layer) { _originalLayer = layer; _newLayer = layer.Copy(); _symbolizer = layer.Symbolizer; _newScheme = _symbolizer.Scheme; _originalScheme = (IColorScheme)_symbolizer.Scheme.Clone(); _raster = layer.DataSet; GetSettings(); }
/// <summary> /// Initializes a new instance of the <see cref="MapRasterLayer"/> class from the specified fileName. /// </summary> /// <param name="fileName">Filename of the corresponding raster file.</param> /// <param name="symbolizer">Symbolizer used for drawing the raster data.</param> public MapRasterLayer(string fileName, IRasterSymbolizer symbolizer) : base(fileName, symbolizer) { LegendText = Path.GetFileNameWithoutExtension(fileName); }