public unsafe void WriteMvgFileNormal() { // _fname = @"C:\工作\3.13\SNW_DBLV_FY3A_VIRR_1000M_NULL_P001_200911131356.mvg"; IGeoDataDriver drv = GeoDataDriver.GetDriverByName("MVG"); IRasterDataProvider prd = drv.Open(_fname, enumDataProviderAccess.ReadOnly) as IRasterDataProvider; Assert.NotNull(prd); IRasterBand band = prd.GetRasterBand(1); Assert.NotNull(band); Int16[] buffer = new Int16[prd.Width * prd.Height]; Console.WriteLine("prd.Width = " + prd.Width); Console.WriteLine("prd.Height = " + prd.Height); Console.WriteLine("band.Width = " + band.Width); Console.WriteLine("band.Height= " + band.Height); fixed(Int16 *ptr = buffer) { IntPtr bufferPtr = new IntPtr(ptr); band.Read(0, 0, prd.Width, prd.Height, bufferPtr, enumDataType.Int16, prd.Width, prd.Height); } string dstFname = @"e:\mvg_writer.bin"; string mapInfo = "{1,1}:{110,35}:{0.01,0.01}"; Int16 valueCount = 2; bool withHdr = true; string values = "{ 0, 1 }"; string valueNames = "{120,99}"; IMvgDataProvider prdWriter = (drv as IRasterDataDriver).Create(dstFname, prd.Width, prd.Height, 1, enumDataType.Int16, "VALUECOUNT=" + valueCount, "VALUES=" + values, "VALUENAMES=" + valueNames, "MAPINFO=" + mapInfo, "WITHHDR=" + withHdr.ToString()) as IMvgDataProvider; IRasterBand bandWriter = prdWriter.GetRasterBand(1); fixed(Int16 *ptr = buffer) { IntPtr bufferPtr = new IntPtr(ptr); bandWriter.Write(0, 0, prd.Width, prd.Height, bufferPtr, enumDataType.Int16, prd.Width, prd.Height); } prd.Dispose(); drv.Dispose(); prdWriter.Dispose(); }
/// <summary> /// ldf文件另存为mvg文件 /// </summary> /// <param name="ldfName">ldf文件名</param> /// <param name="mvgName">mvg文件名</param> /// <param name="bandNo">需要获取的波段号</param> /// <param name="options"> /* * VALUECOUNT = 2 //default:0 * VALUES = "{0,1}" // * VALUENAMES // * SPATIALREF=Proj4 //default:wgs-84 * MAPINFO={X,Y}:{Col,Row}:{ResolutionX,ResolutionY} //default:null * WITHHDR=[TRUE|FALSE] //default:true */ /// </param> public static void FromLDF(string ldfFileName, string mvgFileName, int bandNo, params object[] options) { IGeoDataDriver ldfDrv = null; IRasterDataProvider ldfPrd = null; IRasterBand ldfBand = null; IMvgDriver mvgDrv = null; IMvgDataProvider mvgPrd = null; IRasterBand mvgBand = null; try { ldfDrv = GeoDataDriver.GetDriverByName("LDF"); if (ldfDrv == null) { return; } ldfPrd = ldfDrv.Open(ldfFileName, enumDataProviderAccess.ReadOnly) as IRasterDataProvider; if (ldfPrd == null) { return; } Ldf1Header ldfHeader = (ldfPrd as ILdfDataProvider).Header as Ldf1Header; if (ldfHeader == null || (ldfHeader.DataType != enumDataType.Int16 && ldfHeader.DataType != enumDataType.UInt16)) { throw new NotSupportedException("只支持Int16和UInt16数据格式的LDF文件转换!"); } ldfBand = ldfPrd.GetRasterBand(bandNo); if (ldfBand == null) { return; } //创建MVG文件 mvgDrv = GeoDataDriver.GetDriverByName("MVG") as IMvgDriver; if (mvgDrv == null) { return; } mvgPrd = mvgDrv.Create(mvgFileName, ldfBand.Width, ldfBand.Height, 1, enumDataType.Int16, options) as IMvgDataProvider; if (mvgPrd == null) { return; } MvgHeaderConvertor.FillMvgHeader(ldfHeader, mvgPrd.Header); //重新生成hdr头文件 string hdrFileName = HdrFile.GetHdrFileName(mvgFileName); HdrFile.SaveTo(hdrFileName, mvgPrd.Header.ToHdrFile()); //读数据并写入MVG文件中 mvgBand = mvgPrd.GetRasterBand(1); if (mvgBand == null) { return; } WriteDataFromLdfToMvg(ldfHeader, ldfBand, mvgBand); } finally { if (mvgPrd != null) { (mvgPrd as MvgDataProvider).Disposed(ldfDrv, ldfPrd, ldfBand, mvgDrv, mvgPrd, mvgBand); } } }