GdalRasterLayer.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:44k
源码类别:

GIS编程

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. namespace SharpMap.Layers
  5. {
  6.     /// <summary>
  7.     /// Gdal raster image layer
  8.     /// </summary>
  9.     /// <remarks>
  10.     /// <example>
  11.     /// <code lang="C#">
  12.     /// myMap = new SharpMap.Map(new System.Drawing.Size(500,250);
  13.     /// SharpMap.Layers.GdalRasterLayer layGdal = new SharpMap.Layers.GdalRasterLayer("Blue Marble", @"C:datasrtm30plus.tif");
  14.     /// myMap.Layers.Add(layGdal);
  15.     /// myMap.ZoomToExtents();
  16.     /// </code>
  17.     /// </example>
  18.     /// </remarks>
  19.     
  20.     public class GdalRasterLayer : SharpMap.Layers.Layer, IDisposable
  21.     {
  22.         private SharpMap.Geometries.BoundingBox _Envelope;
  23.         private GDAL.Dataset _GdalDataset;
  24.         private System.Drawing.Size imagesize;
  25.         private string _Filename;
  26.         /// <summary>
  27.         /// Gets or sets the filename of the raster file
  28.         /// </summary>
  29.         public string Filename
  30.         {
  31.             get { return _Filename; }
  32.             set { _Filename = value; }
  33.         }
  34.         /// <summary>
  35.         /// initialize a Gdal based raster layer
  36.         /// </summary>
  37.         /// <param name="strLayerName">Name of layer</param>
  38.         /// <param name="imageFilename">location of image</param>
  39.         public GdalRasterLayer(string strLayerName, string imageFilename)
  40.         {
  41.             this.LayerName = strLayerName;
  42.             this.Filename = imageFilename;
  43.             disposed = false;
  44.             GDAL.gdal.AllRegister();
  45.             try
  46.             {
  47. _GdalDataset = GDAL.gdal.Open(_Filename, GDAL.gdalconst.GA_ReadOnly);
  48. imagesize = new Size(_GdalDataset.RasterXSize, _GdalDataset.RasterYSize);
  49.                 
  50. _Envelope = this.GetExtent();
  51.             }
  52.             catch (Exception ex) { 
  53.                 _GdalDataset = null;
  54.                 throw new Exception("Couldn't load dataset. " + ex.Message + ex.InnerException);
  55.             }
  56.  
  57.         }
  58.         /// <summary>
  59.         /// initialize a Gdal based raster layer
  60.         /// </summary>
  61.         /// <param name="strLayerName">Name of layer</param>
  62.         /// <param name="imageFilename">location of image</param>
  63.         /// <param name="srid">sets the SRID of the data set</param>
  64.         public GdalRasterLayer(string strLayerName, string imageFilename, int srid):this(strLayerName, imageFilename)
  65.         {
  66.             this.SRID = srid;
  67.         }
  68.         #region ILayer Members
  69.         /// <summary>
  70.         /// Renders the layer
  71.         /// </summary>
  72.         /// <param name="g">Graphics object reference</param>
  73.         /// <param name="map">Map which is rendered</param>
  74.         public override void Render(Graphics g, Map map)
  75.         {
  76.             if (disposed)
  77.                 throw (new ApplicationException("Error: An attempt was made to render a disposed layer"));
  78.             
  79.             //if (this.Envelope.Intersects(map.Envelope))
  80.             //{
  81.                 this.GetPreview(_GdalDataset, map.Size, g, map.Envelope);
  82.             //}
  83.             base.Render(g, map);
  84.         }
  85.         /// <summary>
  86.         /// Returns the extent of the layer
  87.         /// </summary>
  88.         /// <returns>Bounding box corresponding to the extent of the features in the layer</returns>
  89.         public override SharpMap.Geometries.BoundingBox Envelope
  90.         {
  91.             get { return _Envelope; }
  92.         }
  93.         private int _SRID = -1;
  94.         /// <summary>
  95.         /// The spatial reference ID (CRS)
  96.         /// </summary>
  97.         public override int SRID
  98.         {
  99.             get { return _SRID; }
  100.             set { _SRID = value; }
  101.         }
  102.         #endregion
  103.         #region ICloneable Members
  104.         /// <summary>
  105.         /// Clones the object
  106.         /// </summary>
  107.         /// <returns></returns>
  108.         public override object Clone()
  109.         {
  110.             throw new NotImplementedException();
  111.         }
  112.         #endregion
  113. #region Disposers and finalizers
  114. private bool disposed = false;
  115. private void Dispose(bool disposing)
  116. {
  117. if (!disposed)
  118. {
  119. if (disposing)
  120.                     if (_GdalDataset != null)
  121. {
  122. try {
  123. _GdalDataset.Dispose();
  124.                         }
  125.                         finally { _GdalDataset = null; }
  126. }
  127. disposed = true;
  128. }
  129. }
  130. /// <summary>
  131. /// Disposes the GdalRasterLayer and release the raster file
  132. /// </summary>
  133. public void Dispose ()
  134. {
  135. this.Dispose (true);
  136. GC.SuppressFinalize(this);
  137. }
  138. /// <summary>
  139. /// Finalizer
  140. /// </summary>
  141. ~GdalRasterLayer()
  142. {
  143. this.Dispose (true);
  144. }
  145. #endregion
  146.         private SharpMap.Geometries.BoundingBox GetExtent()
  147.         {
  148.             if(_GdalDataset!=null)
  149.             {
  150.              double [] geoTrans = new double[6];
  151.             _GdalDataset.GetGeoTransform(geoTrans);        
  152.          GeoTransform GT = new GeoTransform(geoTrans);
  153.                 return new SharpMap.Geometries.BoundingBox( GT.Left,
  154.                                                             GT.Top + (GT.VerticalPixelResolution * _GdalDataset.RasterYSize),
  155.                                                             GT.Left + (GT.HorizontalPixelResolution * _GdalDataset.RasterXSize),
  156.                                                             GT.Top);
  157.             }
  158.             return null;
  159.         }
  160.         private void GetPreview(GDAL.Dataset dataset, System.Drawing.Size size, Graphics g, SharpMap.Geometries.BoundingBox bbox)
  161.         {
  162.          double [] geoTrans = new double[6];
  163.             dataset.GetGeoTransform(geoTrans);        
  164.          GeoTransform GT = new GeoTransform(geoTrans);
  165.             int DsWidth = dataset.RasterXSize;
  166.             int DsHeight = dataset.RasterYSize;
  167.             Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
  168.             int iPixelSize = 3; //Format24bppRgb = byte[b,g,r]
  169.             if (dataset != null)
  170.             {
  171.                 /*
  172.                 if ((float)size.Width / (float)size.Height > (float)DsWidth / (float)DsHeight)
  173.                     size.Width = size.Height * DsWidth / DsHeight;
  174.                 else
  175.                     size.Height = size.Width * DsHeight / DsWidth;
  176.                 */
  177.               
  178.                 double left = Math.Max(bbox.Left, _Envelope.Left);
  179.                 double top = Math.Min(bbox.Top, _Envelope.Top);
  180.                 double right = Math.Min(bbox.Right, _Envelope.Right);
  181.                 double bottom = Math.Max(bbox.Bottom, _Envelope.Bottom);
  182.              
  183.                 int x1 = (int)GT.PixelX(left);
  184.                 int y1 = (int)GT.PixelY(top);
  185.                 int x1width = (int)GT.PixelXwidth(right - left);
  186.                 int y1height = (int)GT.PixelYwidth(bottom - top);
  187.                 
  188.                 bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
  189.                 BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  190.                 try
  191.                 {
  192.                     unsafe
  193.                     {
  194.                         for (int i = 1; i <= (dataset.RasterCount > 3 ? 3 : dataset.RasterCount); ++i)
  195.                         {
  196.                             byte[] buffer = new byte[size.Width * size.Height];
  197.                             GDAL.Band band = dataset.GetRasterBand(i);
  198.                             //band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
  199.                             band.ReadRaster(x1, y1, x1width, y1height, buffer, size.Width, size.Height, 0, 0);
  200.                             int p_indx = 0;
  201.                             int ch = 0;
  202.                             if (band.GetRasterColorInterpretation() == 5) ch = 0;
  203.                             if (band.GetRasterColorInterpretation() == 4) ch = 1;
  204.                             if (band.GetRasterColorInterpretation() == 3) ch = 2;
  205.                             if (band.GetRasterColorInterpretation() != 2)
  206.                             {
  207.                                 for (int y = 0; y < size.Height; y++)
  208.                                 {
  209.                                     byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  210.                                     for (int x = 0; x < size.Width; x++, p_indx++)
  211.                                     {
  212.                                         row[x * iPixelSize + ch] = buffer[p_indx];
  213.                                     }
  214.                                 }
  215.                             }
  216.                             else //8bit Grayscale
  217.                             {
  218.                                 for (int y = 0; y < size.Height; y++)
  219.                                 {
  220.                                     byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  221.                                     for (int x = 0; x < size.Width; x++, p_indx++)
  222.                                     {
  223.                                         row[x * iPixelSize] = buffer[p_indx];
  224.                                         row[x * iPixelSize + 1] = buffer[p_indx];
  225.                                         row[x * iPixelSize + 2] = buffer[p_indx];
  226.                                     }
  227.                                 }
  228.                             }
  229.                         }
  230.                     }
  231.                 }
  232.                 finally
  233.                 {
  234.                     bitmap.UnlockBits(bitmapData);
  235.                 }
  236.             }
  237.             g.DrawImage(bitmap, new System.Drawing.Point(0, 0));
  238.         }
  239.     }
  240.     
  241.    
  242.    
  243. /*   
  244.     /// <summary>
  245.     /// Gdal raster image layer
  246.     /// </summary>
  247.     /// <remarks>
  248.     /// Before you can use the Gdal raster layer, you have to install the FwTools
  249.     /// from http://fwtools.maptools.org/ . This package provides all the nessesary
  250.     /// libraries for correct use.
  251.     /// <example>
  252.     /// <code lang="C#">
  253.     /// myMap = new SharpMap.Map(new System.Drawing.Size(500,250);
  254.     /// SharpMap.Layers.GdalRasterLayer layGdal = new SharpMap.Layers.GdalRasterLayer("Blue Marble", @"C:databluemarble.ecw");
  255.     /// myMap.Layers.Add(layGdal);
  256.     /// myMap.ZoomToExtents();
  257.     /// </code>
  258.     /// </example>
  259.     /// </remarks>
  260.     public class GdalRasterLayer : SharpMap.Layers.Layer, IDisposable
  261.     {
  262.         private SharpMap.Geometries.BoundingBox _Envelope;
  263.         private GDAL.Dataset _GdalDataset;
  264.         private System.Drawing.Size imagesize;
  265.         private int BitDepth = 8;
  266.         private bool DisplayIR = false;
  267.         private bool DisplayCIR = false;
  268.         private double redGain = 1;
  269.         private double greenGain = 1;
  270.         private double blueGain = 1;
  271.         private string _Filename;
  272.         /// <summary>
  273.         /// Gets or sets the filename of the raster file
  274.         /// </summary>
  275.         public string Filename
  276.         {
  277.             get { return _Filename; }
  278.             set { _Filename = value; }
  279.         }
  280.         /// <summary>
  281.         /// Gets or sets the bit depth of the raster file
  282.         /// </summary>
  283.         public int intBitDepth
  284.         {
  285.             get { return BitDepth; }
  286.             set { BitDepth = value; }
  287.         }
  288.         /// <summary>
  289.         /// Gets or sets to display IR Band
  290.         /// </summary>
  291.         public bool bDisplayIR
  292.         {
  293.             get { return DisplayIR; }
  294.             set { DisplayIR = value; }
  295.         }
  296.         /// <summary>
  297.         /// Gets or sets to display color InfraRed
  298.         /// </summary>
  299.         public bool bDisplayCIR
  300.         {
  301.             get { return DisplayCIR; }
  302.             set { DisplayCIR = value; }
  303.         }
  304.         /// <summary>
  305.         /// Gets or sets to display red Gain
  306.         /// </summary>
  307.         public double dblRedGain
  308.         {
  309.             get { return redGain; }
  310.             set { redGain = value; }
  311.         }
  312.         /// <summary>
  313.         /// Gets or sets to display green Gain
  314.         /// </summary>
  315.         public double dblGreenGain
  316.         {
  317.             get { return greenGain; }
  318.             set { greenGain = value; }
  319.         }
  320.         /// <summary>
  321.         /// Gets or sets to display blue Gain
  322.         /// </summary>
  323.         public double dblBlueGain
  324.         {
  325.             get { return blueGain; }
  326.             set { blueGain = value; }
  327.         }
  328.         /// <summary>
  329.         /// initialize a Gdal based raster layer
  330.         /// </summary>
  331.         /// <param name="strLayerName">Name of layer</param>
  332.         /// <param name="imageFilename">location of image</param>
  333.         public GdalRasterLayer(string strLayerName, string imageFilename)
  334.         {
  335.             this.LayerName = strLayerName;
  336.             this.Filename = imageFilename;
  337.             disposed = false;
  338.             GDAL.gdal.AllRegister();
  339.             try
  340.             {
  341. _GdalDataset = GDAL.gdal.Open(_Filename, 1);
  342. imagesize = new Size(_GdalDataset.RasterXSize, _GdalDataset.RasterYSize);
  343. _Envelope = this.GetExtent();   
  344.             }
  345.             catch (Exception ex) { 
  346.                 _GdalDataset = null;
  347.                 throw new Exception("Couldn't load dataset. " + ex.Message + ex.InnerException);
  348.             }
  349.  
  350.         }
  351.         #region ILayer Members
  352.         /// <summary>
  353.         /// Renders the layer
  354.         /// </summary>
  355.         /// <param name="g">Graphics object reference</param>
  356.         /// <param name="map">Map which is rendered</param>
  357.         public override void Render(System.Drawing.Graphics g, Map map)
  358.         {
  359.             if (disposed)
  360.                 throw (new ApplicationException("Error: An attempt was made to render a disposed layer"));
  361.             
  362.             //if (this.Envelope.Intersects(map.Envelope))
  363.             //{
  364.                 this.GetPreview(_GdalDataset, map.Size, g, map.Envelope);
  365.             //}
  366.             base.Render(g, map);
  367.         }
  368.        
  369.         /// <summary>
  370.         /// Returns the extent of the layer
  371.         /// </summary>
  372.         /// <returns>Bounding box corresponding to the extent of the features in the layer</returns>
  373.         public override SharpMap.Geometries.BoundingBox Envelope
  374.         {
  375.             get { return _Envelope; }
  376.         }
  377.         #endregion
  378.         #region ICloneable Members
  379.         /// <summary>
  380.         /// Clones the object
  381.         /// </summary>
  382.         /// <returns></returns>
  383.         public override object Clone()
  384.         {
  385.             throw new NotImplementedException();
  386.         }
  387.         #endregion
  388. #region Disposers and finalizers
  389. private bool disposed = false;
  390. private void Dispose(bool disposing)
  391. {
  392. if (!disposed)
  393. {
  394. if (disposing)
  395.                     if (_GdalDataset != null)
  396. {
  397. try {
  398. _GdalDataset.Dispose();
  399.                         }
  400.                         finally { _GdalDataset = null; }
  401. }
  402. disposed = true;
  403. }
  404. }
  405. /// <summary>
  406. /// Disposes the GdalRasterLayer and release the raster file
  407. /// </summary>
  408. public void Dispose ()
  409. {
  410. this.Dispose (true);
  411. GC.SuppressFinalize(this);
  412. }
  413. /// <summary>
  414. /// Finalizer
  415. /// </summary>
  416. ~GdalRasterLayer()
  417. {
  418. this.Dispose (true);
  419. }
  420. #endregion
  421.         private SharpMap.Geometries.BoundingBox GetExtent()
  422.         {
  423.             if(_GdalDataset!=null)
  424.             {
  425.              double [] geoTrans = new double[6];
  426.             _GdalDataset.GetGeoTransform(geoTrans);        
  427.          GeoTransform GT = new GeoTransform(geoTrans);
  428.                 return new SharpMap.Geometries.BoundingBox( GT.Left,
  429.                                                             GT.Top + (GT.VerticalPixelResolution * _GdalDataset.RasterYSize),
  430.                                                             GT.Left + (GT.HorizontalPixelResolution * _GdalDataset.RasterXSize),
  431.                                                             GT.Top);
  432.             }
  433.             return null;
  434.         }
  435.         private void Get8BitPreview(GDAL.Dataset dataset, System.Drawing.Size size, System.Drawing.Graphics g, SharpMap.Geometries.BoundingBox bbox)
  436.         {
  437.        double [] geoTrans = new double[6];        
  438.             dataset.GetGeoTransform(geoTrans);
  439.             GeoTransform GT = new GeoTransform(geoTrans);
  440.             int DsWidth = imagesize.Width; // dataset.ImageSize.Width;
  441.             int DsHeight = imagesize.Height; // dataset.ImageSize.Height;
  442.             
  443.             int intImginMapW = 0, intImginMapH = 0, intLocX = 0, intLocY = 0;
  444.             
  445.             Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
  446.             
  447.             int iPixelSize = 3; //Format24bppRgb = byte[b,g,r] 
  448.             if (dataset != null)
  449.             {
  450.                 //check if image is in bounding box
  451.                 if ((bbox.Left > _Envelope.Right) || (bbox.Right < _Envelope.Left)
  452.                     || (bbox.Top < _Envelope.Bottom) || (bbox.Bottom > _Envelope.Top))
  453.                     return;
  454.                 double left = Math.Max(bbox.Left, _Envelope.Left);
  455.                 double top = Math.Min(bbox.Top, _Envelope.Top);
  456.                 double right = Math.Min(bbox.Right, _Envelope.Right);
  457.                 double bottom = Math.Max(bbox.Bottom, _Envelope.Bottom);
  458.                 int x1 = (int)Math.Round(GT.PixelX(left));
  459.                 int y1 = (int)Math.Round(GT.PixelY(top));
  460.                 int imgPixWidth = (int)Math.Round(GT.PixelXwidth(right - left));
  461.                 int imgPixHeight = (int)Math.Round(GT.PixelYwidth(bottom - top));
  462.                 
  463.                 //get screen pixels image should fill 
  464.                 double dblBBoxW = bbox.Right - bbox.Left;
  465.                 double dblBBoxtoImgPixX = (double)imgPixWidth / (double)dblBBoxW;
  466.                 intImginMapW = (int)Math.Round(size.Width * dblBBoxtoImgPixX * GT.HorizontalPixelResolution);
  467.                 
  468.                 double dblBBoxH = bbox.Top - bbox.Bottom;
  469.                 double dblBBoxtoImgPixY = (double)imgPixHeight / (double)dblBBoxH;
  470.                 intImginMapH = (int)Math.Round(size.Height * dblBBoxtoImgPixY * -GT.VerticalPixelResolution);
  471.                 if((intImginMapH == 0) || (intImginMapW == 0))
  472.                     return;
  473.                 // ratios of bounding box to image ground space
  474.                 double dblBBoxtoImgX = size.Width / dblBBoxW;
  475.                 double dblBBoxtoImgY = size.Height / dblBBoxH;
  476.                 
  477.                 // set where to display bitmap in Map
  478.                 if (bbox.Left != left)
  479.                 {
  480.                     if (bbox.Right != right)
  481.                         intLocX = (int)Math.Round((_Envelope.Left - bbox.Left) * dblBBoxtoImgX);
  482.                     else
  483.                         intLocX = size.Width - intImginMapW;
  484.                 }
  485.                 if (bbox.Top != top)
  486.                 {
  487.                     if (bbox.Bottom != bottom)
  488.                         intLocY = (int)Math.Round((bbox.Top - _Envelope.Top) * dblBBoxtoImgY);
  489.                     else
  490.                         intLocY = size.Height - intImginMapH;
  491.                 }
  492.                 bitmap = new Bitmap(intImginMapW, intImginMapH, PixelFormat.Format24bppRgb);
  493.                 BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, intImginMapW, intImginMapH), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  494.                 try
  495.                 {
  496.                     unsafe
  497.                     {
  498.                         if ((DisplayIR) && (dataset.RasterCount == 4))
  499.                         {
  500.                             byte[] buffer = new byte[intImginMapW * intImginMapH];
  501.                             GDAL.Band band = dataset.GetRasterBand(4);
  502.                             band.ReadRaster(x1, y1, imgPixWidth, imgPixHeight, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
  503.                             
  504.                             int p_indx = 0;
  505.                             for (int y = 0; y < intImginMapH; y++)
  506.                             {
  507.                                 byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  508.                                 for (int x = 0; x < intImginMapW; x++, p_indx++)
  509.                                 {
  510.                                     row[x * iPixelSize] = buffer[p_indx];
  511.                                     row[x * iPixelSize + 1] = buffer[p_indx];
  512.                                     row[x * iPixelSize + 2] = buffer[p_indx];
  513.                                 }
  514.                             }
  515.                         }
  516.                         else if ((DisplayCIR) && (dataset.RasterCount == 4))
  517.                         {
  518.                             for (int i = 1; i <= 4; ++i)
  519.                             {
  520.                                 if (i == 3) continue;
  521.                                 byte[] buffer = new byte[intImginMapW * intImginMapH];
  522.                                 GDAL.Band band = dataset.GetRasterBand(i);
  523.                                 //band.RasterIO(RWFlag.Read, x1, y1, imgPixWidth, imgPixHeight, buffer, intImginMapW, intImginMapH);
  524.                              band.ReadRaster(x1, y1, imgPixWidth, imgPixHeight, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);                                
  525.                                 int p_indx = 0;
  526.                                 int ch = 0;
  527.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.BlueBand)) ch = 2;
  528.                                 else if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.GreenBand)) ch = 0;
  529.                                 else if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.RedBand)) ch = 1;
  530.                                 else ch = 2;
  531.                                 for (int y = 0; y < intImginMapH; y++)
  532.                                 {
  533.                                     byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  534.                                     for (int x = 0; x < intImginMapW; x++, p_indx++)
  535.                                     {
  536.                                         row[x * iPixelSize + ch] = (byte)(buffer[p_indx]);
  537.                                     }
  538.                                 }
  539.                             }
  540.                         }
  541.                         else
  542.                         {
  543.                             for (int i = 1; i <= (dataset.RasterCount > 3 ? 3 : dataset.RasterCount); ++i)
  544.                             {
  545.                                 byte[] buffer = new byte[intImginMapW * intImginMapH];
  546.                                 GDAL.Band band = dataset.GetRasterBand(i);
  547.                                 //band.RasterIO(RWFlag.Read, x1, y1, imgPixWidth, imgPixHeight, buffer, intImginMapW, intImginMapH);
  548.                                 band.ReadRaster(x1, y1, imgPixWidth, imgPixHeight, buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
  549.                                 int p_indx = 0;
  550.                                 int ch = 0;
  551.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.BlueBand)) ch = 0;
  552.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.GreenBand)) ch = 1;
  553.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.RedBand)) ch = 2;
  554.                                 if ((!int.Equals(band.GetRasterColorInterpretation(), ColorInterp.PaletteIndex)) && (!int.Equals(band.GetRasterColorInterpretation(), ColorInterp.GrayIndex)))
  555.                                 {
  556.                                     for (int y = 0; y < intImginMapH; y++)
  557.                                     {
  558.                                         byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  559.                                         for (int x = 0; x < intImginMapW; x++, p_indx++)
  560.                                         {
  561.                                             if(ch == 2)
  562.                                                 row[x * iPixelSize + ch] = (byte)((buffer[p_indx]) * redGain);
  563.                                             else if( ch == 1)
  564.                                                 row[x * iPixelSize + ch] = (byte)((buffer[p_indx]) * greenGain);
  565.                                             else if (ch == 0)
  566.                                                 row[x * iPixelSize + ch] = (byte)((buffer[p_indx]) * blueGain);
  567.                                         }
  568.                                     }
  569.                                 }
  570.                                 else //8bit Grayscale
  571.                                 {
  572.                                     for (int y = 0; y < intImginMapH; y++)
  573.                                     {
  574.                                         byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  575.                                         for (int x = 0; x < intImginMapW; x++, p_indx++)
  576.                                         {
  577.                                             row[x * iPixelSize] = buffer[p_indx];
  578.                                             row[x * iPixelSize + 1] = buffer[p_indx];
  579.                                             row[x * iPixelSize + 2] = buffer[p_indx];
  580.                                         }
  581.                                     }
  582.                                 }
  583.                             }
  584.                         }
  585.                     }
  586.                 }
  587.                 finally
  588.                 {
  589.                     bitmap.UnlockBits(bitmapData);
  590.                 }
  591.             }
  592.             g.DrawImage(bitmap, new System.Drawing.Point(intLocX, intLocY));
  593.         }
  594.         private void GetPreview(GDAL.Dataset dataset, System.Drawing.Size size, System.Drawing.Graphics g, SharpMap.Geometries.BoundingBox bbox)
  595.         {
  596.        double [] geoTrans = new double[6];        
  597.             dataset.GetGeoTransform(geoTrans);
  598.             GeoTransform GT = new GeoTransform(geoTrans);
  599.             
  600.             int DsWidth = imagesize.Width; //dataset.ImageSize.Width;
  601.             int DsHeight = imagesize.Height; //dataset.ImageSize.Height;
  602.             int intImginMapW = 0, intImginMapH = 0, intLocX = 0, intLocY = 0, intVal = 0;
  603.             if (intBitDepth == 8)
  604.             {
  605.                 Get8BitPreview(dataset, size, g, bbox);
  606.                 return;
  607.             }
  608.             Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format24bppRgb);
  609.             int iPixelSize = 3; //Format24bppRgb = byte[b,g,r] 
  610.             if (dataset != null)
  611.             {
  612.                 //check if image is in bounding box
  613.                 if ((bbox.Left > _Envelope.Right) || (bbox.Right < _Envelope.Left)
  614.                     || (bbox.Top < _Envelope.Bottom) || (bbox.Bottom > _Envelope.Top))
  615.                     return;
  616.                 double left = Math.Max(bbox.Left, _Envelope.Left);
  617.                 double top = Math.Min(bbox.Top, _Envelope.Top);
  618.                 double right = Math.Min(bbox.Right, _Envelope.Right);
  619.                 double bottom = Math.Max(bbox.Bottom, _Envelope.Bottom);
  620.                 int x1 = (int)Math.Round(GT.PixelX(left));
  621.                 int y1 = (int)Math.Round(GT.PixelY(top));
  622.                 int imgPixWidth = (int)Math.Round(GT.PixelXwidth(right - left));
  623.                 int imgPixHeight = (int)Math.Round(GT.PixelYwidth(bottom - top));
  624.                 //get screen pixels image should fill 
  625.                 double dblBBoxW = bbox.Right - bbox.Left;
  626.                 double dblBBoxtoImgPixX = (double)imgPixWidth / (double)dblBBoxW;
  627.                 intImginMapW = (int)Math.Round(size.Width * dblBBoxtoImgPixX * GT.HorizontalPixelResolution);
  628.                 double dblBBoxH = bbox.Top - bbox.Bottom;
  629.                 double dblBBoxtoImgPixY = (double)imgPixHeight / (double)dblBBoxH;
  630.                 intImginMapH = (int)Math.Round(size.Height * dblBBoxtoImgPixY * -GT.VerticalPixelResolution);
  631.                 // ratios of bounding box to image ground space
  632.                 double dblBBoxtoImgX = size.Width / dblBBoxW;
  633.                 double dblBBoxtoImgY = size.Height / dblBBoxH;
  634.                 // set where to display bitmap in Map
  635.                 if (bbox.Left != left)
  636.                 {
  637.                     if (bbox.Right != right)
  638.                         intLocX = (int)Math.Round((_Envelope.Left - bbox.Left) * dblBBoxtoImgX);
  639.                     else
  640.                         intLocX = size.Width - intImginMapW;
  641.                 }
  642.                 if (bbox.Top != top)
  643.                 {
  644.                     if (bbox.Bottom != bottom)
  645.                         intLocY = (int)Math.Round((bbox.Top - _Envelope.Top) * dblBBoxtoImgY);
  646.                     else
  647.                         intLocY = size.Height - intImginMapH;
  648.                 }
  649.                 bitmap = new Bitmap(intImginMapW, intImginMapH, PixelFormat.Format24bppRgb);
  650.                 BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, intImginMapW, intImginMapH), ImageLockMode.ReadWrite, bitmap.PixelFormat);
  651.                 try
  652.                 {
  653.                     unsafe
  654.                     {
  655.                         if ((DisplayIR)&&(dataset.RasterCount == 4))
  656.                         {
  657.                             //UInt16[] buffer = new UInt16[intImginMapW * intImginMapH];
  658.                             Int16[] buffer = new Int16[intImginMapW * intImginMapH];
  659.                             GDAL.Band band = dataset.GetRasterBand(4);
  660.                             //band.RasterIO(RWFlag.Read, x1, y1, imgPixWidth, imgPixHeight, buffer, intImginMapW, intImginMapH);
  661.                             band.ReadRaster(x1, y1, imgPixWidth, imgPixHeight, (short[])buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
  662.                             
  663.                             int p_indx = 0;
  664.                             for (int y = 0; y < intImginMapH; y++)
  665.                             {
  666.                                 byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  667.                                 for (int x = 0; x < intImginMapW; x++, p_indx++)
  668.                                 {
  669.                                     if (intBitDepth == 16)
  670.                                     {
  671.                                         intVal = (buffer[p_indx] / 256);
  672.                                         if(intVal > 255)intVal = 255;
  673.                                     }
  674.                                     else if (intBitDepth == 12)
  675.                                     {
  676.                                         intVal = (buffer[p_indx] / 16);
  677.                                         if (intVal > 255) intVal = 255;
  678.                                     }
  679.                                     row[x * iPixelSize] = (byte)intVal;
  680.                                     row[x * iPixelSize + 1] = (byte)intVal;
  681.                                     row[x * iPixelSize + 2] = (byte)intVal;
  682.                                 }
  683.                             }
  684.                         }
  685.                         else if ((DisplayCIR) && (dataset.RasterCount == 4))
  686.                         {
  687.                             for (int i = 1; i <= 4; ++i)
  688.                             {
  689.                                 if (i == 3) continue;
  690.                                 //UInt16[] buffer = new UInt16[intImginMapW * intImginMapH];
  691.                                 Int16[] buffer = new Int16[intImginMapW * intImginMapH];
  692.                                 GDAL.Band band = dataset.GetRasterBand(i);
  693.                                 //band.RasterIO(RWFlag.Read, x1, y1, imgPixWidth, imgPixHeight, buffer, intImginMapW, intImginMapH);
  694.                                 band.ReadRaster(x1, y1, imgPixWidth, imgPixHeight, (short[])buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
  695.                                 int p_indx = 0;
  696.                                 int ch = 0;
  697.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.BlueBand)) ch = 2;
  698.                                 else if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.GreenBand)) ch = 0;
  699.                                 else if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.RedBand)) ch = 1;
  700.                                 else ch = 2;
  701.                                 for (int y = 0; y < intImginMapH; y++)
  702.                                 {
  703.                                     byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  704.                                     for (int x = 0; x < intImginMapW; x++, p_indx++)
  705.                                     {
  706.                                         if (intBitDepth == 16)
  707.                                         {
  708.                                             intVal = (buffer[p_indx] / 256);
  709.                                             if (intVal > 255)intVal = 255;
  710.                                         }
  711.                                         else if (intBitDepth == 12)
  712.                                         {
  713.                                             intVal = (buffer[p_indx] / 16);
  714.                                             if (intVal > 255)intVal = 255;
  715.                                         }
  716.                                         row[x * iPixelSize + ch] = (byte)intVal;
  717.                                     }
  718.                                 }
  719.                             }
  720.                         }
  721.                         else
  722.                         {
  723.                             for (int i = 1; i <= (dataset.RasterCount > 3 ? 3 : dataset.RasterCount); ++i)
  724.                             {
  725.                                 //UInt16[] buffer = new UInt16[intImginMapW * intImginMapH];
  726.                                 Int16[] buffer = new Int16[intImginMapW * intImginMapH];                                
  727.                                 GDAL.Band band = dataset.GetRasterBand(i);
  728.                                 //band.RasterIO(RWFlag.Read, x1, y1, imgPixWidth, imgPixHeight, buffer, intImginMapW, intImginMapH);
  729.                                 band.ReadRaster(x1, y1, imgPixWidth, imgPixHeight, (short[])buffer, size.Width, size.Height, (int)GT.HorizontalPixelResolution, (int)GT.VerticalPixelResolution);
  730.                             
  731.                                 int p_indx = 0;
  732.                                 int ch = 0;
  733.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.BlueBand)) ch = 0;
  734.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.GreenBand)) ch = 1;
  735.                                 if (int.Equals(band.GetRasterColorInterpretation(), ColorInterp.RedBand)) ch = 2;
  736.                                 if ((!int.Equals(band.GetRasterColorInterpretation(), ColorInterp.PaletteIndex))&& (!int.Equals(band.GetRasterColorInterpretation(), ColorInterp.GrayIndex)))
  737.                                 {
  738.                                     for (int y = 0; y < intImginMapH; y++)
  739.                                     {
  740.                                         byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  741.                                         for (int x = 0; x < intImginMapW; x++, p_indx++)
  742.                                         {
  743.                                             if (intBitDepth == 16)
  744.                                             {
  745.                                                 if (ch == 2)
  746.                                                 {
  747.                                                     intVal = (int)((buffer[p_indx] / 256) * redGain);
  748.                                                     if (intVal > 255) intVal = 255;
  749.                                                 }
  750.                                                 else if (ch == 1)
  751.                                                 {
  752.                                                     intVal = (int)((buffer[p_indx] / 256) * greenGain);
  753.                                                     if (intVal > 255) intVal = 255;
  754.                                                 }
  755.                                                 else if (ch == 0)
  756.                                                 {
  757.                                                     intVal = (int)((buffer[p_indx] / 256) * blueGain);
  758.                                                     if (intVal > 255) intVal = 255;
  759.                                                 }
  760.                                                 row[x * iPixelSize + ch] = (byte)intVal;
  761.                                             }
  762.                                             else if (intBitDepth == 12)
  763.                                             {
  764.                                                 if (ch == 2)
  765.                                                 {
  766.                                                     intVal = (int)((buffer[p_indx] / 16) * redGain);
  767.                                                     if (intVal > 255) intVal = 255;
  768.                                                 }
  769.                                                 else if (ch == 1)
  770.                                                 {
  771.                                                     intVal = (int)((buffer[p_indx] / 16) * greenGain);
  772.                                                     if (intVal > 255) intVal = 255;
  773.                                                 }
  774.                                                 else if (ch == 0)
  775.                                                 {
  776.                                                     intVal = (int)((buffer[p_indx] / 16) * blueGain);
  777.                                                     if (intVal > 255) intVal = 255;
  778.                                                 }
  779.                                                 row[x * iPixelSize + ch] = (byte)intVal;
  780.                                             }
  781.                                         }
  782.                                     }
  783.                                 }
  784.                                 else //Grayscale
  785.                                 {
  786.                                     for (int y = 0; y < intImginMapH; y++)
  787.                                     {
  788.                                         byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
  789.                                         for (int x = 0; x < intImginMapW; x++, p_indx++)
  790.                                         {
  791.                                             if (intBitDepth == 16)
  792.                                             {
  793.                                                 intVal = (buffer[p_indx] / 256);
  794.                                                 if (intVal > 255) intVal = 255;
  795.                                             }
  796.                                             else if (intBitDepth == 12)
  797.                                             {
  798.                                                 intVal = (buffer[p_indx] / 16);
  799.                                                 if (intVal > 255) intVal = 255;
  800.                                             }
  801.                                             row[x * iPixelSize] = (byte)intVal;
  802.                                             row[x * iPixelSize + 1] = (byte)intVal;
  803.                                             row[x * iPixelSize + 2] = (byte)intVal;
  804.                                         }
  805.                                     }
  806.                                 }
  807.                             }
  808.                         }
  809.                     }
  810.                 }
  811.                 finally
  812.                 {
  813.                     bitmap.UnlockBits(bitmapData);
  814.                 }
  815.             }
  816.             g.DrawImage(bitmap, new System.Drawing.Point(intLocX, intLocY));
  817.         }
  818.     }   
  819.    
  820. */   
  821.    
  822.    
  823.    
  824.    
  825.    
  826.     /// <summary>
  827.     /// Types of color interpretation for raster bands.
  828.     /// </summary>
  829.     public enum ColorInterp
  830.     {
  831.      /// <summary>
  832.      /// Undefined
  833.      /// </summary>
  834.         Undefined = 0,
  835.         /// <summary>
  836.         /// Greyscale
  837.         /// </summary>
  838.         GrayIndex = 1,
  839.         /// <summary>
  840.         /// Paletted (see associated color table)
  841.         /// </summary>
  842.         PaletteIndex = 2,
  843.         /// <summary>
  844.         /// Red band of RGBA image
  845.         /// </summary>               
  846.         RedBand = 3,
  847.         /// <summary>
  848.         /// Green band of RGBA image
  849.         /// </summary>
  850.         GreenBand = 4,
  851.         /// <summary>
  852.         /// Blue band of RGBA image
  853.         /// </summary>                       
  854.         BlueBand = 5,
  855.         /// <summary>
  856.         /// Alpha (0=transparent, 255=opaque)
  857.         /// </summary>   
  858.         AlphaBand = 6,
  859.         /// <summary>
  860.         /// Hue band of HLS image 
  861.         /// </summary>                 
  862.         HueBand = 7,
  863.         /// <summary>
  864.         /// Saturation band of HLS image 
  865.         /// </summary>       
  866.         SaturationBand = 8,
  867.         /// <summary>
  868.         /// Lightness band of HLS image
  869.         /// </summary>            
  870.         LightnessBand = 9,
  871.         /// <summary>
  872.         /// Cyan band of CMYK image
  873.         /// </summary>                
  874.         CyanBand = 10,
  875.         /// <summary>
  876.         /// Magenta band of CMYK image
  877.         /// </summary>             
  878.         MagentaBand = 11,
  879.         /// <summary>
  880.         /// Yellow band of CMYK image
  881.         /// </summary>             
  882.         YellowBand = 12,
  883.         /// <summary>
  884.         /// Black band of CMYK image
  885.         /// </summary>                      
  886.         BlackBand = 13,
  887.         /// <summary>
  888.         /// Y Luminance
  889.         /// </summary>                             
  890.         YCbCr_YBand = 14,
  891.         /// <summary>
  892.         /// Cb Chroma
  893.         /// </summary>                              
  894.         YCbCr_CbBand = 15,
  895.         /// <summary>
  896.         /// Cr Chroma
  897.         /// </summary>                                          
  898.         YCbCr_CrBand = 16,
  899.         /// <summary>
  900.         /// Max current value
  901.         /// </summary>
  902.         Max = 16
  903.     };
  904.     
  905.     /// <summary>
  906.     /// Type for Read Write state
  907.     /// </summary>
  908. public enum RWFlag
  909.     {
  910.         /// <summary>
  911.         /// Read dataset
  912.         /// </summary>
  913. Read = 0,
  914. /// <summary>
  915. /// Write dataset
  916. /// </summary>
  917.         Write = 1
  918.     };
  919. /// <summary>
  920. /// Field data type
  921. /// </summary>
  922.     public enum DataType
  923.     {
  924.         /// <summary>
  925.         /// unknown data type
  926.         /// </summary>
  927.      Unknown = 0,
  928.      /// <summary>
  929.      /// byte
  930.      /// </summary>
  931.         Byte = 1,
  932.         /// <summary>
  933.         /// unsigned int 16 bit
  934.         /// </summary>
  935.         UInt16 = 2,
  936.         /// <summary>
  937.         /// signed int 16 bit
  938.         /// </summary>
  939.         Int16 = 3,
  940.         /// <summary>
  941.         /// unsigned int 32 bit
  942.         /// </summary>
  943.         UInt32 = 4,
  944.         /// <summary>
  945.         /// signed int 32 bit
  946.         /// </summary>
  947.         Int32 = 5,
  948.         /// <summary>
  949.         /// float 32 bit
  950.         /// </summary>
  951.         Float32 = 6,
  952.         /// <summary>
  953.         /// float 64 bit
  954.         /// </summary>
  955.         Float64 = 7,
  956.         /// <summary>
  957.         /// c int 16 bit
  958.         /// </summary>
  959.         CInt16 = 8,
  960.         /// <summary>
  961.         /// c int 32 bit
  962.         /// </summary>
  963.         CInt32 = 9,
  964.         /// <summary>
  965.         /// c float 16 bit
  966.         /// </summary>
  967.         CFloat32 = 10,
  968.         /// <summary>
  969.         /// c float 64 bit
  970.         /// </summary>
  971.         CFloat64 = 11,
  972.         /// <summary>
  973.         /// type count
  974.         /// </summary>
  975.         TypeCount = 12
  976.     };    
  977. }