//适用于打开文件的情况 public MvgDataProvider(string fileName, IGeoDataDriver dataDriver, bool isReadOnly) : base(fileName, dataDriver) { _driver = dataDriver; _fileName = fileName; string hdrfile = HdrFile.GetHdrFileName(_fileName); if (File.Exists(hdrfile)) { _filelist = new string[] { _fileName, hdrfile } } ; else { _filelist = new string[] { _fileName } }; _header = new MvgHeader(fileName); FillProviderAttributes(); _fsStream = new FileStream(_fileName, FileMode.Open, isReadOnly ? FileAccess.Read : FileAccess.ReadWrite); if (!isReadOnly) { _binaryWriter = new BinaryWriter(_fsStream); } _binaryReader = new BinaryReader(_fsStream); LoadBands(); }
public static IRasterDataProvider CreateRaster(string outFileName, IRasterDataProvider referProvider, string fname) { int width = referProvider.Width; int height = referProvider.Height; Project.ISpatialReference spatialRef = referProvider.SpatialRef; enumDataType datatype = referProvider.DataType; List <string> options = new List <string>(); options.Add("INTERLEAVE=BSQ"); options.Add("VERSION=LDF"); options.Add("WITHHDR=TRUE"); options.Add("SPATIALREF=" + spatialRef.ToProj4String()); options.Add("MAPINFO={" + 1 + "," + 1 + "}:{" + referProvider.CoordEnvelope.MinX + "," + referProvider.CoordEnvelope.MaxY + "}:{" + referProvider.ResolutionX + "," + referProvider.ResolutionY + "}"); //=env.ToMapInfoString(new Size(width, height)); string hdrfile = HdrFile.GetHdrFileName(Path.ChangeExtension(fname, ".hdr")); if (!string.IsNullOrWhiteSpace(hdrfile) && File.Exists(hdrfile)) { HdrFile hdr = HdrFile.LoadFrom(hdrfile); if (hdr != null && hdr.BandNames != null) { options.Add("BANDNAMES=" + string.Join(",", hdr.BandNames)); } } if (!Directory.Exists(Path.GetDirectoryName(outFileName))) { Directory.CreateDirectory(Path.GetDirectoryName(outFileName)); } IRasterDataDriver raster = RasterDataDriver.GetDriverByName("LDF") as IRasterDataDriver; RasterDataProvider outRaster = raster.Create(outFileName, width, height, 1, datatype, options.ToArray()) as RasterDataProvider; return(outRaster); }
public void Update(CoordEnvelope coordEnvelope) { _isNeedUpdateHeader = false; if (coordEnvelope != null) { _coordEnvelope = coordEnvelope.Clone(); _header.MinX = (float)_coordEnvelope.MinX; _header.MaxX = (float)_coordEnvelope.MaxX; _header.MinY = (float)_coordEnvelope.MinY; _header.MaxY = (float)_coordEnvelope.MaxY; if (_access == MemoryMappedFileAccess.ReadWrite) { //MemoryRasterHeader header = GetExtHeader<MemoryRasterHeader>(); SetExtHeader <MemoryRasterHeader>(_header); string hdr = HdrFile.GetHdrFileName(_fileName); if (File.Exists(hdr)) { File.Delete(hdr); } } else { _isNeedUpdateHeader = true; } } }
/* * 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 */ public override IRasterDataProvider Create(string fileName, int xSize, int ySize, int bandCount, enumDataType dataType, params object[] options) { ISpatialReference spatialRef; MvgHeader mvgHeader; HdrMapInfo mapInfo = null; bool isWithHdr = true; Int16 valueCount = 0; Int16[] values = null; string[] valueNames = null; using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) { ParserMvgHeaderParams(options, out spatialRef, out mapInfo, out isWithHdr, out valueCount, out values, out valueNames); mvgHeader = CreateMvgHeader(xSize, ySize, spatialRef, mapInfo, valueCount, values, valueNames); int headerSize = mvgHeader.HeaderSize; int fileLength = mvgHeader.HeaderSize + xSize * ySize * bandCount * DataTypeHelper.SizeOf(enumDataType.Int16); fileStream.SetLength(fileLength); fileStream.Write(mvgHeader.ToBytes(), 0, headerSize); } if (isWithHdr) { mvgHeader.ToHdrFile().SaveTo(HdrFile.GetHdrFileName(fileName)); } IGeoDataProvider provider = BuildDataProviderByArgs(fileName, enumDataProviderAccess.Update, mvgHeader); return(provider as IRasterDataProvider); }
//以下为一些辅助帮助类 public static IRasterDataProvider CreateRaster(string outFileName, CoordEnvelope env, float resolutionX, float resolutionY, int bandCount, IRasterDataProvider referProvider) { //int bandCount = referProvider.BandCount; //CoordEnvelope outEnv = referProvider.CoordEnvelope; //float resX = referProvider.ResolutionX; //float resY = referProvider.ResolutionY; int width = (int)(Math.Round(env.Width / resolutionX)); int height = (int)(Math.Round(env.Height / resolutionY)); Project.ISpatialReference spatialRef = referProvider.SpatialRef; enumDataType datatype = referProvider.DataType; List <string> options = new List <string>(); options.Add("INTERLEAVE=BSQ"); options.Add("VERSION=LDF"); options.Add("WITHHDR=TRUE"); options.Add("SPATIALREF=" + spatialRef.ToProj4String()); options.Add("MAPINFO={" + 1 + "," + 1 + "}:{" + env.MinX + "," + env.MaxY + "}:{" + resolutionX + "," + resolutionY + "}"); //=env.ToMapInfoString(new Size(width, height)); string hdrfile = HdrFile.GetHdrFileName(referProvider.fileName); if (!string.IsNullOrWhiteSpace(hdrfile) && File.Exists(hdrfile)) { HdrFile hdr = HdrFile.LoadFrom(hdrfile); if (hdr != null && hdr.BandNames != null) { options.Add("BANDNAMES=" + string.Join(",", hdr.BandNames)); } } CheckAndCreateDir(Path.GetDirectoryName(outFileName)); IRasterDataDriver raster = RasterDataDriver.GetDriverByName("LDF") as IRasterDataDriver; RasterDataProvider outRaster = raster.Create(outFileName, width, height, bandCount, datatype, options.ToArray()) as RasterDataProvider; return(outRaster); }
/// <summary> /// 调用GDAL前生成.hdr文件 /// </summary> protected override void CallGDALBefore() { base.CallGDALBefore(); _d1bdHeader = SetFileHeader.Set1BDHeader(fileName); //generate hdr file using (FileStream fs = new FileStream(fileName, FileMode.Open)) { long len = fs.Length; _factOfLine = (int)(len / _sizeOfLine - 1); this._hdr = new HdrFile(); _hdr.Lines = _factOfLine; _hdr.BandNames = TryGetBandNames(); _hdr.Bands = _hdr.BandNames.Count(); _hdr.Samples = _samples; _hdr.HeaderOffset = _offset; if (_d1bdHeader.IsBigEndian) { _hdr.ByteOrder = enumHdrByteOder.Network_IEEE; } else { _hdr.ByteOrder = enumHdrByteOder.Host_intel; } _hdr.MajorFrameOffsets[0] = _maxFrameOffset; _hdr.MajorFrameOffsets[1] = _minFrameOffset; string fname = HdrFile.GetHdrFileName(this._fileName); _hdr.SaveTo(fname); } }
private void CreatHdrFileByLdfFile(string ldfFileName) { string hdrFileName = HdrFile.GetHdrFileName(ldfFileName); if (!File.Exists(hdrFileName)) { CreatHdrFile(HdrFile.GetHdrFileName(ldfFileName)); } }
private void TryUpdateHeader() { using (FileStream fs = new FileStream(_fileName, FileMode.Open, FileAccess.Write)) { SetExtHeader <MemoryRasterHeader>(fs, _header); string hdr = HdrFile.GetHdrFileName(_fileName); if (File.Exists(hdr)) { //HdrFile hdrfile = HdrFile.LoadFrom(hdr); //hdrfile.MapInfo.BaseMapCoordinateXY = new HdrGeoPointCoord(_header.MinX, _header.MaxY); //hdrfile.MapInfo.BaseRowColNumber = new Point(1, 1); //hdrfile.SaveTo(hdr); File.Delete(hdr); } } }
private void PrintHdrInfo(HdrFile hdr) { Console.WriteLine("Hdr File:"); Console.WriteLine(hdr.Samples.ToString()); Console.WriteLine(hdr.Lines.ToString()); Console.WriteLine(hdr.Bands.ToString()); Console.WriteLine(hdr.Intertleave.ToString()); Console.WriteLine(hdr.DataType.ToString()); Console.WriteLine(hdr.ByteOrder.ToString()); Console.WriteLine(hdr.HeaderOffset.ToString()); Console.WriteLine("////"); hdr.SaveTo(HdrFile.GetHdrFileName(_newFileName)); Console.WriteLine("LOAD HDR FILE:"); Console.Write(File.ReadAllText(HdrFile.GetHdrFileName(_newFileName))); Console.WriteLine("////END LOAD HDR FILE"); }
/// <summary> /// 调用GDAL前生成.hdr文件 /// </summary> protected override void CallGDALBefore() { base.CallGDALBefore(); string[] filenames = null; _infoDic = HJXML.ReadXMLFile(fileName, out filenames, out _existBands, out _lines, out _sample); //generate hdr file this._hdr = new HdrFile(); _hdr.Lines = _lines; _hdr.BandNames = TryGetBandNames(); _hdr.Bands = _existBands.Count; _hdr.Samples = _sample; _hdr.HeaderOffset = 0; string fname = HdrFile.GetHdrFileName(this._fileName); _hdr.SaveTo(fname); }
public override IRasterDataProvider Create(string fileName, int xSize, int ySize, int bandCount, enumDataType dataType, params object[] options) { ISpatialReference spatialRef; CoordEnvelope coordEnvelope; bool isWithHdr; int extHeaderSize = 0; HdrMapInfo mapInfo; ParseOptions(xSize, ySize, options, out spatialRef, out coordEnvelope, out mapInfo, out isWithHdr, out extHeaderSize); MemoryRasterHeader h = GetHeader(spatialRef, coordEnvelope); h.BandCount = bandCount; h.DataType = (int)dataType; h.Width = xSize; h.Height = ySize; h.ExtendHeaderLength = extHeaderSize; byte[] headerBytes = new byte[1024]; using (FileStream fs = new FileStream(fileName, FileMode.Create)) { int headerSize = Marshal.SizeOf(typeof(MemoryRasterHeader)); byte[] buffer = new byte[headerSize]; GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); Marshal.StructureToPtr((object)h, handle.AddrOfPinnedObject(), true); handle.Free(); fs.SetLength(headerSize + extHeaderSize + (long)xSize * (long)ySize * (long)bandCount * (long)DataTypeHelper.SizeOf(dataType)); fs.Write(buffer, 0, buffer.Length); fs.Seek(0, SeekOrigin.Begin); fs.Read(headerBytes, 0, (int)Math.Min(headerBytes.Length, fs.Length)); } HdrFile hdrFile = h.ToHdrFile(); if (spatialRef != null) { hdrFile.HdrProjectionInfo = GetHdrProjectionInfo(spatialRef); } if (mapInfo != null) { hdrFile.MapInfo = mapInfo; } hdrFile.SaveTo(HdrFile.GetHdrFileName(fileName)); IGeoDataProvider provider = BuildDataProvider(fileName, headerBytes, enumDataProviderAccess.Update, options); return(provider as IRasterDataProvider); }
public unsafe LandRasterDictionaryTemplate(string rasterFile, string codeFile) { using (IRasterDataProvider prd = GeoDataDriver.Open(rasterFile) as IRasterDataProvider) { _pixelValues = new byte[prd.Width * prd.Height]; fixed(byte *buffer = _pixelValues) { IntPtr ptr = new IntPtr(buffer); prd.GetRasterBand(1).Read(0, 0, prd.Width, prd.Height, ptr, enumDataType.Byte, prd.Width, prd.Height); } _hdrInfo = HdrFile.LoadFrom(HdrFile.GetHdrFileName(rasterFile)); _pixelNames = ParsePixelNames(codeFile); ExtractFieldsFromHdr(); } }
private void CreatHdrFile(string fileName) { HdrFile hdr; string fname = HdrFile.GetHdrFileName(fileName); if (File.Exists(fname)) { return; } else { hdr = _header.ToHdrFile(); if (hdr != null) { hdr.SaveTo(fname); } } }
public override void Delete(string fileName) { string hdrFile = HdrFile.GetHdrFileName(fileName); string[] files = null; if (File.Exists(hdrFile)) { files = new string[] { fileName, hdrFile } } ; else { files = new string[] { fileName } }; foreach (string f in files) { File.Delete(f); } }
public MemoryRasterDataProvider(string mmfName, string fname, enumDataProviderAccess access, IGeoDataDriver driver) : base(fname, driver) { _mmfName = GetMmfName(mmfName); _fileName = fname; _access = access == enumDataProviderAccess.ReadOnly ? MemoryMappedFileAccess.Read : MemoryMappedFileAccess.ReadWrite; string hdrfile = HdrFile.GetHdrFileName(_fileName); if (File.Exists(hdrfile)) { _filelist = new string[] { _fileName, hdrfile } } ; else { _filelist = new string[] { _fileName } }; _header = GetHeader(fname); SetFieldsByHeader(_header); TrySetEnvelopeAndResolutions(); LoadBands(); }
public unsafe XjRasterDictionaryTemplate(string rasterFile, string codeFile) { using (IRasterDataProvider prd = GeoDataDriver.Open(rasterFile) as IRasterDataProvider) { int width = prd.Width; int height = prd.Height; bool isOutOfMemory = false; tryAgain: try { _pixelValues = new int[width * height]; } catch (OutOfMemoryException) { isOutOfMemory = true; } if (isOutOfMemory) { isOutOfMemory = false; width /= 2; height /= 2; goto tryAgain; } _scaleFactor = prd.Width / width; fixed(int *buffer = _pixelValues) { IntPtr ptr = new IntPtr(buffer); prd.GetRasterBand(1).Read(0, 0, prd.Width, prd.Height, ptr, enumDataType.Int32, width, height); } _hdrInfo = HdrFile.LoadFrom(HdrFile.GetHdrFileName(rasterFile)); _pixelNames = ParsePixelNames(codeFile); ExtractFieldsFromHdr(); } }
/// <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); } } }