본문 바로가기
C++ 200제/코딩 IT 정보

GIS Mapping Library DotSpatial, 기초, 응용, 개발 사용 방법

by vicddory 2017. 2. 24.

GIS Mapping Library DotSpatial, 기초, 응용, 개발 사용 방법


◆ BASIC



위의 문서를 통해서 얻을 수 있는 정보는 아래 4가지입니다.


  1. C#으로 DotSpatial의 클래스 라이브러리를 로드하여 사용하는 방법
  2. 레스터 데이터 로드 (포스트 하단 참조)
  3. Shape 파일 작업 
  4. 레스터와 벡터를 분석하고 시각화


GIS Mapping Library DotSpatial 문서를 다운받아 보시면, 기본 프로젝트 설정과 사용 방법, 그리고 소스 코드가 있습니다.


그러한 소스 코드를 구현하면 아래와 같은 결과 화면을 볼 수 있습니다.


Raster Vector Data Analysis[GIS Mapping Library DotSpatial] Raster Vector Data Analysis

아래 코드는 UI 오른쪽 상단의 "View Elevation" 버튼에 대한 소스입니다.



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
#region "Class level variables"
    //the line layer
     MapLineLayer lineLayer;
   //the line feature set
    FeatureSet lineF;
    int lineID= 0;
    //boolean variable for first time mouse click
    Boolean firstClick=false;
   //boolean variable for ski path drawing finished
    Boolean hikingpathPathFinished = false;   
#endregion
 
private void btnDrawPath_Click(object sender, EventArgs e)
        {
            //remove any existing path
            foreach (IMapLineLayer existingPath in map1.GetLineLayers())
            {
                map1.Layers.Remove(existingPath);
            }
            //hikingpath is not finished
            hikingpathPathFinished = false;
 
            //change the mouse cursor
            map1.Cursor = Cursors.Cross;
     
//initialize the polyline featureset
      LineF= new FeatureSet(FeatureType.Line);
 
            //set projection
            lineF.Projection = map1.Projection;
 
            //initialize the featureSet attribute table
            DataColumn column = new DataColumn("ID");
            lineF.DataTable.Columns.Add(column);
 
            //add the featureSet as map layer
            lineLayer = (MapLineLayer)map1.Layers.Add(lineF);
 
            //implement the symbology
            LineSymbolizer symbol = new LineSymbolizer(Color.Blue, 2);
            lineLayer.Symbolizer = symbol;
 
            //Set the legend text
            lineLayer.LegendText = "Hiking path";
 
            //Before clicking the mouse first time
            firstClick = true;
 
        }
