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 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); }
public void ParseFromFile(string infile) { string outfile = Path.Combine(Path.GetDirectoryName(infile), Path.GetFileNameWithoutExtension(infile) + "_COPY.hdr"); HdrFile hdr = HdrFile.LoadFrom(infile); Assert.NotNull(hdr); hdr.SaveTo(outfile); Console.Write(File.ReadAllText(outfile)); //FileAssert.AreEqual(infile, outfile); }
private void WritePolarAWX(string hdr, string dat, RasterIdentify id) { HdrFile hdrObj = HdrFile.LoadFrom(hdr); L1Header = new AWXLevel1HeaderImagePolarOrbit(); L1Header.Write(hdrObj); L2Header = new AWXLevel2HeaderImagePolarOrbit(); if (hdrObj.MapInfo.Name == "Geographic Lat/Lon") { L2Header.ProjectMode = 4; } using (IRasterDataProvider dataprd = GeoDataDriver.Open(dat) as IRasterDataProvider) { if (dataprd != null && dataprd.BandCount != 0) { L2Header.GeographicalScopeNorthLatitude = (short)(dataprd.CoordEnvelope.MaxY * 100); L2Header.GeographicalScopeSouthLatitude = (short)(dataprd.CoordEnvelope.MinY * 100); L2Header.GeographicalScopeWestLongitude = (short)(dataprd.CoordEnvelope.MinX * 100); L2Header.GeographicalScopeEastLongitude = (short)(dataprd.CoordEnvelope.MaxX * 100); L2Header.ProjectCenterLatitude = (short)((L2Header.GeographicalScopeNorthLatitude + L2Header.GeographicalScopeSouthLatitude) / 2); L2Header.ProjectCenterLongitude = (short)((L2Header.GeographicalScopeEastLongitude + L2Header.GeographicalScopeWestLongitude) / 2); L2Header.ProjectHorizontalResolution = (short)(dataprd.ResolutionX * 100 * 100); L2Header.ProjectVerticalResolution = (short)(dataprd.ResolutionY * 100 * 100); } } L2Header.Write(hdrObj, id); Padding = new byte[L1Header.RecordLength - L1Header.Level1HeaderLength - L1Header.Level2HeaderLength]; L1Header.WriteFile(hdr); L2Header.WriteFile(hdr); using (FileStream stream = new FileStream(Path.ChangeExtension(hdr, "AWX"), FileMode.Append)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(Padding); } } using (FileStream stream = new FileStream(dat, FileMode.Open)) { stream.Seek(hdrObj.HeaderOffset, SeekOrigin.Begin); using (BinaryReader br = new BinaryReader(stream)) { byte[] awxProductBytes = br.ReadBytes(Convert.ToInt32(stream.Length - stream.Position)); Product = new AWXProduct(); Product.Read(new MemoryStream(awxProductBytes)); } } using (FileStream stream = new FileStream(Path.ChangeExtension(hdr, "AWX"), FileMode.Append)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(Product.Data); } } }
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 AddOverView(string pngfilename) { if (!File.Exists(pngfilename)) { return; } string hdrfilename = pngfilename.Replace(".overview.png", ".hdr"); if (!File.Exists(hdrfilename)) { return; } HdrFile hdrFile = HdrFile.LoadFrom(hdrfilename); Core.DrawEngine.CoordEnvelope env = CoordEnvelopeFromHdr(hdrFile); Bitmap bmp = LoadImage(pngfilename); _simpleMapControl.AddImageLayer(pngfilename, bmp, env, true); _simpleMapControl.Render(); }
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(); } }
private void WriteStationaryAWX(string hdr, string dat, RasterIdentify id) { HdrFile hdrObj = HdrFile.LoadFrom(hdr); L1Header = new AWXLevel1HeaderImageGeostationaryOrbit(); L1Header.Write(hdrObj); L2Header = new AWXLevel2HeaderImageGeostationary(); if (hdrObj.MapInfo.Name == "Geographic Lat/Lon") { L2Header.ProjectMode = 4; } enumDataType datatype = enumDataType.Unknow; using (IRasterDataProvider dataprd = GeoDataDriver.Open(dat) as IRasterDataProvider) { if (dataprd != null && dataprd.BandCount != 0) { datatype = dataprd.DataType; L2Header.GeographicalScopeNorthLatitude = (short)(dataprd.CoordEnvelope.MaxY * 100); L2Header.GeographicalScopeSouthLatitude = (short)(dataprd.CoordEnvelope.MinY * 100); L2Header.GeographicalScopeWestLongitude = (short)(dataprd.CoordEnvelope.MinX * 100); L2Header.GeographicalScopeEastLongitude = (short)(dataprd.CoordEnvelope.MaxX * 100); L2Header.ProjectCenterLatitude = (short)((L2Header.GeographicalScopeNorthLatitude + L2Header.GeographicalScopeSouthLatitude) / 2); L2Header.ProjectCenterLongitude = (short)((L2Header.GeographicalScopeEastLongitude + L2Header.GeographicalScopeWestLongitude) / 2); L2Header.ProjectHorizontalResolution = (short)(dataprd.ResolutionX * 100 * 100); L2Header.ProjectVerticalResolution = (short)(dataprd.ResolutionY * 100 * 100); } } L2Header.Write(hdrObj, id); Padding = new byte[L1Header.RecordLength - L1Header.Level1HeaderLength - L1Header.Level2HeaderLength]; L1Header.WriteFile(hdr); L2Header.WriteFile(hdr); using (FileStream stream = new FileStream(Path.ChangeExtension(hdr, "AWX"), FileMode.Append)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(Padding); } } using (FileStream stream = new FileStream(dat, FileMode.Open)) { stream.Seek(hdrObj.HeaderOffset, SeekOrigin.Begin); using (BinaryReader br = new BinaryReader(stream)) { byte[] awxProductBytes = new Byte[L2Header.ImageHeight * L2Header.ImageWidth]; switch (datatype) { case enumDataType.Byte: awxProductBytes = br.ReadBytes(Convert.ToInt32(stream.Length - stream.Position)); break; case enumDataType.Int16: for (int i = 0; i < awxProductBytes.Length; i++) { awxProductBytes[i] = BitConverter.GetBytes(br.ReadInt16())[0]; } break; case enumDataType.UInt16: for (int i = 0; i < awxProductBytes.Length; i++) { awxProductBytes[i] = BitConverter.GetBytes(br.ReadUInt16())[0]; } break; case enumDataType.Int32: for (int i = 0; i < awxProductBytes.Length; i++) { awxProductBytes[i] = BitConverter.GetBytes(br.ReadInt32())[0]; } break; case enumDataType.UInt32: for (int i = 0; i < awxProductBytes.Length; i++) { awxProductBytes[i] = BitConverter.GetBytes(br.ReadUInt32())[0]; } break; case enumDataType.Int64: for (int i = 0; i < awxProductBytes.Length; i++) { awxProductBytes[i] = BitConverter.GetBytes(br.ReadInt64())[0]; } break; case enumDataType.UInt64: for (int i = 0; i < awxProductBytes.Length; i++) { awxProductBytes[i] = BitConverter.GetBytes(br.ReadUInt64())[0]; } break; default: awxProductBytes = br.ReadBytes(Convert.ToInt32(stream.Length - stream.Position)); break; } Product = new AWXProduct(); Product.Read(new MemoryStream(awxProductBytes)); } } using (FileStream stream = new FileStream(Path.ChangeExtension(hdr, "AWX"), FileMode.Append)) { using (BinaryWriter bw = new BinaryWriter(stream)) { bw.Write(Product.Data); } } }
public string SaveGeoAdjustByChangeCoordEnvelope(CoordEnvelope envelopeAfterAdjust, IRasterDataProvider srcRaster) { if (srcRaster == null) { return(null); } string drive = "LDF"; CoordEnvelope dstEnvelope = envelopeAfterAdjust; float resx = srcRaster.ResolutionX; float resy = srcRaster.ResolutionY; string extension = Path.GetExtension(srcRaster.fileName).ToUpper(); List <string> opts = new List <string>(); if (extension == ".LDF" || extension == ".LD3" || extension == ".LD2" || extension == ".DAT") { if (extension == ".DAT") { drive = "MEM"; opts.AddRange(new string[] { "INTERLEAVE=BSQ", "VERSION=MEM", "WITHHDR=TRUE", "SPATIALREF=" + (srcRaster.SpatialRef == null?"":srcRaster.SpatialRef.ToProj4String()), "MAPINFO={" + 1 + "," + 1 + "}:{" + envelopeAfterAdjust.MinX + "," + envelopeAfterAdjust.MaxY + "}:{" + srcRaster.ResolutionX + "," + srcRaster.ResolutionY + "}" }); string fileName = GreatFileName(srcRaster.fileName, extension); IRasterDataDriver rasterDriver = GeoDataDriver.GetDriverByName(drive) as IRasterDataDriver; if (rasterDriver == null) { throw new Exception("数据驱动获取失败:" + drive); } using (IRasterDataProvider tProviders = rasterDriver.Create(fileName, srcRaster.Width, srcRaster.Height, srcRaster.BandCount, srcRaster.DataType, opts.ToArray())) { int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, 0, srcRaster.Height); int sample = srcRaster.Width; int typeSize = ClipCutHelper.GetSize(srcRaster.DataType); int bufferSize = sample * rowStep * typeSize; for (int oRow = 0; oRow < srcRaster.Height; oRow += rowStep) { if (oRow + rowStep > srcRaster.Height) { rowStep = srcRaster.Height - oRow; } for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++) { byte[] databuffer = new byte[bufferSize]; unsafe { fixed(byte *ptr = databuffer) { IntPtr buffer = new IntPtr(ptr); srcRaster.GetRasterBand(bandIndex).Read(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); tProviders.GetRasterBand(bandIndex).Write(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); } } } } return(tProviders.fileName); } } else { // opts.AddRange(new string[]{ //"INTERLEAVE=BSQ", //"VERSION=LDF", //"WITHHDR=TRUE", //"SPATIALREF=" + srcRaster.SpatialRef.ToProj4String(), //"MAPINFO={" + 1 + "," + 1 + "}:{" + dstEnvelope.MinX + "," + dstEnvelope.MaxY + "}:{" +resx + "," + resy + "}", ////"BANDNAMES="+ BandNameString(prjSettings.OutBandNos), //}); // if (srcRaster.DataIdentify != null) // { // string satellite = srcRaster.DataIdentify.Satellite; // string sensor = srcRaster.DataIdentify.Sensor; // DateTime dt = srcRaster.DataIdentify.OrbitDateTime; // bool asc = srcRaster.DataIdentify.IsAscOrbitDirection; // if (!string.IsNullOrWhiteSpace(satellite)) // { // opts.Add("SATELLITE=" + satellite); // } // if (!string.IsNullOrWhiteSpace(sensor)) // { // opts.Add("SENSOR=" + sensor); // } // if (dt != DateTime.MinValue && dt != DateTime.MaxValue) // opts.Add("DATETIME=" + dt.ToString("yyyy/MM/dd HH:mm")); // opts.Add("ORBITDIRECTION=" + (asc ? "ASC" : "DESC")); // } //内存拷贝 string srcfileName = srcRaster.fileName; string desfileName = GreatFileName(srcRaster.fileName, extension); File.Copy(srcfileName, desfileName); using (IRasterDataProvider dataPrd = GeoDataDriver.Open(desfileName) as IRasterDataProvider) { (dataPrd as ILdfDataProvider).Update(envelopeAfterAdjust); } string hdrSrcName = Path.ChangeExtension(srcfileName, ".hdr"); string hdrDesName = Path.ChangeExtension(desfileName, ".hdr"); HdrFile hdr = HdrFile.LoadFrom(hdrSrcName); if (hdr.MapInfo != null) { hdr.MapInfo.BaseMapCoordinateXY.Latitude = envelopeAfterAdjust.Center.Y; hdr.MapInfo.BaseMapCoordinateXY.Longitude = envelopeAfterAdjust.Center.X; } if (hdr.GeoPoints != null) { hdr.GeoPoints = null; } HdrFile.SaveTo(hdrDesName, hdr); return(desfileName); } } //仅用于水情MVG else if (extension == ".MVG") { drive = "MEM"; string ext = ".DAT"; opts.AddRange(new string[] { "INTERLEAVE=BSQ", "VERSION=MEM", "WITHHDR=TRUE", "SPATIALREF=" + (srcRaster.SpatialRef == null?"":srcRaster.SpatialRef.ToProj4String()), "MAPINFO={" + 1 + "," + 1 + "}:{" + envelopeAfterAdjust.MinX + "," + envelopeAfterAdjust.MaxY + "}:{" + srcRaster.ResolutionX + "," + srcRaster.ResolutionY + "}" }); string fileName = GreatFileName(srcRaster.fileName, ext); IRasterDataDriver rasterDriver = GeoDataDriver.GetDriverByName(drive) as IRasterDataDriver; if (rasterDriver == null) { throw new Exception("数据驱动获取失败:" + drive); } using (IRasterDataProvider tProviders = rasterDriver.Create(fileName, srcRaster.Width, srcRaster.Height, srcRaster.BandCount, srcRaster.DataType, opts.ToArray())) { int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, 0, srcRaster.Height); int sample = srcRaster.Width; int bufferSize = sample * rowStep; for (int oRow = 0; oRow < srcRaster.Height; oRow += rowStep) { if (oRow + rowStep > srcRaster.Height) { rowStep = srcRaster.Height - oRow; } for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++) { switch (srcRaster.DataType) { case enumDataType.Int16: { Int16[] databuffer = new Int16[bufferSize]; unsafe { fixed(Int16 *ptr = databuffer) { IntPtr buffer = new IntPtr(ptr); srcRaster.GetRasterBand(bandIndex).Read(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); //修改值 for (int i = 0; i < bufferSize; i++) { if (databuffer[i] > 1) { databuffer[i] = 0; } } tProviders.GetRasterBand(bandIndex).Write(0, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); } } break; } } } } return(tProviders.fileName); } } return(null); }