private ICursor QueryGrid(int x, int y) { try { unsafe { fixed(float *buf = new float[2]) { OSGeo.GDAL.Band band = _gDS.GetRasterBand(1); band.ReadRaster(x, y, 1, 1, (IntPtr)buf, 1, 1, OSGeo.GDAL.DataType.GDT_CFloat32, 4, 0); if ((_hasNoDataVal != 0 && buf[0] == _nodata) || (_useIgnoreValue && buf[0] == _ignoreValue)) { return(null); } string[] tags = { "ImageX", "ImageY", "band1" }; object[] values = { x, y, buf[0] }; band.Dispose(); return(new QueryCursor(tags, values)); } } } finally { } }
/// <summary> /// 创建slopeMap,设置无效值 /// </summary> /// <param name="DemPath"></param> /// <param name="OutSlpPath"></param> static void CreateSlopeMap() { double dsm_NoDataValue; int dsm_HasNoDataValue; OSGeo.GDAL.Band dsmBand = dsmDataset.GetRasterBand(1); dsmBand.GetNoDataValue(out dsm_NoDataValue, out dsm_HasNoDataValue); //调用GDal创建slope影像 if (File.Exists(slopePath)) { gdalDriver.Delete(slopePath); } slopeDataSet = gdalDriver.Create(slopePath, dsm_Xsize, dsm_Ysize, 1, OSGeo.GDAL.DataType.GDT_Float32, null); slopeDataSet.SetProjection(dsmDataset.GetProjection()); slopeDataSet.SetGeoTransform(dsm_Transform); OSGeo.GDAL.Band slopeBand = slopeDataSet.GetRasterBand(1); //设置无效值 if (dsm_HasNoDataValue == 0) //dsm没有无效值 { //把DSM和SLOPE全设置成用户指定的值 dsmBand.SetNoDataValue(imgNodata); slopeBand.SetNoDataValue(imgNodata); } else //DSM有无效值时 { //把SLOPE和全局变量设置成DSM的无效值 imgNodata = dsm_NoDataValue; slopeBand.SetNoDataValue(dsm_NoDataValue); } // BufferOnePixel(slopeBand); }
public void InitGridQuery() { if (_gridQueryBand == null) { _gridQueryBand = _gDS.GetRasterBand(1); } }
public RasterBandReader(OSGeo.GDAL.Band gdalBand, GdalBandIO.ReadBlock <T> readBlock) { this.gdalBand = gdalBand; this.readBlock = readBlock; gdalBand.GetBlockSize(out blockDimensions.XSize, out blockDimensions.YSize); }
public RasterBandWriter(OSGeo.GDAL.Band gdalBand, GdalBandIO.WriteBlock <T> writeBlock) { this.gdalBand = gdalBand; this.writeBlock = writeBlock; gdalBand.GetBlockSize(out blockDimensions.XSize, out blockDimensions.YSize); }
public void ReleaseGridQuery() { if (_gridQueryBand != null) { _gridQueryBand.Dispose(); _gridQueryBand = null; } }
public static void WriteIntBlock(OSGeo.GDAL.Band rasterBand, BandBlock <int> block) { rasterBand.WriteRaster(block.XOffset, block.YOffset, block.UsedPortionXSize, block.UsedPortionYSize, block.Buffer, block.UsedPortionXSize, block.UsedPortionYSize, block.PixelSpace, block.LineSpace); }
/// <summary> /// Gdal to DotSpatial at Raster Level /// </summary> /// <param name="dataset">Gdal Dataset</param> /// <param name="bandIndex">Index of Band</param> /// <returns>Dotspatial Raster</returns> public static DotSpatial.Data.IRaster Gdal2DSRaster(OSGeo.GDAL.Dataset dataset, int bandIndex) { DotSpatial.Data.IRaster result = null; OSGeo.GDAL.Band band = dataset.GetRasterBand(bandIndex); if (dataset != null) { switch (band.DataType) { case OSGeo.GDAL.DataType.GDT_Byte: result = new GdalRaster <byte>(dataset, band); break; case OSGeo.GDAL.DataType.GDT_CFloat32: case OSGeo.GDAL.DataType.GDT_CFloat64: case OSGeo.GDAL.DataType.GDT_CInt16: case OSGeo.GDAL.DataType.GDT_CInt32: break; case OSGeo.GDAL.DataType.GDT_Float32: result = new GdalRaster <float>(dataset, band); break; case OSGeo.GDAL.DataType.GDT_Float64: result = new GdalRaster <double>(dataset, band); break; case OSGeo.GDAL.DataType.GDT_Int16: result = new GdalRaster <short>(dataset, band); break; case OSGeo.GDAL.DataType.GDT_UInt16: case OSGeo.GDAL.DataType.GDT_Int32: result = new GdalRaster <int>(dataset, band); break; case OSGeo.GDAL.DataType.GDT_TypeCount: break; case OSGeo.GDAL.DataType.GDT_UInt32: result = new GdalRaster <long>(dataset, band); break; case OSGeo.GDAL.DataType.GDT_Unknown: break; default: break; } } if (result != null) { (result as DotSpatial.Data.Raster).Open(); } return(result); }
public static void ReadDoubleBlock(OSGeo.GDAL.Band rasterBand, BandBlock <double> block) { rasterBand.ReadRaster(block.XOffset, block.YOffset, block.UsedPortionXSize, block.UsedPortionYSize, block.Buffer, block.UsedPortionXSize, block.UsedPortionYSize, block.PixelSpace, block.LineSpace); }
/// <summary> /// 和GetData功能相同,但是使用GDAL直接从文件中读取数据。 /// </summary> /// <param name="fileName">文件名字</param> /// <param name="width">ref用于返回数据的宽度</param> /// <param name="height">ref用于返回数据的高度</param> /// <returns>一维数据数组,按行优先</returns> private double[] GdalGetData(string fileName, ref int width, ref int height) { OSGeo.GDAL.Dataset dataset = OSGeo.GDAL.Gdal.Open(fileName, OSGeo.GDAL.Access.GA_ReadOnly); width = dataset.RasterXSize; height = dataset.RasterYSize; double[] imageBuffer = new double[width * height]; OSGeo.GDAL.Band b = dataset.GetRasterBand(1); b.ReadRaster(0, 0, width, height, imageBuffer, width, height, 0, 0); return(imageBuffer); }
private void PaintWavelet(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, ICancelTracker cancelTracker) { if (CancelTracker.Canceled(cancelTracker) || _gDS == null) { return; } int pixelSpace = 3; _bitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb); BitmapData bitmapData = _bitmap.LockBits(new Rectangle(0, 0, iWidth, iHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; for (int i = 1; i <= (_gDS.RasterCount > 3 ? 3 : _gDS.RasterCount); ++i) { using (OSGeo.GDAL.Band band = _gDS.GetRasterBand(i)) { int ch = 0; switch ((ColorInterp)band.GetRasterColorInterpretation()) { case ColorInterp.BlueBand: ch = 0; break; case ColorInterp.GreenBand: ch = 1; break; case ColorInterp.RedBand: ch = 2; break; } band.ReadRaster(x, y, wWidth, wHeight, new IntPtr(buf.ToInt64() + ch), iWidth, iHeight, OSGeo.GDAL.DataType.GDT_Byte, pixelSpace, stride); band.Dispose(); } } } finally { if (_bitmap != null) { _bitmap.UnlockBits(bitmapData); } } }
public override void Run() { OpenFileDialog openTiffDialog = new OpenFileDialog(); if (openTiffDialog.ShowDialog() == DialogResult.OK) { var tiffName = openTiffDialog.FileName; OSGeo.GDAL.Dataset tiffDataset = OSGeo.GDAL.Gdal.Open(tiffName, OSGeo.GDAL.Access.GA_ReadOnly); OSGeo.GDAL.Band band = tiffDataset.GetRasterBand(1); DotSpatial.Data.IRaster raster = GIS.GDAL.RasterConverter.Gdal2DSRaster(tiffDataset, 1); FileInfo fileInfo = new FileInfo(tiffName); raster.Name = fileInfo.Name; GIS.FrameWork.Application.App.Map.Layers.Add(raster); } }
private static void BufferOnePixel(OSGeo.GDAL.Band TargetBand) { double[] readArr = new double[TargetBand.XSize]; TargetBand.ReadRaster(0, 1, TargetBand.XSize, 1, readArr, TargetBand.XSize, 1, 0, 0); TargetBand.WriteRaster(0, 0, TargetBand.XSize, 1, readArr, TargetBand.XSize, 1, 0, 0); TargetBand.ReadRaster(0, TargetBand.YSize - 2, TargetBand.XSize, 1, readArr, TargetBand.XSize, 1, 0, 0); TargetBand.WriteRaster(0, TargetBand.YSize - 1, TargetBand.XSize, 1, readArr, TargetBand.XSize, 1, 0, 0); readArr = new double[TargetBand.YSize]; TargetBand.ReadRaster(1, 0, 1, TargetBand.YSize, readArr, 1, TargetBand.YSize, 0, 0); TargetBand.WriteRaster(0, 0, 1, TargetBand.YSize, readArr, 1, TargetBand.YSize, 0, 0); TargetBand.ReadRaster(TargetBand.XSize - 2, 0, 1, TargetBand.YSize, readArr, 1, TargetBand.YSize, 0, 0); TargetBand.WriteRaster(TargetBand.XSize - 1, 0, 1, TargetBand.YSize, readArr, 1, TargetBand.YSize, 0, 0); }
public RasterClass(IRasterDataset dataset, string filename, IPolygon polygon) { try { FileInfo fi = new FileInfo(filename); _title = fi.Name; _filename = filename; _dataset = dataset; OSGeo.GDAL.Gdal.AllRegister(); _gDS = OSGeo.GDAL.Gdal.Open(fi.FullName, 0); if (_gDS == null && _gDS.RasterCount == 0) { _valid = false; return; } _iWidth = _gDS.RasterXSize; _iHeight = _gDS.RasterYSize; switch (fi.Extension.ToLower()) { case ".adf": case ".gsd": _type = RasterType.grid; break; //case ".jp2": // _type = RasterType.wavelet; // break; } using (OSGeo.GDAL.Band band = _gDS.GetRasterBand(1)) { if (_gDS.RasterCount == 1) { if (band.DataType != OSGeo.GDAL.DataType.GDT_Byte) { _type = RasterType.grid; } } band.GetMinimum(out _min, out _hasNoDataVal); band.GetMaximum(out _max, out _hasNoDataVal); band.GetNoDataValue(out _nodata, out _hasNoDataVal); } OSGeo.GDAL.Driver driver = _gDS.GetDriver(); double[] tfw = new double[6]; _gDS.GetGeoTransform(tfw); string tfwFilename = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length); switch (fi.Extension.ToLower()) { case ".jpg": case ".jpeg": tfwFilename += ".jgw"; break; case ".jp2": tfwFilename += ".j2w"; break; case ".tif": case ".tiff": tfwFilename += ".tfw"; break; case ".ecw": tfwFilename += ".eww"; break; default: break; } FileInfo tfwInfo = new FileInfo(tfwFilename); _tfw = new TFWFile(tfw[0], tfw[3], tfw[1], tfw[2], tfw[4], tfw[5]); if (tfwInfo.Exists) { _tfw.Filename = tfwFilename; } if (_tfw.X == 0.0 && _tfw.Y == 0.0 && Math.Abs(_tfw.dx_X) == 1.0 && _tfw.dx_Y == 0.0 && Math.Abs(_tfw.dy_Y) == 1.0 && _tfw.dy_X == 0.0 && driver != null) { if (tfwInfo.Exists) { _tfw = new TFWFile(tfwFilename); } else { _tfw.isValid = false; } } else { // Bei dem Driver schein es nicht Pixelmitte sein, oder ist das bei GDAL generell //if (driver.ShortName.ToLower() == "jp2openjpeg") { _tfw.X += (_tfw.dx_X / 2.0D + _tfw.dx_Y / 2.0D); _tfw.Y += (_tfw.dy_X / 2.0D + _tfw.dy_Y / 2.0D); } } FileInfo fiPrj = new FileInfo(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".prj"); if (fiPrj.Exists) { StreamReader sr = new StreamReader(fiPrj.FullName); string wkt = sr.ReadToEnd(); sr.Close(); _sRef = gView.Framework.Geometry.SpatialReference.FromWKT(wkt); } else { fiPrj = new FileInfo(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".wkt"); if (fiPrj.Exists) { StreamReader sr = new StreamReader(fiPrj.FullName); string wkt = sr.ReadToEnd(); sr.Close(); _sRef = gView.Framework.Geometry.SpatialReference.FromWKT(wkt); } } if (polygon != null) { _polygon = polygon; } else { calcPolygon(); } } catch (Exception ex) { string errMsg = ex.Message; _valid = false; } }
private void PaintImage(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, ICancelTracker cancelTracker) { if (CancelTracker.Canceled(cancelTracker) || _gDS == null) { return; } int pixelSpace = 3; _bitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb); BitmapData bitmapData = _bitmap.LockBits(new Rectangle(0, 0, iWidth, iHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; List <Color> colors = new List <Color>(); for (int i = 1; i <= (_gDS.RasterCount > 3 ? 3 : _gDS.RasterCount); ++i) { using (OSGeo.GDAL.Band band = _gDS.GetRasterBand(i)) { int ch = 0; switch ((ColorInterp)band.GetRasterColorInterpretation()) { case ColorInterp.BlueBand: ch = 0; break; case ColorInterp.GreenBand: ch = 1; break; case ColorInterp.RedBand: ch = 2; break; case ColorInterp.GrayIndex: for (int iColor = 0; iColor < 256; iColor++) { colors.Add(Color.FromArgb(255, iColor, iColor, iColor)); } break; case ColorInterp.PaletteIndex: OSGeo.GDAL.ColorTable colTable = band.GetRasterColorTable(); if (colTable == null) { break; } int colCount = colTable.GetCount(); for (int iColor = 0; iColor < colCount; iColor++) { OSGeo.GDAL.ColorEntry colEntry = colTable.GetColorEntry(iColor); colors.Add(Color.FromArgb( colEntry.c4, colEntry.c1, colEntry.c2, colEntry.c3)); } break; } band.ReadRaster(x, y, wWidth, wHeight, new IntPtr(buf.ToInt64() + ch), iWidth, iHeight, OSGeo.GDAL.DataType.GDT_Byte, pixelSpace, stride); band.Dispose(); } } if (colors.Count > 0) { unsafe { byte *ptr = (byte *)(bitmapData.Scan0); for (int i = 0; i < bitmapData.Height; i++) { if (CancelTracker.Canceled(cancelTracker)) { return; } for (int j = 0; j < bitmapData.Width; j++) { // write the logic implementation here byte c = ptr[0]; Color col = colors[(int)c]; ptr[0] = col.B; ptr[1] = col.G; ptr[2] = col.R; ptr += pixelSpace; } ptr += bitmapData.Stride - bitmapData.Width * pixelSpace; } } } } catch (Exception ex) { string msg = ex.Message; } finally { if (_bitmap != null) { _bitmap.UnlockBits(bitmapData); } } }
public static RasterBandWriter <byte> NewByteWriter(OSGeo.GDAL.Band gdalBand) { return(new RasterBandWriter <byte>(gdalBand, GdalBandIO.WriteByteBlock)); }
private void GetPreview(OSGeo.GDAL.Dataset dataset, System.Drawing.Size size, Graphics g, SharpMap.Geometries.BoundingBox bbox) { double[] geoTrans = new double[6]; dataset.GetGeoTransform(geoTrans); GeoTransform GT = new GeoTransform(geoTrans); int DsWidth = dataset.RasterXSize; int DsHeight = dataset.RasterYSize; Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb); int iPixelSize = 3; //Format24bppRgb = byte[b,g,r] if (dataset != null) { /* * if ((float)size.Width / (float)size.Height > (float)DsWidth / (float)DsHeight) * size.Width = size.Height * DsWidth / DsHeight; * else * size.Height = size.Width * DsHeight / DsWidth; */ double left = Math.Max(bbox.Left, _Envelope.Left); double top = Math.Min(bbox.Top, _Envelope.Top); double right = Math.Min(bbox.Right, _Envelope.Right); double bottom = Math.Max(bbox.Bottom, _Envelope.Bottom); int x1 = (int)GT.PixelX(left); int y1 = (int)GT.PixelY(top); int x1width = (int)GT.PixelXwidth(right - left); int y1height = (int)GT.PixelYwidth(bottom - top); bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat); try { unsafe { for (int i = 1; i <= (dataset.RasterCount > 3 ? 3 : dataset.RasterCount); ++i) { byte[] buffer = new byte[size.Width * size.Height]; OSGeo.GDAL.Band band = dataset.GetRasterBand(i); //band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution); band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, 0, 0); int p_indx = 0; int ch = 0; //#warning Check correspondance between enum and integer values if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_BlueBand) { ch = 0; } if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_GreenBand) { ch = 1; } if (band.GetRasterColorInterpretation() == OSGeo.GDAL.ColorInterp.GCI_RedBand) { ch = 2; } if (band.GetRasterColorInterpretation() != OSGeo.GDAL.ColorInterp.GCI_PaletteIndex) { for (int y = 0; y < size.Height; y++) { byte *row = (byte *)bitmapData.Scan0 + (y * bitmapData.Stride); for (int x = 0; x < size.Width; x++, p_indx++) { row[x * iPixelSize + ch] = buffer[p_indx]; } } } else //8bit Grayscale { for (int y = 0; y < size.Height; y++) { byte *row = (byte *)bitmapData.Scan0 + (y * bitmapData.Stride); for (int x = 0; x < size.Width; x++, p_indx++) { row[x * iPixelSize] = buffer[p_indx]; row[x * iPixelSize + 1] = buffer[p_indx]; row[x * iPixelSize + 2] = buffer[p_indx]; } } } } } } finally { bitmap.UnlockBits(bitmapData); } } g.DrawImage(bitmap, new System.Drawing.Point(0, 0)); }
private void PaintHillShade(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, double mag, ICancelTracker cancelTracker) { if (CancelTracker.Canceled(cancelTracker) || _gDS == null) { return; } int pixelSpace = 4; Bitmap bitmap = new Bitmap(iWidth, iHeight + 100, PixelFormat.Format32bppArgb); BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, iWidth, iHeight + 100), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); OSGeo.GDAL.Band band = null; try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; List <Color> colors = new List <Color>(); using (band = _gDS.GetRasterBand(1)) { band.ReadRaster(x, y, wWidth, wHeight, buf, iWidth, iHeight, OSGeo.GDAL.DataType.GDT_CFloat32, pixelSpace, stride); band.Dispose(); } double cx = _tfw.cellX / mag; double cy = _tfw.cellY / mag; Vector3d sun = new Vector3d(_hillShade); sun.Normalize(); int rowStride = stride / pixelSpace; Color col = Color.White; unsafe { byte * ptr = (byte *)(bitmapData.Scan0); float *v = (float *)(bitmapData.Scan0); for (int i = 0; i < iHeight; i++) { if (CancelTracker.Canceled(cancelTracker)) { return; } for (int j = 0; j < iWidth; j++) { if ((_hasNoDataVal == 1 && *v == _nodata) || (_useIgnoreValue && *v == _ignoreValue)) { ptr[0] = ptr[1] = ptr[2] = ptr[3] = 0; } else { double c = *v; col = GridColorClass.FindColor(c, _colorClasses); if (!_useHillShade) { ptr[0] = (byte)col.B; ptr[1] = (byte)col.G; ptr[2] = (byte)col.R; ptr[3] = (byte)col.A; // alpha } else { double c1 = (j < iWidth - 1) ? (*(v + 1)) : c; double c2 = (i < iHeight - 1) ? (*(v + rowStride)) : c; c1 = ((_hasNoDataVal != 0 && c1 == _nodata) || (_useIgnoreValue && c1 == _ignoreValue)) ? c : c1; c2 = ((_hasNoDataVal != 0 && c2 == _nodata) || (_useIgnoreValue && c2 == _ignoreValue)) ? c : c2; Vector3d v1 = new Vector3d(cx, 0.0, c1 - c); v1.Normalize(); Vector3d v2 = new Vector3d(0.0, -cy, c2 - c); v2.Normalize(); Vector3d vs = v2 % v1; vs.Normalize(); double h = Math.Min(Math.Max(0.0, sun * vs), 1.0); //double h = Math.Abs(sun * vs); double a = col.A; double r = col.R * h; double g = col.G * h; double b = col.B * h; ptr[0] = (byte)b; ptr[1] = (byte)g; ptr[2] = (byte)r; ptr[3] = (byte)a; // alpha } } ptr += pixelSpace; v++; } ptr += bitmapData.Stride - (bitmapData.Width) * pixelSpace; v = (float *)ptr; } } if (bitmap != null) { bitmap.UnlockBits(bitmapData); } _bitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format32bppArgb); using (Graphics gr = Graphics.FromImage(_bitmap)) { gr.DrawImage(bitmap, 0, 0); } } catch { } finally { if (bitmap != null) { bitmap.Dispose(); } } }
public static RasterBandWriter <int> NewIntWriter(OSGeo.GDAL.Band gdalBand) { return(new RasterBandWriter <int>(gdalBand, GdalBandIO.WriteIntBlock)); }
public static RasterBandReader <double> NewDoubleReader(OSGeo.GDAL.Band gdalBand) { return(new RasterBandReader <double>(gdalBand, GdalBandIO.ReadDoubleBlock)); }
public static RasterBandReader <float> NewFloatReader(OSGeo.GDAL.Band gdalBand) { return(new RasterBandReader <float>(gdalBand, GdalBandIO.ReadFloatBlock)); }
public static RasterBandReader <int> NewIntReader(OSGeo.GDAL.Band gdalBand) { return(new RasterBandReader <int>(gdalBand, GdalBandIO.ReadIntBlock)); }
public static RasterBandReader <short> NewShortReader(OSGeo.GDAL.Band gdalBand) { return(new RasterBandReader <short>(gdalBand, GdalBandIO.ReadShortBlock)); }
public static RasterBandWriter <float> NewFloatWriter(OSGeo.GDAL.Band gdalBand) { return(new RasterBandWriter <float>(gdalBand, GdalBandIO.WriteFloatBlock)); }
private void PaintGrid(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight) { if (_gDS == null) { return; } int pixelSpace = 4; _bitmap = new Bitmap(iWidth, iHeight, PixelFormat.Format32bppArgb); BitmapData bitmapData = _bitmap.LockBits(new Rectangle(0, 0, iWidth, iHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); OSGeo.GDAL.Band band = null; try { int stride = bitmapData.Stride; IntPtr buf = bitmapData.Scan0; List <Color> colors = new List <Color>(); using (band = _gDS.GetRasterBand(1)) { band.ReadRaster(x, y, wWidth, wHeight, buf, iWidth, iHeight, OSGeo.GDAL.DataType.GDT_CFloat32, pixelSpace, stride); band.Dispose(); } unsafe { byte * ptr = (byte *)(bitmapData.Scan0); float *v = (float *)(bitmapData.Scan0); for (int i = 0; i < bitmapData.Height; i++) { for (int j = 0; j < bitmapData.Width; j++) { if (_renderRawGridValues) { byte *vb = (byte *)v; ptr[0] = 111; // *vb; vb++; ptr[1] = 122; // *vb; vb++; ptr[2] = 133; // *vb; vb++; ptr[3] = 144; // *vb; } else { if (_hasNoDataVal == 1 && *v == _nodata) { ptr[0] = ptr[1] = ptr[2] = ptr[3] = 0; } else { double c = (*v - _min) / (_max - _min); double a = _minColor.A + c * (_maxColor.A - _minColor.A); double r = _minColor.R + c * (_maxColor.R - _minColor.R); double g = _minColor.G + c * (_maxColor.G - _minColor.G); double b = _minColor.B + c * (_maxColor.B - _minColor.B); ptr[0] = (byte)b; ptr[1] = (byte)g; ptr[2] = (byte)r; ptr[3] = (byte)a; // alpha } } ptr += pixelSpace; v++; } ptr += bitmapData.Stride - bitmapData.Width * pixelSpace; v = (float *)ptr; } } } finally { if (_bitmap != null) { _bitmap.UnlockBits(bitmapData); } } }
public static RasterBandWriter <double> NewDoubleWriter(OSGeo.GDAL.Band gdalBand) { return(new RasterBandWriter <double>(gdalBand, GdalBandIO.WriteDoubleBlock)); }
public static RasterBandReader <byte> NewByteReader(OSGeo.GDAL.Band gdalBand) { return(new RasterBandReader <byte>(gdalBand, GdalBandIO.ReadByteBlock)); }
/// <summary> /// 波段直方图匹配。 /// </summary> /// <param name="InputBand">输入的波段。</param> /// <param name="MatchBand">要匹配到的波段。</param> /// <param name="InputCumu">输入的数据集的累积概率表。</param> /// <param name="MatchingCumu">要匹配到的数据集的累积概率表。</param> /// <param name="OutDataType">输出的数据类型。</param> /// <returns>操作成功或失败。</returns> public static OSGeo.GDAL.Band HistogramMatching(OSGeo.GDAL.Band InputBand, OSGeo.GDAL.Band MatchBand, double[] InputCumu, double[] MatchingCumu, OSGeo.GDAL.DataType OutDataType) { try { int[] LUT = HistogramMatching(InputCumu, MatchingCumu); //进行波段直方图匹配。 int xSize = InputBand.XSize; int ySize = InputBand.YSize; OSGeo.GDAL.Driver Dri = OSGeo.GDAL.Gdal.GetDriverByName("Gtiff"); if (Dri == null) { throw new Exception("无法获取GDAL Driver。"); } string tmpFileName = Tools.Common.GetTempFileName(); FrmProgress FP = new FrmProgress() { Text = "正在进行波段直方图匹配...", CanCancel = false, }; Thread t = new Thread(() => { FP.ShowDialog(); }); t.SetApartmentState(ApartmentState.STA); t.Start(); OSGeo.GDAL.Dataset DS = Dri.Create(tmpFileName, xSize, ySize, 1, OutDataType, null); FP.Output("已创建临时输出数据集\"" + tmpFileName + "\""); Tools.Common.GetMinimum(InputBand, out double inputBandMin); Tools.Common.GetMinimum(MatchBand, out double matchBandMin); for (int Row = 0; Row < ySize; Row++) { FP.SetProgress1("正在处理:", Row + 1, ySize, "行"); double[] Values = new double[xSize]; //读取DN到数组 InputBand.ReadRaster(0, Row, xSize, 1, Values, xSize, 1, 0, 0); for (int Col = 0; Col < xSize; Col++) { //防止GDAL自带方法算出的统计数据越界。 if ((Values[Col] - inputBandMin) <= 0) { Values[Col] = matchBandMin + LUT[0]; continue; } if ((Values[Col] - inputBandMin) >= LUT.Length - 1) { Values[Col] = matchBandMin + LUT[LUT.Length - 1]; continue; } Values[Col] = matchBandMin + LUT[(int)(Values[Col] - inputBandMin)]; } //写结果到新栅格 DS.GetRasterBand(1).WriteRaster(0, Row, xSize, 1, Values, xSize, 1, 0, 0); DS.FlushCache(); Thread.Sleep(1); if (FP.Canceled) { Thread.Sleep(500); FP.Finish(); throw new OperationCanceledException("操作被用户取消。"); } } FP.Finish(); Dri.Dispose(); return(DS.GetRasterBand(1)); } catch (Exception err) { MessageBox.Show(err.ToString()); return(null); } }
private ICursor QueryImage(int x, int y) { unsafe { int bandCount = _gDS.RasterCount; string[] tags = new string[bandCount + 2]; object[] values = new object[bandCount + 2]; List <Color> colors = new List <Color>(); for (int i = 1; i <= bandCount; ++i) { OSGeo.GDAL.Band band = _gDS.GetRasterBand(i); string bandName = ""; switch ((ColorInterp)band.GetRasterColorInterpretation()) { case ColorInterp.BlueBand: bandName = "(blue)"; break; case ColorInterp.GreenBand: bandName = "(green)"; break; case ColorInterp.RedBand: bandName = "(red)"; break; case ColorInterp.GrayIndex: for (int iColor = 0; iColor < 256; iColor++) { colors.Add(Color.FromArgb(255, iColor, iColor, iColor)); } break; case ColorInterp.PaletteIndex: tags = new string[tags.Length + 4]; values = new object[values.Length + 4]; OSGeo.GDAL.ColorTable colTable = band.GetRasterColorTable(); if (colTable == null) { break; } int colCount = colTable.GetCount(); for (int iColor = 0; iColor < colCount; iColor++) { OSGeo.GDAL.ColorEntry colEntry = colTable.GetColorEntry(iColor); colors.Add(Color.FromArgb( colEntry.c4, colEntry.c1, colEntry.c2, colEntry.c3)); } break; } int c = 0; int *buf = &c; band.ReadRaster(x, y, 1, 1, (IntPtr)buf, 1, 1, OSGeo.GDAL.DataType.GDT_Int32, 4, 0); band.Dispose(); tags[i + 1] = "Band " + i.ToString() + " " + bandName; values[i + 1] = c; if (colors.Count > 0 && c >= 0 && c < colors.Count) { Color col = colors[c]; tags[i + 2] = "Alpha"; values[i + 2] = col.A; tags[i + 3] = "Red"; values[i + 3] = col.R; tags[i + 4] = "Green"; values[i + 4] = col.G; tags[i + 5] = "Blue"; values[i + 5] = col.B; } } tags[0] = "ImageX"; values[0] = x; tags[1] = "ImageY"; values[1] = y; return(new QueryCursor(tags, values)); } }
public static RasterBandWriter <short> NewShortWriter(OSGeo.GDAL.Band gdalBand) { return(new RasterBandWriter <short>(gdalBand, GdalBandIO.WriteShortBlock)); }