public string SaveGeoAdjust(CoordEnvelope envelopeBeforeAdjust, IRasterDataProvider rasterRaster, IRasterDataProvider srcRaster) { if (srcRaster == null) { return(null); } Size oSize = new Size(rasterRaster.Width, rasterRaster.Height); //位置映射参数 int tBeginRow = -1, tEndRow = -1, tBeginCol = -1, tEndCol = -1; int oBeginRow = -1, oEndRow = -1, oBeginCol = -1, oEndCol = -1; bool isInternal = new RasterMoasicClipHelper().ComputeBeginEndRowCol(rasterRaster.CoordEnvelope, oSize, envelopeBeforeAdjust, oSize, ref oBeginRow, ref oBeginCol, ref oEndRow, ref oEndCol, ref tBeginRow, ref tBeginCol, ref tEndRow, ref tEndCol); string drive = "LDF"; CoordEnvelope dstEnvelope = envelopeBeforeAdjust; float resx = srcRaster.ResolutionX; float resy = srcRaster.ResolutionY; string ext = Path.GetExtension(srcRaster.fileName); if (string.IsNullOrWhiteSpace(ext)) { ext = ".LDF"; } else { ext = ext.ToUpper(); } List <string> opts = new List <string>(); if (ext == ".LDF" || ext == ".DAT") { if (ext == ".LDF") { 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), "SENSOR=AVHRR" }); if (srcRaster.DataIdentify != null) { string satellite = srcRaster.DataIdentify.Satellite; DateTime dt = srcRaster.DataIdentify.OrbitDateTime; bool asc = srcRaster.DataIdentify.IsAscOrbitDirection; if (!string.IsNullOrWhiteSpace(satellite)) { opts.Add("SATELLITE=" + satellite); } if (dt != DateTime.MinValue && dt != DateTime.MaxValue) { opts.Add("DATETIME=" + dt.ToString("yyyy/MM/dd HH:mm")); } opts.Add("ORBITDIRECTION=" + (asc ? "ASC" : "DESC")); } } else { drive = "MEM"; opts.AddRange(new string[] { "INTERLEAVE=BSQ", "VERSION=MEM", "WITHHDR=TRUE", "SPATIALREF=" + (srcRaster.SpatialRef == null?"":srcRaster.SpatialRef.ToProj4String()), "MAPINFO={" + 1 + "," + 1 + "}:{" + envelopeBeforeAdjust.MinX + "," + envelopeBeforeAdjust.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, rasterRaster.Width, rasterRaster.Height, srcRaster.BandCount, srcRaster.DataType, opts.ToArray())) { if (isInternal) { int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, oBeginRow, oEndRow); int stepCount = (int)((oEndRow - oBeginRow) / rowStep + 0.5) * srcRaster.BandCount; int step = 0; int sample = (oEndCol - oBeginCol); int typeSize = ClipCutHelper.GetSize(srcRaster.DataType); int bufferSize = sample * rowStep * typeSize; for (int oRow = oBeginRow; oRow < oEndRow; oRow += rowStep) { if (oRow + rowStep > oEndRow) { rowStep = oEndRow - oRow; } for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++) { step++; byte[] databuffer = new byte[bufferSize]; unsafe { fixed(byte *ptr = databuffer) { IntPtr buffer = new IntPtr(ptr); srcRaster.GetRasterBand(bandIndex).Read(oBeginCol, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); tProviders.GetRasterBand(bandIndex).Write(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); } } } } } return(tProviders.fileName); } } //仅用于水情MVG else if (ext == ".MVG") { drive = "MEM"; ext = ".DAT"; opts.AddRange(new string[] { "INTERLEAVE=BSQ", "VERSION=MEM", "WITHHDR=TRUE", "SPATIALREF=" + (rasterRaster.SpatialRef == null?"":rasterRaster.SpatialRef.ToProj4String()), "MAPINFO={" + 1 + "," + 1 + "}:{" + envelopeBeforeAdjust.MinX + "," + envelopeBeforeAdjust.MaxY + "}:{" + rasterRaster.ResolutionX + "," + rasterRaster.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, rasterRaster.Width, rasterRaster.Height, srcRaster.BandCount, srcRaster.DataType, opts.ToArray())) { if (isInternal) { int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, oBeginRow, oEndRow); int stepCount = (int)((oEndRow - oBeginRow) / rowStep + 0.5) * srcRaster.BandCount; int step = 0; int sample = (oEndCol - oBeginCol); int bufferSize = sample * rowStep; for (int oRow = oBeginRow; oRow < oEndRow; oRow += rowStep) { if (oRow + rowStep > oEndRow) { rowStep = oEndRow - oRow; } for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++) { step++; 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(oBeginCol, 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(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); } } break; } } } } } return(tProviders.fileName); } } return(null); }
public IRasterDataProvider Cut(IRasterDataProvider srcRaster, BlockDefWithAOI blockDefsAoi, int samplePercent, string driver, string outdir, Action <int, string> progressCallback, params object[] options) { IRasterDataProvider tProviders; if (progressCallback != null) { progressCallback(0, "开始数据裁切"); } if (blockDefsAoi == null) { return(null); } //位置映射参数 int tBeginRow = -1, tEndRow = -1, tBeginCol = -1, tEndCol = -1; int oBeginRow = -1, oEndRow = -1, oBeginCol = -1, oEndCol = -1; CoordEnvelope oEnvelope = srcRaster.CoordEnvelope; CoordEnvelope tEnvelope = blockDefsAoi.ToEnvelope(); Size oSize = new Size(srcRaster.Width, srcRaster.Height); Size tSize = ClipCutHelper.GetTargetSize(blockDefsAoi, srcRaster.ResolutionX, srcRaster.ResolutionY); //Size tSize = GetTargetSize(blockDefsAoi, srcRaster.ResolutionX, srcRaster.ResolutionY); bool isInternal = new RasterMoasicClipHelper().ComputeBeginEndRowCol(oEnvelope, oSize, tEnvelope, tSize, ref oBeginRow, ref oBeginCol, ref oEndRow, ref oEndCol, ref tBeginRow, ref tBeginCol, ref tEndRow, ref tEndCol); string blockFilename = ClipCutHelper.GetBlockFilename(blockDefsAoi, srcRaster.fileName, outdir, driver); tSize = new Size(oEndCol - oBeginCol, oEndRow - oBeginRow); IRasterDataDriver rasterDriver = GeoDataDriver.GetDriverByName(driver) as IRasterDataDriver; int oWidth = 0; int oHeight = 0; float tResolutionX; float tResolutionY; if (samplePercent > 0 && samplePercent < 100) { oHeight = (int)(tSize.Width * samplePercent * 1f / 100 + 0.5); oWidth = (int)(tSize.Width * samplePercent * 1f / 100 + 0.5); tResolutionX = srcRaster.ResolutionX * samplePercent * 1f / 100; tResolutionY = srcRaster.ResolutionY * samplePercent * 1f / 100; } else { oHeight = tSize.Height; oWidth = tSize.Width; tResolutionX = srcRaster.ResolutionX; tResolutionY = srcRaster.ResolutionY; } string[] optionString = new string[] { "INTERLEAVE=BSQ", "VERSION=LDF", "WITHHDR=TRUE", "SPATIALREF=" + srcRaster.SpatialRef.ToProj4String(), "MAPINFO={" + 1 + "," + 1 + "}:{" + tEnvelope.MinX + "," + tEnvelope.MaxY + "}:{" + tResolutionX + "," + tResolutionY + "}" }; tProviders = rasterDriver.Create(blockFilename, oWidth, oHeight, srcRaster.BandCount, srcRaster.DataType, optionString); tProviders.GetRasterBand(1).Fill(-999.0f); int[] oAoi = ClipCutHelper.GetOffsetIndex(blockDefsAoi.AOIIndexes, oBeginRow, oBeginCol, srcRaster.Width, oWidth); int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, oBeginRow, oEndRow); int stepCount = (int)((oEndRow - oBeginRow) / rowStep + 0.5) * srcRaster.BandCount; int step = 0; int percent = 0; for (int oRow = oBeginRow; oRow < oEndRow; oRow += rowStep) { if (oRow + rowStep > oEndRow) { rowStep = oEndRow - oRow; } for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++) { step++; percent = (int)(step * 1.0f / stepCount * 100); if (progressCallback != null) { progressCallback(percent, "完成数据裁切" + percent + "%"); } int sample = (oEndCol - oBeginCol); int typeSize = ClipCutHelper.GetSize(srcRaster.DataType); int bufferSize = sample * rowStep * typeSize; byte[] databuffer = new byte[bufferSize]; unsafe { fixed(byte *ptr = databuffer) { IntPtr buffer = new IntPtr(ptr); srcRaster.GetRasterBand(bandIndex).Read(oBeginCol, oRow, sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); //byte[] dataNewBf = ClipCutHelper.WriteValueToBuffer(databuffer, oAoi, typeSize); byte[] dataNewBf = WriteValueToBuffer(databuffer, oAoi, typeSize); fixed(byte *p = dataNewBf) { IntPtr newBuffer = new IntPtr(p); if (samplePercent > 0 && samplePercent < 100) { //tProviders.GetRasterBand(1).Fill(999.0d); tProviders.GetRasterBand(bandIndex).Write((int)(tBeginCol * samplePercent * 1f / 100 + 0.5), (int)((tBeginRow + (oRow - oBeginRow)) * samplePercent * 1f / 100 + 0.5), (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5), newBuffer, srcRaster.DataType, (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5)); } else { //tProviders.GetRasterBand(1).Fill(999.0d); tProviders.GetRasterBand(bandIndex).Write(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, newBuffer, srcRaster.DataType, sample, rowStep); } } } } } } if (tProviders != null) { tProviders.Dispose(); } return(tProviders); }