private void btnOK_Click(object sender, EventArgs e) { try { BenMAPLine inputBenMAPLine = new BenMAPLine(); string err = ""; inputBenMAPLine = DataSourceCommonClass.LoadAQGFile(txtAirQualitySurface.Text, ref err); if (inputBenMAPLine == null) { MessageBox.Show(err); return; } SaveFileDialog saveCSV = new SaveFileDialog(); saveCSV.Filter = "csv file(*.csv)|*.csv"; saveCSV.FilterIndex = 2; saveCSV.RestoreDirectory = true; saveCSV.InitialDirectory = CommonClass.ResultFilePath + @"\Result\AQG"; if (saveCSV.ShowDialog() == DialogResult.Cancel) { return; } string filePath = saveCSV.FileName.Substring(0, saveCSV.FileName.LastIndexOf(@"\") + 1); string fileName = saveCSV.FileName.Substring(saveCSV.FileName.LastIndexOf(@"\") + 1); DataSourceCommonClass.SaveModelDataLineToNewFormatCSV(inputBenMAPLine, saveCSV.FileName); this.DialogResult = DialogResult.OK; } catch (Exception ex) { Logger.LogError(ex.Message); } }
private void btnMap_Click(object sender, EventArgs e) { try { updataMonitorAdvance(); if (!settingcorrect) { return; } WaitShow("Filtering monitors. Please wait."); mDataLine.MonitorAdvance = MonitorAdvanceFilterTemp; List <MonitorValue> lstMonitorValues = DataSourceCommonClass.GetMonitorData(bcg.GridType, bcg.Pollutant, mDataLine); DataSourceCommonClass.UpdateMonitorDicMetricValue(bcg.Pollutant, lstMonitorValues); string shapeFile = CommonClass.DataFilePath + @"\Data\Shapefiles\United States\State_epa2.shp"; MonitorMap frm = new MonitorMap(); frm.GridShapeFile = shapeFile; frm.LstMonitorPoints = lstMonitorValues; WaitClose(); frm.ShowDialog(); } catch { waitMess.Close(); } }
private void SaveNewFormat_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "csv files (*.csv)|*.csv"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; string _fileName = ""; string _filePath = ""; string shpFile = ""; _filePath = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf(@"\") + 1); if (sfd.ShowDialog() == DialogResult.OK) { switch (_currentStat) { case "baseline": _filePath = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf(@"\") + 1); _fileName = sfd.FileName.Substring(sfd.FileName.LastIndexOf(@"\") + 1).Replace("aqg", "shp"); DataSourceCommonClass.SaveModelDataLineToNewFormatCSV(_bgc.Base, sfd.FileName); break; case "control": string _filePathControl = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf(@"\") + 1); DataSourceCommonClass.SaveModelDataLineToNewFormatCSV(_bgc.Control, sfd.FileName); break; } } }
public void ParaserAQG(string strPath) { try { string tip = "Reading and checking the data file."; _strPath = strPath; WaitShow(tip); string err = ""; BenMAPLine benMapLine = DataSourceCommonClass.LoadAQGFile(txtExistingAQG.Text, ref err); System.Threading.Thread.Sleep(100); if (benMapLine == null) { WaitClose(); MessageBox.Show(err); return; } WaitClose(); if (benMapLine.Pollutant.PollutantID != _bgc.Pollutant.PollutantID) { MessageBox.Show("The AQG's pollutant does not match the selected pollutant. Please select another file."); return; } else if (benMapLine.GridType.GridDefinitionID != _bgc.GridType.GridDefinitionID) { MessageBox.Show("The AQG's grid definition does not match the selected grid definition. Please select another file."); return; } if (benMapLine.ShapeFile != null && !benMapLine.ShapeFile.Contains(@"\")) { string AppPath = Application.StartupPath; string _filePath = txtExistingAQG.Text.Substring(0, txtExistingAQG.Text.LastIndexOf(@"\") + 1); string strShapePath = string.Format("{0}\\Result\\Tmp\\{1}", CommonClass.DataFilePath, benMapLine.ShapeFile); benMapLine.ShapeFile = _filePath + benMapLine.ShapeFile; DataSourceCommonClass.SaveBenMAPLineShapeFile(_bgc.GridType, _bgc.Pollutant, benMapLine, strShapePath); } else if (benMapLine.ShapeFile != null) { DataSourceCommonClass.SaveBenMAPLineShapeFile(_bgc.GridType, _bgc.Pollutant, benMapLine, benMapLine.ShapeFile); } switch (_currentStat) { case "baseline": _bgc.Base = benMapLine; break; case "control": _bgc.Control = benMapLine; break; } this.DialogResult = System.Windows.Forms.DialogResult.OK; } catch (Exception ex) { Logger.LogError(ex); } }
private void CreateShapeFile(BaseControlGroup b, string state, string filePath) { string msg = string.Empty; ESIL.DBUtility.FireBirdHelperBase fb = new ESIL.DBUtility.ESILFireBirdHelper(); ModelDataLine modelDataLine = new ModelDataLine(); try { modelDataLine.DatabaseFilePath = filePath; System.Data.DataTable dtModel = CommonClass.ExcelToDataTable(filePath); DataSourceCommonClass.UpdateModelDataLineFromDataSet(b.Pollutant, modelDataLine, dtModel); switch (state) { case "baseline": b.Base = null; b.Base = modelDataLine; break; case "control": b.Control = null; b.Control = modelDataLine; break; } if (modelDataLine.ModelAttributes.Count == 0) { msg = "Error reading files."; return; } int threadId = -1; AsyncDelegate asyncD = new AsyncDelegate(AsyncCreateFile); IAsyncResult ar = asyncD.BeginInvoke(b, modelDataLine, state, out threadId, null, null); return; } catch (Exception ex) { Logger.LogError(ex); return; } finally { if (msg != string.Empty) { MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }
private void btnSave_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "aqg files (*.aqgx)|*.aqgx"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; sfd.InitialDirectory = CommonClass.ResultFilePath + @"\Result\AQG"; string _fileName = ""; string _filePath = ""; string shpFile = ""; _filePath = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf(@"\") + 1); FeatureSet fs = new FeatureSet(); if (sfd.ShowDialog() == DialogResult.OK) { switch (_currentStat) { case "baseline": _filePath = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf(@"\") + 1); _fileName = sfd.FileName.Substring(sfd.FileName.LastIndexOf(@"\") + 1).Replace("aqgx", "shp"); DataSourceCommonClass.CreateAQGFromBenMAPLine(_bgc.Base, sfd.FileName); _bgc.Base.ShapeFile = shpFile; break; case "control": string _filePathControl = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf(@"\") + 1); _fileName = sfd.FileName.Substring(sfd.FileName.LastIndexOf(@"\") + 1).Replace("aqgx", "shp"); DataSourceCommonClass.CreateAQGFromBenMAPLine(_bgc.Control, sfd.FileName); _bgc.Control.ShapeFile = shpFile; break; } MessageBox.Show("AQG saved.", "File saved"); } }
private void btnOK_Click(object sender, EventArgs e) { try { if (txtAirQualitySurface.Text != "") { string err = ""; BenMAPLine benMAPLine = DataSourceCommonClass.LoadAQGFile(txtAirQualitySurface.Text, ref err); if (benMAPLine == null) { MessageBox.Show(err); return; } if (benMAPLine is MonitorDataLine) { MonitorDataLine monitorDataLine = (benMAPLine as MonitorDataLine); if (monitorDataLine.MonitorNeighbors != null && monitorDataLine.MonitorNeighbors.Count > 0) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Neighbors files (*.csv)|*.csv"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; sfd.InitialDirectory = CommonClass.ResultFilePath + @"\Result\AQG"; string _filePath = ""; _filePath = sfd.FileName.Substring(0, sfd.FileName.LastIndexOf(@"\") + 1); if (sfd.ShowDialog() == DialogResult.OK) { DataTable dt = new DataTable(); dt.Columns.Add("Col"); dt.Columns.Add("Row"); dt.Columns.Add("MonitorName"); dt.Columns.Add("Weight"); dt.Columns.Add("Distance"); foreach (MonitorNeighborAttribute m in monitorDataLine.MonitorNeighbors) { DataRow dr = dt.NewRow(); dr["Col"] = m.Col; dr["Row"] = m.Row; dr["MonitorName"] = m.MonitorName; dr["Weight"] = m.Weight; dr["Distance"] = m.Distance; dt.Rows.Add(dr); } CommonClass.SaveCSV(dt, sfd.FileName); } } else { MessageBox.Show("No neighbors in this air quality grid."); return; } } else { MessageBox.Show("This air quality grid is not a monitor direct grid."); return; } } } catch { } }
private bool DrawMointorMap() { bool ok = false; double value = 0.0; string msg = string.Empty; try { if (rbtnClosestMonitor.Checked) { _mDataLine.InterpolationMethod = InterpolationMethodEnum.ClosestMonitor; } else if (rbtnVoronoi.Checked) { _mDataLine.InterpolationMethod = InterpolationMethodEnum.VoronoiNeighborhoodAveragin; } else if (rbtnFixedRadiums.Checked) { _mDataLine.InterpolationMethod = InterpolationMethodEnum.FixedRadius; ok = double.TryParse(txtRadiums.Text, out value); if (!ok) { msg = "To select fixed radius interpolation you must provide a radius in kilometers."; txtRadiums.Text = string.Empty; return(false); } _mDataLine.FixedRadius = value; } int selectedIndex = cboMonitorType.SelectedIndex; switch (selectedIndex) { case 0: _mDataLine.MonitorDirectType = selectedIndex; if (cboMonitorDataSet.Text != string.Empty && cboMonitorLibraryYear.Text != string.Empty) { DataRowView drv = cboMonitorDataSet.SelectedItem as DataRowView; int dsID = int.Parse(drv["MonitorDataSetID"].ToString()); _mDataLine.MonitorDataSetID = dsID; drv = cboMonitorLibraryYear.SelectedItem as DataRowView; _mDataLine.MonitorLibraryYear = int.Parse(drv["Yyear"].ToString()); } else { msg = string.Format("Settings are not complete."); return(false); } _strPath = "Monitor data: library"; break; case 1: _mDataLine.MonitorDirectType = selectedIndex; if (txtMonitorDataFile.Text != string.Empty) { _mDataLine.MonitorDataFilePath = txtMonitorDataFile.Text; } else { msg = string.Format("Settings are not complete."); return(false); } _strPath = "Monitor data: " + txtMonitorDataFile.Text; break; } if (_monitorAdvance != null) { _mDataLine.MonitorAdvance = _monitorAdvance; } ModelDataLine m = new ModelDataLine(); switch (_currentStat) { case "baseline": DataSourceCommonClass.UpdateModelValuesMonitorData(_bgcMonitor.GridType, _bgcMonitor.Pollutant, ref _mDataLine); lock (CommonClass.LstBaseControlGroup) { foreach (BaseControlGroup bc in CommonClass.LstBaseControlGroup) { if (bc.Pollutant.PollutantID == _bgcMonitor.Pollutant.PollutantID) { _mDataLine.GridType = _bgcMonitor.GridType; _mDataLine.Pollutant = _bgcMonitor.Pollutant; _mDataLine.ShapeFile = _mDataLine.Pollutant.PollutantID + "G" + CommonClass.GBenMAPGrid.GridDefinitionID + "B" + _currentStat + ".shp"; string shipFile = string.Format("{0}\\Tmp\\{1}", CommonClass.DataFilePath, _mDataLine.ShapeFile); bc.Base = _mDataLine; DataSourceCommonClass.SaveBenMAPLineShapeFile(_mDataLine.GridType, _mDataLine.Pollutant, _mDataLine, shipFile); bc.Base.ShapeFile = ""; } } } _bgcMonitor.Base = _mDataLine; break; case "control": DataSourceCommonClass.UpdateModelValuesMonitorData(_bgcMonitor.GridType, _bgcMonitor.Pollutant, ref _mDataLine); lock (CommonClass.LstBaseControlGroup) { foreach (BaseControlGroup bc in CommonClass.LstBaseControlGroup) { if (bc.Pollutant.PollutantID == _bgcMonitor.Pollutant.PollutantID) { _mDataLine.GridType = _bgcMonitor.GridType; _mDataLine.Pollutant = _bgcMonitor.Pollutant; _mDataLine.ShapeFile = _mDataLine.Pollutant.PollutantID + "G" + CommonClass.GBenMAPGrid.GridDefinitionID + "C" + _currentStat + ".shp"; string shipFile = string.Format("{0}\\Tmp\\{1}", CommonClass.DataFilePath, _mDataLine.ShapeFile); bc.Control = _mDataLine; DataSourceCommonClass.SaveBenMAPLineShapeFile(_mDataLine.GridType, _mDataLine.Pollutant, _mDataLine, shipFile); bc.Control.ShapeFile = ""; } } } _bgcMonitor.Control = _mDataLine; break; } return(true); } catch (Exception ex) { Logger.LogError(ex); return(false); } }
private void btnMap_Click(object sender, EventArgs e) { bool ok = false; double value = 0; string msg = string.Empty; try { if (rbtnClosestMonitor.Checked) { _mDataLine.InterpolationMethod = InterpolationMethodEnum.ClosestMonitor; } else if (rbtnVoronoi.Checked) { _mDataLine.InterpolationMethod = InterpolationMethodEnum.VoronoiNeighborhoodAveragin; } else if (rbtnFixedRadiums.Checked) { _mDataLine.InterpolationMethod = InterpolationMethodEnum.FixedRadius; ok = double.TryParse(txtRadiums.Text, out value); if (!ok) { msg = "To select fixed radius interpolation you must provide a radius in kilometers."; txtRadiums.Text = string.Empty; return; } _mDataLine.FixedRadius = value; } int selectedIndex = cboMonitorType.SelectedIndex; switch (selectedIndex) { case 0: _mDataLine.MonitorDirectType = selectedIndex; if (cboMonitorDataSet.Text != string.Empty && cboMonitorLibraryYear.Text != string.Empty) { DataRowView drv = cboMonitorDataSet.SelectedItem as DataRowView; int dsID = int.Parse(drv["MonitorDataSetID"].ToString()); _mDataLine.MonitorDataSetID = dsID; drv = cboMonitorLibraryYear.SelectedItem as DataRowView; _mDataLine.MonitorLibraryYear = int.Parse(drv["Yyear"].ToString()); } else { msg = string.Format("Settings are not complete."); return; } _strPath = "Monitor data: library"; break; case 1: _mDataLine.MonitorDirectType = selectedIndex; if (txtMonitorDataFile.Text != string.Empty) { _mDataLine.MonitorDataFilePath = txtMonitorDataFile.Text; } else { msg = string.Format("Settings are not complete."); return; } _strPath = "Monitor data: " + txtMonitorDataFile.Text; break; } if (_monitorAdvance != null) { _mDataLine.MonitorAdvance = _monitorAdvance; } _mDataLine.GridType = _bgcMonitor.GridType; _mDataLine.Pollutant = _bgcMonitor.Pollutant; _mDataLine.ShapeFile = _mDataLine.Pollutant.PollutantID + "G" + CommonClass.GBenMAPGrid.GridDefinitionID + "M" + _currentStat + ".shp"; string shapeFile = string.Format("{0}\\Tmp\\{1}", CommonClass.DataFilePath, _mDataLine.ShapeFile); List <MonitorValue> lstMonitorValues = DataSourceCommonClass.GetMonitorData(_bgcMonitor.GridType, _bgcMonitor.Pollutant, _mDataLine); DataSourceCommonClass.UpdateMonitorDicMetricValue(_bgcMonitor.Pollutant, lstMonitorValues); if (_bgcMonitor.GridType is ShapefileGrid) { shapeFile = CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.MainSetup.SetupName + "\\" + (_bgcMonitor.GridType as ShapefileGrid).ShapefileName + ".shp"; } else if (_bgcMonitor.GridType is RegularGrid) { shapeFile = CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.MainSetup.SetupName + "\\" + (_bgcMonitor.GridType as RegularGrid).ShapefileName + ".shp"; } MonitorMap frm = new MonitorMap(); frm.GridShapeFile = shapeFile; frm.LstMonitorPoints = lstMonitorValues; frm.ShowDialog(); } catch (Exception ex) { Logger.LogError(ex); } }
private void btnExport_Click(object sender, EventArgs e) { try { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "CSV File|*.csv"; if (saveFileDialog1.ShowDialog() == DialogResult.Cancel) { return; } lblExport.Text = "Checking the monitor filter setting..."; lblExport.Refresh(); updataMonitorAdvance(); if (!settingcorrect) { lblExport.Text = ""; return; } lblExport.Text = ""; lblExport.Refresh(); prgBarExport.Visible = true; mDataLine.MonitorAdvance = MonitorAdvanceFilterTemp; List <MonitorValue> lstMonitorValues = DataSourceCommonClass.GetMonitorData(bcg.GridType, bcg.Pollutant, mDataLine); prgBarExport.Maximum = lstMonitorValues.Count(); prgBarExport.Value = 0; FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.UTF8); sw.WriteLine("Monitor Name,Monitor Description,Latitude,Longitude,Metric,Seasonal Metric,Statistic,Values"); foreach (MonitorValue mv in lstMonitorValues) { sw.Write(mv.MonitorName); sw.Write(","); sw.Write("\"" + mv.MonitorMethod + "\""); sw.Write(","); sw.Write(mv.Latitude); sw.Write(","); sw.Write(mv.Longitude); sw.Write(","); if (mv.Metric != null) { sw.Write(mv.Metric.MetricName); } sw.Write(","); if (mv.SeasonalMetric != null) { sw.Write(mv.SeasonalMetric.SeasonalMetricName); } sw.Write(","); sw.Write(mv.Statistic); sw.Write(","); string value = string.Empty; string commandText = string.Format("select VValues from MonitorEntries where MonitorID={0}", mv.MonitorID); ESIL.DBUtility.FireBirdHelperBase fb = new ESIL.DBUtility.ESILFireBirdHelper(); byte[] blob = null; object ob = fb.ExecuteScalar(CommonClass.Connection, CommandType.Text, commandText); blob = ob as byte[]; value = System.Text.Encoding.Default.GetString(blob); sw.Write("\"" + value + "\""); sw.WriteLine(); prgBarExport.Value++; prgBarExport.Refresh(); } sw.Flush(); sw.Close(); fs.Close(); prgBarExport.Visible = false; MessageBox.Show("Export is complete.", "File saved", MessageBoxButtons.OK); } catch { prgBarExport.Visible = false; } }
private string AsyncCreateFile(BaseControlGroup bcg, ModelDataLine m, string currentStat, out int threadId) { threadId = -1; string shapeFile = string.Empty; string strShapePath = string.Empty; string AppPath = Application.StartupPath; string str = string.Empty; try { if (CommonClass.LstAsynchronizationStates == null) { CommonClass.LstAsynchronizationStates = new List <string>(); } lock (CommonClass.LstAsynchronizationStates) { str = string.Format("{0}{1}", bcg.Pollutant.PollutantName.ToLower(), currentStat); CommonClass.LstAsynchronizationStates.Add(str); if (currentStat != "") { CommonClass.CurrentMainFormStat = currentStat.Substring(0, 1).ToUpper() + currentStat.Substring(1) + " is being created."; } } lock (CommonClass.NodeAnscyStatus) { CommonClass.NodeAnscyStatus = string.Format("{0};{1};on", bcg.Pollutant.PollutantName.ToLower(), _currentStat); } DateTime dt = DateTime.Now; shapeFile = string.Format("{0}{1}{2}{3}{4}{5}.shp", new string[] { bcg.Pollutant.PollutantName.ToLower(), currentStat, dt.ToString("yyyyMMdd"), dt.Hour.ToString("00"), dt.Minute.ToString("00"), dt.Second.ToString("00") }); Random random = new Random(); shapeFile = string.Format("{0}{1}{2}.shp", new string[] { bcg.Pollutant.PollutantName.ToLower(), currentStat, random.Next(100).ToString() }); shapeFile = bcg.Pollutant.PollutantID + "G" + CommonClass.GBenMAPGrid.GridDefinitionID + "B" + currentStat + ".shp"; strShapePath = string.Format("{0}\\Tmp\\{1}", CommonClass.DataFilePath, shapeFile); Dictionary <int, string> dicSeasonStatics = new Dictionary <int, string>(); DataSourceCommonClass.UpdateModelValuesModelData(DataSourceCommonClass.DicSeasonStaticsAll, bcg.GridType, bcg.Pollutant, m, strShapePath); lock (CommonClass.LstAsynchronizationStates) { CommonClass.LstAsynchronizationStates.Remove(str); if (CommonClass.LstAsynchronizationStates.Count == 0) { CommonClass.CurrentMainFormStat = "Current Setup: " + CommonClass.MainSetup.SetupName; } } lock (CommonClass.NodeAnscyStatus) { CommonClass.NodeAnscyStatus = string.Format("{0};{1};off", bcg.Pollutant.PollutantName.ToLower(), _currentStat); } switch (_currentStat) { case "baseline": DataSourceCommonClass.CreateAQGFromBenMAPLine(saveBCG.Base, saveAQGPath); saveBCG.Base.ShapeFile = ""; break; case "control": DataSourceCommonClass.CreateAQGFromBenMAPLine(saveBCG.Control, saveAQGPath); saveBCG.Control.ShapeFile = ""; break; } return(str); } catch (Exception ex) { Logger.LogError(ex); return(string.Empty); } }
private void openAQG(string sourceFilePath, string currentState, BaseControlGroup bcg) { try { string err = ""; BenMAPLine benMapLine = DataSourceCommonClass.LoadAQGFile(sourceFilePath, ref err); if (benMapLine == null) { WaitClose(); MessageBox.Show(err); return; } if (bcg.Pollutant != null && benMapLine.Pollutant.PollutantID != bcg.Pollutant.PollutantID) { WaitClose(); MessageBox.Show("The AQG's pollutant does not match the selected pollutant. Please select another file."); if (isGridTypeChanged) { CommonClass.GBenMAPGrid = benMAPGridOld; isGridTypeChanged = false; } return; } else if (benMapLine.GridType.GridDefinitionID != bcg.GridType.GridDefinitionID) { WaitClose(); MessageBox.Show("The AQG's grid definition does not match the selected grid definition. Please select another file."); if (isGridTypeChanged) { CommonClass.GBenMAPGrid = benMAPGridOld; isGridTypeChanged = false; } return; } if (bcg.Pollutant == null) { bcg.Pollutant = benMapLine.Pollutant; } if (benMapLine.ShapeFile != null && !benMapLine.ShapeFile.Contains(@"\")) { string AppPath = Application.StartupPath; string _filePath = sourceFilePath.Substring(0, sourceFilePath.LastIndexOf(@"\") + 1); string strShapePath = string.Format("{0}\\Tmp\\{1}", CommonClass.DataFilePath, benMapLine.ShapeFile); benMapLine.ShapeFile = _filePath + benMapLine.ShapeFile; DataSourceCommonClass.SaveBenMAPLineShapeFile(bcg.GridType, bcg.Pollutant, benMapLine, strShapePath); } else if (benMapLine.ShapeFile != null && benMapLine.ShapeFile.Contains(@"\")) { DataSourceCommonClass.SaveBenMAPLineShapeFile(bcg.GridType, bcg.Pollutant, benMapLine, benMapLine.ShapeFile); } switch (currentState) { case "baseline": if (bcg.Pollutant == null || bcg.Pollutant.PollutantID == benMapLine.Pollutant.PollutantID) { bcg.Base = benMapLine; if (CommonClass.LstPollutant == null || CommonClass.LstPollutant.Count == 0) { CommonClass.LstPollutant = new List <BenMAPPollutant>(); CommonClass.LstPollutant.Add(benMapLine.Pollutant); bcg.Pollutant = benMapLine.Pollutant; } } break; case "control": if (bcg.Pollutant == null || bcg.Pollutant.PollutantID == benMapLine.Pollutant.PollutantID) { bcg.Control = benMapLine; } if (CommonClass.LstPollutant == null || CommonClass.LstPollutant.Count == 0) { CommonClass.LstPollutant = new List <BenMAPPollutant>(); CommonClass.LstPollutant.Add(benMapLine.Pollutant); bcg.Pollutant = benMapLine.Pollutant; } break; } } catch (Exception ex) { Logger.LogError(ex); } }
private void btnOK_Click(object sender, EventArgs e) { try { List <ModelResultAttribute> lstResult = new List <ModelResultAttribute>(); BenMAPLine inputBenMAPLine = new BenMAPLine(); List <ModelResultAttribute> outModelResultAttributes = new List <ModelResultAttribute>(); List <Dictionary <string, float> > lstBig = new List <Dictionary <string, float> >(); List <ModelResultAttribute> lstSmall = new List <ModelResultAttribute>(); string err = ""; inputBenMAPLine = DataSourceCommonClass.LoadAQGFile(txtAirQualitySurface.Text, ref err); if (inputBenMAPLine == null) { MessageBox.Show(err); return; } DataRowView drOutput = (cboAggregationSurface.SelectedItem) as DataRowView; int outputGridDefinitionID = Convert.ToInt32(drOutput["GridDefinitionID"]); if (inputBenMAPLine.GridType.GridDefinitionID == outputGridDefinitionID) { MessageBox.Show("The AQG file you want to aggregate is already at the spatial resolution of the selected aggregation surface."); return; } SaveFileDialog saveOutAQG = new SaveFileDialog(); saveOutAQG.Filter = "aqgx file|*.aqgx"; saveOutAQG.InitialDirectory = CommonClass.ResultFilePath + @"\Result\AQG"; saveOutAQG.RestoreDirectory = true; if (saveOutAQG.ShowDialog() == DialogResult.Cancel) { return; } WaitShow("Aggregating air quality surface..."); string filePath = saveOutAQG.FileName.Substring(0, saveOutAQG.FileName.LastIndexOf(@"\") + 1); string fileName = saveOutAQG.FileName.Substring(saveOutAQG.FileName.LastIndexOf(@"\") + 1).Replace("aqgx", "shp"); int GridFrom = inputBenMAPLine.GridType.GridDefinitionID; if (GridFrom == 28) { GridFrom = 27; } int GridTo = outputGridDefinitionID; if (GridTo == 28) { GridTo = 27; } if (GridFrom == GridTo) { outModelResultAttributes = inputBenMAPLine.ModelResultAttributes; } else { string str = string.Format("select sourcecolumn, sourcerow, targetcolumn, targetrow,Percentage from GridDefinitionPercentages a,GridDefinitionPercentageEntries b where a.PercentageID=b.PercentageID and a.SourceGridDefinitionID={0} and a.TargetGridDefinitionID={1} and normalizationstate in (0,1)", GridFrom, GridTo); ESIL.DBUtility.FireBirdHelperBase fb = new ESIL.DBUtility.ESILFireBirdHelper(); DataSet ds; int iCount = Convert.ToInt32(fb.ExecuteScalar(CommonClass.Connection, CommandType.Text, "select count(*) from (" + str + " ) a")); if (iCount == 0) { Configuration.ConfigurationCommonClass.creatPercentageToDatabase(GridTo, GridFrom); iCount = 1; } if (iCount != 0) { Dictionary <string, Dictionary <string, double> > dicRelationShip = new Dictionary <string, Dictionary <string, double> >(); ds = fb.ExecuteDataset(CommonClass.Connection, CommandType.Text, str); foreach (DataRow dr in ds.Tables[0].Rows) { if (dicRelationShip.ContainsKey(dr["targetcolumn"].ToString() + "," + dr["targetrow"].ToString())) { if (!dicRelationShip[dr["targetcolumn"].ToString() + "," + dr["targetrow"].ToString()].ContainsKey(dr["sourcecolumn"].ToString() + "," + dr["sourcerow"].ToString())) { dicRelationShip[dr["targetcolumn"].ToString() + "," + dr["targetrow"].ToString()].Add(dr["sourcecolumn"].ToString() + "," + dr["sourcerow"].ToString(), Convert.ToDouble(dr["Percentage"])); } } else { dicRelationShip.Add(dr["targetcolumn"].ToString() + "," + dr["targetrow"].ToString(), new Dictionary <string, double>()); dicRelationShip[dr["targetcolumn"].ToString() + "," + dr["targetrow"].ToString()].Add(dr["sourcecolumn"].ToString() + "," + dr["sourcerow"].ToString(), Convert.ToDouble(dr["Percentage"])); } } ds.Dispose(); Dictionary <string, ModelResultAttribute> dicModelFrom = new Dictionary <string, ModelResultAttribute>(); foreach (ModelResultAttribute mo in inputBenMAPLine.ModelResultAttributes) { dicModelFrom.Add(mo.Col + "," + mo.Row, mo); } foreach (KeyValuePair <string, Dictionary <string, double> > gra in dicRelationShip) { if (gra.Value == null || gra.Value.Count == 0) { continue; } ModelResultAttribute anew = new ModelResultAttribute(); anew.Col = Convert.ToInt32(gra.Key.Split(new char[] { ',' }).ToArray()[0]); anew.Row = Convert.ToInt32(gra.Key.Split(new char[] { ',' }).ToArray()[1]); anew.Values = new Dictionary <string, float>(); Dictionary <string, float> dicValue = new Dictionary <string, float>(); for (int i = 0; i < gra.Value.Count; i++) { if (dicModelFrom.ContainsKey(gra.Value.ToList()[i].Key)) { foreach (KeyValuePair <string, float> k in dicModelFrom[gra.Value.ToList()[i].Key].Values) { if (!anew.Values.ContainsKey(k.Key)) { anew.Values.Add(k.Key, Convert.ToSingle(k.Value * gra.Value.ToList()[i].Value)); } else { anew.Values[k.Key] += Convert.ToSingle(k.Value * gra.Value.ToList()[i].Value); } } } } if (anew.Values != null && anew.Values.Count > 0) { List <string> lstKey = anew.Values.Keys.ToList(); foreach (string k in lstKey) { anew.Values[k] = Convert.ToSingle(anew.Values[k] / gra.Value.Sum(p => p.Value)); } } outModelResultAttributes.Add(anew); } } } inputBenMAPLine.ModelResultAttributes = outModelResultAttributes; inputBenMAPLine.ModelAttributes = null; inputBenMAPLine.GridType = Grid.GridCommon.getBenMAPGridFromID(outputGridDefinitionID); DataSourceCommonClass.CreateAQGFromBenMAPLine(inputBenMAPLine, saveOutAQG.FileName); WaitClose(); MessageBox.Show("File saved.", "File saved"); this.DialogResult = DialogResult.OK; } catch (Exception ex) { WaitClose(); Logger.LogError(ex.Message); } }
private void btnGo_Click(object sender, EventArgs e) { try { bool ok = false; double value = 0.0; if (rbtnClosestMonitor.Checked) { _monitorRollbackLine.InterpolationMethod = InterpolationMethodEnum.ClosestMonitor; } else if (rbtnVoronoiNeighborhood.Checked) { _monitorRollbackLine.InterpolationMethod = InterpolationMethodEnum.VoronoiNeighborhoodAveragin; } else if (rbtnFixedRadius.Checked) { _monitorRollbackLine.InterpolationMethod = InterpolationMethodEnum.FixedRadius; ok = double.TryParse(txtFixRadio.Text, out value); if (!ok || Convert.ToDouble(txtFixRadio.Text) <= 0) { MessageBox.Show("To select fixed radius interpolation you must provide a radius in kilometers."); txtFixRadio.Text = string.Empty; return; } _monitorRollbackLine.FixedRadius = value; } MonitorDataLine baselinegrid = new MonitorDataLine(); SaveFileDialog sfd = new SaveFileDialog(); if (chbMakeBaselineGrid.Checked) { baselinegrid = new MonitorDataLine() { CreateTime = _monitorRollbackLine.CreateTime, GridType = _monitorRollbackLine.GridType, FixedRadius = _monitorRollbackLine.FixedRadius, InterpolationMethod = _monitorRollbackLine.InterpolationMethod, MonitorAdvance = _monitorRollbackLine.MonitorAdvance, MonitorDataFilePath = _monitorRollbackLine.MonitorDataFilePath, MonitorDataSetID = _monitorRollbackLine.MonitorDataSetID, MonitorDefinitionFile = _monitorRollbackLine.MonitorDefinitionFile, MonitorDirectType = _monitorRollbackLine.MonitorDirectType, MonitorLibraryYear = _monitorRollbackLine.MonitorLibraryYear, MonitorNeighbors = _monitorRollbackLine.MonitorNeighbors, Pollutant = _monitorRollbackLine.Pollutant, ShapeFile = _monitorRollbackLine.ShapeFile }; sfd.Title = "Save the baseline Grid."; sfd.Filter = "AGQ files (*.aqgx)|*.aqgx"; sfd.RestoreDirectory = true; sfd.InitialDirectory = CommonClass.ResultFilePath + @"\Result\AQG"; if (sfd.ShowDialog() != DialogResult.OK) { return; } } SaveFileDialog sfd2 = new SaveFileDialog(); sfd2.Title = "Save the rolled back Grid."; sfd2.Filter = "AGQ files (*.aqgx)|*.aqgx"; sfd2.RestoreDirectory = true; sfd2.InitialDirectory = CommonClass.ResultFilePath + @"\Result\AQG"; if (sfd2.ShowDialog() != DialogResult.OK) { return; } if (chbMakeBaselineGrid.Checked) { WaitShow("Saving the baseline grid..."); DataSourceCommonClass.UpdateModelValuesMonitorData(baselinegrid.GridType, baselinegrid.Pollutant, ref baselinegrid); DataSourceCommonClass.CreateAQGFromBenMAPLine(baselinegrid, sfd.FileName); WaitClose(); makeBaselineGrid = "T" + sfd.FileName; } else { makeBaselineGrid = "F"; } saveAQGPath = sfd2.FileName; int threadId = -1; AsynDelegateRollBack asyncD = new AsynDelegateRollBack(AsyncUpdateMonitorRollbackData); IAsyncResult ar = asyncD.BeginInvoke(_currentStat, _monitorRollbackLine, out threadId, null, null); this.DialogResult = DialogResult.OK; } catch (Exception ex) { WaitClose(); Logger.LogError(ex); } }
private string AsyncUpdateMonitorRollbackData(string currentStat, MonitorModelRollbackLine monitorRollbackLine, out int threadId) { threadId = -1; string str = string.Empty; try { if (CommonClass.LstAsynchronizationStates == null) { CommonClass.LstAsynchronizationStates = new List <string>(); } lock (CommonClass.LstAsynchronizationStates) { str = string.Format("{0}{1}", monitorRollbackLine.Pollutant.PollutantName.ToLower(), currentStat); CommonClass.LstAsynchronizationStates.Add(str); if (currentStat != "") { CommonClass.CurrentMainFormStat = currentStat.Substring(0, 1).ToUpper() + currentStat.Substring(1) + " is being created."; } } lock (CommonClass.NodeAnscyStatus) { CommonClass.NodeAnscyStatus = string.Format("{0};{1};on", monitorRollbackLine.Pollutant.PollutantName.ToLower(), _currentStat); } switch (_currentStat) { case "baseline": RollBackDalgorithm.UpdateMonitorDataRollBack(ref _monitorRollbackLine); lock (CommonClass.LstBaseControlGroup) { foreach (BaseControlGroup bc in CommonClass.LstBaseControlGroup) { if (bc.Pollutant.PollutantID == _monitorRollbackLine.Pollutant.PollutantID) { _monitorRollbackLine.ShapeFile = _monitorRollbackLine.Pollutant.PollutantID + "G" + CommonClass.GBenMAPGrid.GridDefinitionID + "B" + _currentStat + ".shp"; string shipFile = string.Format("{0}\\Tmp\\{1}", CommonClass.DataFilePath, _monitorRollbackLine.ShapeFile); bc.Base = _monitorRollbackLine; DataSourceCommonClass.SaveBenMAPLineShapeFile(_monitorRollbackLine.GridType, _monitorRollbackLine.Pollutant, _monitorRollbackLine, shipFile); DataSourceCommonClass.CreateAQGFromBenMAPLine(bc.Base, saveAQGPath); bc.Base.ShapeFile = ""; } } } break; case "control": RollBackDalgorithm.UpdateMonitorDataRollBack(ref _monitorRollbackLine); lock (CommonClass.LstBaseControlGroup) { foreach (BaseControlGroup bc in CommonClass.LstBaseControlGroup) { if (bc.Pollutant.PollutantID == _monitorRollbackLine.Pollutant.PollutantID) { _monitorRollbackLine.ShapeFile = _monitorRollbackLine.Pollutant.PollutantID + "G" + CommonClass.GBenMAPGrid.GridDefinitionID + "C" + _currentStat + ".shp"; string shipFile = string.Format("{0}\\Tmp\\{1}", CommonClass.DataFilePath, _monitorRollbackLine.ShapeFile); bc.Control = _monitorRollbackLine; DataSourceCommonClass.SaveBenMAPLineShapeFile(_monitorRollbackLine.GridType, _monitorRollbackLine.Pollutant, _monitorRollbackLine, shipFile); DataSourceCommonClass.CreateAQGFromBenMAPLine(bc.Control, saveAQGPath); bc.Control.ShapeFile = ""; } } } break; } lock (CommonClass.LstAsynchronizationStates) { CommonClass.LstAsynchronizationStates.Remove(str); if (CommonClass.LstAsynchronizationStates.Count == 0) { CommonClass.CurrentMainFormStat = "Current Setup: " + CommonClass.MainSetup.SetupName; } } lock (CommonClass.NodeAnscyStatus) { CommonClass.NodeAnscyStatus = string.Format("{0};{1};off", monitorRollbackLine.Pollutant.PollutantName.ToLower(), _currentStat); } return(str); } catch (Exception ex) { Logger.LogError(ex); return(string.Empty); } }
private void MonitorRollbackSettings2_Load(object sender, EventArgs e) { try { if (CommonClass.GBenMAPGrid == null) { return; } mainMap.ProjectionModeReproject = ActionMode.Never; mainMap.ProjectionModeDefine = ActionMode.Never; if (_monitorRollbackLine.RollbackGrid is ShapefileGrid) { if (File.Exists(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.MainSetup.SetupName + "\\" + (_monitorRollbackLine.RollbackGrid as ShapefileGrid).ShapefileName + ".shp")) { mainMap.Layers.Add(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.MainSetup.SetupName + "\\" + (_monitorRollbackLine.RollbackGrid as ShapefileGrid).ShapefileName + ".shp"); } } else if (_monitorRollbackLine.RollbackGrid is RegularGrid) { if (File.Exists(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.MainSetup.SetupName + "\\" + (_monitorRollbackLine.RollbackGrid as RegularGrid).ShapefileName + ".shp")) { mainMap.Layers.Add(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.MainSetup.SetupName + "\\" + (_monitorRollbackLine.RollbackGrid as RegularGrid).ShapefileName + ".shp"); } } PolygonLayer playerRegion = mainMap.Layers[mainMap.Layers.Count - 1] as PolygonLayer; playerRegion.DataSet.DataTable.Columns.Add("MyColorIndex", typeof(int)); for (int i = 0; i < playerRegion.DataSet.DataTable.Rows.Count; i++) { playerRegion.DataSet.DataTable.Rows[i]["MyColorIndex"] = i; dicMyColorIndex.Add(Convert.ToInt32(playerRegion.DataSet.DataTable.Rows[i]["COL"]).ToString() + "," + Convert.ToInt32(playerRegion.DataSet.DataTable.Rows[i]["ROW"]).ToString(), i); } Color cRegion = Color.Transparent; PolygonSymbolizer TransparentRegion = new PolygonSymbolizer(cRegion); TransparentRegion.OutlineSymbolizer = new LineSymbolizer(Color.Black, 1); playerRegion.Symbolizer = TransparentRegion; lstMonitorValues = DataSourceCommonClass.GetMonitorData(_monitorRollbackLine.GridType, _monitorRollbackLine.Pollutant, _monitorRollbackLine); IFeatureSet fsPoints = new FeatureSet(); MonitorValue mv = null; Feature feature = null; List <DotSpatial.Topology.Coordinate> lstCoordinate = new List <DotSpatial.Topology.Coordinate>(); List <double> fsInter = new List <double>(); if (lstMonitorValues != null && lstMonitorValues.Count > 0) { PolygonScheme myScheme = new PolygonScheme(); PolygonCategory pcin = new PolygonCategory(); pcin.Symbolizer.SetFillColor(Color.Red); myScheme.Categories.Add(pcin); DotSpatial.Topology.Point point; for (int i = 0; i < lstMonitorValues.Count; i++) { mv = lstMonitorValues[i]; point = new DotSpatial.Topology.Point(mv.Longitude, mv.Latitude); feature = new Feature(point); fsPoints.AddFeature(feature); } mainMap.Layers.Add(fsPoints); } } catch (Exception ex) { Logger.LogError(ex); } }
private bool CreateShapeFile(BaseControlGroup b) { string msg = string.Empty; System.Data.DataSet ds; string currentStat = string.Empty; try { currentStat = _currentStat; ModelDataLine modelDataLine = new ModelDataLine(); if (tabControl1.SelectedIndex == 0) { WaitShow("Loading model data file."); modelDataLine.DatabaseFilePath = txtModelDatabase.Text; System.Data.DataTable dtModel = CommonClass.ExcelToDataTable(txtModelDatabase.Text); DataSourceCommonClass.UpdateModelDataLineFromDataSet(b.Pollutant, modelDataLine, dtModel); } else { StreamReader sr = new StreamReader(txtModelFile.Text); string csvDataLine; csvDataLine = ""; string fileDataLine; fileDataLine = sr.ReadLine(); string[] sMatchArray = fileDataLine.Split(new char[] { ',' }); if (!(sMatchArray[0].Replace("\"", "") == b.Pollutant.PollutantName && sMatchArray[1] == b.GridType.GridDefinitionName && sMatchArray[2].Replace("\"", "") == "Model")) { MessageBox.Show("The pollutant or grid definition do not match."); return(false); } WaitShow("Loading model data file..."); string strLine = ""; ds = new System.Data.DataSet(); DataTable dt = new DataTable(); DataRow dr = null; string[] strArray; strLine = sr.ReadLine(); if (strLine != null && strLine.Length > 0) { strArray = strLine.Split(','); for (int i = 0; i < strArray.Count(); i++) { dt.Columns.Add(strArray[i]); } } while (strLine != null) { strLine = sr.ReadLine(); if (strLine != null && strLine.Length > 0) { dr = dt.NewRow(); strArray = strLine.Split(','); for (int i = 0; i < strArray.Count(); i++) { dr[i] = strArray[i]; } dt.Rows.Add(dr); } } ds.Tables.Add(dt); modelDataLine.DatabaseFilePath = txtModelFile.Text; DataSourceCommonClass.UpdateModelDataLineFromDataSetNewFormat(b.Pollutant, ref modelDataLine, ds); sr.Close(); switch (currentStat) { case "baseline": b.Base = null; b.Base = modelDataLine; b.Base.GridType = b.GridType; break; case "control": b.Control = null; b.Control = modelDataLine; b.Control.GridType = b.GridType; break; } } System.Threading.Thread.Sleep(100); WaitClose(); switch (currentStat) { case "baseline": b.Base = null; b.Base = modelDataLine; break; case "control": b.Control = null; b.Control = modelDataLine; break; } if (modelDataLine.ModelAttributes.Count == 0 && modelDataLine.ModelResultAttributes.Count == 0) { msg = "Error reading files."; return(false); } int threadId = -1; AsyncDelegate asyncD = new AsyncDelegate(AsyncCreateFile); IAsyncResult ar = asyncD.BeginInvoke(b, modelDataLine, currentStat, out threadId, null, null); return(true); } catch (Exception ex) { WaitClose(); Logger.LogError(ex); return(false); } finally { if (msg != string.Empty) { MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }