-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathSharpMapHost.cs
More file actions
512 lines (442 loc) · 17.3 KB
/
SharpMapHost.cs
File metadata and controls
512 lines (442 loc) · 17.3 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Copyright 2014-, SharpMapTeam
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System.ComponentModel;
using System.Drawing.Drawing2D;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
namespace SharpMap.UI.WPF
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using GeoAPI.Geometries;
using Data.Providers;
using Forms;
using Layers;
using Rendering.Decoration;
using Rendering.Decoration.ScaleBar;
using KeyEventArgs = KeyEventArgs;
using MouseEventArgs = MouseEventArgs;
/// <summary>
/// Extends WindowsFormsHost and encapsulates SharpMap specific code.
/// </summary>
public class SharpMapHost : WindowsFormsHost, INotifyPropertyChanged
{
// Dependency Property to store MapLayers.
public static readonly DependencyProperty MapLayersProperty =
DependencyProperty.Register("MapLayers", typeof (ObservableCollection<ILayer>), typeof (SharpMapHost), new PropertyMetadata(SetMapLayersCallback));
// Dependency Property store store BackgroundLayer.
public static readonly DependencyProperty BackgroundLayerProperty =
DependencyProperty.Register("BackgroundLayer", typeof (Layer), typeof (SharpMapHost), new PropertyMetadata(SetBackgroundLayerCallback));
// Dependency Property to store ActiveTool.
public static readonly DependencyProperty ActiveToolProperty =
DependencyProperty.Register("ActiveTool", typeof (MapBox.Tools), typeof (SharpMapHost), new PropertyMetadata(SetActiveToolCallback));
// Dependency Property to store MaxExtent.
public static readonly DependencyProperty MaxExtentProperty =
DependencyProperty.Register("MaxExtent", typeof (Envelope), typeof (SharpMapHost), new PropertyMetadata(SetMaxExtentCallback));
// Dependency Property to store MapExtent.
public static readonly DependencyProperty MapExtentProperty =
DependencyProperty.Register("MapExtent", typeof (Envelope), typeof (SharpMapHost), new PropertyMetadata(MapExtentCallback));
// Dependency Property to store Rotation kept in MapTransform
public static readonly DependencyProperty MapRotationProperty =
DependencyProperty.Register("MapRotation", typeof(float), typeof(SharpMapHost), new PropertyMetadata(MapRotationCallback));
// Dependency Property used when a new geometry is defined.
public static readonly DependencyProperty DefinedGeometryProperty =
DependencyProperty.Register("DefinedGeometry", typeof (IGeometry), typeof (SharpMapHost), new PropertyMetadata(GeometryDefinedCallback));
// Dependency Property used when right click in a MapFeature.
public static readonly DependencyProperty FeatureRightClickedCommandProperty =
DependencyProperty.Register("FeatureRightClickedCommand", typeof (ICommand), typeof (SharpMapHost));
private readonly MapBox _mapBox;
private VectorLayer _editLayer;
private GeometryProvider _editLayerGeoProvider;
private Coordinate _currentMouseCoordinate;
/// <summary>
/// Initializes a new instance of the <see cref="SharpMapHost"/> class.
/// </summary>
public SharpMapHost()
{
_mapBox = new MapBox{
BackColor = Color.White,
Map = new Map{
SRID = 900913
}
};
Child = _mapBox;
MapLayers = new ObservableCollection<ILayer>();
var scaleBar = new ScaleBar{
Anchor = MapDecorationAnchor.LeftBottom
};
_mapBox.Map.Decorations.Add(scaleBar);
_mapBox.PanOnClick = false;
KeyDown += OnKeyDown;
_mapBox.MouseMove += MapBoxOnMouseMove;
}
public ObservableCollection<ILayer> MapLayers
{
get
{
return (ObservableCollection<ILayer>) GetValue(MapLayersProperty);
}
set
{
SetValue(MapLayersProperty, value);
}
}
public Layer BackgroundLayer
{
get
{
return (Layer) GetValue(BackgroundLayerProperty);
}
set
{
SetValue(BackgroundLayerProperty, value);
}
}
public MapBox.Tools ActiveTool
{
get
{
return (MapBox.Tools) GetValue(ActiveToolProperty);
}
set
{
SetValue(ActiveToolProperty, value);
}
}
public string CurrentMouseCoordinateString
{
get
{
return _currentMouseCoordinate != null ? string.Format("{0:0}, {1:0}", _currentMouseCoordinate.X, _currentMouseCoordinate.Y) : "";
}
}
public Coordinate CurrentMouseCoordinate
{
get
{
return _currentMouseCoordinate;
}
}
public Envelope MaxExtent
{
get
{
return (Envelope) GetValue(MaxExtentProperty);
}
set
{
SetValue(MaxExtentProperty, value);
}
}
public Envelope MapExtent
{
get
{
return _mapBox.Map.Envelope;
}
set
{
SetValue(MapExtentProperty, value);
}
}
public float MapRotation
{
get
{
return _mapBox.Map.MapTransformRotation;
}
set
{
SetValue(MapRotationProperty, value);
}
}
public IGeometry DefinedGeometry
{
get
{
return (IGeometry) GetValue(DefinedGeometryProperty);
}
set
{
SetValue(DefinedGeometryProperty, value);
}
}
/// <summary>
/// The command that is invoked when a feature is right clicked
/// </summary>
public ICommand FeatureRightClickedCommand
{
get
{
return GetValue(FeatureRightClickedCommandProperty) as ICommand;
}
set
{
SetValue(FeatureRightClickedCommandProperty, value);
}
}
/// <summary>
/// Gets called when changes on MapLayers
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The event arguments</param>
private static void SetMapLayersCallback(object sender, DependencyPropertyChangedEventArgs args)
{
var host = sender as SharpMapHost;
if (host == null)
{
return;
}
var oldLayers = args.OldValue as ObservableCollection<ILayer>;
if (oldLayers != null)
{
oldLayers.CollectionChanged -= host.OnMapLayerChanged;
}
var newLayers = args.NewValue as ObservableCollection<ILayer>;
if (newLayers != null)
{
newLayers.CollectionChanged += host.OnMapLayerChanged;
}
}
/// <summary>
/// Gets called when changes on BackgroundLayer
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The event arguments</param>
private static void SetBackgroundLayerCallback(object sender, DependencyPropertyChangedEventArgs args)
{
var host = sender as SharpMapHost;
if (host == null)
{
return;
}
var mapBox = host._mapBox;
var layer = args.NewValue as Layer;
if (layer != null)
{
mapBox.Map.BackgroundLayer.Add(layer);
}
mapBox.Refresh();
}
/// <summary>
/// Gets called when changes on ActiveTool
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The event arguments</param>
private static void SetActiveToolCallback(object sender, DependencyPropertyChangedEventArgs args)
{
var host = sender as SharpMapHost;
if (host == null)
{
return;
}
var mapBox = host._mapBox;
var newTool = (MapBox.Tools) args.NewValue;
mapBox.ActiveTool = newTool;
}
/// <summary>
/// Gets called when changes on MaxExtent
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The event arguments</param>
private static void SetMaxExtentCallback(object sender, DependencyPropertyChangedEventArgs args)
{
var host = sender as SharpMapHost;
if (host == null)
{
return;
}
var mapBox = host._mapBox;
var extent = (Envelope) args.NewValue;
if (extent != null)
{
mapBox.Map.EnforceMaximumExtents = true;
mapBox.Map.MaximumExtents = extent;
}
}
/// <summary>
/// Gets called when changes on MapExtent
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The event arguments</param>
private static void MapExtentCallback(object sender, DependencyPropertyChangedEventArgs args)
{
var host = sender as SharpMapHost;
if (host == null)
{
return;
}
var mapBox = host._mapBox;
var extent = (Envelope) args.NewValue;
mapBox.Map.ZoomToBox(extent);
mapBox.Refresh();
}
/// <summary>
/// Gets called when changes on MapExtent
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The event arguments</param>
private static void MapRotationCallback(object sender, DependencyPropertyChangedEventArgs args)
{
var host = sender as SharpMapHost;
if (host == null)
{
return;
}
var mapBox = host._mapBox;
float rotation = (float)args.NewValue;
var mapTransform = new Matrix();
mapTransform.RotateAt(rotation, new PointF(mapBox.Width / 2f, mapBox.Height / 2f));
mapBox.Map.MapTransform = mapTransform;
mapBox.Refresh();
}
/// <summary>
/// Gets called when changes on GeometryDefined
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="args">The event arguments</param>
private static void GeometryDefinedCallback(object sender, DependencyPropertyChangedEventArgs args)
{
var host = sender as SharpMapHost;
if (host == null)
{
return;
}
if (host._editLayer == null)
{
host._editLayer = new VectorLayer("EditLayer");
host._editLayerGeoProvider = new GeometryProvider(new List<IGeometry>());
host._editLayer.DataSource = host._editLayerGeoProvider;
host.MapLayers.Add(host._editLayer);
}
host._editLayerGeoProvider.Geometries.Clear();
var geom = (IGeometry) args.NewValue;
if (geom != null)
{
host._editLayerGeoProvider.Geometries.Add(geom);
}
host._mapBox.Refresh();
}
/// <summary>
/// Gets called when keyboard key pressed. Pans the map according to arrow keys.
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="keyEventArgs">The event arguments</param>
private void OnKeyDown(object sender, KeyEventArgs keyEventArgs)
{
var currentEnvelope = _mapBox.Map.Envelope;
var dX = currentEnvelope.Width/2;
var dY = currentEnvelope.Height/2;
var x = _mapBox.Map.Center.X;
var y = _mapBox.Map.Center.Y;
switch (keyEventArgs.Key)
{
case Key.Left:
x -= dX;
keyEventArgs.Handled = true;
break;
case Key.Right:
x += dX;
keyEventArgs.Handled = true;
break;
case Key.Up:
y += dY;
keyEventArgs.Handled = true;
break;
case Key.Down:
y -= dY;
keyEventArgs.Handled = true;
break;
}
_mapBox.Map.Center = new Coordinate(x, y);
_mapBox.Refresh();
}
/// <summary>
/// Gets called when changes in MapLayers
/// </summary>
/// <param name="sender">The sender object</param>
/// <param name="e">The event arguments</param>
private void OnMapLayerChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
{
var layers = e.NewItems;
if (layers != null)
{
foreach (var layer in layers)
{
var castedLayer = layer as ILayer;
if (castedLayer != null && _mapBox.Map.Layers.All(l => l.LayerName != castedLayer.LayerName))
_mapBox.Map.Layers.Add(castedLayer);
}
}
}
break;
case NotifyCollectionChangedAction.Remove:
{
var layers = e.OldItems;
if (layers != null)
{
foreach (var layer in layers)
{
var castedLayer = layer as ILayer;
if (castedLayer != null && _mapBox.Map.Layers.Any(l => l.LayerName == castedLayer.LayerName))
_mapBox.Map.Layers.Remove(castedLayer);
}
}
}
break;
case NotifyCollectionChangedAction.Reset:
_mapBox.Map.Layers.Clear();
break;
}
_mapBox.Refresh();
}
public void ZoomToExtents()
{
_mapBox.Map.ZoomToExtents();
_mapBox.Refresh();
}
public void ZoomToEnvelope(Envelope env)
{
_mapBox.Map.ZoomToBox(env);
_mapBox.Refresh();
}
/// <summary>
/// Gets called when mouse moves over map
/// </summary>
/// <param name="worldPos">The click coordinate</param>
/// <param name="mouseEventArgs">The event arguments</param>
private void MapBoxOnMouseMove(Coordinate worldPos, MouseEventArgs mouseEventArgs)
{
_currentMouseCoordinate = worldPos;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("CurrentMouseCoordinate"));
PropertyChanged(this, new PropertyChangedEventArgs("CurrentMouseCoordinateString"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}