-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrmMain.cs
411 lines (362 loc) · 14.8 KB
/
FrmMain.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MapWinGIS;
namespace BTL_HQTCSDL
{
public partial class FrmMain : Form
{
List<string> listLayerName = new List<string>();
ContextMenu rightClickMenu = new ContextMenu();
int idxLayerTmp = -1;
int idxLayerRaster = -1;
int currHandle = 0;
int currNodeIdx = 0;
MapWinGIS.Image img;
Shapefile tmpLayer;
FrmLoadData frmLoad = new FrmLoadData();
FrmExportToDb frmExport = new FrmExportToDb();
public FrmMain()
{
InitializeComponent();
ConfigConTrol();
}
private void ConfigConTrol()
{
//
axMap1.ShowRedrawTime = false;
axMap1.GrabProjectionFromData = true;
axMap1.CursorMode = tkCursorMode.cmPan;
axMap1.SendMouseMove = true;
axMap1.Measuring.ShowBearing = false;
axMap1.Measuring.ShowLength = false;
axMap1.Measuring.ShowTotalLength = false;
axMap1.Measuring.MeasuringType = tkMeasuringType.MeasureDistance;
axMap1.Measuring.LineColor = Convert.ToUInt32(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue));
axMap1.MeasuringChanged += new AxMapWinGIS._DMapEvents_MeasuringChangedEventHandler(axMap1_MeasuringChanged);
this.axMap1.MouseMoveEvent += new AxMapWinGIS._DMapEvents_MouseMoveEventHandler(axMap1_MouseMoveEvent);
//
//
MenuItem itemOpenTable = new MenuItem("Mở bảng thuộc tính");
itemOpenTable.Click += new EventHandler(ItemOpenTable_Click);
MenuItem itemDel = new MenuItem("Xóa");
itemDel.Click += new EventHandler(ItemDel_Click);
rightClickMenu.MenuItems.Add(itemOpenTable);
rightClickMenu.MenuItems.Add(itemDel);
//
tvListLayer.CheckBoxes = true;
tvListLayer.AfterCheck += new TreeViewEventHandler(TvListLayer_AfterCheck);
tvListLayer.NodeMouseClick += new TreeNodeMouseClickEventHandler(TvListLayer_NodeMouseClick);
//
btnAnotation.Click += new EventHandler(BtnAnotation_Click);
btnVP.Click += new EventHandler(BtnVP_Click);
btnImport.Click += new EventHandler(BtnImport_Click);
btnExport.Click += new EventHandler(BtnExport_Click);
btnShowCellInfo.Click += new EventHandler(BtnShowCellInfo_Click);
btnExport.Enabled = false;
btnAnotation.Checked = false;
btnVP.Checked = false;
//
zoom_in.Click += new EventHandler(zoom_in_Click);
zoom_out.Click += new EventHandler(zoom_out_Click);
zoom_more.Click += new EventHandler(zoom_more_Click);
pan.Click += new EventHandler(pan_Click);
//
frmLoad.AddFromDb += new FrmLoadData.SfLoad(frmLoad_AddFromDb);
//
frmExport.SelShapeIdx += new FrmExportToDb.SelIdx(frmExport_SelShapeIdx);
//
}
void frmExport_SelShapeIdx(int idx)
{
tmpLayer.SelectNone();
tmpLayer.set_ShapeSelected(idx, true);
axMap1.ZoomToShape(idxLayerTmp, idx);
}
private void BtnShowCellInfo_Click(object sender, EventArgs e)
{
List<string> listName = new List<string>();
for (int i = 0; i < tvListLayer.Nodes.Count; i++)
{
listName.Add(tvListLayer.Nodes[i].Text);
}
frmQuery frmQuery = new frmQuery(listName, frmLoad.ConnStr);
frmQuery.Show(this);
//if (idxLayerRaster < 0)
//{
// return;
//}
//btnShowCellInfo.Checked = !btnShowCellInfo.Checked;
}
private void BtnExport_Click(object sender, EventArgs e)
{
axMap1.Measuring.Clear();
axMap1.Redraw();
frmExport.ConnStr = frmLoad.ConnStr;
frmExport.Sf = tmpLayer;
frmExport.ShowDialog(this);
BtnAnotation_Click(this.btnAnotation, e);
}
private void BtnImport_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Mo tap tin du lieu";
ofd.Filter = "(*.shp)|*.shp|(*.tif)|*.tif";
if (ofd.ShowDialog() == DialogResult.OK)
{
if (ofd.FileName.ToLower().EndsWith(".shp"))
{
MapWinGIS.Shapefile tmpSf = new Shapefile();
var result = tmpSf.Open(@ofd.FileName, null);
int iHandle = axMap1.AddLayer(tmpSf, true);
AddLayer(axMap1.get_LayerName(iHandle));
}
else if (ofd.FileName.ToLower().EndsWith(".tif") ||
ofd.FileName.ToLower().EndsWith(".png") ||
ofd.FileName.ToLower().EndsWith(".jpg"))
{
img = new MapWinGIS.Image();
img.Open(@ofd.FileName);
var ds = img.GeoProjection;
idxLayerRaster = axMap1.AddLayer(img, true);
AddLayer(axMap1.get_LayerName(idxLayerRaster));
}
}
}
private void BtnAnotation_Click(object sender, EventArgs e)
{
btnAnotation.Checked = !btnAnotation.Checked;
if (btnAnotation.Checked)
{
btnExport.Enabled = true;
//toolStripButton6.Enabled
axMap1.CursorMode = tkCursorMode.cmMeasure;
tmpLayer = new Shapefile();
if (!tmpLayer.CreateNewWithShapeID("", ShpfileType.SHP_POLYLINE))
{
MessageBox.Show("Failed to create shapefile: " + tmpLayer.ErrorMsg[tmpLayer.LastErrorCode]);
return;
}
tmpLayer.SelectionColor = Convert.ToUInt32(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red));
int fldX = tmpLayer.EditAddField("ID", FieldType.INTEGER_FIELD, 9, 12);
//int fldY = sf.EditAddField("y", FieldType. .DOUBLE_FIELD, 9, 12);
//int fldArea = sf.EditAddField("area", FieldType.DOUBLE_FIELD, 9, 12);
tmpLayer.GeoProjection = axMap1.GeoProjection;
idxLayerTmp = axMap1.AddLayer(tmpLayer, true);
axMap1.set_ShapeLayerLineColor(idxLayerTmp, Convert.ToUInt32(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)));
}
else
{
btnExport.Enabled = false;
axMap1.CursorMode = tkCursorMode.cmPan;
if (idxLayerTmp >= 0)
{
axMap1.RemoveLayer(idxLayerTmp);
}
}
}
private void ItemDel_Click(object sender, EventArgs e)
{
axMap1.RemoveLayer(currHandle);
tvListLayer.Nodes.RemoveAt(currNodeIdx);
axMap1.Redraw();
}
private void ItemOpenTable_Click(object sender, EventArgs e)
{
//var sfTmp = axMap1.get_Shapefile(currHandle);
//if (sfTmp == null)
//{
//}
}
private void TvListLayer_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode node_here = tvListLayer.GetNodeAt(e.X, e.Y);
tvListLayer.SelectedNode = node_here;
if (node_here == null) return;
currHandle = axMap1.get_LayerHandle(node_here.Index);
currNodeIdx = node_here.Index;
if (e.Button == MouseButtons.Right)
{
if (axMap1.get_Shapefile(currHandle) == null)
{
rightClickMenu.MenuItems[0].Text = "Chọn band render";
}
else
{
rightClickMenu.MenuItems[0].Text = "Mở bảng thuộc tính";
}
rightClickMenu.Show(tvListLayer, new System.Drawing.Point(e.X, e.Y));
}
}
void AddLayer(string layerName)
{
listLayerName.Add(layerName);
TreeNode tmp = new TreeNode(layerName);
tmp.Checked = true;
tvListLayer.Nodes.Add(tmp);
}
private void TvListLayer_AfterCheck(object sender, TreeViewEventArgs e)
{
tvListLayer.SelectedNode = e.Node;
axMap1.set_LayerVisible(e.Node.Index, e.Node.Checked);
axMap1.Redraw();
}
private void axMap1_MouseMoveEvent(object sender, AxMapWinGIS._DMapEvents_MouseMoveEvent e)
{
if (btnShowCellInfo.Checked)
{
string stt = "";
int row;
int column;
double X = 0;
double Y = 0;
axMap1.PixelToProj(e.x, e.y, ref X, ref Y);
stt += "Kinh độ: " + Convert.ToString(Math.Round(X, 3)) +
" Vĩ độ: " + Convert.ToString(Math.Round(Y, 3)) +
". Giá trị Band: ";
MapWinGIS.Image img = axMap1.get_Image(idxLayerRaster);
img.ProjectionToImage(X, Y, out column, out row);
double[] vals = new double[img.NoBands];
for (int i = 1; i <= img.NoBands; i++)
{
//GdalRasterBand rst = img.get_Band(i);
double pVal = 0;
//rst.get_Value(row, column, out pVal);
vals[i - 1] = pVal;
stt += i.ToString() + " : " + Math.Round(pVal, 3).ToString() + "; ";
}
toolStripStatusLabel1.Text = stt;
}
else
{
lbPrj.Text = "Hệ tọa độ : " + axMap1.GeoProjection.ExportToProj4();
double x, y;
x = y = 0;
axMap1.PixelToProj(e.x, e.y, ref x, ref y);
x = Math.Round(x, 3);
y = Math.Round(y, 3);
toolStripStatusLabel1.Text = "Kinh độ: " + Convert.ToString(x) + " Vĩ độ: " + Convert.ToString(y);
}
}
private void zoom_in_Click(object sender, EventArgs e)
{
axMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomIn;
}
private void zoom_out_Click(object sender, EventArgs e)
{
axMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomOut;
}
private void pan_Click(object sender, EventArgs e)
{
axMap1.CursorMode = MapWinGIS.tkCursorMode.cmPan;
}
private void zoom_more_Click(object sender, EventArgs e)
{
axMap1.ZoomToLayer(currHandle);
}
/****************************/
public void LayersInfo()
{
int pointCount = 0;
int lineCount = 0;
int polyCount = 0;
int imgCount = 0;
for (int i = 0; i < axMap1.NumLayers; i++)
{
object layer = axMap1.get_GetObject(axMap1.get_LayerHandle(i));
Shapefile sf_test = layer as Shapefile;
if (sf_test != null)
{
switch (sf_test.ShapefileType)
{
case ShpfileType.SHP_POINT:
pointCount++;
break;
case ShpfileType.SHP_POLYLINE:
lineCount++;
break;
case ShpfileType.SHP_POLYGON:
polyCount++;
break;
}
}
else
{
MapWinGIS.Image img = layer as MapWinGIS.Image;
if (img != null)
{
imgCount++;
}
}
}
string s = string.Format("Thong tin map:" + Environment.NewLine +
"So Point shapefiles: {0}" + Environment.NewLine +
"So Polyline shapefiles: {1}" + Environment.NewLine +
"So Polygon shapefiles: {2}" + Environment.NewLine +
"So Images: {3}", pointCount, lineCount, polyCount, imgCount);
MessageBox.Show(s);
}
private void infoToolStripMenuItem_Click(object sender, EventArgs e)
{
LayersInfo();
}
private void edit_1_Click(object sender, EventArgs e)
{
}
private void rasterToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void BtnVP_Click(object sender, EventArgs e)
{
frmLoad.Show();
}
void frmLoad_AddFromDb(string sfName)
{
var strQuery = string.Format("select * from " + sfName);
int iHandle = axMap1.AddLayerFromDatabase(frmLoad.ConnStr, strQuery, true);
var tmpSf = axMap1.get_Shapefile(iHandle);
currHandle = iHandle;
AddLayer(sfName);
}
void axMap1_MeasuringChanged(object sender, AxMapWinGIS._DMapEvents_MeasuringChangedEvent e)
{
if (e.action == tkMeasuringAction.PointAdded)
{
}
if (e.action == tkMeasuringAction.MesuringStopped)
{
if (btnAnotation.Checked)
{
var fldX = tmpLayer.Table.FieldIndexByName["ID"];
Shape shp = new Shape();
shp.Create(ShpfileType.SHP_POLYLINE);
//shp_tmp.EditAddShape()
double x, y;
//Debug.WriteLine("Measured points (in map units.): " + axMap1.Measuring.PointCount);
for (int i = 0; i < axMap1.Measuring.PointCount; i++)
{
if (axMap1.Measuring.get_PointXY(i, out x, out y))
{
//var c = 0;
MapWinGIS.Point ptn = new MapWinGIS.Point();
ptn.x = x;
ptn.y = y;
shp.InsertPoint(ptn, ref i);
//Debug.WriteLine("x={0}; y={1}", x, y);
}
}
var idx = tmpLayer.EditAddShape(shp);
tmpLayer.EditCellValue(fldX, idx, idx);
//var a= axMap1.AddLayer(tmpLayer,true);
axMap1.set_LayerVisible(idxLayerTmp, true);
axMap1.Redraw();
}
}
}
}
}