예제 #1
0
 /// <summary>
 /// 拷贝元数据至新的数据集。
 /// </summary>
 /// <param name="InputDS">输入的数据集。</param>
 /// <param name="TargetDS">要将元数据拷贝到的数据集。</param>
 public static void CopyMetadata(OSGeo.GDAL.Dataset InputDS, OSGeo.GDAL.Dataset TargetDS)
 {
     if (InputDS == null || TargetDS == null)
     {
         throw new ArgumentNullException("数据集不能为空。");
     }
     //写入投影和变换信息
     double[] TransformArgs = new double[6];
     InputDS.GetGeoTransform(TransformArgs);
     TargetDS.SetGeoTransform(TransformArgs);
     TargetDS.SetProjection(InputDS.GetProjection());
     //DS.SetMetadata(InputDS.GetMetadata());
 }
예제 #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
 /// <summary>
 /// Gets the size of the whole image, but doesn't keep the image open
 /// unless it was already open.
 /// </summary>
 private void ReadHeader()
 {
     try
     {
         _dataset = Gdal.Open(Filename, Access.GA_Update);
     }
     catch
     {
         try
         {
             _dataset = Gdal.Open(Filename, Access.GA_ReadOnly);
         }
         catch (Exception ex)
         {
             throw new GdalException(ex.ToString());
         }
     }
     Width = _dataset.RasterXSize;
     Height = _dataset.RasterYSize;
     NumBands = _dataset.RasterCount;
     double[] test = new double[6];
     _dataset.GetGeoTransform(test);
     ProjectionString = _dataset.GetProjection();
     Bounds = new RasterBounds(Height, Width, test);
     WorldFile.Affine = test;
     Close();
 }