/// <summary> /// 创建结果影像数据 /// </summary> /// <param name="imageBox">结果数组</param> /// <param name="imgInfo">影像图层信息</param> public static void createImageLayer(float[] imageBox, Image imgInfo) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "请选择保存路径"; sfd.Filter = "Raster Flie|*.tif"; if (sfd.ShowDialog() != DialogResult.OK) { return; } int[] bandMap = { 1 }; IRasterDataset rs = DatasetFactory.CreateRasterDataset(sfd.FileName, imgInfo.layerW, imgInfo.layerH, 1, PixelDataType.Float32, "GTiff", null); rs.SpatialReference = imgInfo.sr; rs.Write(0, 0, imgInfo.layerW, imgInfo.layerH, imageBox, imgInfo.layerW, imgInfo.layerH, PixelDataType.Float32, 1, bandMap); rs.SetGeoTransform(imgInfo.geoTrans); //处理影像数据空间位置偏移 MessageBox.Show("分析完成"); }
//private string BandNameString(ILdfDataProvider fileRaster) //{ // if (fileRaster == null) // return null; // string[] bandNames = (fileRaster as ILdfDataProvider).Hdr.BandNames; // if (bandNames == null || bandNames.Length == 0) // return null; // string bandNameString = ""; // foreach (string b in bandNames) // { // bandNameString = bandNameString + b + ","; // } // return bandNameString.TrimEnd(','); //} internal AbstractWarpDataset CreateOutFile(string outfilename, int dstBandCount, Size outSize, DataType dataType, PrjEnvelope prjEnv, SpatialReference spaRef, string[] options) { string dir = Path.GetDirectoryName(outfilename); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } Envelope env = new Envelope { MaxX = prjEnv.MaxX, MinX = prjEnv.MinX, MaxY = prjEnv.MaxY, MinY = prjEnv.MinY }; var rDs = DatasetFactory.CreateRasterDataset(outfilename, outSize.Width, outSize.Height, dstBandCount, dataType, "GTiff", null); //env double[] geoTrans = Enumerable.Repeat(0.0, 6).ToArray(); geoTrans[0] = prjEnv.MinX; geoTrans[3] = prjEnv.MaxY; geoTrans[1] = prjEnv.Width / outSize.Width; geoTrans[5] = -prjEnv.Height / outSize.Height; rDs.SetProjection(spaRef.ExportToWkt()); return(new WarpDataset(rDs, outfilename)); }
public AbstractWarpDataset[] Clip(AbstractWarpDataset srcRaster, BlockDef[] blockDefs, int samplePercent, string driver, string outdir, Action <int, string> progressCallback, params object[] options) { AbstractWarpDataset[] tProviders = new AbstractWarpDataset[blockDefs.Length]; if (progressCallback != null) { progressCallback(0, "开始数据分幅"); } for (int blockNums = 0; blockNums < blockDefs.Length; blockNums++) { if (progressCallback != null) { progressCallback((int)((blockNums + 1.0) / blockDefs.Length * 100), string.Format("正在分幅第{0}/{1}个文件...", (blockNums + 1).ToString(), blockDefs.Length)); } //位置映射参数 int tBeginRow = -1, tEndRow = -1, tBeginCol = -1, tEndCol = -1; int oBeginRow = -1, oEndRow = -1, oBeginCol = -1, oEndCol = -1; Envelope oEnvelope = srcRaster.GetEnvelope(); Envelope tEnvelope = blockDefs[blockNums].ToEnvelope(); Size oSize = new Size(srcRaster.Width, srcRaster.Height); Size tSize = ClipCutHelper.GetTargetSize(blockDefs[blockNums], 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[blockNums], srcRaster.fileName, outdir, driver); 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.ExportToProj4(), "MAPINFO={" + 1 + "," + 1 + "}:{" + tEnvelope.MinX + "," + tEnvelope.MaxY + "}:{" + tResolutionX + "," + tResolutionY + "}" }; string[] _options = new string[] { "header_offset=128" }; double[] geoTrans = new double[] { tEnvelope.MinX, Convert.ToDouble(tResolutionX.ToString("f6")), 0, tEnvelope.MaxY, 0, -Convert.ToDouble(tResolutionY.ToString("f6")) }; var rDs = DatasetFactory.CreateRasterDataset(blockFilename, oWidth, oHeight, srcRaster.BandCount, srcRaster.DataType, "ENVI", null); rDs.SetGeoTransform(geoTrans); rDs.SetProjection(srcRaster.SpatialRef.ExportToWkt()); tProviders[blockNums] = new WarpDataset(rDs, blockFilename); int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, oBeginRow, oEndRow); for (int oRow = oBeginRow; oRow < oEndRow; oRow += rowStep) { if (oRow + rowStep > oEndRow) { rowStep = oEndRow - oRow; } for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++) { 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).ReadRaster(oBeginCol, oRow, sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); if (samplePercent > 0 && samplePercent < 100) { tProviders[blockNums].GetRasterBand(bandIndex).WriteRaster( (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, (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5), srcRaster.DataType, 0, 0); } else { tProviders[blockNums].GetRasterBand(bandIndex).WriteRaster(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); } } } } } } return(tProviders); }
public AbstractWarpDataset Clip(AbstractWarpDataset srcRaster, BlockDef blockDefs, int samplePercent, string driver, string outdir, Action <int, string> progressCallback, out double validPercent, params object[] options) { AbstractWarpDataset 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; Envelope oEnvelope = srcRaster.GetEnvelope(); Envelope 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); 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.ExportToProj4(), "MAPINFO={" + 1 + "," + 1 + "}:{" + tEnvelope.MinX + "," + tEnvelope.MaxY + "}:{" + tResolutionX + "," + tResolutionY + "}" }; string[] _options = new string[] { "header_offset=128" }; double[] geoTrans = new double[] { tEnvelope.MinX, Convert.ToDouble(tResolutionX.ToString("f6")), 0, tEnvelope.MaxY, 0, -Convert.ToDouble(tResolutionY.ToString("f6")) }; var rDs = DatasetFactory.CreateRasterDataset(blockFilename, oWidth, oHeight, srcRaster.BandCount, srcRaster.DataType, "ENVI", null); rDs.SetGeoTransform(geoTrans); rDs.SetProjection(srcRaster.SpatialRef.ExportToWkt()); tProviders = new WarpDataset(rDs, blockFilename); 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 = 0; 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).ReadRaster(oBeginCol, oRow, sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); if (validPixelByte < validPer) { foreach (byte b in databuffer) { if (b != 0) { validPixelByte++; } } } if (validPixelByte == 0) { continue; } if (samplePercent > 0 && samplePercent < 100) { tProviders.GetRasterBand(bandIndex).WriteRaster( (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, (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5), srcRaster.DataType, 0, 0); } else { tProviders.GetRasterBand(bandIndex).WriteRaster(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); } } } } } validPercent = validPixelByte * 1.0d / allPixelByte; if (progressCallback != null) { progressCallback(100, "完成数据分幅"); } return(tProviders); }