示例#1
0
 /// <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);
 }
示例#2
0
        ///// <summary>
        ///// 根据栅格图层名字返回栅格图层数据数组
        ///// </summary>
        ///// <param name="layerName">图层名字</param>
        ///// <param name="width">ref类型,数据的宽度</param>
        ///// <param name="height">ref类型,数据的高度</param>
        ///// <returns>数据数组,按行优先</returns>
        //private double[] GetData(string layerName, ref int width, ref int height)
        //{
        //    var map = GIS.FrameWork.Application.App.Map;
        //    var layers = map.Layers;
        //    ILayer selectedLayer = null;
        //    for (int i = 0; i < layers.Count; i++)
        //    {
        //        var layer = layers[i];
        //        if (layer.LegendText == layerName)
        //        {
        //            selectedLayer = layer;
        //            break;
        //        }
        //    }

        //    if (selectedLayer == null)
        //    {
        //        return null; // 图层名无效
        //    }

        //    try
        //    {
        //        RasterLayer rasterLayer = selectedLayer as RasterLayer;
        //        IRaster rasterDataSet = rasterLayer.DataSet;
        //        Dataset dataset = GIS.GDAL.RasterConverter.Ds2GdalRaster(rasterDataSet, null, new int[] { 1 });
        //        width = dataset.RasterXSize;
        //        height = dataset.RasterYSize;
        //        double[] imageBuffer = new double[width * height];
        //        Band band = dataset.GetRasterBand(1);
        //        band.ReadRaster(0, 0, width, height, imageBuffer, width, height, 0, 0);
        //        return imageBuffer;
        //    }
        //    catch
        //    {
        //        return null; // 图层不是栅格图层或将图层转化为GDAL dataset失败或从GDAL dataset中读取数据失败
        //    }
        //}



        /// <summary>
        /// 和GetData功能相同,但是使用GDAL直接从文件中读取数据。
        /// 获取空间参照信息
        /// </summary>
        /// <param name="fileName">文件名字</param>
        /// <param name="width">ref用于返回数据的宽度</param>
        /// <param name="height">ref用于返回数据的高度</param>
        /// <returns>一维数据数组,按行优先</returns>
        protected 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;
            this.geoTransform = new double[6];

            dataset.GetGeoTransform(geoTransform);
            this.projStr  = dataset.GetProjection();
            this.tiffType = dataset.GetType();

            double[]        imageBuffer = new double[width * height];
            OSGeo.GDAL.Band b           = dataset.GetRasterBand(1);
            b.ReadRaster(0, 0, width, height, imageBuffer, width, height, 0, 0);
            double noDataVal;
            int    hasVal;

            b.GetNoDataValue(out noDataVal, out hasVal);
            this.noDataVal = noDataVal;
            return(imageBuffer);
        }
示例#3
0
        private void ReadARGB()
        {
            if (_dataset.RasterCount < 4)
            {
                throw new GdalException("ARGB Format was indicated but there are only " + _dataset.RasterCount + " bands!");
            }
            _alpha = _red;
            _red = _dataset.GetRasterBand(2);
            _green = _dataset.GetRasterBand(3);
            _blue = _dataset.GetRasterBand(4);

            Width = _red.XSize;
            Height = _red.YSize;

            _image = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);

            byte[] a = new byte[Width * Height];
            byte[] r = new byte[Width * Height];
            byte[] g = new byte[Width * Height];
            byte[] b = new byte[Width * Height];

            
            _alpha.ReadRaster(0, 0, Width, Height, a, Width, Height, 0, 0);
            _red.ReadRaster(0, 0, Width, Height, r, Width, Height, 0, 0);
            _green.ReadRaster(0, 0, Width, Height, g, Width, Height, 0, 0);
            _blue.ReadRaster(0, 0, Width, Height, b, Width, Height, 0, 0);

            BitmapData bData = _image.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Stride = bData.Stride;
            _image.UnlockBits(bData);
            Values = new byte[Width * Height * 4];
            BytesPerPixel = 4;
            for (int row = 0; row < Height; row++)
            {
                for (int col = 0; col < Width; col++)
                {
                    Values[row * Stride + col * BytesPerPixel] = b[row * Width + col];
                    Values[row * Stride + col * BytesPerPixel + 1] = g[row * Width + col];
                    Values[row * Stride + col * BytesPerPixel + 2] = r[row * Width + col];
                    Values[row * Stride + col * BytesPerPixel + 3] = a[row * Width + col];
                }
            }
            WriteBytes();
        }
