Beispiel #1
0
        /// <summary>
        /// 从影像拿到帖图
        /// </summary>
        /// <param name="inMapPath"></param>
        /// <param name="saveMapPath"></param>
        void getDom(string inMapPath, string saveMapPath)
        {
            Gdal.AllRegister();
            Dataset ds = Gdal.Open(inMapPath, Access.GA_ReadOnly);

            double[] geoTran = new double[6];
            ds.GetGeoTransform(geoTran);

            Envelope enve = new Envelope();

            geom.GetEnvelope(enve);
            int xoff, yoff, xend, yend, xSize, ySize;

            Tools.geoToImageSpace(geoTran, enve.MinX, enve.MaxY, out xoff, out yoff);
            Tools.geoToImageSpace(geoTran, enve.MaxX, enve.MinY, out xend, out yend);
            xSize = xend - xoff;
            ySize = yend - yoff;
            if (File.Exists(saveMapPath))
            {
                File.Delete(saveMapPath);
            }
            OSGeo.GDAL.Driver dr    = Gdal.GetDriverByName("GTiff");
            Dataset           outDs = dr.Create(saveMapPath, xSize, ySize, 3, DataType.GDT_UInt16, null);

            for (int i = 1; i <= ds.RasterCount; i++)
            {
                double[] values = new double[xSize * ySize];
                ds.GetRasterBand(i).ReadRaster(xoff, yoff, xSize, ySize, values, xSize, ySize, 0, 0);
                outDs.GetRasterBand(i).WriteRaster(0, 0, xSize, ySize, values, xSize, ySize, 0, 0);
            }
            ds.Dispose();
            outDs.Dispose();
        }
Beispiel #2
0
        /// <summary>
        /// 栅格重建,srPath输入,dsPath输出,1为1=>1, 2为1=>4, 3为1=>9,
        /// </summary>
        /// <param name="srPath"></param>
        /// <param name="dsPath"></param>
        /// <param name="level"></param>
        public static void ImgReProject(string srPath, string dsPath, int level = 2)
        {
            Console.WriteLine("ReprojectImage开始,lever 2");
            double[] geoTran  = new double[6];
            double[] geoTran2 = new double[6];
            //读
            OSGeo.GDAL.Gdal.AllRegister();
            Dataset ds = Gdal.Open(srPath, Access.GA_ReadOnly);

            ds.GetGeoTransform(geoTran);
            geoTran2    = geoTran;
            geoTran2[1] = geoTran[1] / level;
            geoTran2[5] = geoTran[5] / level;
            //建
            OSGeo.GDAL.Driver gdalDriver = Gdal.GetDriverByName("HFA");
            Dataset           ods        = gdalDriver.Create(dsPath, ds.RasterXSize * level, ds.RasterYSize * level, 1, DataType.GDT_Float32, null);

            ods.SetProjection(ds.GetProjection());
            ods.SetGeoTransform(geoTran2);
            ods.GetRasterBand(1).SetNoDataValue(-999999);
            //写
            Gdal.ReprojectImage(ds, ods, null, null, ResampleAlg.GRA_NearestNeighbour, 0, 0.02, null, null);
            ds.Dispose();
            ods.Dispose();
            Console.WriteLine("ReprojectImage完成");
        }
Beispiel #3
0
        public static string SaveDataInFile(string filename, IEnumerable data, int width, int height, DataType dataType, double[] argin = null, string shapeSrs = null)
        {
            OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff");
            using (Dataset outputDataset = outputDriver.Create(filename, width, height, 1, dataType, null))
            {
                if (argin != null)
                {
                    outputDataset.SetGeoTransform(argin);
                }

                Band outputband = outputDataset.GetRasterBand(1);
                switch (dataType)
                {
                case DataType.GDT_UInt16:
                    outputband.WriteRaster(0, 0, width, height, (short[])data, width, height, 0, 0);
                    break;

                case DataType.GDT_Byte:
                    outputband.WriteRaster(0, 0, width, height, (byte[])data, width, height, 0, 0);
                    break;
                }

                if (!string.IsNullOrEmpty(shapeSrs))
                {
                    outputDataset.SetProjection(shapeSrs);
                }

                outputDataset.FlushCache();
                outputband.FlushCache();
            }

            return(filename);
        }
Beispiel #4
0
        private Dataset CreatePngDataset(int idOverview, int xoff, int yoff, int tileSize, bool hasAlpha)
        {
            string[]          aryOption = { "" };
            OSGeo.GDAL.Driver drv       = Gdal.GetDriverByName("MEM");

            int     nBand    = hasAlpha ? _ds.RasterCount + 1 : _ds.RasterCount;
            Dataset dsReturn = drv.Create("filememory", tileSize, tileSize, nBand, _ds.GetRasterBand(1).DataType, aryOption);

            dsReturn.SetProjection(_ds.GetProjection());

            double[] _gt = new double[6];
            _ds.GetGeoTransform(_gt);

            if (idOverview != -1)
            {
                _gt[1] *= Math.Pow(2, (double)(idOverview + 1));
                _gt[5] *= Math.Pow(2, (double)(idOverview + 1));
            }

            _gt[0] += xoff * _gt[1]; // X origin
            _gt[3] += yoff * _gt[5]; // Y origin
            dsReturn.SetGeoTransform(_gt);

            return(dsReturn);
        }
        public void WriterRasterResult(string nFileName, ResultInfo resultInfo)
        {
            try
            {
                _fileName   = nFileName;
                _resultInfo = resultInfo;

                OSGeo.GDAL.Gdal.AllRegister();
                OSGeo.GDAL.Driver nDriver = OSGeo.GDAL.Gdal.GetDriverByName("GTiff");

                double[] inGeo    = _resultInfo.InGeo;
                string[] strs     = new string[] { "INTERLEAVE = PIXEL" };
                string   prjWGS84 = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"912\"]],AUTHORITY[\"EPSG\",\"4326\"]]";

                _dataSet = nDriver.Create(_fileName,
                                          _resultInfo.Width,
                                          _resultInfo.Height,
                                          1,
                                          OSGeo.GDAL.DataType.GDT_Byte,
                                          strs);

                _pixels1 = new double[_resultInfo.Width * _resultInfo.Height];


                _dataSet.SetGeoTransform(inGeo);
                _dataSet.SetProjection(prjWGS84);
                _dataSet.Dispose();
            }
            catch (System.Exception err)
            {
                throw new Exception("数据写入失败。" + err.Message);
            }
        }
Beispiel #6
0
        /// <summary>
        /// 从影像拿到帖图
        /// </summary>
        /// <param name="inMapPath"></param>
        /// <param name="saveMapPath"></param>
        void getDom(string inMapPath, string saveMapPath)
        {
            Gdal.AllRegister();
            OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
            //打开DOM
            Dataset ds = Gdal.Open(inMapPath, Access.GA_ReadOnly);

            double[] geoTran = new double[6];
            ds.GetGeoTransform(geoTran);
            //拿边界
            Envelope enve = new Envelope();

            geom.GetEnvelope(enve);
            int xoff, yoff, xend, yend, xSize, ySize;

            Tools.geoToImageSpace(geoTran, enve.MinX, enve.MaxY, out xoff, out yoff);
            Tools.geoToImageSpace(geoTran, enve.MaxX, enve.MinY, out xend, out yend);
            xSize = xend - xoff;
            ySize = yend - yoff;

            //创建新文件
            if (File.Exists(saveMapPath))
            {
                File.Delete(saveMapPath);
            }

            OSGeo.GDAL.Driver dr    = Gdal.GetDriverByName("GTIFF");
            Dataset           outDs = dr.Create(saveMapPath, xSize, ySize, 3, DataType.GDT_Byte, null);// --------------------for test

            geoTran[0] = enve.MinX;
            geoTran[3] = enve.MaxY;
            outDs.SetGeoTransform(geoTran);

            for (int i = 1; i <= ds.RasterCount; i++)
            {
                int[] values = new int[xSize * ySize];
                ds.GetRasterBand(i).ReadRaster(xoff, yoff, xSize, ySize, values, xSize, ySize, 0, 0);

                Band band = outDs.GetRasterBand(i);
                band.WriteRaster(0, 0, xSize, ySize, values, xSize, ySize, 0, 0);
                band.FlushCache();
                outDs.FlushCache();
            }
            for (int b = 1; b < 4; b++)
            {
                double min, max, mean, std;
                Band   oriBand = ds.GetRasterBand(b);
                Band   band    = outDs.GetRasterBand(b);

                //P1 是否接受小误差         --------------------for test
                oriBand.ComputeStatistics(true, out min, out max, out mean, out std, null, "");
                band.SetStatistics(min, max, mean, std);

                band.FlushCache();
            }
            outDs.Dispose();
            ds.Dispose();
        }
