public static string GetBlockFilename(BlockDef blockItem, string filename, string outdir, string driver) { RasterIdentifyForClip rid = new RasterIdentifyForClip(filename); string exts = string.Empty; if (IsAngleFile(filename)) { exts = filename.Substring(filename.IndexOf('.')); } else { exts = GetExtByDriver(driver); } rid.RegionIdentify = blockItem.Name; string outFilename = Path.Combine(outdir, rid.ToWksFileName(exts)); if (!File.Exists(outFilename)) { return(outFilename); } else { while (File.Exists(outFilename)) { outFilename = Path.Combine(outdir, UpdateFilename(outFilename, blockItem.Name, rid)); } } return(outFilename); }
public static Size GetTargetSize(BlockDef blockItem, double lonResolution, double latResolution) { double fWidth = (blockItem.MaxX - blockItem.MinX) / lonResolution; double fHeight = (blockItem.MaxY - blockItem.MinY) / latResolution; return(new Size(GetInteger(fWidth, lonResolution), GetInteger(fHeight, latResolution))); }
public IRasterDataProvider Clip(IRasterDataProvider srcRaster, BlockDef blockDefs, int samplePercent, string driver, string outdir, Action <int, string> progressCallback, out double validPercent, params object[] options) { IRasterDataProvider tProviders = null; if (progressCallback != null) { progressCallback(0, "开始数据分幅"); } //位置映射参数 int tBeginRow = -1, tEndRow = -1, tBeginCol = -1, tEndCol = -1; int oBeginRow = -1, oEndRow = -1, oBeginCol = -1, oEndCol = -1; CoordEnvelope oEnvelope = srcRaster.CoordEnvelope; CoordEnvelope tEnvelope = blockDefs.ToEnvelope(); Size oSize = new Size(srcRaster.Width, srcRaster.Height); Size tSize = ClipCutHelper.GetTargetSize(blockDefs, 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(blockDefs, srcRaster.fileName, outdir, driver); 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); int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, oBeginRow, oEndRow); int sample = (oEndCol - oBeginCol); int typeSize = ClipCutHelper.GetSize(srcRaster.DataType); long allPixelByte = sample * (oEndRow - oBeginRow) * typeSize * srcRaster.BandCount; long validPixelByte = 0; long validPer = (int)(allPixelByte * 0.1f); 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 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); if (validPixelByte < validPer) { foreach (byte b in databuffer) { if (b != 0) { validPixelByte++; } } } if (validPixelByte == 0) { continue; } if (samplePercent > 0 && samplePercent < 100) { 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), buffer, srcRaster.DataType, (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5)); } else { tProviders.GetRasterBand(bandIndex).Write(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, buffer, srcRaster.DataType, sample, rowStep); } } } } } validPercent = validPixelByte * 1.0d / allPixelByte; if (progressCallback != null) { progressCallback(100, "完成数据分幅"); } return(tProviders); }