示例#4
0
        private void ReadRGB()
        {

            if (_dataset.RasterCount < 3)
            {
                throw new GdalException("RGB Format was indicated but there are only " + _dataset.RasterCount + " bands!");
            }
            _green = _dataset.GetRasterBand(2);
            _blue = _dataset.GetRasterBand(3);

            Width = _red.XSize;
            Height = _red.YSize;

            _image = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);


            byte[] r = new byte[Width * Height];
            byte[] g = new byte[Width * Height];
            byte[] b = new byte[Width * Height];

            _red.ReadRaster(0, 0, Width, Height, r, Width, Height, 0, 0);
            _green.ReadRaster(0, 0, Width, Height, g, Width, Height, 0, 0);
            _blue.ReadRaster(0, 0, Width, Height, b, Width, Height, 0, 0);

            BitmapData bData = _image.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            Stride = bData.Stride;
            _image.UnlockBits(bData);
            byte[] vals = new byte[Width * Height * 4];
            BytesPerPixel = 4;
            int stride = Stride;
            int bpp = BytesPerPixel;
            int width = Width;
            int height = Height;
            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    vals[row * stride + col * bpp] = b[row * width + col];
                    vals[row * stride + col * bpp + 1] = g[row * width + col];
                    vals[row * stride + col * bpp + 2] = r[row * width + col];
                    vals[row * stride + col * bpp + 3] = 255;
                }
            }
            Values = vals;
            WriteBytes();
        }
示例#5
0
        private Bitmap ReadGrayIndex(int xOffset, int yOffset, int xSize, int ySize, Band first)
        {
            Band first_o;
            int width = xSize;
            int height = ySize;
            if (first.GetOverviewCount() > 0 && _overview >= 0)
            {
                first_o = first.GetOverview(_overview);
            }
            else
            {
                first_o = first;
            }
            if (xOffset + width > first_o.XSize)
            {
                width = first_o.XSize - xOffset;
            }
            if (yOffset + height > first_o.YSize)
            {
                height = first_o.YSize - yOffset;
            }
            Bitmap result = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            byte[] r = new byte[width * height];
            first.ReadRaster(xOffset, yOffset, width, height, r, width, height, 0, 0);
            BitmapData bData =
                result.LockBits(new Rectangle(0, 0, xSize, ySize),
                                ImageLockMode.ReadWrite,
                                PixelFormat.Format32bppArgb);
            int stride = Math.Abs(bData.Stride);

            byte[] vals = new byte[height * stride];

            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    byte value = r[row * width + col];
                    vals[row * stride + col * 4] = value;
                    vals[row * stride + col * 4 + 1] = value;
                    vals[row * stride + col * 4 + 2] = value;
                    vals[row * stride + col * 4 + 3] = 255;
                }
            }
            Marshal.Copy(vals, 0, bData.Scan0, vals.Length);
            result.UnlockBits(bData);
            return result;
        }
