forked from SharpMap/SharpMap.DeltaShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryTool.cs
More file actions
160 lines (136 loc) · 5.11 KB
/
QueryTool.cs
File metadata and controls
160 lines (136 loc) · 5.11 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
using System;
using System.Collections;
using System.Linq;
using System.Windows.Forms;
using DelftTools.Utils;
using GeoAPI.Extensions.Coverages;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Extensions.Coverages;
using SharpMap.Layers;
namespace SharpMap.UI.Tools
{
public class QueryTool : MapTool
{
private readonly ToolTip tooltipControl = new ToolTip
{
AutomaticDelay = 1000,
ToolTipIcon = ToolTipIcon.Info
};
public QueryTool()
{
Name = "Query";
}
/// <summary>
/// Use this property to enable or disable tool. When the measure tool is deactivated, it cleans up old measurements.
/// </summary>
public override bool IsActive
{
get { return base.IsActive; }
set
{
base.IsActive = value;
if (!IsActive)
Clear();
}
}
private void Clear()
{
tooltipControl.SetToolTip((Control) MapControl, "");
}
private string GetCoverageValues(Coordinate worldPosition)
{
if (!IsActive || worldPosition == null)
return "";
var layerValues = "";
Coordinate coordinate = new Coordinate(worldPosition.X, worldPosition.Y);
var allLayers = Map.GetAllVisibleLayers(true);
foreach (var layer in allLayers)
{
if (!(layer is ICoverageLayer) || layer.Envelope == null)
continue;
var envelope = (Envelope) layer.Envelope.Clone();
envelope.ExpandBy(layer.Envelope.Width*0.1); // 10% margin
if (!envelope.Contains(coordinate))
continue;
var coverage = ((ICoverageLayer)layer).Coverage;
if (coverage.IsTimeDependent)
{
if (layer is ITimeNavigatable)
{
var timeNav = (layer as ITimeNavigatable);
if (timeNav.TimeSelectionStart.HasValue)
{
var time = timeNav.TimeSelectionStart.Value;
coverage = coverage.FilterTime(time);
}
}
else
{
continue; //nothing to filter on
}
}
SetEvaluationTolerance(coverage);
var value = coverage.Evaluate(coordinate);
string valueString = GetValueString(value);
if ((coverage is IRegularGridCoverage) && (coverage.Components.Count > 0))
{
if (coverage.Components.Count > 1) throw new NotImplementedException("Query tool for multi-component grid-coverages not implemented");
if (coverage.Components[0].NoDataValues.Contains(value))
{
valueString = "No data";
}
}
layerValues += layer.Name + " : " + valueString + "\n";
}
return layerValues;
}
private static string GetValueString(object value)
{
var valueString = "";
var enumerable = value as IEnumerable;
if (enumerable != null)
{
valueString = enumerable.Cast<object>().Aggregate(valueString, (current, valueItem) => current + (valueItem + ","));
valueString = valueString == "" ? "<empty>" : valueString.TrimEnd(',');
}
else
{
valueString = (value ?? "<empty>").ToString();
}
return valueString;
}
private void SetEvaluationTolerance(ICoverage coverage)
{
var featureCoverage1 = coverage as FeatureCoverage;
if (featureCoverage1 != null)
{
var tolerance = Map.PixelSize * 5;
var featureCoverage = featureCoverage1;
if (featureCoverage.EvaluateTolerance != tolerance)
{
featureCoverage.EvaluateTolerance = tolerance;
}
}
var networkCoverage = coverage as NetworkCoverage;
if (networkCoverage != null)
{
var tolerance = Map.PixelSize * 20;
if (networkCoverage.EvaluateTolerance != tolerance)
{
networkCoverage.EvaluateTolerance = tolerance;
}
}
}
private System.Drawing.Point previousLocation;
public override void OnMouseMove(Coordinate worldPosition, MouseEventArgs e)
{
if (previousLocation.X == e.Location.X && previousLocation.Y == e.Location.Y)
{
return;
}
previousLocation = e.Location;
tooltipControl.SetToolTip((Control)MapControl, GetCoverageValues(worldPosition));
}
}
}