//应用变量 private void TryApplyVars(IGxdDocument doc, string fileName) { ILayoutTemplate temp = doc.GxdTemplateHost.LayoutTemplate; RasterIdentify identify = new RasterIdentify(fileName); Dictionary <string, string> vars = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(identify.Satellite)) { string sate = identify.Satellite.ToUpper(); if (sate.Contains("FY3")) { sate = sate.Replace("FY3", "FY-3"); } else if (sate.Contains("FY2")) { sate = sate.Replace("FY2", "FY-2"); } vars.Add("{Satellite}", sate); } if (!string.IsNullOrEmpty(identify.Sensor)) { vars.Add("{Sensor}", identify.Sensor); } if (!string.IsNullOrEmpty(identify.ProductName)) { vars.Add("{Product}", identify.ProductName); } if (string.IsNullOrWhiteSpace(DateString)) { if (identify.OrbitDateTime != DateTime.MinValue) { identify.OrbitDateTime.AddHours(8); vars.Add("{OrbitDateTime}", identify.OrbitDateTime.AddHours(8).ToString("yyyy年MM月dd日 HH:mm") + " (北京时)"); } if (identify.MaxOrbitDate != DateTime.MaxValue && identify.MinOrbitDate != DateTime.MinValue) { DateTime minTime = identify.MinOrbitDate.AddHours(8); DateTime maxTime = identify.MaxOrbitDate.AddHours(8); vars.Add("{MinOrbitDateTime~MaxOrbitDateTime}", minTime.ToString("yyyy年MM月dd日 HH:mm") + " ~ " + maxTime.ToString("yyyy年MM月dd日 HH:mm")); } } else { vars.Add("{OrbitDateTime}", DateString); vars.Add("{MinOrbitDateTime~MaxOrbitDateTime}", DateString); } if (!string.IsNullOrWhiteSpace(TimesCount)) { vars.Add("{ValidDays}", "有效天数:" + TimesCount); } else { vars.Add("{ValidDays}", ""); } if (!IsVector(fileName)) { using (RasterDataProvider rdd = GeoDataDriver.Open(fileName) as RasterDataProvider) { if (rdd != null) { string resolutionStr = Math.Round(rdd.ResolutionX, 4).ToString(); vars.Add("{resolution}", resolutionStr); if (rdd.SpatialRef == null) { vars["{resolution}"] += "度"; vars.Add("{projection}", "等经纬度"); } else if (rdd.SpatialRef.GeographicsCoordSystem == null) { vars.Add("{projection}", ""); } else if (rdd.SpatialRef.ProjectionCoordSystem == null) { vars["{resolution}"] += "度"; vars.Add("{projection}", "等经纬度"); } else { string targatName = string.Empty; string projectName = rdd.SpatialRef.ProjectionCoordSystem.Name.Name; GetProjectName(projectName, out targatName); vars.Add("{projection}", targatName); } } } } temp.ApplyVars(vars); }
/// <summary> /// 单文件计算其DVI /// </summary> /// <param name="inputFile"></param> /// <param name="bandNos"></param> /// <param name="zoom"></param> /// <param name="aoiTemplate"></param> /// <param name="progressTracker"></param> /// <returns></returns> private IExtractResult CalcDVIAlgorithm1(string inputFile, int[] bandNos, float zoom, double[] bandZoom, float[] cloudyArgs, Int16 defCloudy, bool isFitterCloud, string aoiTemplate, Action <int, string> progressTracker) { RasterProcessModel <short, short> rfr = null; List <RasterMaper> fileIns = new List <RasterMaper>(); RasterMaper[] fileOuts = null; bool isAppCloud; if (!bool.TryParse(_argumentProvider.GetArg("isAppCloud").ToString(), out isAppCloud)) { PrintInfo("合并交互云参数设置不正确!"); return(null); } IRasterDataProvider clmPrd = null; try { //输入数据(LDF) IRasterDataProvider inRaster = RasterDataDriver.Open(inputFile) as IRasterDataProvider; if (inRaster == null) { PrintInfo("读取栅格文件失败:" + inRaster); return(null); } //输出数据(DVI) string outFileName = GetFileName(new string[] { inRaster.fileName }, _subProductDef.ProductDef.Identify, _identify, ".dat", null); IRasterDataDriver dd = RasterDataDriver.GetDriverByName("MEM") as IRasterDataDriver; string mapInfo = inRaster.CoordEnvelope.ToMapInfoString(new Size(inRaster.Width, inRaster.Height)); string[] opts = new string[] { "INTERLEAVE=BSQ", "VERSION=MEM", "WITHHDR=TRUE", "SPATIALREF=" + inRaster.SpatialRef.ToProj4String(), mapInfo }; RasterDataProvider outRaster = dd.Create(outFileName, inRaster.Width, inRaster.Height, 1, enumDataType.Int16, opts) as RasterDataProvider; string clmFile = GetClmFile(inputFile); int cloudCH = GetCloudCHNO(); //栅格数据映射 fileIns.Add(new RasterMaper(inRaster, bandNos)); if (isAppCloud) { if (!string.IsNullOrEmpty(clmFile) && File.Exists(clmFile)) { clmPrd = GeoDataDriver.Open(clmFile) as IRasterDataProvider; if (clmPrd.BandCount < cloudCH) { PrintInfo("请选择正确的云数据通道进行计算."); isAppCloud = false; } else { fileIns.Add(new RasterMaper(clmPrd, new int[] { cloudCH })); } } else { isAppCloud = false; } } RasterMaper fileOut = new RasterMaper(outRaster, new int[] { 1 }); fileOuts = new RasterMaper[] { fileOut }; //创建处理模型 rfr = new RasterProcessModel <short, short>(progressTracker); rfr.SetRaster(fileIns.ToArray(), fileOuts); rfr.SetTemplateAOI(aoiTemplate); rfr.RegisterCalcModel(new RasterCalcHandler <short, short>((rvInVistor, rvOutVistor, aoi) => { if (rvInVistor[0].RasterBandsData != null) { if (rvInVistor == null) { return; } short[] inBand0 = rvInVistor[0].RasterBandsData[0]; //第1个输入文件的第1个波段的各像素值 short[] inBand1 = rvInVistor[0].RasterBandsData[1]; //第1个输入文件的第2个波段的各像素值 short[] inBand2 = rvInVistor[0].RasterBandsData[2]; //第1个输入文件的第3个波段的各像素值 short[] inBand3 = rvInVistor[0].RasterBandsData[3]; //第1个输入文件的第4个波段的各像素值 short[] inBand12 = isAppCloud ? rvInVistor[1].RasterBandsData[0] : null; //第2个输入文件的第1个波段的各像素值 short[] ndvi = new short[inBand0.Length]; if (string.IsNullOrWhiteSpace(aoiTemplate)) { for (int index = 0; index < inBand0.Length; index++) { if ((isAppCloud && inBand12[index] != 0) || (isFitterCloud && inBand1[index] / bandZoom[1] > cloudyArgs[0] && inBand3[index] / bandZoom[3] < cloudyArgs[1] && Math.Abs(inBand2[index] / bandZoom[2] - inBand3[index] / bandZoom[3]) > cloudyArgs[2])) { rvOutVistor[0].RasterBandsData[0][index] = defCloudy; continue; } //第1个输出文件的第1个波段存储NDVI值 rvOutVistor[0].RasterBandsData[0][index] = (short)((inBand1[index] / bandZoom[1] - inBand0[index] / bandZoom[0]) * zoom / 100); } } else if (aoi != null && aoi.Length != 0) { int index; for (int i = 0; i < aoi.Length; i++) { index = aoi[i]; if ((isAppCloud && inBand12[index] != 0) || (isFitterCloud && inBand1[index] / bandZoom[1] > cloudyArgs[0] && inBand3[index] / bandZoom[3] < cloudyArgs[1] && Math.Abs(inBand2[index] / bandZoom[2] - inBand3[index] / bandZoom[3]) > cloudyArgs[2])) { rvOutVistor[0].RasterBandsData[0][index] = defCloudy; continue; } //第1个输出文件的第1个波段存储NDVI值 rvOutVistor[0].RasterBandsData[0][index] = (short)((inBand1[index] / bandZoom[1] - inBand0[index] / bandZoom[0]) * zoom / 100); } } } })); //执行 rfr.Excute(); FileExtractResult res = new FileExtractResult(_subProductDef.Identify, outFileName, true); res.SetDispaly(false); return(res); } finally { if (fileIns != null) { for (int i = 0; i < fileIns.Count; i++) { fileIns[i].Raster.Dispose(); } } if (fileOuts != null) { for (int i = 0; i < fileOuts.Length; i++) { fileOuts[i].Raster.Dispose(); } } if (clmPrd != null) { clmPrd.Dispose(); } } }
public void Calc() { Stopwatch sw = new Stopwatch(); sw.Start(); Action <int, string> progres = new Action <int, string>( (progress, text) => { //this.progressBar1.Value = progress; //this.Text = text; //Application.DoEvents(); Console.WriteLine(progress); } ); //举例计算NDVI RasterProcessModel <ushort, short> rfr = null; RasterMaper[] fileIns = null; RasterMaper[] fileOuts = null; try { string fin = @"E:\Smart\MAS\ldf\FY3A_MERSI_GBAL_L1_20120517_0235_0250M_MS_GLL_DXX.ldf"; //fin = @"E:\Smart\ldf\FY3A_MERSI_海南省_GLL_L1_20120911_D_0250M_MS.ldf"; fin = @"E:\Smart\MAS\ldf\FY3B_MERSI_20120510_0640.LDF"; RasterDataProvider or = RasterDataDriver.Open(fin) as RasterDataProvider; //初始化输出数据,这里输出为dat,可根据实际需求,自行决定输出文件格式 IRasterDataDriver dd = RasterDataDriver.GetDriverByName("MEM") as IRasterDataDriver; string mapInfo = or.CoordEnvelope.ToMapInfoString(new Size(or.Width * 2, or.Height * 2)); //"MAPINFO={X,Y}:{Col,Row}:{ResolutionX,ResolutionY}"; //mapInfo = "MAPINFO={1,1}:{108.0904,20.1572}:{0.0025,0.0025}"; //输出数据和输入数据的范围可以不一致 RasterDataProvider tr = dd.Create(@"E:\Smart\ldf\FY3A_MERSI_海南省_GLL_L1_20120911_D_0250M_MS_NDVI.dat", or.Width, or.Height, 1, enumDataType.Int16, mapInfo) as RasterDataProvider; tr.GetRasterBand(1).Fill(-999); RasterMaper fileIn = new RasterMaper(or, new int[] { 3, 4 }); RasterMaper fileOut = new RasterMaper(tr, new int[] { 1 }); fileIns = new RasterMaper[] { fileIn }; fileOuts = new RasterMaper[] { fileOut }; //创建处理器 rfr = new RasterProcessModel <ushort, short>(progres); rfr.SetRaster(fileIns, fileOuts); rfr.SetTemplateAOI("vector:海陆模版"); rfr.RegisterCalcModel(new RasterCalcHandler <ushort, short>((rvInVistor, rvOutVistor, aoiIndex) => { if (rvInVistor[0].RasterBandsData != null) { if (rvInVistor == null) { return; } ushort[] inBand0 = rvInVistor[0].RasterBandsData[0]; //第1个输入文件的第1个波段的各像素值 ushort[] inBand1 = rvInVistor[0].RasterBandsData[1]; //第1个输入文件的第2个波段的各像素值 short[] ndvi = new short[inBand0.Length]; if (aoiIndex != null) { int index; for (int i = 0; i < aoiIndex.Length; i++) { index = aoiIndex[i]; rvOutVistor[0].RasterBandsData[0][index] = (short)((inBand1[index] - inBand0[index]) * 1000f / (inBand1[index] + inBand0[index])); } } //else //{ // for (int i = 0; i < inBand0.Length; i++) // { // //第1个输出文件的第1个波段存储NDVI值 // rvOutVistor[0].RasterBandsData[0][i] = (short)((inBand1[i] - inBand0[i]) * 1000f / (inBand1[i] + inBand0[i])); // //通道1和通道2的差值 // //fileOutVistor[0].RasterBandsData[1][p] = (short)(inBand0[p] - inBand1[p]); // } //} } })); rfr.Excute(); } finally { if (fileIns != null) { for (int i = 0; i < fileIns.Length; i++) { fileIns[i].Raster.Dispose(); } } if (fileOuts != null) { for (int i = 0; i < fileOuts.Length; i++) { fileOuts[i].Raster.Dispose(); } } } sw.Stop(); Console.WriteLine((sw.ElapsedMilliseconds / 1000f).ToString() + "秒"); }