示例#6
0
        /// <summary>
        /// 切分图像||
        /// 解决图像过大导致的计算缓慢||
        /// 输入图像路径,输出分幅路径||
        /// 重叠区根据图像分辨率确定,约为实地100~200米
        /// </summary>
        /// <param name="inPaht"></param>
        /// <returns></returns>
        public static List <string> getSubImg(string inPaht)
        {
            //注册插件
            OSGeo.GDAL.Gdal.AllRegister();
            OSGeo.GDAL.Driver gdalDriver = OSGeo.GDAL.Gdal.GetDriverByName("HFA");
            //读进数据
            OSGeo.GDAL.Dataset inDS = OSGeo.GDAL.Gdal.Open(inPaht, OSGeo.GDAL.Access.GA_ReadOnly);

            //根据栅格分辨率确定重叠区大小
            double[] dsTraansform = new double[6];
            inDS.GetGeoTransform(dsTraansform);
            if (dsTraansform[1] < 0.16)
            {
                xBuf = 1000; yBuf = 1000;
            }                            //1000*0.1=>100M
            if (dsTraansform[1] < 0.3)
            {
                xBuf = 500; yBuf = 500;
            }                          //500*0.25=>125M
            else if (dsTraansform[1] < 0.6)
            {
                xBuf = 300; yBuf = 300;
            }                          //300*0.5=>150M
            else if (dsTraansform[1] < 1.1)
            {
                xBuf = 200; yBuf = 150;
            }                          //150*1=>200M
            else if (dsTraansform[1] < 2.1)
            {
                xBuf = 100; yBuf = 100;
            }                          //100*2=>200M
            else
            {
                xBuf = 50; yBuf = 50;
            }                        //50*5=>250M


            //获取数据XY相元数量
            int oriXcount = inDS.RasterXSize;
            int oriYcount = inDS.RasterYSize;

            //用来返回的文件路径列表
            List <string> imgFilePaths = new List <string>();

            if (oriXcount > userSetX || oriYcount > userSetY)
            {
                //确定文件行列数
                int u, v;
                u = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(oriXcount) / userSetX)); //行文件数
                v = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(oriYcount) / userSetY)); //列文件数

                //循环列
                for (int i = 0; i < v; i++)
                {
                    //循环行
                    for (int j = 0; j < u; j++)
                    {
                        ////////////  定义起点  /////////////
                        int offX = j * userSetX;
                        int offY = i * userSetY;

                        //////////////定义边缘栅格大小/////////////////
                        int thinkJ    = userSetX * (j + 1) + xBuf;
                        int thinkI    = userSetY * (i + 1) + yBuf;
                        int subXcount = oriXcount - thinkJ > 0 ? userSetX + xBuf : oriXcount - userSetX * j;
                        int subYcount = oriYcount - thinkI > 0 ? userSetY + yBuf : oriYcount - userSetY * i;

                        ////////////  把原栅格读进内存,内容为oriValue /////////////
                        OSGeo.GDAL.Band oriBand  = inDS.GetRasterBand(1);
                        double[]        oriValue = new double[subXcount * subYcount];

                        oriBand.ReadRaster
                        (
                            offX,      //起点X索引
                            offY,      //起点Y索引
                            subXcount, //X方向相元数量
                            subYcount, //Y方向相元数量
                            oriValue,  //各相元值
                            subXcount, //执行读入的X方向数量
                            subYcount, //执行读入的Y方向数量
                            0,         //执行读入的起点X索引
                            0          //执行读入的起点Y索引
                        );

                        ////////////  创建子栅格 /////////////
                        string             imgFilePath = StaticTools.tempFilePath("img", i.ToString() + "_" + j.ToString());
                        OSGeo.GDAL.Dataset subDs       = gdalDriver.Create
                                                         (
                            imgFilePath,
                            subXcount,
                            subYcount,
                            1,
                            OSGeo.GDAL.DataType.GDT_Float32,
                            null
                                                         );
                        subDs.SetProjection(inDS.GetProjectionRef());

                        //获取数据Transfrom
                        double[] oriTransFrom = new double[6];
                        inDS.GetGeoTransform(oriTransFrom);
                        oriTransFrom[0] = oriTransFrom[0] + offX * oriTransFrom[1] + offY * oriTransFrom[2];
                        oriTransFrom[3] = oriTransFrom[3] + offX * oriTransFrom[4] + offY * oriTransFrom[5];
                        subDs.SetGeoTransform(oriTransFrom);
                        ////////////  把值写入子栅格 /////////////
                        subDs.GetRasterBand(1).WriteRaster
                        (
                            0,
                            0,
                            subXcount,
                            subYcount,
                            oriValue,
                            subXcount,
                            subYcount,
                            0,
                            0
                        );

                        ///////////////// 返回子栅格路径 ////////////////////
                        imgFilePaths.Add(imgFilePath);
                        subDs.Dispose();
                    }
                }
            }
            else
            {
                imgFilePaths.Add(inPaht);
            }
            inDS.Dispose();
            return(imgFilePaths);
        }