Beispiel #7
0
        private static void StartClippingProcess(Layer layer, string rasterName, double rasterCellSize, out Dataset outAlignedRaster)
        {
            DriverUtils.RegisterGdalOgrDrivers();

            //Extrac srs from input feature
            string           inputShapeSrs;
            SpatialReference spatialRefrence = layer.GetSpatialRef();

            spatialRefrence.ExportToWkt(out inputShapeSrs);

            Envelope envelope = new Envelope();

            layer.GetExtent(envelope, 0);

            //Compute the out raster cell resolutions
            int x_res = Convert.ToInt32((envelope.MaxX - envelope.MinX) / rasterCellSize);
            int y_res = Convert.ToInt32((envelope.MaxY - envelope.MinY) / rasterCellSize);

            Dataset oldRasterDataset = Gdal.Open(rasterName, Access.GA_ReadOnly);

            //No of bands in older dataset
            int rasterBands = oldRasterDataset.RasterCount;

            //Create new tiff in disk
            string tempRaster = "tempValueRaster.tif";

            if (File.Exists(tempRaster))
            {
                File.Delete(tempRaster);
            }

            OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff");
            outAlignedRaster = outputDriver.Create(tempRaster, x_res, y_res, rasterBands, DataType.GDT_Float32, null);

            //Geotransform
            double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize };
            outAlignedRaster.SetGeoTransform(argin);


            //Set no data
            for (int rasBand = 1; rasBand <= rasterBands; rasBand++)
            {
                Band band = outAlignedRaster.GetRasterBand(rasBand);
                band.Fill(GdalUtilConstants.NoDataValue, 0.0);
            }

            //band.SetNoDataValue(GdalUtilConstants.NoDataValue);
            outAlignedRaster.SetProjection(inputShapeSrs);

            string[] reprojectOptions = { "NUM_THREADS = ALL_CPUS", "WRITE_FLUSH = YES" };
            Gdal.ReprojectImage(oldRasterDataset, outAlignedRaster, null, inputShapeSrs, ResampleAlg.GRA_NearestNeighbour, 0.0, 0.0, null, null, reprojectOptions);

            //flush cache
            oldRasterDataset.FlushCache();
            oldRasterDataset.Dispose();
        }
Beispiel #8
0
        /// <summary>
        /// 改变栅格值
        /// </summary>
        /// <param name="dsm"></param>
        /// <param name="savePath"></param>
        public static void editRasterValue(string dsm, string savePath, double doubles = 2)
        {
            Gdal.AllRegister();
            Dataset inData = Gdal.Open(dsm, Access.GA_ReadOnly);
            int     xSize  = inData.RasterXSize;
            int     ySize  = inData.RasterYSize;

            OSGeo.GDAL.Driver dr    = Gdal.GetDriverByName("HFA");
            Dataset           newDs = dr.Create(savePath, xSize, ySize, 1, inData.GetRasterBand(1).DataType, null);

            double[] GeoTrans = new double[6];
            inData.GetGeoTransform(GeoTrans);
            newDs.SetGeoTransform(GeoTrans);

            int userSetX   = 1000;
            int userSetY   = 1000;
            int pixelCount = xSize / userSetX + 1;
            int lineCount  = ySize / userSetY + 1;

            Console.WriteLine("起动单线编辑栅格值,Size为{0}*{1}", xSize, ySize);
            Console.WriteLine("分块为为{0}*{1},改变值为原来的{2}倍", userSetX, userSetY, doubles);

            for (int iline = 0; iline < lineCount; iline++)
            {
                for (int ipixel = 0; ipixel < pixelCount; ipixel++)
                {
                    int subXsize = userSetX;
                    int subYsize = userSetY;

                    int offx = subXsize * ipixel;
                    int offy = subYsize * iline;

                    if (ipixel == pixelCount - 1)
                    {
                        subXsize = xSize - subXsize * ipixel - 1;
                    }
                    if (iline == lineCount - 1)
                    {
                        subYsize = ySize - subYsize * iline - 1;
                    }

                    double[] rasterValue = new double[subXsize * subYsize];
                    inData.GetRasterBand(1).ReadRaster(offx, offy, subXsize, subYsize, rasterValue, subXsize, subYsize, 0, 0);

                    for (int v = 0; v < rasterValue.Length; v++)
                    {
                        rasterValue[v] = rasterValue[v] * doubles;
                    }
                    newDs.GetRasterBand(1).WriteRaster(offx, offy, subXsize, subYsize, rasterValue, subXsize, subYsize, 0, 0);
                    Console.WriteLine("编辑栅格值,已完成:{0}/{1}", iline * ipixel, lineCount * pixelCount);
                }
            }
            newDs.Dispose();
            inData.Dispose();
        }
Beispiel #9
0
        // 将给定Tif转换为Web Mercator投影
        public void TransformTiff(Dataset ds, double[] VerticeX, double[] VerticeY, string FilePath, ResampleAlg eResampleMethod)
        {
            //获取Web墨卡托坐标系
            SpatialReference Mercator = new SpatialReference("");

            Mercator.ImportFromEPSG(3857);             // Web Mercator
            Mercator.SetMercator(0d, 0d, 1d, 0d, 0d);
            string MercatorWkt;

            Mercator.ExportToWkt(out MercatorWkt);
            //原栅格坐标信息
            SpatialReference Raster_spf = new SpatialReference(ds.GetProjectionRef());
            //影像四个顶点投影转换
            CoordinateTransformation coordinateTrans = Osr.CreateCoordinateTransformation(Raster_spf, Mercator);

            coordinateTrans.TransformPoints(4, VerticeX, VerticeY, null);            //VerticeX和VerticeY存储的是影像四个顶点坐标
            coordinateTrans.Dispose();
            //计算重投影后栅格顶点坐标
            double dbMinx = 0;
            double dbMaxx = 0;
            double dbMiny = 0;
            double dbMaxy = 0;

            dbMinx = Math.Min(Math.Min(Math.Min(VerticeX[0], VerticeX[1]), VerticeX[2]), VerticeX[3]);
            dbMaxx = Math.Max(Math.Max(Math.Max(VerticeX[0], VerticeX[1]), VerticeX[2]), VerticeX[3]);
            dbMiny = Math.Min(Math.Min(Math.Min(VerticeY[0], VerticeY[1]), VerticeY[2]), VerticeY[3]);
            dbMaxy = Math.Max(Math.Max(Math.Max(VerticeY[0], VerticeY[1]), VerticeY[2]), VerticeY[3]);
            //计算新的仿射变换参数
            double[] newTransform = new double[6];
            newTransform[0] = dbMinx;         //左上角点x坐标
            newTransform[3] = dbMaxy;         //左上角点y坐标
            newTransform[1] = 100;            //像素宽度
            newTransform[5] = -100;           //像素高度
            //计算大小
            int width  = (int)Math.Ceiling(Math.Abs(dbMaxx - dbMinx) / 100.0);
            int height = (int)Math.Ceiling(Math.Abs(dbMaxy - dbMiny) / 100.0);

            //创建新的栅格影像
            OSGeo.GDAL.Driver pGDalDriver = Gdal.GetDriverByName("GTiff");
            Dataset           poDataset   = pGDalDriver.Create(FilePath, width, height, 1, DataType.GDT_Float32, null);

            poDataset.SetGeoTransform(newTransform);
            poDataset.SetProjection(MercatorWkt);
            //重投影
            Gdal.ReprojectImage(ds, poDataset, ds.GetProjectionRef(), MercatorWkt, eResampleMethod, 0, 0, new Gdal.GDALProgressFuncDelegate(ProgressFunc), null, null);
            //设置NoData值
            Band band = poDataset.GetRasterBand(1);

            band.SetNoDataValue(-10000000);
            poDataset.FlushCache();
            poDataset.Dispose();
        }
Beispiel #10
0
        /// <summary>初始数据源
        /// </summary>
        /// <param name="strFilename">文件名</param>
        /// <param name="oFileEncoding">文件编码类型</param>
        /// <param name="nLayerindex">层序号</param>
        /// <param name="bUpdata">是否可以修改</param>
        /// <returns></returns>
        public bool CreateDataset(string strFilename, Encoding oFileEncoding, int nLayerindex = 0, bool bUpdata = false)
        {
            int bXSize, bYSize;
            int w, h;

            w      = 100;
            h      = 100;
            bXSize = w;
            bYSize = 1;

            Gdal.AllRegister();
            OSGeo.GDAL.Driver drv = Gdal.GetDriverByName("GTiff");
            if (drv == null)
            {
                Console.WriteLine("Can't get driver.");
                System.Environment.Exit(-1);
            }
            Console.WriteLine("Using driver " + drv.LongName);

            string[] options = new string[] { "BLOCKXSIZE=" + bXSize, "BLOCKYSIZE=" + bYSize };
            Dataset  ds      = drv.Create(strFilename, w, h, 1, DataType.GDT_Byte, options);

            if (ds == null)
            {
                Console.WriteLine("Can't open " + strFilename);
                System.Environment.Exit(-1);
            }

            GCP[] GCPs = new GCP[] {
                new GCP(44.5, 27.5, 0, 0, 0, "info0", "id0"),
                new GCP(45.5, 27.5, 0, 100, 0, "info1", "id1"),
                new GCP(44.5, 26.5, 0, 0, 100, "info2", "id2"),
                new GCP(45.5, 26.5, 0, 100, 100, "info3", "id3")
            };
            ds.SetGCPs(GCPs, "");

            Band ba = ds.GetRasterBand(1);

            byte[] buffer = new byte[w * h];
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    buffer[i * w + j] = (byte)(i * 256 / w);
                }
            }
            ba.WriteRaster(0, 0, w, h, buffer, w, h, 0, 0);
            ba.FlushCache();
            ds.FlushCache();
            return(true);
        }
