728x90
반응형
728x170
▶ MainForm.cs
using System;
using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;
using EGIS.ShapeFileLib;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
this.inputShapeFileButton.Click += inputShapeFileButton_Click;
this.outputShapeFileButton.Click += outputShapeFileButton_Click;
this.filterComboBox.SelectedIndexChanged += filterComboBox_SelectedIndexChanged;
this.filterCheckedListBox.Click += filterCheckedListBox_Click;
this.filterCheckedListBox.ItemCheck += filterCheckedListBox_ItemCheck;
this.generateButton.Click += generateButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 입력 도형 파일 버튼 클릭시 처리하기 - inputShapefileButton_Click(sender, e)
/// <summary>
/// 입력 도형 파일 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void inputShapeFileButton_Click(object sender, EventArgs e)
{
try
{
if(this.openFileDialog.ShowDialog(this) == DialogResult.OK)
{
LoadInputShapeFile(this.openFileDialog.FileName);
}
}
catch(Exception exception)
{
MessageBox.Show(exception.ToString());
}
finally
{
this.outputPanel.Enabled = !string.IsNullOrEmpty(this.inputShapeFileTextBox.Text);
}
}
#endregion
#region 출력 도형 파일 버튼 클릭시 처리하기 - outputShapeFileButton_Click(sender, e)
/// <summary>
/// 출력 도형 파일 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void outputShapeFileButton_Click(object sender, EventArgs e)
{
if(this.saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
this.outputShapeFileTextBox.Text = this.saveFileDialog.FileName;
}
}
#endregion
#region 필터 콤보 박스 선택 인덱스 변경시 처리하기 - filterComboBox_SelectedIndexChanged(sender, e)
/// <summary>
/// 필터 콤보 박스 선택 인덱스 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void filterComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(this.filterComboBox.SelectedIndex >= 0)
{
Cursor = Cursors.WaitCursor;
this.filterCheckedListBox.Items.Clear();
this.filterCheckedListBox.Enabled = false;
this.filterCheckedListBox.SuspendLayout();
this.filterCheckedListBox.BeginUpdate();
DbfReader reader = null;
try
{
reader = new DbfReader(Path.ChangeExtension(this.inputShapeFileTextBox.Text, ".dbf"));
string[] recordArray = reader.GetDistinctRecords(this.filterComboBox.SelectedIndex);
if(recordArray.Length > 1000 && this.limitRecordCountCheckBox.Checked)
{
Array.Resize(ref recordArray, 1000);
}
this.filterCheckedListBox.Items.AddRange(recordArray);
}
finally
{
this.filterCheckedListBox.EndUpdate();
this.filterCheckedListBox.Enabled = true;
this.filterCheckedListBox.ResumeLayout();
Cursor = Cursors.Default;
reader.Close();
this.generateButton.Enabled = this.filterCheckedListBox.CheckedIndices.Count > 0;
}
}
}
#endregion
#region 필터 체크 리스트 박스 클릭시 처리하기 - filterCheckedListBox_Click(sender, e)
/// <summary>
/// 필터 체크 리스트 박스 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void filterCheckedListBox_Click(object sender, EventArgs e)
{
this.generateButton.Enabled = this.filterCheckedListBox.CheckedIndices.Count > 0;
}
#endregion
#region 필터 체크 리스트 박스 항목 체크시 처리하기 - filterCheckedListBox_ItemCheck(sender, e)
/// <summary>
/// 필터 체크 리스트 박스 항목 체크시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void filterCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
this.generateButton.Enabled = (this.filterCheckedListBox.CheckedIndices.Count > 1) || (e.NewValue == CheckState.Checked);
}
#endregion
#region 출력 도형 파일 생성 버튼 클릭시 처리하기 - generateButton_Click(sender, e)
/// <summary>
/// 출력 도형 파일 생성 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void generateButton_Click(object sender, EventArgs e)
{
GenerateOutputShapeFile();
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 입력 도형 파일 로드하기 - LoadInputShapeFile(filePath)
/// <summary>
/// 입력 도형 파일 로드하기
/// </summary>
/// <param name="filePath">파일 경로</param>
private void LoadInputShapeFile(string filePath)
{
this.inputShapeFileTextBox.Text = filePath;
this.filterComboBox.Items.Clear();
this.filterCheckedListBox.Items.Clear();
DbfReader reader = null;
try
{
reader = new DbfReader(Path.ChangeExtension(filePath, ".dbf"));
this.filterComboBox.Items.AddRange(reader.GetFieldNames());
}
finally
{
if(reader != null)
{
reader.Close();
}
}
this.filterComboBox.SelectedIndex = 0;
}
#endregion
#region 출력 도형 파일 생성하기 - GenerateOutputShapeFile()
/// <summary>
/// 출력 도형 파일 생성하기
/// </summary>
private void GenerateOutputShapeFile()
{
if(string.IsNullOrEmpty(this.outputShapeFileTextBox.Text) || this.filterCheckedListBox.CheckedIndices.Count == 0)
{
return;
}
int fieldIndex = this.filterComboBox.SelectedIndex;
if(fieldIndex == -1)
{
MessageBox.Show(this, "선택된 필드가 없습니다.", "INFORMATION");
return;
}
ShapeFile shapeFile = new ShapeFile(this.inputShapeFileTextBox.Text);
DbfReader reader = new DbfReader(Path.ChangeExtension(this.inputShapeFileTextBox.Text, ".dbf"));
StringCollection stringCollection = new StringCollection();
for(int i = 0; i < this.filterCheckedListBox.CheckedIndices.Count; i++)
{
stringCollection.Add(this.filterCheckedListBox.Items[this.filterCheckedListBox.CheckedIndices[i]] as string);
}
string rootDirectoryPath = Path.GetDirectoryName(this.outputShapeFileTextBox.Text);
string shapeFileName = Path.GetFileNameWithoutExtension(this.outputShapeFileTextBox.Text);
ShapeFileWriter writer;
ShapeType shapeType = shapeFile.ShapeType;
if(shapeFile.ShapeType == ShapeType.PolygonZ)
{
shapeType = ShapeType.Polygon;
}
DbfFieldDesc[] fieldDescriptionArray = reader.DbfRecordHeader.GetFieldDescriptions();
for(int i = 0; i < fieldDescriptionArray.Length; i++)
{
fieldDescriptionArray[i].FieldType = DbfFieldType.Character;
}
if(this.appendCheckBox.Checked)
{
writer = ShapeFileWriter.OpenWriter(rootDirectoryPath, shapeFileName);
}
else
{
writer = ShapeFileWriter.CreateWriter(rootDirectoryPath, shapeFileName, shapeType, fieldDescriptionArray);
}
try
{
Cursor = Cursors.WaitCursor;
this.toolStripProgressBar.Maximum = shapeFile.RecordCount;
this.toolStripProgressBar.Value = 0;
ShapeFileEnumerator enumerator = shapeFile.GetShapeFileEnumerator();
for(int i = 0; i < shapeFile.RecordCount; i++)
{
string[] fieldArray = reader.GetFields(i);
bool include = false;
for(int j = 0; !include && j < stringCollection.Count; j++)
{
include = (string.Compare(fieldArray[fieldIndex].Trim(), stringCollection[j], true) == 0);
}
if(include)
{
if(shapeType == ShapeType.Point)
{
writer.AddRecord(shapeFile.GetShapeDataD(i)[0], 1, fieldArray);
}
else
{
writer.AddRecord(shapeFile.GetShapeDataD(i), fieldArray);
}
}
this.toolStripProgressBar.Increment(1);
}
MessageBox.Show("신규 도형 파일이 성공적으로 생성되었습니다.");
}
finally
{
Cursor = Cursors.Default;
writer.Close();
shapeFile.Close();
reader.Close();
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요