첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker

댓글을 달아 주세요