Beispiel #11
0
        public bool TransToGrid(double[] padfX, double[] padfY, double[] padfZ, double[] dEnvelopes, string outFilename, double dPixCellSize = 0.0005, double dEnvelope_offsetX = 0, double dEnvelope_offsetY = 0, DataType typeData = DataType.GDT_Float64)
        {
            if (padfX.Length != padfY.Length && padfX.Length != padfZ.Length)
            {
                return(false);
            }

            //计算图像大小-按数据边界(dEnvelopes:MinX,MaxX,MinY,MaxY)
            int nNumPoints = padfX.Length;
            int nXSize     = (int)((dEnvelopes[1] - dEnvelopes[0] + 2 * dEnvelope_offsetX) / dPixCellSize);
            int nYSize     = (int)((dEnvelopes[3] - dEnvelopes[2] + 2 * dEnvelope_offsetY) / dPixCellSize);


            // 创建输出数据集,格式为GeoTiff格式
            OSGeo.GDAL.Driver poDriver  = Gdal.GetDriverByName(_DstDriverName);
            Dataset           poDataset = poDriver.Create(outFilename, nXSize, nYSize, 1, typeData, null);

            if (_SpatialWKT != "")
            {
                poDataset.SetProjection(_SpatialWKT);
            }


            // 离散点内插方法,参数生成
            string poOptions = _GdalAlg.InitOptions();

            double[] pData = new double[nXSize * nYSize];
            Gdal.GridCreate(poOptions, nNumPoints, padfX, padfY, padfZ,
                            dEnvelopes[0], dEnvelopes[1], dEnvelopes[2], dEnvelopes[3], nXSize, nYSize,
                            typeData, pData, nXSize * nYSize * 8, null, null);

            // 设置六参数
            double[] adfGeoTransform = new double[] { dEnvelopes[0] - dEnvelope_offsetX, dPixCellSize, 0, dEnvelopes[3] + dEnvelope_offsetY, 0, -dPixCellSize };
            poDataset.SetGeoTransform(adfGeoTransform);

            // 写入影像
            Band poBand = poDataset.GetRasterBand(1);

            poBand.WriteRaster(0, 0, nXSize, nYSize, pData, nXSize, nYSize, 0, 0);
            poBand.SetNoDataValue(_NoDataValue);

            poBand.FlushCache();
            poDataset.FlushCache();

            // 释放资源 关闭图像
            poBand.Dispose();
            poDriver.Dispose();
            poDataset.Dispose();
            return(true);
        }
Beispiel #12
0
        public void Sloping(string DemPath, string OutSlpPath)
        {
            Gdal.AllRegister();
            Stopwatch sw = new Stopwatch(); sw.Start();

            Console.WriteLine("【开始创建SlopeMap!】");
            int    hasVal;
            double demNodata;

            double[] geoTransform = new double[6];
            //打开
            Dataset InDataset = Gdal.Open(DemPath, Access.GA_Update);

            InDataset.GetRasterBand(1).GetNoDataValue(out demNodata, out hasVal);
            InDataset.GetGeoTransform(geoTransform);

            //调用GDal创建影像,声明影像格式
            OSGeo.GDAL.Driver gdalDriver = Gdal.GetDriverByName("HFA");
            slopeDs = gdalDriver.Create(OutSlpPath, InDataset.RasterXSize, InDataset.RasterYSize, 1, DataType.GDT_Float32, null);
            slopeDs.SetProjection(InDataset.GetProjection());
            slopeDs.SetGeoTransform(geoTransform);
            if (hasVal == 0)
            {
                InDataset.GetRasterBand(1).SetNoDataValue(-100000);
                slopeDs.GetRasterBand(1).SetNoDataValue(-100000);
                slpNodata = -100000;
            }
            else
            {
                slopeDs.GetRasterBand(1).SetNoDataValue(demNodata);
                slpNodata = demNodata;
            }
            InDataset.Dispose();

            asyRE = new AutoResetEvent(false);
            Thread th = new Thread(() =>
            {
                MySloping(DemPath);
            });

            th.Start();
            asyRE.WaitOne();
            InDataset = Gdal.Open(DemPath, Access.GA_ReadOnly);
            //FixOutLineRaster(InDataset.GetRasterBand(1), demNodata);
            //处理外围的Raster
            BufferOnePixel(slopeDs.GetRasterBand(1));
            slopeDs.Dispose();
            sw.Stop(); Console.WriteLine("【SlopeMap完成,用时:" + sw.Elapsed.ToString() + "】");
        }
Beispiel #13
0
        //矢量转栅格
        private void RasterizeLayer(Layer layer, string outRaster, string field, float resolution, int xSize, int ySize)
        {
            const double noDataValue      = -9999;          // NoData值
            string       outputRasterFile = outRaster;
            Envelope     envelope         = new Envelope(); //原图层外接矩形

            layer.GetExtent(envelope, 0);
            //新建栅格图层
            OSGeo.GDAL.Driver outputDriver  = Gdal.GetDriverByName("GTiff");
            Dataset           outputDataset = outputDriver.Create(outputRasterFile, xSize, ySize, 1, DataType.GDT_Int32, null);//DataType.GDT_Float64
            //获取原矢量图层坐标系
            string inputShapeSrs;

            OSGeo.OSR.SpatialReference spatialRefrence = layer.GetSpatialRef();
            spatialRefrence.ExportToWkt(out inputShapeSrs);
            outputDataset.SetProjection(inputShapeSrs);

            double[] argin = new double[] { envelope.MinX, resolution, 0, envelope.MaxY, 0, -resolution };
            outputDataset.SetGeoTransform(argin);

            Band band = outputDataset.GetRasterBand(1);

            band.SetNoDataValue(noDataValue);

            outputDataset.FlushCache();
            outputDataset.Dispose();
            //矢量转栅格
            int[]    bandlist   = new int[] { 1 };
            double[] burnValues = new double[] { 10.0 };
            Dataset  myDataset  = Gdal.Open(outputRasterFile, Access.GA_Update);

            string[] rasterizeOptions;
            rasterizeOptions = new string[] { "ATTRIBUTE=" + field, "ALL_TOUCHED=TRUE" };
            int tets = Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion");

            myDataset.FlushCache();
            myDataset.Dispose();
        }
Beispiel #14
0
        private void buttonOk_Click(object sender, EventArgs e)
        {
            int    width     = 0;
            int    height    = 0;
            int    times     = 0;
            double threshold = 0.0;

            if (this.comboBoxInitialUrbanImage.SelectedItem == null || this.comboBoxLandSuitable.SelectedItem == null || this.comboBoxPg.SelectedItem == null)
            {
                MessageBox.Show("清先选择图层");
                return;
            }
            string initialUrbanImageLayerName = this.comboBoxInitialUrbanImage.SelectedItem.ToString();
            string pgLayerName           = this.comboBoxPg.SelectedItem.ToString();
            string landSuitableLayerName = this.comboBoxLandSuitable.SelectedItem.ToString();

            try
            {
                times     = Convert.ToInt32(this.textBoxTimes.Text);
                threshold = Convert.ToDouble(this.textBoxThreshold.Text);
            }
            catch
            {
                MessageBox.Show("请输入正确的times 和 threshold参数");
                return;
            }


            double[] imageBuffer    = GetData(initialUrbanImageLayerName, ref width, ref height);
            double[] pg             = GetData(pgLayerName, ref width, ref height);
            double[] suitableBuffer = GetData(landSuitableLayerName, ref width, ref height);

            if (imageBuffer == null || pg == null || suitableBuffer == null)
            {
                MessageBox.Show("图层名无效,图层不是栅格图层,将图层转化为GDAL dataset失败,从GDAL dataset中读取数据失败");
                return;
            }



            unsafe
            {
                fixed(double *pImageBuffer = imageBuffer, pSuitableBuffer = suitableBuffer, pPg = pg)
                {
                    // 调用c++接口得到模拟后的数据
                    SWIGTYPE_p_double result = simple_ca.simple_ca_logistic(new SWIGTYPE_p_double(new IntPtr(pImageBuffer), false),
                                                                            new SWIGTYPE_p_double(new IntPtr(pSuitableBuffer), false),
                                                                            new SWIGTYPE_p_double(new IntPtr(pPg), false),
                                                                            width,
                                                                            height, threshold, times);
                    //simple_ca.draw_image(new SWIGTYPE_p_double(new IntPtr(pImageBuffer), false), width, height);
                    //// 从指针中得到模拟结果,并纵向翻转
                    IntPtr  resultPointer = HandleRef.ToIntPtr(SWIGTYPE_p_double.getCPtr(result));
                    double *pResultImg    = (double *)resultPointer.ToPointer();

                    double[] resultImg = new double[width * height];
                    for (int row = 0; row < height; row++)
                    {
                        for (int col = 0; col < width; col++)
                        {
                            resultImg[row * width + col] = pResultImg[(height - row - 1) * width + col];
                        }
                    }

                    // 新建 GDAL dataset
                    OSGeo.GDAL.Driver  driver  = OSGeo.GDAL.Gdal.GetDriverByName("MEM");
                    OSGeo.GDAL.Dataset dataset = driver.Create("", width, height, 1, OSGeo.GDAL.DataType.GDT_Float64, null);
                    dataset.WriteRaster(0, 0, width, height, resultImg, width, height, 1, new int[1] {
                        1
                    }, 0, 0, 0);

                    // 将GDAL dataset转化为IRaster数据集
                    DotSpatial.Data.IRaster raster = GIS.GDAL.RasterConverter.Gdal2DSRaster(dataset, 1);
                    raster.Name = "Result";
                    this.Map.Layers.Add(raster);
                }
            }
        }