//GIS Mapping Library DotSpatial
private void map1_MouseDown(object sender, MouseEventArgs e)
        {
            //if hiking path is fininshed, don't draw any line
            if (hikingpathPathFinished == true)
                return;
            
            if (e.Button == MouseButtons.Left)
            {
                //left click - fill array of coordinates
            
                //coordinate of clicked point
                Coordinate coord = map1.PixelToProj(e.Location);
                
                //first time left click - create empty line feature
                if (firstClick)
                {
                    //Create a new List called lineArray.
                    //This list will store the Coordinates
                    //We are going to store the mouse click coordinates into this array.
                    List<Coordinate> lineArray = new List<Coordinate>();
 
                    //Create an instance for LineString class.
                    //We need to pass collection of list coordinates
                    LineString lineGeometry = new LineString(lineArray);
 
                    //Add the linegeometry to line feature
                    IFeature lineFeature = lineF.AddFeature(lineGeometry);
 
                    //add first coordinate to the line feature
                    lineFeature.Coordinates.Add(coord);
 
                    //set the line feature attribute
                    lineID = lineID + 1;
                    lineFeature.DataRow["ID"= lineID;
 
                    firstClick = false;
                }
                else
                {
                    //second or more clicks - add points to the existing feature
                    IFeature existingFeature = lineF.Features[lineF.Features.Count - 1];
 
                    existingFeature.Coordinates.Add(coord);
 
                    //refresh the map if line has 2 or more points
                    if (existingFeature.Coordinates.Count >= 2)
                    {
                        lineF.InitializeVertices();
                        map1.ResetBuffer();
                    }
                }
            }
            Else if (e.Button == MouseButtons.Right)
            {
                //right click - reset first mouse click
                firstClick = true;
                map1.ResetBuffer();
                lineF.SaveAs("c:\\MW\\linepath.shp"true);
                MessageBox.Show("The line shapefile has been saved.");
                map1.Cursor = Cursors.Arrow;
                //the ski path is finished
                hikingpathPathFinished = true;
            }
        }
 
public List<PathPoint> ExtractElevation
(double startX, double startY, double endX, double endY, IMapRasterLayer raster)
{
    double curX = startX;
    double curY = startY;
    double curElevation = 0;
 
    List<PathPoint> pathPointList = new List<PathPoint>();
 
    int numberofpoints = 100;
 
    double constXdif = ((endX - startX) / numberofpoints);
    double constYdif = ((endY - startY) / numberofpoints);
 
    for (int i = 0; i <= numberofpoints; i++) {
        PathPoint newPathPoint = new PathPoint();
 
        if ((i == 0)) {
            curX = startX;
            curY = startY;
        }
        else {
             curX = curX + constXdif;
             curY = curY + constYdif;
        }
 
        Coordinate coordinate = new Coordinate(curX, curY);
 
         RcIndex rowColumn = raster.DataSet.Bounds.ProjToCell(coordinate);
 
         curElevation = raster.DataSet.Value[rowColumn.Row, rowColumn.Column];
 
        //set the properties of new PathPoint
        newPathPoint.X = curX;
        newPathPoint.Y = curY;
        newPathPoint.Elevation = curElevation;
        pathPointList.Add(newPathPoint);
     }
 
    return pathPointList;
}
 
cs


개인적으로는 저 문서와 더불어 공식 홈페이지 자료와 코드 프로젝트의 게시글을 보시길 추천합니다.


Hiking Path Graph[GIS Mapping Library DotSpatial] Hiking Path Graph

◆ DotSpatial Mapping



공식 홈페이지에 접속해서 맨 오른쪽의 Download를 눌러 데모 프로그램과 DLL을 다운 받을 수 있습니다.


DotSpatial Mapping - Download site[GIS Mapping Library DotSpatial] DotSpatial Mapping - Download site


◆ Download DotSpitial

우선 공식 사이트로 이동해 DotSpatial_Release.1.0.845 RC2.zip을 다운 받습니다.


현재 최신버전이고 다른 버전은 다운로드 사이트를 참조하세요.


DotSpitial 설치 - 다운로드DotSpitial 설치 - 다운로드


다운 받은 압축 파일의 내부에 있는 실행 파일로 데모 프로그램은 실행해 볼 수 있습니다.


DotSpitial 설치 - 폴더 크기 확인DotSpitial 설치 - 폴더 크기 확인


Creating a Project

비주얼 스튜디오를 실행한 뒤, WinForm으로 새로운 프로젝트를 생성합니다.


DotSpitial 설치 - Creating a ProjectDotSpitial 설치 - Creating a Project


Changing Target Platform

DotSpatial은 .NET Framework 4 버전을 사용해야 합니다. 프로젝트를 우클릭하여 속성 창에 들어가 버전을 변경합니다.


DotSpitial 설치 - Changing Target PlatformDotSpitial 설치 - Changing Target Platform



Adding ToolEbox Items

도구 상자에 DotSpatial 아이템을 드래그해서 폼을 꾸밀 수 있습니다. 도구 상자에서 우클릭하여 새로운 아이템들을 추가해줄 준비를 합니다.


DotSpitial 설치 - Adding ToolEbox ItemsDotSpitial 설치 - Adding ToolEbox Items


그리고 난 뒤, 바로 우클릭을 통해서 새로운 아이템들을 실제로 추가해 줍니다.


DotSpitial 설치 - Choose ItemsDotSpitial 설치 - Choose Items


이후에 Browser를 눌러서 DotSpatial.Controls를 추가합니다.


DotSpitial 설치 - Insert DotSpatial.ControlsDotSpitial 설치 - Insert DotSpatial.Controls


그러면, 아래 그림과 같이 도구 상자에 아이콘이 추가된 것을 볼 수 있습니다.


DotSpitial 설치 - 도구상자 아이콘 확인DotSpitial 설치 - 도구상자 아이콘 확인


Laying Out the User Interface

우선, 4개의 버튼을 상단에 추가합니다. 정가운데에는 Map Control도 추가해 줍니다.


DotSpitial 설치 - Map Control 추가[GIS Mapping Library DotSpatial] DotSpitial 설치 - Map Control 추가


Adding DotSpatial References


DotSpitial 설치 - Adding DotSpatial ReferencesDotSpitial 설치 - Adding DotSpatial References


이어서 참조 추가를 통해서 아래의 두 파일을 선택해 추가합니다.


  1. DotSpatial.Data
  2. DotSpatial.Symbology

Setting Control Properties


DotSpitial 설치 - Setting Control PropertiesDotSpitial 설치 - Setting Control Properties


아래처럼 버튼의 Name과 Text를 교체합니다.


Button1 -> Open File

Open File - 추가 코드


1
uxMap.AddLayer();
cs


Button2 -> Zoom In

Zoom In - 추가 코드


1
uxMap.ZoomToMaxExtent();
cs


Button3 -> Zoom Wide

Zoom Wide - 추가 코드


1
2
3
4
private void uxPan_Click(object sender, EventArgs e)
{
     uxMap.FunctionMode = DotSpatial.Controls.FunctionMode.Pan;
}
cs


Button4 -> Pan

map1 -> uxMap

uxMap - 추가 코드


1
uxMap.ZoomIn();
cs


이 과정까지 완료되었다면 다 된 겁니다.


마지막으로 제가 참조한 사이트들입니다.





관련 글




GIS Mapping Library DotSpatial, 기초, 응용, 개발 사용 방법

ⓒ written by vicddory

댓글