private void CheckArg(BlockArg inArg) { if (string.IsNullOrWhiteSpace(inArg.BlockType)) { throw new Exception("节点BlockType必须为以下值中的一个:identify、custom"); } if (inArg.BlockType == "identify") { if (string.IsNullOrWhiteSpace(inArg.BlockIdentify)) { throw new Exception("BlockType为identify时候,BlockIdentify节点必须有值"); } //if (inArg.BlockIdentify != "005" && inArg.BlockIdentify != "010") // throw new Exception("BlockType为identify时候,BlockIdentify节点必须为以下值中的一个:005、010"); } else if (inArg.BlockType == "custom") { if (inArg.BlockEnvelopes == null || inArg.BlockEnvelopes.Length == 0) { throw new Exception("BlockType为custom时候,Blocks节点必须有值"); } } if (string.IsNullOrWhiteSpace(inArg.InputFilename)) { throw new Exception("参数InputFilename为空值"); } if (!File.Exists(inArg.InputFilename)) { throw new Exception("参数InputFilename提供的文件不存在或者不可访问[" + inArg.InputFilename + "]"); } }
public static BlockArg ParseXml(string argXml) { if (string.IsNullOrWhiteSpace(argXml)) { throw new ArgumentNullException("argXml", "参数文件为空"); } if (!File.Exists(argXml)) { throw new FileNotFoundException("参数文件不存在", argXml); } string inputfilename; string outputDir; string blockType = ""; string blockIdentify = ""; PrjEnvelopeItem[] envelopes = null; XElement xml = XElement.Load(argXml); XElement inputfilenameX = xml.Element("InputFilename"); if (inputfilenameX == null || string.IsNullOrWhiteSpace(inputfilenameX.Value)) { inputfilename = null; } inputfilename = inputfilenameX.Value; XElement outputDirX = xml.Element("OutputDir"); outputDir = outputDirX.Value; if (!Directory.Exists(outputDir)) { Directory.CreateDirectory(outputDir); } XElement blockTypeX = xml.Element("BlockType"); if (blockTypeX != null) { blockType = blockTypeX.Value; } if (blockType == "identify") { XElement blockIdentifyX = xml.Element("BlockIdentify"); if (blockIdentifyX != null) { blockIdentify = blockIdentifyX.Value; } } else { envelopes = ParseEnvelopes(xml.Element("Blocks")); } BlockArg arg = new BlockArg(); arg.InputFilename = inputfilename; arg.OutputDir = outputDir; arg.BlockType = blockType; arg.BlockIdentify = blockIdentify; arg.BlockEnvelopes = envelopes; return(arg); }
private BlockDef[] GenericBlock(BlockArg arg) { if (arg.BlockType == "identify") { return(GenericBlockIdentify(arg.BlockIdentify)); } else if (arg.BlockType == "custom") { List <BlockDef> blockList = new List <BlockDef>(); foreach (PrjEnvelopeItem prjEnv in arg.BlockEnvelopes) { PrjEnvelope env = prjEnv.PrjEnvelope; blockList.Add(new BlockDef(prjEnv.Name, env.MinX, env.MinY, env.MaxX, env.MaxY)); } return(blockList.ToArray()); } else { return(null); } }
public void Do(string xmlArg) { BlockArg inArg = BlockArg.ParseXml(xmlArg); string outDir = inArg.OutputDir; IRasterDataProvider inRaster = null; OutputArg outArg = new OutputArg(); try { CheckArg(inArg); BlockDef[] blocks = GenericBlock(inArg); inRaster = GeoDataDriver.Open(inArg.InputFilename) as IRasterDataProvider; DataIdentify dataIdentify = inRaster.DataIdentify; outArg.OrbitFilename = Path.GetFileName(inArg.InputFilename); outArg.Satellite = dataIdentify.Satellite; outArg.Sensor = dataIdentify.Sensor; outArg.Level = "L1"; outArg.ProjectionIdentify = ParseProjectionIdentify(Path.GetFileName(inArg.InputFilename)); outArg.ObservationDate = dataIdentify.OrbitDateTime.ToString("yyyyMMdd"); outArg.ObservationTime = dataIdentify.OrbitDateTime.ToString("HHmm"); outArg.Station = ParseStation(Path.GetFileName(inArg.InputFilename)); List <FileArg> fileArgs = new List <FileArg>(); string retMessage = ""; for (int blockIndex = 0; blockIndex < blocks.Length; blockIndex++) { BlockDef blockDef = blocks[blockIndex]; if (blocks == null) { continue; } CoordEnvelope oEnvelope = inRaster.CoordEnvelope; CoordEnvelope tEnvelope = blockDef.ToEnvelope(); IRasterDataProvider outBlockRasters = null; try { if (!IsInteractived(oEnvelope, tEnvelope)) { continue; } double validPercent = 0; outBlockRasters = _rasterClipProcess.Clip(inRaster, blockDef, 100, "LDF", outDir, new Action <int, string>(OnProgress), out validPercent); if (outBlockRasters == null) { continue; } if (validPercent == 0) { string filename = outBlockRasters.fileName; outBlockRasters.Dispose(); outBlockRasters = null; TryDeleteLdfFiles(filename); continue; } IRasterDataProvider outfileRaster = outBlockRasters; string overViewFilename = OverViewHelper.OverView(outfileRaster, _blockPngSize); FileArg fileArg = new FileArg(); CoordEnvelope env = null; float resolutionX; float resolutionY; env = outfileRaster.CoordEnvelope; resolutionX = outfileRaster.ResolutionX; resolutionY = outfileRaster.ResolutionY; fileArg.OutputFilename = Path.GetFileName(outfileRaster.fileName); fileArg.Thumbnail = (string.IsNullOrWhiteSpace(overViewFilename) ? "" : Path.GetFileName(overViewFilename)); fileArg.ExtendFiles = Path.ChangeExtension(Path.GetFileName(outfileRaster.fileName), "hdr"); fileArg.Envelope = new PrjEnvelopeItem(blockDef.Name, env == null ? null : new RasterProject.PrjEnvelope(env.MinX, env.MaxX, env.MinY, env.MaxY)); fileArg.ResolutionX = resolutionX.ToString(); fileArg.ResolutionY = resolutionY.ToString(); fileArg.Length = new FileInfo(outfileRaster.fileName).Length; fileArgs.Add(fileArg); } catch (Exception ex) { retMessage += (ex.Message + " Block:" + blockDef.Name + tEnvelope == null ? "" : tEnvelope.ToString()); } finally { if (outBlockRasters != null) { if (outBlockRasters != null) { outBlockRasters.Dispose(); } } } } outArg.OutputFiles = fileArgs.ToArray(); outArg.LogLevel = "info"; if (string.IsNullOrWhiteSpace(retMessage)) { outArg.LogInfo = "分幅成功"; } else { outArg.LogInfo = retMessage; } } catch (Exception ex) { LogFactory.WriteLine(ex); outArg.LogLevel = "error"; outArg.LogInfo = ex.Message; Console.WriteLine(ex.Message); } finally { string outXmlFilename = Path.Combine(inArg.OutputDir, Path.GetFileName(inArg.InputFilename) + ".xml"); OutputArg.WriteXml(outArg, outXmlFilename); if (inRaster != null) { inRaster.Dispose(); } } }