public GDAL_Info(Dataset ds) { Projection = ds.GetProjectionRef(); Resolution = new Size(ds.RasterXSize, ds.RasterYSize); Bands = new DataBandInfo[ds.RasterCount]; for (int i = 0; i < ds.RasterCount; i++) { Band band = ds.GetRasterBand(i + 1); Bands[i] = new DataBandInfo(); int temp; band.GetMaximum(out Bands[i].MaxValue, out temp); band.GetMaximum(out Bands[i].MinValue, out temp); band.GetNoDataValue(out Bands[i].NODATAValue, out temp); ColorInterp clr = band.GetRasterColorInterpretation(); switch (clr) { case ColorInterp.GCI_RedBand: Bands[i].Name = "RedBand"; Bands[i].Image = Resource1.red_layer; BppType += "R"; break; case ColorInterp.GCI_GreenBand: Bands[i].Name = "GreenBand"; Bands[i].Image = Resource1.green_layer; BppType += "G"; break; case ColorInterp.GCI_BlueBand: Bands[i].Name = "BlueBand"; Bands[i].Image = Resource1.blue_layer; BppType += "B"; break; default: Bands[i].Name = clr.ToString(); Bands[i].Image = null; BppType += "?"; break; } BppType += "[" + Gdal.GetDataTypeName(band.DataType) + "]"; Bpp += (ushort)Gdal.GetDataTypeSize(band.DataType); if (i + 1 < ds.RasterCount) BppType += ","; } BppType += " (" + Bpp + ")"; Driver = ds.GetDriver().LongName; Metadata = ds.GetMetadata(""); ImgMetadata = ds.GetMetadata("IMAGE_STRUCTURE"); SubDSMetadata = ds.GetMetadata("SUBDATASETS"); GeoLocMetadata = ds.GetMetadata("GEOLOCATION"); GDALInfoGetPosition(ds, 0.0, 0.0, out UpperLeftX, out UpperLeftY); GDALInfoGetPosition(ds, 0.0, ds.RasterYSize, out UpperRightX, out UpperRightY); GDALInfoGetPosition(ds, ds.RasterXSize, 0.0, out LowerLeftX, out LowerLeftY); GDALInfoGetPosition(ds, ds.RasterXSize, ds.RasterYSize, out LowerRightX, out LowerRightY); GDALInfoGetPosition(ds, ds.RasterXSize / 2, ds.RasterYSize / 2, out CenterX, out CenterY); }
public bool MakeTifFile(double[] et, string source, string dest) { bool result = false; OSGeo.GDAL.Gdal.AllRegister(); OSGeo.GDAL.Dataset in_dataset = Gdal.Open(source, Access.GA_ReadOnly); int width = in_dataset.RasterXSize; int height = in_dataset.RasterYSize; Driver driver = Gdal.GetDriverByName("GTiff"); Dataset out_dataset = driver.Create(dest, width, height, 1, DataType.GDT_Float64, null); string projectionRef = in_dataset.GetProjectionRef(); double[] geoTransform = new double[6]; out_dataset.SetProjection(projectionRef); out_dataset.SetGeoTransform(geoTransform); out_dataset.GetRasterBand(1).WriteRaster(0, 0, width, height, et, width, height, 0, 0); out_dataset.GetRasterBand(1).FlushCache(); out_dataset.FlushCache(); out_dataset.Dispose(); result = true; return(result); }
/// <summary> /// initialize a Gdal based raster layer /// </summary> /// <param name="strLayerName">Name of layer</param> /// <param name="imageFilename">location of image</param> public GdalRasterLayer(string strLayerName, string imageFilename) { SpotPoint = new PointF(0, 0); Projection = ""; BitDepth = 8; NonSpotGain = new double[] {1, 1, 1, 1}; SpotGain = new double[] {1, 1, 1, 1}; Gain = new double[] {1, 1, 1, 1}; NonSpotGamma = 1; SpotGamma = 1; Gamma = 1; TransparentColor = Color.Empty; ColorCorrect = true; LayerName = strLayerName; Filename = imageFilename; //TilingSize = new Size(256, 128); Gdal.AllRegister(); try { _gdalDataset = Gdal.OpenShared(Filename, Access.GA_ReadOnly); // have gdal read the projection Projection = _gdalDataset.GetProjectionRef(); // no projection info found in the image...check for a prj if (Projection == "" && File.Exists(imageFilename.Substring(0, imageFilename.LastIndexOf(".", StringComparison.CurrentCultureIgnoreCase)) + ".prj")) { Projection = File.ReadAllText(imageFilename.Substring(0, imageFilename.LastIndexOf(".", StringComparison.CurrentCultureIgnoreCase)) + ".prj"); } _imageSize = new Size(_gdalDataset.RasterXSize, _gdalDataset.RasterYSize); _envelope = GetExtent(); HistoBounds = new Rectangle((int)_envelope.MinX, (int)_envelope.MinY, (int)_envelope.Width, (int)_envelope.Height); Bands = _gdalDataset.RasterCount; } catch (Exception ex) { _gdalDataset = null; throw new Exception("Couldn't load " + imageFilename + "\n\n" + ex.Message + ex.InnerException); } }
protected void OpenDataset(string imageFilename) { try { _gdalDataset = Gdal.OpenShared(imageFilename, Access.GA_ReadOnly); // have gdal read the projection Projection = _gdalDataset.GetProjectionRef(); // no projection info found in the image...check for a prj if (Projection == "" && File.Exists(imageFilename.Substring(0, imageFilename.LastIndexOf(".", StringComparison.CurrentCultureIgnoreCase)) + ".prj")) { Projection = File.ReadAllText(imageFilename.Substring(0, imageFilename.LastIndexOf(".", StringComparison.CurrentCultureIgnoreCase)) + ".prj"); } _imageSize = new Size(_gdalDataset.RasterXSize, _gdalDataset.RasterYSize); _envelope = GetExtent(); HistoBounds = new Rectangle((int) _envelope.MinX, (int) _envelope.MinY, (int) _envelope.Width, (int) _envelope.Height); Bands = _gdalDataset.RasterCount; } catch (Exception ex) { _gdalDataset = null; throw new Exception("Couldn't load " + imageFilename + "\n\n" + ex.Message + ex.InnerException); } }
/// <summary> /// Initializes a new instance of the MapAround.DataProviders.GDALRasterProvider. /// </summary> /// <param name="fileName">Image file name</param> public GDALRasterProvider(string fileName) { _fileName = fileName; Gdal.AllRegister(); try { _gdalDataset = Gdal.OpenShared(_fileName, Access.GA_ReadOnly); // have gdal read the projection _strProjection = _gdalDataset.GetProjectionRef(); // no projection info found in the image...check for a prj if (string.IsNullOrEmpty(_strProjection) && File.Exists(_fileName.Substring(0, _fileName.LastIndexOf(".")) + ".prj")) { _strProjection = File.ReadAllText(_fileName.Substring(0, _fileName.LastIndexOf(".")) + ".prj"); } _imagesize = new Size(_gdalDataset.RasterXSize, _gdalDataset.RasterYSize); _bandCount = _gdalDataset.RasterCount; } catch (Exception ex) { _gdalDataset = null; throw new Exception("Failed loading " + _fileName + "\n" + ex.Message + ex.InnerException); } }
/// <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); }
/// <summary> /// initialize a Gdal based raster layer /// </summary> /// <param name="strLayerName">Name of layer</param> /// <param name="imageFilename">location of image</param> public GdalRasterLayer(string strLayerName, string imageFilename) { this.LayerName = strLayerName; this.Filename = imageFilename; disposed = false; Gdal.AllRegister(); try { _GdalDataset = Gdal.OpenShared(_Filename, Access.GA_ReadOnly); // have gdal read the projection _projection = _GdalDataset.GetProjectionRef(); // no projection info found in the image...check for a prj if (_projection == "" && File.Exists(imageFilename.Substring(0, imageFilename.LastIndexOf(".")) + ".prj")) { _projection = File.ReadAllText(imageFilename.Substring(0, imageFilename.LastIndexOf(".")) + ".prj"); } _imagesize = new Size(_GdalDataset.RasterXSize, _GdalDataset.RasterYSize); _Envelope = this.GetExtent(); _histoBounds = new Rectangle((int)_Envelope.Left, (int)_Envelope.Bottom, (int)_Envelope.Width, (int)_Envelope.Height); _lbands = _GdalDataset.RasterCount; } catch (Exception ex) { _GdalDataset = null; throw new Exception("Couldn't load " + imageFilename + "\n\n" + ex.Message + ex.InnerException); } }