///// <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); }