forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleTool.cs
More file actions
284 lines (244 loc) · 9.97 KB
/
SampleTool.cs
File metadata and controls
284 lines (244 loc) · 9.97 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common.Logging;
using GeoAPI.Geometries;
using SharpMap;
using SharpMap.Data;
using SharpMap.Forms;
using SharpMap.Forms.Tools;
using SharpMap.Layers;
namespace WinFormSamples
{
public class SampleTool : MapTool, IDisposable
{
private ILog Logger = LogManager.GetLogger(typeof(SampleTool));
public SampleTool() : base("Sample", "A sample tool that does nothing really useful")
{
Logger.Debug(fmh => fmh("Created \"{0}\"-Tool, {1}", Name,Description));
Enabled = true;
}
void IDisposable.Dispose()
{
HandleCancel();
}
private ToolTip _toolTip;
public SampleTool(MapBox mapBox1) : this()
{
MapControl = mapBox1;
MapControl.MapChanged += (sender, args) => { HandleCancel(); };
_cts.Token.Register(HandleCancel);
}
public override bool DoKeyDown(Coordinate mapPosition, KeyEventArgs keyEventArgs)
{
Logger.Debug(fmh => fmh("KeyDown {1} at {0}", mapPosition, keyEventArgs.KeyCode));
return base.DoKeyDown(mapPosition, keyEventArgs);
}
public override bool DoKeyUp(Coordinate mapPosition, KeyEventArgs keyEventArgs)
{
Logger.Debug(fmh => fmh("KeyUp {1} at {0}", mapPosition, keyEventArgs.KeyCode));
return base.DoKeyUp(mapPosition, keyEventArgs);
}
public override bool DoMouseDoubleClick(Coordinate mapPosition, MouseEventArgs mouseEventArgs)
{
Logger.Debug(fmh => fmh("MouseDoubleClick {1} at {0}", mapPosition, mouseEventArgs.Button));
return base.DoMouseDoubleClick(mapPosition, mouseEventArgs);
}
public override bool DoMouseDown(Coordinate mapPosition, MouseEventArgs mouseEventArgs)
{
Logger.Debug(fmh => fmh("MouseDown {1} at {0}", mapPosition, mouseEventArgs.Button));
return base.DoMouseDown(mapPosition, mouseEventArgs);
}
public override bool DoMouseEnter()
{
Logger.Debug(fmh => fmh("MouseEnter"));
return base.DoMouseEnter();
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
if (!Enabled)
{
if (_toolTip != null) _toolTip.RemoveAll();
}
}
private CancellationTokenSource _cts = new CancellationTokenSource();
public override bool DoMouseHover(Coordinate mapPosition)
{
Logger.Debug(fmh => fmh("MouseHover at {0}", mapPosition));
_cts.Cancel();
_cts.Token.WaitHandle.WaitOne();
_cts.Dispose();
_cts = new CancellationTokenSource();
_cts.Token.Register(HandleCancel);
var t = new Task<FeatureDataRow>(FindGeoNearPoint, mapPosition, _cts.Token);
t.Start();
t.ContinueWith(res => ShowToolTip(res.Result));
return base.DoMouseHover(mapPosition);
}
BackgroundWorker _bw = new BackgroundWorker();
void HandleCancel()
{
if (_toolTip != null)
{
MapControl.Invoke(new MethodInvoker(() =>
{
_toolTip.Hide(MapControl);
_toolTip.Dispose();
}));
_toolTip = null;
}
}
private void ShowToolTip(FeatureDataRow fdr)
{
if (fdr != null)
{
MapControl.BeginInvoke(new Action(() =>
{
var _t = new ToolTip();
_toolTip = _t;
_t/*oolTip*/.ToolTipTitle = fdr.Table.TableName;
_t/*oolTip*/.Show(ToText(fdr), MapControl);
}));
}
else
{
//MapControl.BeginInvoke(new MethodInvoker( () => _toolTip.Hide(MapControl)));
}
}
private string ToText(FeatureDataRow fdr)
{
var sb = new StringBuilder();
if (fdr.Geometry != null)
{
sb.AppendFormat("Geometry:\n Type: {0}\n SRID: {1}\n",
fdr.Geometry.GeometryType, fdr.Geometry.SRID);
switch (fdr.Geometry.Dimension)
{
case Dimension.Surface:
sb.AppendFormat(" Area: {0}\n", fdr.Geometry.Area);
break;
case Dimension.Curve:
sb.AppendFormat(" Length: {0}\n", fdr.Geometry.Length);
break;
case Dimension.Point:
sb.AppendFormat(" Position: {0}\n", fdr.Geometry.AsText());
break;
}
}
sb.Append("Data:\n");
foreach (DataColumn col in fdr.Table.Columns)
sb.AppendFormat(" {0}: {1}\n", col.Caption, fdr[col] == DBNull.Value ? "NULL" : fdr[col]);
Logger.Debug(fmh => fmh("\n{0}\n{1}", fdr.Table.TableName, sb.ToString(0, sb.Length-1)));
return sb.ToString(0, sb.Length - 1);
}
private FeatureDataRow FindGeoNearPoint(object/*Coordinate*/ coord)
{
var mapPosition = (Coordinate) coord;
var env = new Envelope(mapPosition);
env.ExpandBy(5 * Map.PixelWidth);
var g = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(Map.Factory.ToGeometry(env));
var fdrs = new List<Tuple<double, FeatureDataRow>>();
var fds = new FeatureDataSet();
var tableCount = 0;
var layersToQuery = GetLayersToQuery(Map);
for (var i = layersToQuery.Count - 1; i >= 0; i--)
{
if (_cts.Token.IsCancellationRequested)
{
Logger.Debug("Cancellation requested");
return null;
}
var l = layersToQuery[i];
if (l.Enabled && l.MinVisible < Map.Zoom &&
l.MaxVisible >= Map.Zoom)
{
if (!l.IsQueryEnabled) continue;
l.ExecuteIntersectionQuery(env, fds);
for (var j = tableCount; j < fds.Tables.Count; j++)
{
var fdt = fds.Tables[j];
for (var k = 0; k < fdt.Rows.Count; k++)
{
var fdr = (FeatureDataRow) fdt.Rows[k];
if (g.Intersects(fdr.Geometry))
{
var distance = g.Geometry.InteriorPoint.Distance(fdr.Geometry);
if (fdr.Geometry.Dimension == Dimension.Surface)
distance += 5* Map.PixelWidth;
fdrs.Add(Tuple.Create(distance, fdr));
}
}
}
tableCount = fds.Tables.Count;
}
}
if (fdrs.Count > 0)
{
fdrs.Sort((t1, t2) => t1.Item1.CompareTo(t2.Item1));
return fdrs[0].Item2;
}
return null;
}
private static List<ICanQueryLayer> GetLayersToQuery(Map map)
{
var res = new List<ICanQueryLayer>();
for (var i = 0; i < map.BackgroundLayer.Count; i++)
{
if (map.BackgroundLayer[i] is ICanQueryLayer)
{
if (((ICanQueryLayer)map.BackgroundLayer[i]).IsQueryEnabled)
res.Add((ICanQueryLayer)map.BackgroundLayer[i]);
}
}
for (var i = 0; i < map.Layers.Count; i++)
{
if (map.Layers[i] is ICanQueryLayer)
{
if (((ICanQueryLayer)map.Layers[i]).IsQueryEnabled)
res.Add((ICanQueryLayer)map.Layers[i]);
}
}
return res;
}
public MapBox MapControl { get; private set; }
public override bool DoMouseLeave()
{
Logger.Debug(fmh => fmh("MouseLeave"));
//_toolTip.RemoveAll();
return base.DoMouseLeave();
}
public override bool DoMouseMove(Coordinate mapPosition, MouseEventArgs mouseEventArgs)
{
Logger.Debug(fmh => fmh("MouseMove {1} at {0}", mapPosition, mouseEventArgs.Button));
//_toolTip.RemoveAll();
return base.DoMouseMove(mapPosition, mouseEventArgs);
}
public override bool DoMouseUp(Coordinate mapPosition, MouseEventArgs mouseEventArgs)
{
Logger.Debug(fmh => fmh("MouseUp {1} at {0}", mapPosition, mouseEventArgs.Button));
return base.DoMouseUp(mapPosition, mouseEventArgs);
}
public override bool DoMouseWheel(Coordinate mapPosition, MouseEventArgs mouseEventArgs)
{
Logger.Debug(fmh => fmh("MouseWheel {1} at {0}", mapPosition, mouseEventArgs.Button));
return base.DoMouseWheel(mapPosition, mouseEventArgs);
}
public override void Cancel()
{
Logger.Debug(fmh => fmh("Cancel"));
base.Cancel();
}
public override void DoPaint(PaintEventArgs e)
{
Logger.Debug(fmh => fmh("Paint"));
base.DoPaint(e);
}
}
}