Beispiel #15
0
        //http://www.gisremotesensing.com/2015/09/vector-to-raster-conversion-using-gdal-c.html
        public static void Rasterize(string inputFeature, string outRaster, string fieldName, int cellSize)
        {
            // Define pixel_size and NoData value of new raster
            int          rasterCellSize   = cellSize;
            const double noDataValue      = -9999;
            string       outputRasterFile = outRaster;

            //Register the vector drivers
            Ogr.RegisterAll();
            //Reading the vector data
            DataSource dataSource = Ogr.Open(inputFeature, 0);
            var        count      = dataSource.GetLayerCount();
            Layer      layer      = dataSource.GetLayerByIndex(0);
            var        litems     = layer.GetFeatureCount(0);
            var        lname      = layer.GetName();
            Envelope   envelope   = new Envelope();

            layer.GetExtent(envelope, 0);
            //Compute the out raster cell resolutions
            int x_res = Convert.ToInt32((envelope.MaxX - envelope.MinX) / rasterCellSize);
            int y_res = Convert.ToInt32((envelope.MaxY - envelope.MinY) / rasterCellSize);

            Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY);
            Console.WriteLine("X resolution: " + x_res);
            Console.WriteLine("X resolution: " + y_res);
            //Register the raster drivers
            Gdal.AllRegister();
            //Check if output raster exists & delete (optional)
            if (File.Exists(outputRasterFile))
            {
                File.Delete(outputRasterFile);
            }
            //Create new tiff
            OSGeo.GDAL.Driver outputDriver  = Gdal.GetDriverByName("GTiff");
            Dataset           outputDataset = outputDriver.Create(outputRasterFile, x_res, y_res, 1, DataType.GDT_Float64, null);
            //Extrac srs from input feature
            string           inputShapeSrs;
            SpatialReference spatialRefrence = layer.GetSpatialRef();

            spatialRefrence.ExportToWkt(out inputShapeSrs);
            //Assign input feature srs to outpur raster
            outputDataset.SetProjection(inputShapeSrs);
            //Geotransform
            double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize };
            outputDataset.SetGeoTransform(argin);
            //Set no data
            Band band = outputDataset.GetRasterBand(1);

            band.SetNoDataValue(noDataValue);
            //close tiff
            outputDataset.FlushCache();
            outputDataset.Dispose();
            //Feature to raster rasterize layer options
            //No of bands (1)
            int[] bandlist = new int[] { 1 };
            //Values to be burn on raster (10.0)
            double[] burnValues = new double[] { 10.0 };
            Dataset  myDataset  = Gdal.Open(outputRasterFile, Access.GA_Update);

            //additional options
            string[] rasterizeOptions;
            //rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE", "ATTRIBUTE=" + fieldName }; //To set all touched pixels into raster pixel
            rasterizeOptions = new string[] { "ATTRIBUTE=" + fieldName };
            //Rasterize layer
            //Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, null, null, null); // To burn the given burn values instead of feature attributes
            Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion");
        }
Beispiel #16
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);
        }
        public static void ConvertFeatureToRaster(Layer layer, out Dataset outputDataset, double rasterCellSize, string fieldName)
        {
            DriverUtils.RegisterGdalOgrDrivers();

            Envelope envelope = new Envelope();

            layer.GetExtent(envelope, 0);

            //Compute the out raster cell resolutions
            int x_res = Convert.ToInt32((envelope.MaxX - envelope.MinX) / rasterCellSize);
            int y_res = Convert.ToInt32((envelope.MaxY - envelope.MinY) / rasterCellSize);

            //Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY);
            //Console.WriteLine("X resolution: " + x_res);
            //Console.WriteLine("X resolution: " + y_res);

            //Create new tiff in disk
            string tempRaster = "tempZoneRaster.tif";

            if (File.Exists(tempRaster))
            {
                File.Delete(tempRaster);
            }
            OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff");
            outputDataset = outputDriver.Create(tempRaster, x_res, y_res, 1, DataType.GDT_Int32, null);

            //Extrac srs from input feature
            string           inputShapeSrs;
            SpatialReference spatialRefrence = layer.GetSpatialRef();

            spatialRefrence.ExportToWkt(out inputShapeSrs);

            //Assign input feature srs to outpur raster
            outputDataset.SetProjection(inputShapeSrs);

            //Geotransform
            double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize };
            outputDataset.SetGeoTransform(argin);

            //Set no data
            Band band = outputDataset.GetRasterBand(1);

            //band.SetNoDataValue(GdalUtilConstants.NoDataValue);
            band.Fill(GdalUtilConstants.NoDataValue, 0.0);

            //Feature to raster rasterize layer options

            //No of bands (1)
            int[] bandlist = new int[] { 1 };

            //Values to be burn on raster (10.0)
            double[] burnValues = new double[] { 10.0 };
            //Dataset myDataset = Gdal.Open(outputRasterFile, Access.GA_Update);

            //additional options

            string[] rasterizeOptions;
            //rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE", "ATTRIBUTE=" + fieldName }; //To set all touched pixels into raster pixel

            rasterizeOptions = new string[] { "ATTRIBUTE=" + fieldName };

            //Rasterize layer
            //Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, null, null, null); // To burn the given burn values instead of feature attributes
            //Gdal.RasterizeLayer(outputDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion");

            Gdal.RasterizeLayer(outputDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, null, null);
        }
