///将inputRaster拼接到tFilename。 private OutFileArg SpliceToExistFile(IRasterDataProvider inputRaster, string tFilename) { OutFileArg fileArg = new OutFileArg(); string tmpDir = Path.GetDirectoryName(tFilename); string tmpFilename = CreateTempFilename(tmpDir); try { using (IRasterDataProvider tRaster = RasterDataDriver.Open(tFilename) as IRasterDataProvider) { if (inputRaster.BandCount != tRaster.BandCount) { throw new ExceptionExt(2, "待拼接文件和目标文件的波段数不同,无法完成文件拼接[" + inputRaster.fileName + "]" + "[" + tFilename + "]"); } CoordEnvelope tEnv = inputRaster.CoordEnvelope.Union(tRaster.CoordEnvelope); PrjEnvelope tEnvelope = CoordToEnvelope(tEnv); using (IRasterDataProvider tmpFileRaster = CreateMosaicFile(inputRaster, tEnvelope, tmpFilename)) { RasterMoasicProcesser mo = new RasterMoasicProcesser(); mo.Moasic(new IRasterDataProvider[] { inputRaster, tRaster }, tmpFileRaster, true, new string[] { "0" }, action); fileArg.Envelope = new PrjEnvelopeItem("", tEnvelope); fileArg.ResolutionX = tmpFileRaster.ResolutionX.ToString(); fileArg.ResolutionY = tmpFileRaster.ResolutionY.ToString(); } } if (File.Exists(tmpFilename)) { try { string tmpHdrFilename = FilenameIdentify.HdrFileName(tmpFilename); string tmpOverviewFilename = FilenameIdentify.OverviewFileName(tmpFilename); string tHdrFilename = FilenameIdentify.HdrFileName(tFilename); string tOverviewFilename = FilenameIdentify.OverviewFileName(tFilename); File.Delete(tFilename);//删除原文件 if (File.Exists(tHdrFilename)) { File.Delete(tHdrFilename); } if (File.Exists(tOverviewFilename)) { File.Delete(tOverviewFilename); } //拼接后的文件重命名为目标文件 File.Move(tmpFilename, tFilename); fileArg.OutputFilename = Path.GetFileName(tFilename); fileArg.Length = new FileInfo(tFilename).Length; if (File.Exists(tmpHdrFilename)) { File.Move(tmpHdrFilename, tHdrFilename); fileArg.ExtendFiles = Path.GetFileName(tHdrFilename); } if (File.Exists(tmpOverviewFilename)) { File.Move(tmpOverviewFilename, tOverviewFilename); fileArg.Thumbnail = Path.GetFileName(tOverviewFilename); } else if (File.Exists(tFilename)) { OnProgress(0, "生成缩略图"); tOverviewFilename = OverViewHelper.OverView(tFilename, _mosaicSize); OnProgress(100, "完成缩略图"); fileArg.Thumbnail = Path.GetFileName(tOverviewFilename); } } catch (Exception ex) { throw new Exception("拼接完成后,重命名新拼接文件失败。" + ex.Message, ex); } } return(fileArg); } finally { TryDeleteLdfs(tmpFilename); } }
/// <summary> /// 镶嵌 /// </summary> /// <returns></returns> private void MosaicToFile() { using (IRasterDataProvider inputRaster = RasterDataDriver.Open(inArg.InputFilename) as IRasterDataProvider) { StringBuilder str = new StringBuilder(); List <OutFileArg> outFiles = new List <OutFileArg>(); string logLevel = "info"; if (inArg.MosaicEnvelopes == null) { logLevel = "error"; str.AppendLine("镶嵌(Mosaic)必须指定镶嵌输出范围"); return; } else { for (int i = 0; i < inArg.MosaicEnvelopes.Length; i++) { PrjEnvelopeItem envItem = inArg.MosaicEnvelopes[i]; try { if (envItem == null) { str.AppendLine("镶嵌(Mosaic)必须指定镶嵌输出范围"); continue; } if (!ProjectionFactory.HasInvildEnvelope(inputRaster, envItem.PrjEnvelope)) { str.AppendLine("数据不在范围内:" + envItem.Name + envItem.PrjEnvelope.ToString()); continue; } string mosaicFilename = GetSpliceFilename(inputRaster, inArg, envItem.Name); mosaicFilename = Path.Combine(inArg.OutputDir, mosaicFilename); if (!Directory.Exists(inArg.OutputDir)) { Directory.CreateDirectory(inArg.OutputDir); } //文件不存在,则需要先创建目标文件。 IRasterDataProvider mosaicRaster = null; if (!File.Exists(mosaicFilename)) { mosaicRaster = CreateMosaicFile(inputRaster, envItem.PrjEnvelope, mosaicFilename); } else { mosaicRaster = RasterDataDriver.Open(mosaicFilename, enumDataProviderAccess.Update, null) as IRasterDataProvider; } if (File.Exists(mosaicFilename)) { OutFileArg fileArg = new OutFileArg(); try { Mosaic(inputRaster, mosaicRaster); fileArg.Envelope = new PrjEnvelopeItem(envItem.Name, envItem.PrjEnvelope); fileArg.ResolutionX = mosaicRaster.ResolutionX.ToString(); fileArg.ResolutionY = mosaicRaster.ResolutionY.ToString(); fileArg.OutputFilename = mosaicFilename; fileArg.Length = new FileInfo(mosaicFilename).Length; } finally { if (mosaicRaster != null) { mosaicRaster.Dispose(); } } try { string tHdrFilename = FilenameIdentify.HdrFileName(mosaicFilename); string tOverviewFilename = FilenameIdentify.OverviewFileName(mosaicFilename); if (File.Exists(mosaicFilename)) { tOverviewFilename = OverViewHelper.OverView(mosaicFilename, _mosaicSize); fileArg.Thumbnail = Path.GetFileName(tOverviewFilename); } if (File.Exists(tHdrFilename)) { fileArg.ExtendFiles = Path.GetFileName(tHdrFilename); } } catch (Exception ex) { str.AppendLine("拼接完成后,生成缩略图失败" + ex.Message); } outFiles.Add(fileArg); } } catch (Exception ex) { logLevel = "error"; str.AppendLine("镶嵌区域失败:" + envItem.Name + envItem.PrjEnvelope.ToString()); str.AppendLine(" 输入文件:" + inArg.InputFilename); str.AppendLine(" 详细信息:" + ex.Message); GeoDo.Tools.LogFactory.WriteLine(ex); } finally { } } } //写输出参数。 MosaicOutputArg outArg = new MosaicOutputArg(); outArg.InputFilename = Path.GetFileName(inArg.InputFilename); outArg.OutputFiles = outFiles.ToArray(); outArg.LogLevel = logLevel; outArg.LogInfo = str.ToString(); string outXml = FilenameIdentify.OutPutXmlFilename(inArg.OutputDir, inArg.InputFilename); outArg.ToXml(outXml); } }
/// <summary> /// 拼接 /// 需要保证tFilename不存在。 /// </summary> private void SpliceToFile() { using (IRasterDataProvider inputRaster = RasterDataDriver.Open(inArg.InputFilename) as IRasterDataProvider) { StringBuilder str = new StringBuilder(); List <OutFileArg> outFiles = new List <OutFileArg>(); string logLevel = "info"; foreach (PrjEnvelopeItem envItem in inArg.MosaicEnvelopes) { try { if (!ProjectionFactory.HasInvildEnvelope(inputRaster, envItem.PrjEnvelope)) { str.AppendLine("数据不在范围内:" + envItem.Name + envItem.PrjEnvelope.ToString()); continue; } string tFilename = GetSpliceFilename(inputRaster, inArg, envItem.Name); tFilename = Path.Combine(inArg.OutputDir, tFilename); if (!Directory.Exists(inArg.OutputDir)) { Directory.CreateDirectory(inArg.OutputDir); } //文件不存在,直接复制当前文件为目标文件 if (!File.Exists(tFilename)) { File.Copy(inArg.InputFilename, tFilename); OutFileArg fileArg = new OutFileArg(); string tHdrFilename = FilenameIdentify.HdrFileName(tFilename); string tOverviewFilename = FilenameIdentify.OverviewFileName(tFilename); fileArg.Envelope = new PrjEnvelopeItem(envItem.Name, CoordToEnvelope(inputRaster.CoordEnvelope)); fileArg.ResolutionX = inputRaster.ResolutionX.ToString(); fileArg.ResolutionY = inputRaster.ResolutionY.ToString(); fileArg.OutputFilename = tFilename; fileArg.Length = new FileInfo(tFilename).Length; if (File.Exists(FilenameIdentify.HdrFileName(inArg.InputFilename))) { File.Copy(FilenameIdentify.HdrFileName(inArg.InputFilename), tHdrFilename); fileArg.ExtendFiles = Path.GetFileName(tHdrFilename); } if (File.Exists(FilenameIdentify.OverviewFileName(inArg.InputFilename))) { File.Copy(FilenameIdentify.OverviewFileName(inArg.InputFilename), tOverviewFilename); fileArg.Thumbnail = Path.GetFileName(tOverviewFilename); } else if (File.Exists(tFilename)) { OnProgress(0, "生成缩略图"); tOverviewFilename = OverViewHelper.OverView(tFilename, _mosaicSize); OnProgress(100, "完成缩略图"); fileArg.Thumbnail = Path.GetFileName(tOverviewFilename); } outFiles.Add(fileArg); } else { OutFileArg fileArg = SpliceToExistFile(inputRaster, tFilename); fileArg.Envelope.Name = envItem.Name; outFiles.Add(fileArg); } } catch (Exception ex) { logLevel = "error"; str.AppendLine("拼接区域失败:" + envItem.Name + envItem.PrjEnvelope.ToString()); str.AppendLine(" 输入文件:" + inArg.InputFilename); str.AppendLine(" 详细信息:" + ex.Message); GeoDo.Tools.LogFactory.WriteLine(ex); } } //写输出参数。 MosaicOutputArg outArg = new MosaicOutputArg(); outArg.InputFilename = Path.GetFileName(inArg.InputFilename); outArg.OutputFiles = outFiles.ToArray(); outArg.LogLevel = logLevel; outArg.LogInfo = str.ToString(); string outXml = FilenameIdentify.OutPutXmlFilename(inArg.OutputDir, inArg.InputFilename); outArg.ToXml(outXml); } }