■ ShapeFile 클래스의 GetShapeFileEnumerator 메소드를 사용해 도형 파일 특성 설명을 나열하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.Drawing.Imaging; 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.openMenuItem.Click += openMenuItem_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Open 메뉴 항목 클릭시 처리하기 - openMenuItem_Click(sender, e) /// <summary> /// Open 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void openMenuItem_Click(object sender, EventArgs e) { try { if(this.openFileDialog.ShowDialog(this) == DialogResult.OK) { DateTime startTime = DateTime.Now; ReadShapeFile(this.openFileDialog.FileName); this.richTextBox.AppendText ( string.Format ( "Time to output attributes : {0}s", ((TimeSpan)DateTime.Now.Subtract(startTime)).TotalSeconds ) ); this.richTextBox.ScrollToCaret(); } } catch(Exception exception) { MessageBox.Show(exception.ToString()); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 레코드 그리드 포인트 리스트 구하기 - GetRecordGridPointList(shapefile, recordIndex, gridCountX, gridCountY) /// <summary> /// 레코드 그리드 포인트 리스트 구하기 /// </summary> /// <param name="shapefile">도형 파일</param> /// <param name="recordIndex">레코드 인덱스</param> /// <param name="gridCountX">그리드 카운트 X</param> /// <param name="gridCountY">그리드 카운트 Y</param> /// <returns>레코드 그리드 포인트 리스트</returns> private List<PointD> GetRecordGridPointList(ShapeFile shapefile, int recordIndex, int gridCountX, int gridCountY) { if(shapefile.ShapeType != ShapeType.Polygon) { throw new Exception("only polygon shapefiles supported"); } List<PointD> gridPointList = new List<PointD>(); ReadOnlyCollection<PointD[]> shapePointArrayCollection = shapefile.GetShapeDataD(recordIndex); foreach(PointD[] shapePointArray in shapePointArrayCollection) { double minimumX = double.PositiveInfinity; double minimumY = double.PositiveInfinity; double maximumX = double.NegativeInfinity; double maximumY = double.NegativeInfinity; for(int i = shapePointArray.Length - 1; i >= 0; --i) { if(minimumX > shapePointArray[i].X) { minimumX = shapePointArray[i].X; } if(minimumY > shapePointArray[i].Y) { minimumY = shapePointArray[i].Y; } if(maximumX < shapePointArray[i].X) { maximumX = shapePointArray[i].X; } if(maximumY < shapePointArray[i].Y) { maximumY = shapePointArray[i].Y; } } double cellWidth = (maximumX - minimumX) / gridCountX; double cellHeight = (maximumY - minimumY) / gridCountY; double x = minimumX; while(x <= maximumX) { double y = minimumY; while(y <= maximumY) { if(GeometryAlgorithms.PointInPolygon(shapePointArray, x, y)) { gridPointList.Add(new PointD(x, y)); } y += cellHeight; } x += cellWidth; } } return gridPointList; } #endregion #region 포인트 그리드 생성하기 - GeneratePointGrid(shapefile) /// <summary> /// 포인트 그리드 생성하기 /// </summary> /// <param name="shapefile">도형 파일</param> private void GeneratePointGrid(ShapeFile shapefile) { for(int n = 0; n < shapefile.RecordCount; n++) { List<PointD> gridPointList = GetRecordGridPointList(shapefile, n, 50, 50); using(Bitmap bitmap = new Bitmap(360, 180)) { using(Graphics graphics = Graphics.FromImage(bitmap)) { for(int i = gridPointList.Count - 1; i >= 0; --i) { float x = (float)gridPointList[i].X; if(x > 180) { x -= 180; } x += 180; float y = 180 - ((float)gridPointList[i].Y + 90); graphics.FillEllipse(Brushes.Red, x, y, 2, 2); } } bitmap.Save(string.Format("record_{0}.bmp", n), ImageFormat.Bmp); } } } #endregion #region 도형 파일 읽기 - ReadShapeFile(filePath) /// <summary> /// 도형 파일 읽기 /// </summary> /// <param name="filePath">파일 경로</param> private void ReadShapeFile(string filePath) { ShapeFile.MapFilesInMemory = true; this.richTextBox.Clear(); this.richTextBox.ScrollToCaret(); bool outputPointsChecked = this.outputPointsCheckBox.Checked; bool outputZValuesChecked = this.outputZValuesCheckBox.Checked; bool outputMValuesChecked = this.outputMValuesCheckBox.Checked; ShapeFile shapeFile = new ShapeFile(filePath); try { this.richTextBox.AppendText(string.Format("파일 경로 : {0}\n", shapeFile.FilePath )); this.richTextBox.AppendText(string.Format("범위 : {0}\n", shapeFile.Extent )); this.richTextBox.AppendText(string.Format("도형 수 : {0}\n", shapeFile.RecordCount)); this.richTextBox.AppendText(string.Format("도형 타입 : {0}\n", shapeFile.ShapeType )); shapeFile.RenderSettings = new RenderSettings(filePath, "", Font); DbfReader reader = shapeFile.RenderSettings.DbfReader; this.richTextBox.AppendText("\n--- DBF 필드 설명 ---\n"); foreach(DbfFieldDesc fieldDescription in reader.DbfRecordHeader.GetFieldDescriptions()) { this.richTextBox.AppendText(fieldDescription + "\n"); } if(outputZValuesChecked) { outputZValuesChecked = (shapeFile.ShapeType == ShapeType.PolygonZ || shapeFile.ShapeType == ShapeType.PointZ); } if(outputPointsChecked || outputZValuesChecked || outputMValuesChecked) { this.richTextBox.AppendText("포인트 데이터 출력...\n"); this.richTextBox.Refresh(); this.richTextBox.ScrollToCaret(); using(StreamWriter writer = new StreamWriter("d:\\output.txt")) { ShapeFileEnumerator enumerator = shapeFile.GetShapeFileEnumerator(); int recordIndex = 0; while(enumerator.MoveNext()) { writer.WriteLine(string.Format("레코드 : {0}", recordIndex)); if(outputPointsChecked) { ReadOnlyCollection<PointD[]> pointArrayCollection = enumerator.Current; foreach(PointD[] pointArray in pointArrayCollection) { if(pointArray.Length < 50) { writer.Write(string.Format("[포인트 수 : {0}]", pointArray.Length)); for(int i = 0; i < pointArray.Length; i++) { if(i > 0) { writer.Write(','); } writer.Write(pointArray[i].ToString()); } writer.WriteLine(); } } } if(outputZValuesChecked) { ReadOnlyCollection<double[]> zValueArrayCollection = enumerator.GetCurrentZValues(); if(zValueArrayCollection != null) { writer.Write("Z 값 배열 :"); foreach(double[] zValueArray in zValueArrayCollection) { writer.Write("[높이 수 : {0}]", zValueArray.Length); for(int i = 0; i < zValueArray.Length; i++) { if(i > 0) { writer.Write(','); } writer.Write(zValueArray[i]); } } } writer.WriteLine(); } if(outputMValuesChecked) { ReadOnlyCollection<double[]> mValueArrayCollection = shapeFile.GetShapeMDataD(recordIndex); if(mValueArrayCollection != null) { foreach(double[] mValueArray in mValueArrayCollection) { if(mValueArray.Length < 50) { writer.Write("[측정 수 : {0}]", mValueArray.Length); for(int i = 0; i < mValueArray.Length; i++) { if(i > 0) { writer.Write(", "); } writer.Write(mValueArray[i]); } writer.WriteLine(); } } } writer.WriteLine(); } recordIndex++; } enumerator.Dispose(); } this.richTextBox.AppendText("포인트 데이터 파일 출력\n"); } GeneratePointGrid(shapeFile); } finally { shapeFile.Close(); shapeFile.Dispose(); } } #endregion } } |