Beispiel #18
0
        /// <summary>
        /// 导出当前视图。
        /// </summary>
        /// <param name="InputImg">影像对象。</param>
        /// <param name="OutDataType">输出数据类型。</param>
        /// <param name="OutPath">输出路径。</param>
        /// <returns>返回操作成功或失败。</returns>
        public static bool ExportView(Img InputImg, OSGeo.GDAL.DataType OutDataType, string OutPath)
        {
            try
            {
                if (OutPath == InputImg.Path)
                {
                    throw new Exception("输出路径与源文件相同。");
                }
                if (InputImg.GDALDataset == null)
                {
                    throw new ArgumentNullException("输入数据集为空。");
                }
                if (String.IsNullOrWhiteSpace(OutPath.Trim()))
                {
                    throw new ArgumentNullException("输出路径为空或非法。");
                }
                OSGeo.GDAL.Driver Dri = OSGeo.GDAL.Gdal.GetDriverByName("Gtiff");
                if (Dri == null)
                {
                    throw new Exception("无法获取GDAL Driver。");
                }

                int xSize   = InputImg.Width;
                int ySize   = InputImg.Height;
                int Bandnum = 3;
                if (InputImg.IsGrayscale)
                {
                    Bandnum = 1;
                }
                else
                {
                    Bandnum = 3;
                }

                FrmProgress FP = new FrmProgress()
                {
                    Text = "正在导出影像...",
                };

                Thread t = new Thread(() =>
                {
                    FP.ShowDialog();
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();

                OSGeo.GDAL.Dataset DS = Dri.Create(OutPath, xSize, ySize, Bandnum, OutDataType, null);
                FP.Output("已创建输出数据集\"" + OutPath + "\",数据类型为" + OutDataType.ToString() + "。");
                Tools.Common.CopyMetadata(InputImg.GDALDataset, DS);

                for (int i = 0; i < Bandnum; i++) //遍历每个波段
                {
                    FP.SetProgress2("正在处理", i + 1, Bandnum, "波段");
                    for (int Row = 0; Row < ySize; Row++)   //遍历每一行(y)
                    {
                        FP.SetProgress1("正在处理", Row + 1, ySize, "行");
                        double[] Values = new double[xSize];

                        //读取DN到数组
                        InputImg.GDALDataset.GetRasterBand(InputImg.BandList[i]).ReadRaster(0, Row, xSize, 1, Values, xSize, 1, 0, 0);
                        for (int Col = 0; Col < xSize; Col++)   //对每一个值进行计算
                        {
                            //无拉伸不做处理直接导出
                            if (InputImg.StretchMode == 0)
                            {
                                break;
                            }
                            //已经有处理的情形
                            switch (InputImg.StretchMode)
                            {
                            case 1:
                            case 2:
                            case 3:
                            case 4:
                            case 9:
                            {
                                //线性
                                Values[Col] = LinearStretch(Values[Col], InputImg.StretchInfo[InputImg.BandList[i] - 1].Item1, InputImg.StretchInfo[InputImg.BandList[i] - 1].Item2);
                                break;
                            }

                            case 5:
                            {
                                //直方图均衡
                                Values[Col] = EqualizationStretch(Values[Col], InputImg.CumulativeProbability[InputImg.BandList[i] - 1], InputImg.Min[InputImg.BandList[i] - 1], InputImg.Max[InputImg.BandList[i] - 1]);
                                break;
                            }

                            case 6:
                            {
                                //高斯
                                Values[Col] = GaussianStretch(Values[Col], InputImg.CumulativeProbability[InputImg.BandList[i] - 1], InputImg.Min[InputImg.BandList[i] - 1], InputImg.Mean[InputImg.BandList[i] - 1], InputImg.Stddev[InputImg.BandList[i] - 1]);
                                break;
                            }

                            case 7:
                            {
                                //平方根
                                Values[Col] = SquarerootStretch(Values[Col], InputImg.Min[InputImg.BandList[i] - 1], InputImg.Max[InputImg.BandList[i] - 1]);
                                break;
                            }

                            case 8:
                            {
                                //对数
                                Values[Col] = LinearStretch(LogarithmicStretch(Values[Col], InputImg.Min[InputImg.BandList[i] - 1], InputImg.Max[InputImg.BandList[i] - 1]), InputImg.Min[InputImg.BandList[i] - 1], InputImg.Max[InputImg.BandList[i] - 1]);
                                break;
                            }

                            default:
                            {
                                //不是0-9还能是啥?
                                throw new Exception("拉伸模式未知。");
                            }
                            }
                        }
                        //写结果到新栅格
                        DS.GetRasterBand(i + 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();
                DS.Dispose();
                return(true);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return(false);
            }
        }
Beispiel #19
0
        static void Main(string[] args)
        {
            //verify gdal dlls are available; see: http://trac.osgeo.org/gdal/wiki/GdalOgrCsharpUsage
            string GDAL_HOME = @";c:\Program Files (x86)\FWTools2.4.7\bin";
            string path      = Environment.GetEnvironmentVariable("PATH");

            path += ";" + GDAL_HOME;
            SetEnvironmentVariable("PATH", path);

            // string tifDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string tifDirectory    = @"C:\dev\quito\For_Geoportal\For_Geoportal\Climate\";
            string resultDirectory = tifDirectory + @"out\";

            if (args.Length == 1)
            {
                tifDirectory = args[0];
            }

            Gdal.AllRegister();
            OSGeo.GDAL.Driver srcDrv = Gdal.GetDriverByName("GTiff");

            //if app scales, or additional implementations made (e.g. database table), use DI for this dependency
            IColorRepository colorRepo = new ColorRepository();

            //see: http://sharpmap.codeplex.com/discussions/421752
            GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();

            Console.WriteLine(tifDirectory);
            DirectoryInfo di        = new DirectoryInfo(tifDirectory);
            DirectoryInfo resultDir = new DirectoryInfo(resultDirectory);

            if (!resultDir.Exists)
            {
                resultDir.Create();
            }
            Console.WriteLine("Processing {0} files matching '*.tif'");
            foreach (FileInfo fi in di.GetFiles("*.tif"))
            {
                Console.WriteLine("Processing " + fi.Name + "...");

                //read data from source raster
                Dataset src   = Gdal.Open(fi.FullName, Access.GA_ReadOnly);
                Band    band  = src.GetRasterBand(1);
                float[] r     = new float[band.XSize * band.YSize];
                byte[]  red   = new byte[band.XSize * band.YSize];
                byte[]  green = new byte[band.XSize * band.YSize];
                byte[]  blue  = new byte[band.XSize * band.YSize];
                band.ReadRaster(0, 0, band.XSize, band.YSize, r, band.XSize, band.YSize, 0, 0);

                //assign values to rgb rasters to produce new raster with rgb bands matching color pattern
                for (int cell = 0; cell < r.Length; cell++)
                {
                    RGBColors colors = colorRepo.ColorsOfValueInFile(fi.Name, r[cell]);
                    red[cell]   = (byte)colors.Red;
                    green[cell] = (byte)colors.Green;
                    blue[cell]  = (byte)colors.Blue;
                }

                //write new output
                using (Dataset output = srcDrv.Create(resultDirectory + fi.Name, band.XSize, band.YSize, 3, DataType.GDT_Byte, null))
                {
                    if (output == null)
                    {
                        Console.WriteLine("Can't create " + args[0]);
                        System.Environment.Exit(-1);
                    }
                    //set metadata
                    output.SetProjection(src.GetProjection());
                    double[] geotransform = new double[0];
                    src.GetGeoTransform(geotransform);
                    output.SetGeoTransform(geotransform);

                    //prepare data for write
                    int[] colorData = new int[red.Length * 3];
                    red.CopyTo(colorData, 0);
                    green.CopyTo(colorData, red.Length);
                    blue.CopyTo(colorData, red.Length + green.Length);

                    //write data to disk
                    output.WriteRaster(0, 0, band.XSize, band.YSize, colorData, band.XSize, band.YSize, 3, null, 0, 0, 0);
                    output.FlushCache();
                }
            }
        }
Beispiel #20
0
        public static void Rasterize(string inputFeature, string referRaster, string outRaster, string fieldName = "")
        {
            //Register the raster drivers
            Gdal.AllRegister();
            //Register the vector drivers
            Ogr.RegisterAll();
            //Open referRaster
            Dataset _referRaster = Gdal.Open(referRaster, Access.GA_ReadOnly);

            //Get geoTransform Args
            double[] _geoTransform = new double[6];
            _referRaster.GetGeoTransform(_geoTransform);
            // Define pixel_size and NoData value of new raster
            //int rasterCellSize = cellSize;
            double _xCellSize = _geoTransform[1];
            double _yCellSize = -_geoTransform[5];

            const double noDataValue      = -10000;
            string       outputRasterFile = outRaster;

            //Reading the vector data
            DataSource dataSource = Ogr.Open(inputFeature, 0);
            Layer      layer      = dataSource.GetLayerByIndex(0);
            Envelope   envelope   = new Envelope();

            layer.GetExtent(envelope, 0);
            //Compute the out raster cell resolutions
            int x_res = _referRaster.RasterXSize;
            int y_res = _referRaster.RasterYSize;

            //Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY);
            //Console.WriteLine("X resolution: " + x_res);
            //Console.WriteLine("X resolution: " + y_res);

            //Check if output raster exists & delete (optional)
            if (File.Exists(outputRasterFile))
            {
                File.Delete(outputRasterFile);
            }
            //Create new tiff
            DataType dType = _referRaster.GetRasterBand(1).DataType;

            //OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff");
            OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("HFA");

            Dataset outputDataset = outputDriver.Create(outputRasterFile, x_res, y_res, 1, dType, null);
            //Extrac srs from input feature
            string inputShapeSrs;

            OSGeo.OSR.SpatialReference spatialRefrence = layer.GetSpatialRef();
            if (spatialRefrence != null)
            {
                spatialRefrence.ExportToWkt(out inputShapeSrs);
                //Assign input feature srs to outpur raster
                outputDataset.SetProjection(inputShapeSrs);
            }
            //Geotransform
            outputDataset.SetGeoTransform(_geoTransform);
            //Set no data
            Band band = outputDataset.GetRasterBand(1);

            band.SetNoDataValue(noDataValue);
            //close tiff
            outputDataset.FlushCache();
            outputDataset.Dispose();
            //Feature to raster rasterize layer options
            //No of bands (1)
            int[] bandlist = new int[] { 1 };
            //Values to be burn on raster (10.0)
            double[] burnValues = new double[] { 10.0 };
            Dataset  myDataset  = Gdal.Open(outputRasterFile, Access.GA_Update);

            //additional options
            string[] rasterizeOptions;
            //rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE", "ATTRIBUTE=" + fieldName }; //To set all touched pixels into raster pixel
            //rasterizeOptions = new string[] { "ATTRIBUTE=" + fieldName };
            rasterizeOptions = new string[] { };
            //Rasterize layer
            //Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, null, null, null); // To burn the given burn values instead of feature attributes
            //Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion");
            Gdal.RasterizeLayer(
                myDataset, //NEW inDS
                1,         //BAND
                bandlist,  //int[] bandlist = new int[] { 1 };Band数量
                layer,     // 待转
                IntPtr.Zero,
                IntPtr.Zero,
                1,
                burnValues, //Values to be burn on raster (10.0)
                rasterizeOptions,
                new Gdal.GDALProgressFuncDelegate(ProgressFunc),
                "Raster conversion"
                );
            myDataset.FlushCache();
            myDataset.Dispose();
        }
Beispiel #21
0
        private void buttonOk_Click(object sender, EventArgs e)
        {
            double a0 = 0.0;
            double a1 = 0.0;
            int    moorNeighbourSize      = 0;
            double tSpon                  = 0.0;
            double sigma                  = 0.0;
            int    amountOfNewDevelopment = 0;


            // 读取配置参数
            try
            {
                a0 = Convert.ToDouble(this.textBoxA0.Text);
                a1 = Convert.ToDouble(this.textBoxA1.Text);
                moorNeighbourSize      = Convert.ToInt32(this.textBoxMoorNeighbourSize.Text);
                tSpon                  = Convert.ToDouble(this.textBoxTSpon.Text);
                sigma                  = Convert.ToDouble(this.textBoxSigma.Text);
                amountOfNewDevelopment = Convert.ToInt32(this.textBoxAmountOfNewDevelopment.Text);
            }
            catch
            {
                MessageBox.Show("请输入正确的a0 ,a1 , moorNeighbourSize, tSpon, sigma, amountOfNewDevelopment参数");
                return;
            }

            // 读取图层
            if (this.comboBoxInitialData.SelectedItem == null || this.comboBoxPgData.SelectedItem == null || this.comboBoxSuitableData.SelectedItem == null)
            {
                MessageBox.Show("清先选择图层");
                return;
            }
            string initialUrbanImageLayerName = this.comboBoxInitialData.SelectedItem.ToString();
            string pgLayerName           = this.comboBoxPgData.SelectedItem.ToString();
            string landSuitableLayerName = this.comboBoxSuitableData.SelectedItem.ToString();

            int width  = 0;
            int height = 0;

            double[] imageBuffer    = GetData(initialUrbanImageLayerName, ref width, ref height);
            double[] suitableBuffer = GetData(landSuitableLayerName, ref width, ref height);
            double[] pg             = GetData(pgLayerName, ref width, ref height);

            if (imageBuffer == null || pg == null || suitableBuffer == null)
            {
                MessageBox.Show("图层名无效,图层不是栅格图层,将图层转化为GDAL dataset失败,从GDAL dataset中读取数据失败");
                return;
            }

            //this.logisticPatchCa = new LogisticPatchCa(a0,
            //    a1,
            //    moorNeighbourSize,
            //    tSpon,
            //    sigma,
            //    height,
            //    width,
            //    imageBuffer,
            //    pg,
            //    suitableBuffer,
            //    amountOfNewDevelopment);

            //this.SetSimulate();
            //this.SetProgressBar();
            unsafe
            {
                fixed(double *pImageBuffer = imageBuffer, pSuitableBuffer = suitableBuffer, pPg = pg)
                {
                    // 调用c++接口得到模拟后的数据
                    SWIGTYPE_p_double result = logistic_patch_ca.logistic_patch_ca1(
                        a0,
                        a1,
                        moorNeighbourSize,
                        tSpon,
                        sigma,
                        height,
                        width,
                        new SWIGTYPE_p_double(new IntPtr(pImageBuffer), false),
                        new SWIGTYPE_p_double(new IntPtr(pSuitableBuffer), false),
                        new SWIGTYPE_p_double(new IntPtr(pPg), false),
                        amountOfNewDevelopment,
                        true);
                    // 从指针中得到模拟结果,并纵向翻转
                    IntPtr  resultPointer = HandleRef.ToIntPtr(SWIGTYPE_p_double.getCPtr(result));
                    double *pResultImg    = (double *)resultPointer.ToPointer();

                    double[] resultImg = new double[width * height];
                    for (int row = 0; row < height; row++)
                    {
                        for (int col = 0; col < width; col++)
                        {
                            resultImg[row * width + col] = pResultImg[(height - row - 1) * width + col];
                        }
                    }

                    // 新建 GDAL dataset
                    OSGeo.GDAL.Driver  driver  = OSGeo.GDAL.Gdal.GetDriverByName("MEM");
                    OSGeo.GDAL.Dataset dataset = driver.Create("", width, height, 1, OSGeo.GDAL.DataType.GDT_Float64, null);
                    dataset.WriteRaster(0, 0, width, height, resultImg, width, height, 1, new int[1] {
                        1
                    }, 0, 0, 0);

                    // 将GDAL dataset转化为IRaster数据集
                    DotSpatial.Data.IRaster raster = GIS.GDAL.RasterConverter.Gdal2DSRaster(dataset, 1);
                    raster.Name = "Result";
                    this.Map.Layers.Add(raster);
                }
            }
        }
Beispiel #22
0
        /// <summary>
        /// Brovey变换融合。融合前会将多光谱数据集的每一波段与全色数据集做直方图匹配。
        /// </summary>
        /// <param name="PanDS">全色数据集。</param>
        /// <param name="MSDS">多光谱数据集</param>
        /// <param name="PanCumu">全色数据集的累积概率表。</param>
        /// <param name="MSCumu">多光谱数据集的累积概率表。</param>
        /// <param name="MSBandList">多光谱数据集的波段组合。</param>
        /// <param name="OutDataType">输出数据类型。</param>
        /// <param name="OutPath">输出路径。</param>
        /// <returns>返回操作成功或失败。</returns>
        public static bool Brovey(OSGeo.GDAL.Dataset PanDS, OSGeo.GDAL.Dataset MSDS, double[][] PanCumu, double[][] MSCumu, int[] MSBandList, OSGeo.GDAL.DataType OutDataType, string OutPath)
        {
            try
            {
                if (PanDS == null)
                {
                    throw new ArgumentNullException("输入的全色数据集为空。");
                }
                if (PanDS.RasterCount > 1)
                {
                    throw new RankException("全色数据集波段大于1。");
                }
                if (MSDS == null)
                {
                    throw new ArgumentNullException("输入的多光谱数据集为空。");
                }
                if (String.IsNullOrWhiteSpace(OutPath.Trim()))
                {
                    throw new ArgumentNullException("输出路径为空或非法。");
                }
                OSGeo.GDAL.Driver Dri = OSGeo.GDAL.Gdal.GetDriverByName("Gtiff");
                if (Dri == null)
                {
                    throw new Exception("无法获取GDAL Driver。");
                }

                int panWidth  = PanDS.RasterXSize;
                int panHeight = PanDS.RasterYSize;
                int msWidth   = MSDS.RasterXSize;
                int msHeight  = MSDS.RasterYSize;
                //int rat = (int)Math.Ceiling((double)panHeight / msHeight);

                if (panWidth < msWidth)
                {
                    throw new RankException("全色数据集宽度小于多光谱数据集宽度。");
                }
                if (panHeight < msHeight)
                {
                    throw new RankException("全色数据集高度小于多光谱数据集高度。");
                }
                //if (rat <= 0)
                //    throw new ArgumentOutOfRangeException("全色高度:多光谱高度小于1。");

                FrmProgress FP = new FrmProgress()
                {
                    Text = "正在进行Brovey融合...",
                };

                Thread t = new Thread(() =>
                {
                    FP.ShowDialog();
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();

                #region 预处理
                //创建临时文件,进行直方图匹配。
                string             HisMatFileName = Tools.Common.GetTempFileName();
                OSGeo.GDAL.Dataset MatchingDS     = Dri.CreateCopy(HisMatFileName, MSDS, 0, null, null, null);
                FP.Output("已创建直方图匹配临时文件\"" + HisMatFileName + "\"");
                for (int band = 1; band <= MSDS.RasterCount; band++)
                {
                    FP.Output("正在进行直方图匹配(" + band.ToString() + "/" + MSDS.RasterCount.ToString() + ")...");
                    Band b = Tools.BaseProcess.HistogramMatching(MSDS.GetRasterBand(band), PanDS.GetRasterBand(1), MSCumu[band - 1], PanCumu[0], OutDataType);
                    if (b == null)
                    {
                        throw new ArgumentNullException("直方图匹配返回波段为空。");
                    }
                    for (int row = 0; row < b.YSize; row++)
                    {
                        double[] tmp = new double[b.XSize];
                        b.ReadRaster(0, row, b.XSize, 1, tmp, b.XSize, 1, 0, 0);
                        MatchingDS.GetRasterBand(band).WriteRaster(0, row, MatchingDS.GetRasterBand(band).XSize, 1, tmp, MatchingDS.GetRasterBand(band).XSize, 1, 0, 0);
                        MatchingDS.FlushCache();
                        Thread.Sleep(1);
                    }
                }

                //创建临时文件,进行重采样。
                double[]           resamptmp;
                string             ResampFileName = Tools.Common.GetTempFileName();
                OSGeo.GDAL.Dataset ResampDS       = Dri.Create(ResampFileName, panWidth, panHeight, 3, MSDS.GetRasterBand(1).DataType, null);
                FP.Output("已创建重采样临时文件\"" + ResampFileName + "\"");
                //Tools.Common.CopyMetadata(PanDS, tmpDS);
                //Gdal.ReprojectImage(MSDS, tmpDS, null, null, 0, 0, 0, null, null);

                //将直方图匹配后的图像重采样。
                for (int i = 0; i < 3; i++)
                {
                    FP.Output("正在对直方图匹配后影像进行重采样(" + (i + 1).ToString() + "/" + "3)...");
                    resamptmp = new double[panWidth * panHeight];
                    MatchingDS.GetRasterBand(MSBandList[i]).ReadRaster(0, 0, msWidth, msHeight, resamptmp, panWidth, panHeight, 0, 0);
                    ResampDS.GetRasterBand(i + 1).WriteRaster(0, 0, panWidth, panHeight, resamptmp, panWidth, panHeight, 0, 0);
                    ResampDS.FlushCache();
                    Thread.Sleep(1);
                    if (FP.Canceled)
                    {
                        Thread.Sleep(500);

                        FP.Finish();
                        throw new OperationCanceledException("操作被用户取消。");
                    }
                }
                //释放直方图匹配图像资源。
                MatchingDS.Dispose();
                if (File.Exists(HisMatFileName))
                {
                    File.Delete(HisMatFileName);
                }
                FP.Output("已删除直方图匹配临时文件");
                #endregion

                //创建输出数据集。
                OSGeo.GDAL.Dataset DS = Dri.Create(OutPath, panWidth, panHeight, 3, OutDataType, null);
                FP.Output("已创建输出数据集\"" + OutPath + "\",数据类型为" + OutDataType.ToString() + "。");
                Tools.Common.CopyMetadata(PanDS, DS);

                double[]   p;
                double[][] ms;
                double[]   ds;

                for (int row = 0; row < panHeight; row++)
                {
                    FP.SetProgress2("正在处理", row + 1, panHeight, "行");
                    //将全色波段行读取进数组。
                    p = new double[panWidth];
                    PanDS.GetRasterBand(1).ReadRaster(0, row, panWidth, 1, p, panWidth, 1, 0, 0);

                    //将重采样后的多光谱行读取进数组。
                    ms    = new double[3][];
                    ms[0] = new double[panWidth];
                    ms[1] = new double[panWidth];
                    ms[2] = new double[panWidth];
                    ResampDS.GetRasterBand(1).ReadRaster(0, row, panWidth, 1, ms[0], panWidth, 1, 0, 0);
                    ResampDS.GetRasterBand(2).ReadRaster(0, row, panWidth, 1, ms[1], panWidth, 1, 0, 0);
                    ResampDS.GetRasterBand(3).ReadRaster(0, row, panWidth, 1, ms[2], panWidth, 1, 0, 0);

                    //遍历三波段
                    for (int bandcount = 0; bandcount < 3; bandcount++)
                    {
                        FP.SetProgress1("正在处理", bandcount + 1, 3, "波段");
                        ds = new double[panWidth];  //临时数组
                        for (long i = 0; i < panWidth; i++)
                        {
                            ds[i] = p[i] * ms[bandcount][i] / (ms[0][i] + ms[1][i] + ms[2][i]);                  //Brovey公式
                        }
                        DS.GetRasterBand(bandcount + 1).WriteRaster(0, row, panWidth, 1, ds, panWidth, 1, 0, 0); //计算完后的数据写入
                        DS.FlushCache();
                    }
                    Thread.Sleep(1);
                    if (FP.Canceled)
                    {
                        Thread.Sleep(500);

                        FP.Finish();
                        throw new OperationCanceledException("操作被用户取消。");
                    }
                }
                //释放重采样图像资源。
                ResampDS.Dispose();
                if (File.Exists(ResampFileName))
                {
                    File.Delete(ResampFileName);
                }
                FP.Output("已删除重采样临时文件");

                /*一次性读取一时爽,遇到大文件爆内存。
                *  一次性读取一时爽,遇到大文件爆内存。
                *  一次性读取一时爽,遇到大文件爆内存。*/
                //p = new double[panWidth * panHeight];
                //ms = new double[3][];
                //ms[0] = new double[panWidth * panHeight];
                //ms[1] = new double[panWidth * panHeight];
                //ms[2] = new double[panWidth * panHeight];

                //PanDS.GetRasterBand(1).ReadRaster(0, 0, panWidth, panHeight, p, panWidth, panHeight, 0, 0);
                //MSDS.GetRasterBand(MSBandList[0]).ReadRaster(0, 0, msWidth, msHeight, ms[0], panWidth, panHeight, 0, 0);
                //MSDS.GetRasterBand(MSBandList[1]).ReadRaster(0, 0, msWidth, msHeight, ms[1], panWidth, panHeight, 0, 0);
                //MSDS.GetRasterBand(MSBandList[2]).ReadRaster(0, 0, msWidth, msHeight, ms[2], panWidth, panHeight, 0, 0);

                //for (int bandcount = 0; bandcount < 3; bandcount++)
                //{
                //    ds = new double[panWidth * panHeight];
                //    for (int row = 0; row < panHeight; row++)
                //    {
                //        for (int col = 0; col < panWidth; col++)
                //        {
                //            ds[row * panWidth + col] = p[row * panWidth + col] * ms[bandcount][row * panWidth + col] / (ms[0][row * panWidth + col] + ms[1][row * panWidth + col] + ms[2][row * panWidth + col]);
                //        }
                //    }
                //    DS.GetRasterBand(bandcount + 1).WriteRaster(0, 0, panWidth, panHeight, ds, panWidth, panHeight, 0, 0);
                //    DS.FlushCache();
                //}

                FP.Finish();
                Dri.Dispose();
                DS.Dispose();
                return(true);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                return(false);
            }
        }
Beispiel #23
0
        /// <summary>
        /// IHS融合。融合前会将全色数据集与多光谱数据集转换到IHS空间下的I波段做直方图匹配。
        /// </summary>
        /// <param name="PanDS">全色数据集。</param>
        /// <param name="MSDS">多光谱数据集</param>
        /// <param name="PanCumu">全色数据集的累积概率表。</param>
        /// <param name="MSCumu">多光谱数据集的累积概率表。</param>
        /// <param name="MSBandList">多光谱数据集的波段组合。</param>
        /// <param name="OutDataType">输出数据类型。</param>
        /// <param name="OutPath">输出路径。</param>
        /// <returns>返回操作成功或失败。</returns>
        public static bool IHSFusion(OSGeo.GDAL.Dataset PanDS, OSGeo.GDAL.Dataset MSDS, double[][] PanCumu, double[][] MSCumu, int[] MSBandList, OSGeo.GDAL.DataType OutDataType, string OutPath)
        {
            try
            {
                if (PanDS == null)
                {
                    throw new ArgumentNullException("输入的全色数据集为空。");
                }
                if (PanDS.RasterCount > 1)
                {
                    throw new RankException("全色数据集波段大于1。");
                }
                if (MSDS == null)
                {
                    throw new ArgumentNullException("输入的多光谱数据集为空。");
                }
                if (String.IsNullOrWhiteSpace(OutPath.Trim()))
                {
                    throw new ArgumentNullException("输出路径为空或非法。");
                }
                OSGeo.GDAL.Driver Dri = OSGeo.GDAL.Gdal.GetDriverByName("Gtiff");
                if (Dri == null)
                {
                    throw new Exception("无法获取GDAL Driver。");
                }

                int panWidth  = PanDS.RasterXSize;
                int panHeight = PanDS.RasterYSize;
                int msWidth   = MSDS.RasterXSize;
                int msHeight  = MSDS.RasterYSize;

                if (panWidth < msWidth)
                {
                    throw new RankException("全色数据集宽度小于多光谱数据集宽度。");
                }
                if (panHeight < msHeight)
                {
                    throw new RankException("全色数据集高度小于多光谱数据集高度。");
                }

                FrmProgress FP = new FrmProgress()
                {
                    Text = "正在进行IHS融合...",
                };

                Thread t = new Thread(() =>
                {
                    FP.ShowDialog();
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();

                #region 预处理
                //创建临时文件,进行重采样。
                double[]           resamptmp;
                string             ResampFileName = Tools.Common.GetTempFileName();
                OSGeo.GDAL.Dataset ResampDS       = Dri.Create(ResampFileName, panWidth, panHeight, MSDS.RasterCount, MSDS.GetRasterBand(1).DataType, null);
                FP.Output("已创建重采样临时文件\"" + ResampFileName + "\"");

                //将多光谱影像重采样。
                for (int i = 0; i < 3; i++)
                {
                    FP.Output("正在进行重采样(" + (i + 1).ToString() + "/" + "3)...");
                    resamptmp = new double[panWidth * panHeight];
                    MSDS.GetRasterBand(MSBandList[i]).ReadRaster(0, 0, msWidth, msHeight, resamptmp, panWidth, panHeight, 0, 0);
                    ResampDS.GetRasterBand(i + 1).WriteRaster(0, 0, panWidth, panHeight, resamptmp, panWidth, panHeight, 0, 0);
                    ResampDS.FlushCache();
                    Thread.Sleep(1);
                    if (FP.Canceled)
                    {
                        Thread.Sleep(500);

                        FP.Finish();
                        throw new OperationCanceledException("操作被用户取消。");
                    }
                }

                double[] BandMax = new double[3];
                double[] BandMin = new double[3];
                for (int i = 0; i < 3; i++)
                {
                    FP.Output("正在计算重采样影像统计数据(" + (i + 1).ToString() + "/" + "3)...");
                    Common.ComputeRasterMinMax(ResampDS.GetRasterBand(i + 1), out BandMin[i], out BandMax[i]);
                }
                #endregion

                //多光谱RGB转IHS。
                string             MSIHSFileName = Tools.Common.GetTempFileName();
                OSGeo.GDAL.Dataset MSIHSDS       = Dri.Create(MSIHSFileName, panWidth, panHeight, 3, OSGeo.GDAL.DataType.GDT_Float32, null);
                FP.Output("已创建IHS临时文件\"" + MSIHSFileName + "\"");

                //按行读入,进行拉伸后再IHS转换,写入。
                for (int row = 0; row < panHeight; row++)
                {
                    FP.SetProgress1("正在处理IHS临时文件(", row + 1, panHeight, ")");
                    double[]   iArr     = new double[panWidth];
                    double[]   hArr     = new double[panWidth];
                    double[]   sArr     = new double[panWidth];
                    double[][] MSRGBArr = new double[3][];
                    MSRGBArr[0] = new double[panWidth];
                    MSRGBArr[1] = new double[panWidth];
                    MSRGBArr[2] = new double[panWidth];
                    //读。
                    ResampDS.GetRasterBand(1).ReadRaster(0, row, panWidth, 1, MSRGBArr[0], panWidth, 1, 0, 0);
                    ResampDS.GetRasterBand(2).ReadRaster(0, row, panWidth, 1, MSRGBArr[1], panWidth, 1, 0, 0);
                    ResampDS.GetRasterBand(3).ReadRaster(0, row, panWidth, 1, MSRGBArr[2], panWidth, 1, 0, 0);
                    //按行逐像元转IHS。
                    for (long i = 0; i < panWidth; i++)
                    {
                        double[] tmp = Common.RGB2IHS(
                            new double[3] {
                            Common.LinearStretchDouble(MSRGBArr[0][i], BandMin[0], BandMax[0]),
                            Common.LinearStretchDouble(MSRGBArr[1][i], BandMin[1], BandMax[1]),
                            Common.LinearStretchDouble(MSRGBArr[2][i], BandMin[2], BandMax[2])
                        });
                        iArr[i] = tmp[0]; hArr[i] = tmp[1]; sArr[i] = tmp[2];
                    }
                    //写。
                    MSIHSDS.GetRasterBand(1).WriteRaster(0, row, panWidth, 1, iArr, panWidth, 1, 0, 0);
                    MSIHSDS.GetRasterBand(2).WriteRaster(0, row, panWidth, 1, hArr, panWidth, 1, 0, 0);
                    MSIHSDS.GetRasterBand(3).WriteRaster(0, row, panWidth, 1, sArr, panWidth, 1, 0, 0);
                    MSIHSDS.FlushCache();
                    Thread.Sleep(1);
                    if (FP.Canceled)
                    {
                        Thread.Sleep(500);

                        FP.Finish();
                        throw new OperationCanceledException("操作被用户取消。");
                    }
                }
                //释放重采样数据集资源。
                ResampDS.Dispose();
                if (File.Exists(ResampFileName))
                {
                    File.Delete(ResampFileName);
                }
                FP.Output("已删除重采样临时文件");

                //全色匹配多光谱I
                FP.Output("正在计算IHS统计数据...");
                Common.GetStatistics(MSIHSDS.GetRasterBand(1), out double Imin, out double Imax, out double Imean, out double Istddev);   //多光谱统计信息。
                int[] IHis = new int[(int)(Imax - Imin) + 1];
                MSIHSDS.GetRasterBand(1).GetHistogram(Imin, Imax, (int)(Imax - Imin) + 1, IHis, 1, 0, null, null);
                double[] ICumu = new double[IHis.Length];
                for (int i = 0; i < IHis.Length; i++)
                {
                    ICumu[i] = (double)IHis[i] / (MSIHSDS.RasterXSize * MSIHSDS.RasterYSize);
                    if (i > 0)
                    {
                        ICumu[i] += ICumu[i - 1];   //累积概率
                    }
                }
                FP.Output("正在进行I波段直方图匹配...");
                Band PanMatIBand = Tools.BaseProcess.HistogramMatching(PanDS.GetRasterBand(1), MSIHSDS.GetRasterBand(1), PanCumu[0], ICumu, OSGeo.GDAL.DataType.GDT_Float32);
                if (PanMatIBand == null)
                {
                    throw new ArgumentNullException("直方图匹配返回波段为空。");
                }
                //全色融合。
                //创建输出数据集。
                OSGeo.GDAL.Dataset DS = Dri.Create(OutPath, panWidth, panHeight, 3, OutDataType, null);
                FP.Output("已创建输出数据集\"" + OutPath + "\",数据类型为" + OutDataType.ToString() + "。");
                Tools.Common.CopyMetadata(PanDS, DS);
                for (int row = 0; row < panHeight; row++)
                {
                    FP.SetProgress1("正在融合(", row + 1, panHeight, ")");
                    //读取匹配多光谱I后的全色波段。
                    double[] Itmp = new double[panWidth];
                    PanMatIBand.ReadRaster(0, row, panWidth, 1, Itmp, panWidth, 1, 0, 0);

                    //读入IHS变换后的多光谱影像的H、S波段。
                    double[] Htmp = new double[panWidth];
                    double[] Stmp = new double[panWidth];
                    MSIHSDS.GetRasterBand(2).ReadRaster(0, row, panWidth, 1, Htmp, panWidth, 1, 0, 0);
                    MSIHSDS.GetRasterBand(3).ReadRaster(0, row, panWidth, 1, Stmp, panWidth, 1, 0, 0);

                    double[] Rtmp = new double[panWidth];
                    double[] Gtmp = new double[panWidth];
                    double[] Btmp = new double[panWidth];
                    for (long i = 0; i < panWidth; i++)
                    {
                        double[] tmp = Common.IHS2RGB(new double[3] {
                            Itmp[i], Htmp[i], Stmp[i]
                        });
                        Rtmp[i] = tmp[0]; Gtmp[i] = tmp[1]; Btmp[i] = tmp[2];
                    }
                    DS.GetRasterBand(1).WriteRaster(0, row, panWidth, 1, Rtmp, panWidth, 1, 0, 0);
                    DS.GetRasterBand(2).WriteRaster(0, row, panWidth, 1, Gtmp, panWidth, 1, 0, 0);
                    DS.GetRasterBand(3).WriteRaster(0, row, panWidth, 1, Btmp, panWidth, 1, 0, 0);
                    DS.FlushCache();
                    Thread.Sleep(1);
                    if (FP.Canceled)
                    {
                        Thread.Sleep(500);

                        FP.Finish();
                        throw new OperationCanceledException("操作被用户取消。");
                    }
                }
                //释放IHS图像资源。
                MSIHSDS.Dispose();
                PanMatIBand.Dispose();
                if (File.Exists(MSIHSFileName))
                {
                    File.Delete(MSIHSFileName);
                }
                FP.Output("已删除IHS临时文件");

                FP.Finish();
                Dri.Dispose();
                DS.Dispose();
                return(true);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
                return(false);
            }
        }
Beispiel #24
0
        /// <summary>
        /// RGB转灰度显示。
        /// </summary>
        /// <param name="InputImg">影像对象。</param>
        /// <param name="RGBWeight">RGB权重数组,长度为3。</param>
        /// <param name="OutDataType">输出数据类型。</param>
        /// <param name="OutPath">输出路径。</param>
        /// <returns>返回操作成功或失败。</returns>
        public static bool RGB2GrayScale(Img InputImg, double[] RGBWeight, OSGeo.GDAL.DataType OutDataType, string OutPath)
        {
            try
            {
                if (OutPath == InputImg.Path)
                {
                    throw new Exception("输出路径与源文件相同。");
                }
                if (InputImg.GDALDataset == null)
                {
                    throw new ArgumentNullException("输入数据集为空。");
                }
                if (String.IsNullOrWhiteSpace(OutPath.Trim()))
                {
                    throw new ArgumentNullException("输出路径为空或非法。");
                }
                if (RGBWeight.Length != 3)
                {
                    throw new ArgumentException("权重数组长度无效,应为3。");
                }
                if (Math.Abs(RGBWeight[0] + RGBWeight[1] + RGBWeight[2] - 1) > 1E-7)
                {
                    throw new ArgumentOutOfRangeException("RGB分量权重设置错误。总和应为1。");
                }
                OSGeo.GDAL.Driver Dri = OSGeo.GDAL.Gdal.GetDriverByName("Gtiff");
                if (Dri == null)
                {
                    throw new Exception("无法获取GDAL Driver。");
                }

                int xSize = InputImg.Width;
                int ySize = InputImg.Height;

                FrmProgress FP = new FrmProgress()
                {
                    Text = "正在导出灰度影像...",
                };

                Thread t = new Thread(() =>
                {
                    FP.ShowDialog();
                });
                t.SetApartmentState(ApartmentState.STA);
                t.Start();

                OSGeo.GDAL.Dataset DS = Dri.Create(OutPath, xSize, ySize, 1, OutDataType, null);
                FP.Output("已创建输出数据集\"" + OutPath + "\",数据类型为" + OutDataType.ToString() + "。");
                Tools.Common.CopyMetadata(InputImg.GDALDataset, DS);

                for (int Row = 0; Row < ySize; Row++)
                {
                    FP.SetProgress1("正在处理", Row + 1, ySize, "行");
                    double[] RValues    = new double[xSize];
                    double[] GValues    = new double[xSize];
                    double[] BValues    = new double[xSize];
                    double[] GrayValues = new double[xSize];

                    InputImg.GDALDataset.GetRasterBand(InputImg.BandList[0]).ReadRaster(0, Row, xSize, 1, RValues, xSize, 1, 0, 0);
                    InputImg.GDALDataset.GetRasterBand(InputImg.BandList[1]).ReadRaster(0, Row, xSize, 1, GValues, xSize, 1, 0, 0);
                    InputImg.GDALDataset.GetRasterBand(InputImg.BandList[2]).ReadRaster(0, Row, xSize, 1, BValues, xSize, 1, 0, 0);
                    for (int Col = 0; Col < xSize; Col++)
                    {
                        //拉伸到0~255后再加权计算。
                        RValues[Col]    = LinearStretch(RValues[Col], InputImg.Min[InputImg.BandList[0] - 1], InputImg.Max[InputImg.BandList[0] - 1]);
                        GValues[Col]    = LinearStretch(GValues[Col], InputImg.Min[InputImg.BandList[1] - 1], InputImg.Max[InputImg.BandList[1] - 1]);
                        BValues[Col]    = LinearStretch(BValues[Col], InputImg.Min[InputImg.BandList[2] - 1], InputImg.Max[InputImg.BandList[2] - 1]);
                        GrayValues[Col] = RGBWeight[0] * RValues[Col] + RGBWeight[1] * GValues[Col] + RGBWeight[2] * BValues[Col];
                    }

                    //写结果到新栅格
                    DS.GetRasterBand(1).WriteRaster(0, Row, xSize, 1, GrayValues, xSize, 1, 0, 0);
                    DS.FlushCache();
                }

                FP.Finish();
                Dri.Dispose();
                DS.Dispose();
                return(true);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                return(false);
            }
        }