示例#7
0
        private void ReadArgb()
        {
            if (_dataset.RasterCount < 4)
            {
                throw new GdalException("ARGB Format was indicated but there are only " + _dataset.RasterCount + " bands!");
            }
            _alpha = _red;
            _red = _dataset.GetRasterBand(2);
            _green = _dataset.GetRasterBand(3);
            _blue = _dataset.GetRasterBand(4);

            int tw = TileCollection.TileWidth;
            int th = TileCollection.TileHeight;
            for (int row = 0; row < TileCollection.NumTilesTall(); row++)
            {
                for (int col = 0; col < TileCollection.NumTilesWide(); col++)
                {
                    int width = TileCollection.GetTileWidth(col);
                    int height = TileCollection.GetTileHeight(row);
                    InRamImageData id = new InRamImageData(width, height);

                    Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);

                    byte[] red = new byte[width * height];
                    byte[] g = new byte[width * height];
                    byte[] b = new byte[width * height];
                    byte[] a = new byte[width * height];

                    _red.ReadRaster(col * tw, row * th, width, height, red, width, height, 0, 0);
                    _green.ReadRaster(col * tw, row * th, width, height, g, width, height, 0, 0);
                    _blue.ReadRaster(col * tw, row * th, width, height, b, width, height, 0, 0);
                    _alpha.ReadRaster(col * tw, row * th, width, height, a, width, height, 0, 0);

                    BitmapData bData = image.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite,
                                                       PixelFormat.Format32bppArgb);
                    Stride = bData.Stride;
                    image.UnlockBits(bData);
                    byte[] vals = new byte[Width * Height * 4];
                    int stride = Stride;
                    const int bpp = 4;
                    for (int r = 0; r < height; r++)
                    {
                        for (int c = 0; c < width; c++)
                        {
                            vals[r * stride + c * bpp] = b[r * width + c];
                            vals[r * stride + c * bpp + 1] = g[r * width + c];
                            vals[r * stride + c * bpp + 2] = red[r * width + c];
                            vals[r * stride + c * bpp + 3] = a[r * width + c];
                        }
                    }
                    id.Values = vals;
                    id.CopyValuesToBitmap();
                    TileCollection.Tiles[row, col] = id;
                }
            }
            SetTileBounds(Bounds.AffineCoefficients);
        }
        private void ReadRGB()
        {

            if (_dataset.RasterCount < 3)
            {
                throw new GdalException("RGB Format was indicated but there are only " + _dataset.RasterCount + " bands!");
            }
            _green = _dataset.GetRasterBand(2);
            _blue = _dataset.GetRasterBand(3);

            int tw = TileCollection.TileWidth;
            int th = TileCollection.TileHeight;
            int ntt = TileCollection.NumTilesTall();
            int ntw = TileCollection.NumTilesWide();
            ProgressMeter pm = new ProgressMeter(_prog, "Reading Tiles ", ntt*ntw);
            for (int row = 0; row < ntt; row++)
            {
                for (int col = 0; col < ntw; col++)
                {

                    int width = TileCollection.GetTileWidth(col);
                    int height = TileCollection.GetTileHeight(row);
                    MWImageData id = new MWImageData(width, height);

                    
                    Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);

                    byte[] red = new byte[width*height];
                    byte[] g = new byte[width*height];
                    byte[] b = new byte[width*height];

                    _red.ReadRaster(col * tw, row * th, width, height, red, width, height, 0, 0);
                    _green.ReadRaster(col * tw, row * th, width, height, g, width, height, 0, 0);
                    _blue.ReadRaster(col * tw, row * th, width, height, b, width, height, 0, 0);

                    BitmapData bData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite,
                                                       PixelFormat.Format32bppArgb);
                    Stride = bData.Stride;
                    image.UnlockBits(bData);
                    byte[] vals = new byte[width*height*4];
                    int stride = Stride;
                    const int bpp = 4;
                    for (int r = 0; r < height; r++)
                    {
                        for (int c = 0; c < width; c++)
                        {
                            vals[r*stride + c*bpp] = b[r*width + c];
                            vals[r*stride + c*bpp + 1] = g[r*width + c];
                            vals[r*stride + c*bpp + 2] = red[r*width + c];
                            vals[r*stride + c*bpp + 3] = 255;
                        }
                    }
                    id.Values = vals;
                    id.WriteBytes();
                    TileCollection.Tiles[row, col] = id;
                    pm.CurrentValue = row*ntw + col;
                }
            }
            pm.Reset();
            SetTileBounds(Bounds.AffineCoefficients);
        }