private void btnOK_Click(object sender, EventArgs e) { FireBirdHelperBase fb = new ESILFireBirdHelper(); string commandText = string.Empty; _columns = int.Parse(nudColumns.Value.ToString()); _rrows = int.Parse(nudRows.Value.ToString()); try { if (cboGridType.SelectedIndex == 0) { _shapeFileName = lblShapeFileName.Text; } else { _gridType = 0; try { _columns = int.Parse(nudColumns.Value.ToString()); _rrows = int.Parse(nudRows.Value.ToString()); _colPerLongitude = int.Parse(nudColumnsPerLongitude.Value.ToString()); _rowPerLatitude = int.Parse(nudRowsPerLatitude.Value.ToString()); _minLongitude = System.Convert.ToDouble(txtMinimumLongitude.Text); _minLatitude = System.Convert.ToDouble(txtMinimumLatitude.Text); } catch { MessageBox.Show("Input data was not in a correct format."); return; } } if (IsEditor) { commandText = string.Format("select Ttype from GridDefinitions where GridDefinitionID=" + _gridID + ""); int type = Convert.ToInt32(fb.ExecuteScalar(CommonClass.Connection, new CommandType(), commandText)); switch (_gridType) { case 1: if (_shapeFilePath == string.Empty) { commandText = string.Format("Update GridDefinitions set GridDefinitionName='{0}'WHERE GridDefinitionID={1}", txtGridID.Text, _gridID); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); this.DialogResult = DialogResult.OK; return; } if (type == 1) { commandText = string.Format("Update GridDefinitions set GridDefinitionName='{0}',COLUMNS={1},RROWS={2},TTYPE={3} WHERE GridDefinitionID={4}", txtGridID.Text, _shapeCol, _shapeRow, _gridType, _gridID); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); if (File.Exists(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + _shapeFileName + ".shp")) { RenameorOverride ro = new RenameorOverride(); DialogResult dr = ro.ShowDialog(); if (dr == DialogResult.Cancel) { return; } else { if (dr == DialogResult.OK) { Renamefile frm = new Renamefile(); frm.newfileName = _shapeFileName; frm.manage = "GridDefinition"; DialogResult dresult = frm.ShowDialog(); if (dresult == DialogResult.OK) { _shapeFileName = frm.newfileName; } else { return; } } commandText = string.Format("Update ShapefileGriddefinitiondetails set shapefilename='{0}' where GridDefinitionID={1}", _shapeFileName, _gridID); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); } } } else { DialogResult rtn = MessageBox.Show("Replace the 'Regular Grid' with 'Shapefile'?", "", MessageBoxButtons.YesNo); if (rtn == DialogResult.Yes) { if (File.Exists(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + _shapeFileName + ".shp")) { RenameorOverride ro = new RenameorOverride(); DialogResult dr = ro.ShowDialog(); if (dr == DialogResult.Cancel) { return; } else { if (dr == DialogResult.OK) { Renamefile frm = new Renamefile(); frm.newfileName = _shapeFileName; frm.manage = "GridDefinition"; DialogResult dresult = frm.ShowDialog(); if (dresult == DialogResult.OK) { _shapeFileName = frm.newfileName; } else { return; } } commandText = string.Format("insert into ShapeFileGridDefinitionDetails (GridDefinitionID,ShapeFileName) values ({0},'{1}')", _gridID, _shapeFileName); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); } } commandText = string.Format("Update GridDefinitions set GridDefinitionName='{0}',COLUMNS={1},RROWS={2},TTYPE={3} WHERE GridDefinitionID={4}", txtGridID.Text, _shapeCol, _shapeRow, _gridType, _gridID); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); commandText = "delete From RegularGridDefinitionDetails where GridDefinitionID=" + _gridID + ""; fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); } else { return; } } IFeatureSet fs = FeatureSet.Open(_shapeFilePath); try { if (fs.DataTable.Columns["ROW"].DataType == typeof(System.String) || fs.DataTable.Columns["COL"].DataType == typeof(System.String)) { for (int iDt = 0; iDt < fs.DataTable.Rows.Count; iDt++) { int r = (int)Convert.ToDouble(fs.DataTable.Rows[iDt]["ROW"].ToString()); int c = (int)Convert.ToDouble(fs.DataTable.Rows[iDt]["COL"].ToString()); fs.DataTable.Rows[iDt]["ROW"] = r; fs.DataTable.Rows[iDt]["COL"] = c; } } fs.SaveAs(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + _shapeFileName + ".shp", true); } finally { fs.Close(); } break; case 0: if (Math.Abs(_minLongitude) > 180 || Math.Abs(_minLatitude) > 90) { MessageBox.Show("Longitude must be less than 180 degrees and latitude less than 90 degrees. Please check the longitude and latitude values."); return; } if (type == 0) { commandText = string.Format("Update GridDefinitions set GridDefinitionName='{0}',COLUMNS={1},RROWS={2},TTYPE={3} WHERE GridDefinitionID={4}", txtGridID.Text, _columns, _rrows, _gridType, _gridID); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); commandText = string.Format("Update RegularGriddefinitionDetails set minimumlatitude={0},minimumlongitude={1},columnsperlongitude={2},rowsperlatitude={3} where GridDefinitionID={4}", _minLatitude, _minLongitude, _colPerLongitude, _rowPerLatitude, _gridID); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); } else { DialogResult rtn = MessageBox.Show("Replace the 'Shapefile' with 'Regular Grid'?", "", MessageBoxButtons.YesNo); if (rtn == DialogResult.Yes) { commandText = string.Format("Update GridDefinitions set GridDefinitionName='{0}',COLUMNS={1},RROWS={2},TTYPE={3} WHERE GridDefinitionID={4}", txtGridID.Text, _columns, _rrows, _gridType, _gridID); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); commandText = "delete From ShapefileGriddefinitiondetails where GridDefinitionID=" + _gridID + ""; fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); commandText = string.Format("insert into RegularGriddefinitionDetails (GridDefinitionID,MinimumLatitude,MinimumLongitude,ColumnsPerLongitude,RowsPerLatitude,ShapeFileName) values ({0},{1},{2},{3},{4},'{5}')", _gridID, _minLatitude, _minLongitude, _colPerLongitude, _rowPerLatitude, txtGridID.Text); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); } else { return; } } fs = getFeatureSetFromRegularGrid(_columns, _rrows, _minLatitude, _minLongitude, _colPerLongitude, _rowPerLatitude); try { fs.SaveAs(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + txtGridID.Text + ".shp", true); } finally { fs.Close(); } break; } if (!chkBoxCreatePercentage.Checked) { WaitShow("Updating the grid definition..."); commandText = string.Format("delete from GridDefinitionPercentageEntries where PercentageID in ( select PercentageID from GridDefinitionPercentages where SourceGridDefinitionID={0} or TargetGridDefinitionID={0})", _gridID); fb.ExecuteNonQuery(CommonClass.Connection, CommandType.Text, commandText); commandText = string.Format("delete from GridDefinitionPercentages where SourceGridDefinitionID={0} or TargetGridDefinitionID={0}", _gridID); fb.ExecuteNonQuery(CommonClass.Connection, CommandType.Text, commandText); this.DialogResult = DialogResult.OK; WaitClose(); return; } } else { commandText = "select GridDefinitionID from GridDefinitions where GridDefinitionName='" + txtGridID.Text + "' and SetupID=" + CommonClass.ManageSetup.SetupID; object obj = fb.ExecuteScalar(CommonClass.Connection, new CommandType(), commandText); if (obj != null) { MessageBox.Show("This grid definition name is already in use. Please enter a different name."); return; } commandText = string.Format("select max(GRIDDEFINITIONID) from GRIDDEFINITIONS"); _gridID = Convert.ToInt32(fb.ExecuteScalar(CommonClass.Connection, new CommandType(), commandText)) + 1; switch (_gridType) { case 1: if (File.Exists(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + _shapeFileName + ".shp")) { Renamefile frm = new Renamefile(); frm.newfileName = _shapeFileName; frm.manage = "GridDefinition"; DialogResult dr = frm.ShowDialog(); if (dr == DialogResult.OK) { _shapeFileName = frm.newfileName; } else { return; } } commandText = string.Format("INSERT INTO GRIDDEFINITIONS (GridDefinitionID,SetUpID,GridDefinitionName,Columns,Rrows,Ttype,DefaultType) VALUES(" + _gridID + ",{0},'{1}',{2},{3},{4},{5})", CommonClass.ManageSetup.SetupID, txtGridID.Text, _shapeCol, _shapeRow, _gridType, 0); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); commandText = string.Format("INSERT INTO SHAPEFILEGRIDDEFINITIONDETAILS (GridDefinitionID,ShapeFileName) VALUES(" + _gridID + ",'{0}')", _shapeFileName); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); CommonClass.DeleteShapeFileName(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + _shapeFileName + ".shp"); IFeatureSet fs = FeatureSet.Open(_shapeFilePath); try { if (fs.DataTable.Columns["ROW"].DataType == typeof(System.String) || fs.DataTable.Columns["COL"].DataType == typeof(System.String)) { for (int iDt = 0; iDt < fs.DataTable.Rows.Count; iDt++) { int r = (int)Convert.ToDouble(fs.DataTable.Rows[iDt]["ROW"].ToString()); int c = (int)Convert.ToDouble(fs.DataTable.Rows[iDt]["COL"].ToString()); fs.DataTable.Rows[iDt]["ROW"] = r; fs.DataTable.Rows[iDt]["COL"] = c; } } fs.SaveAs(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + _shapeFileName + ".shp", true); } finally { fs.Close(); } break; case 0: DialogResult rtn = MessageBox.Show("Save this grid?", "", MessageBoxButtons.YesNo); if (rtn == DialogResult.No) { this.DialogResult = DialogResult.Cancel; return; } else { commandText = string.Format("INSERT INTO GRIDDEFINITIONS (GridDefinitionID,SetUpID,GridDefinitionName,Columns,Rrows,Ttype,DefaultType) VALUES(" + _gridID + ",{0},'{1}',{2},{3},{4},{5})", CommonClass.ManageSetup.SetupID, txtGridID.Text, _columns, _rrows, _gridType, 0); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); if (Math.Abs(_minLongitude) > 180 || Math.Abs(_minLatitude) > 90) { MessageBox.Show("Please input valid longitude and latitude values."); return; } commandText = string.Format("INSERT INTO RegularGridDefinitionDetails (GridDefinitionID,MinimumLatitude,MinimumLongitude,ColumnsPerLongitude,RowsPerLatitude,ShapeFileName) VALUES ({0},{1},{2},{3},{4},'{5}')", _gridID, txtMinimumLatitude.Text, txtMinimumLongitude.Text, nudColumnsPerLongitude.Value, nudRowsPerLatitude.Value, txtGridID.Text); fb.ExecuteNonQuery(CommonClass.Connection, new CommandType(), commandText); fs = getFeatureSetFromRegularGrid(_columns, _rrows, _minLatitude, _minLongitude, _colPerLongitude, _rowPerLatitude); CommonClass.DeleteShapeFileName(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + txtGridID.Text + ".shp"); try { fs.SaveAs(CommonClass.DataFilePath + @"\Data\Shapefiles\" + CommonClass.ManageSetup.SetupName + "\\" + txtGridID.Text + ".shp", true); } finally { fs.Close(); } } break; } if (!chkBoxCreatePercentage.Checked) { this.DialogResult = DialogResult.OK; return; } } lblprogress.Visible = true; lblprogress.Refresh(); progressBar1.Visible = true; progressBar1.Refresh(); BenMAPGrid addBenMAPGrid = Grid.GridCommon.getBenMAPGridFromID(_gridID); try { commandText = "select a.GridDefinitionID,SetupID,GridDefinitionName,Columns,RRows,TType,b.ShapeFileName from GridDefinitions a,ShapeFileGridDefinitionDetails b " + " where a.GridDefinitionID=b.GridDefinitionID and a.TType=1 and a.SetupID=" + addBenMAPGrid.SetupID + " union " + " select a.GridDefinitionID,SetupID,GridDefinitionName,Columns,RRows,TType,b.ShapeFileName from GridDefinitions a,RegularGridDefinitionDetails b " + " where a.GridDefinitionID=b.GridDefinitionID and a.TType=0 and a.SetupID=" + addBenMAPGrid.SetupID; fb = new ESIL.DBUtility.ESILFireBirdHelper(); System.Data.DataSet ds = fb.ExecuteDataset(CommonClass.Connection, CommandType.Text, commandText); commandText = string.Format("delete from GridDefinitionPercentageEntries where PercentageID in ( select PercentageID from GridDefinitionPercentages where SourceGridDefinitionID={0} or TargetGridDefinitionID={0})", addBenMAPGrid.GridDefinitionID); fb.ExecuteNonQuery(CommonClass.Connection, CommandType.Text, commandText); commandText = string.Format("delete from GridDefinitionPercentages where SourceGridDefinitionID={0} or TargetGridDefinitionID={0}", addBenMAPGrid.GridDefinitionID); fb.ExecuteNonQuery(CommonClass.Connection, CommandType.Text, commandText); string AppPath = Application.StartupPath; if (ds.Tables[0].Rows.Count == 1) { this.DialogResult = System.Windows.Forms.DialogResult.OK; } foreach (DataRow dr in ds.Tables[0].Rows) { int gridDefinitionID = Convert.ToInt32(dr["GridDefinitionID"]); if (gridDefinitionID == addBenMAPGrid.GridDefinitionID) { continue; } int bigGridID, smallGridID; bigGridID = gridDefinitionID; smallGridID = addBenMAPGrid.GridDefinitionID; AsyncgetRelationshipFromBenMAPGridPercentage dlgt = new AsyncgetRelationshipFromBenMAPGridPercentage(getRelationshipFromBenMAPGridPercentage); lstAsyns.Add(bigGridID + "," + smallGridID); lstAsyns.Add(smallGridID + "," + bigGridID); iAsyns++; iAsyns++; IAsyncResult ar = dlgt.BeginInvoke(bigGridID, smallGridID, new AsyncCallback(outPut), dlgt); IAsyncResult ar2 = dlgt.BeginInvoke(smallGridID, bigGridID, new AsyncCallback(outPut), dlgt); } progressBar1.Step = 1; progressBar1.Minimum = 1; progressBar1.Maximum = iAsyns + 1; this.Enabled = false; } catch (Exception ex) { Logger.LogError(ex); } } catch (Exception ex) { Logger.LogError(ex.Message); } }
private void btnLoadData_Click(object sender, EventArgs e) { try { string tip = "Loading the datafile."; LoadVariableDatabase frm = new LoadVariableDatabase(); frm.DefinitionID = txtGridDefinition.Text; DialogResult rtn = frm.ShowDialog(); if (rtn != DialogResult.OK) { return; } txtGridDefinition.Text = frm.DefinitionID; txtGridDefinition.ReadOnly = false; string strPath = frm.DataPath; WaitShow(tip); _dtOrigin = CommonClass.ExcelToDataTable(strPath); int colCount = _dtOrigin.Columns.Count; if (_dtOrigin == null || colCount <= 2) { WaitClose(); MessageBox.Show("Failed to load variable dataset."); return; } int icol = -1; int irow = -1; for (int i = 0; i < _dtOrigin.Columns.Count; i++) { if (_dtOrigin.Columns[i].ColumnName.ToLower().Replace(" ", "") == "row") { irow = i; } if (_dtOrigin.Columns[i].ColumnName.ToLower().Replace(" ", "") == "col" || _dtOrigin.Columns[i].ColumnName.ToLower().Replace(" ", "") == "column") { icol = i; } } string warningtip = ""; if (irow < 0) { warningtip = "'Row', "; } if (icol < 0) { warningtip += "'Column', "; } if (warningtip != "") { warningtip = warningtip.Substring(0, warningtip.Length - 2); WaitClose(); warningtip = "Please check the column header of " + warningtip + ". It is incorrect or does not exist."; MessageBox.Show(warningtip, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } string header = string.Empty; DataColumn[] cols = new DataColumn[colCount]; _dtOrigin.Columns.CopyTo(cols, 0); for (int i = 0; i < colCount; i++) { if (i == icol || i == irow) { continue; } header = _dtOrigin.Columns[i].ColumnName; dt = new DataTable(header); dt = _dtOrigin.DefaultView.ToTable(false, _dtOrigin.Columns[icol].ColumnName, _dtOrigin.Columns[irow].ColumnName, header); dt.TableName = header; dt.Columns[0].ColumnName = "ccolumn"; dt.Columns[1].ColumnName = "row"; dt.Columns[2].ColumnName = "vvalue"; if (_dsSelectedData.Tables.Contains(dt.TableName)) { string commandText = string.Format("select SETUPVARIABLEID from SETUPVARIABLES where SETUPVARIABLENAME='{0}' and SETUPVARIABLEDATASETID='{1}'", dt.TableName, variabledatasetID); ESIL.DBUtility.FireBirdHelperBase fb = new ESIL.DBUtility.ESILFireBirdHelper(); object obj = fb.ExecuteScalar(CommonClass.Connection, new CommandType(), commandText); WaitClose(); DialogResult dr = MessageBox.Show("The variable name '" + dt.TableName + "' is already in use. Please enter a different name. Click cancel to skip this variable.", "Error", MessageBoxButtons.OKCancel); if (dr == DialogResult.OK) { Renamefile frmRename = new Renamefile(); frmRename.lblRename.Text = "Please rename the variable:"; frmRename.newfileName = dt.TableName; frmRename.manage = "Variable"; frmRename.datasetID = Convert.ToInt16(variabledatasetID); if (frmRename.ShowDialog() == DialogResult.OK) { string newvariableName = frmRename.newfileName; dt.TableName = newvariableName; } else { continue; } } else { continue; } } WaitShow(tip); lstDataSetVariable.Items.Add(dt.TableName); _dsSelectedData.Tables.Add(dt); DataTable _dt = dt.Copy(); _dsSelectedDataTemp.Tables.Add(_dt); } lstDataSetVariable.SelectedIndex = 0; txtGridDefinition.Enabled = false; WaitClose(); } catch (Exception ex) { WaitClose(); MessageBox.Show("Failed to load variable dataset."); Logger.LogError(ex); } }