forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormMapBox.cs
More file actions
586 lines (513 loc) · 21.3 KB
/
FormMapBox.cs
File metadata and controls
586 lines (513 loc) · 21.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using SharpMap;
using SharpMap.CoordinateSystems;
using SharpMap.CoordinateSystems.Transformations;
using SharpMap.Forms;
using SharpMap.Forms.Tools;
using SharpMap.Layers;
using System.Drawing.Drawing2D;
using SharpMap.Data;
using SharpMap.Rendering.Decoration;
using WinFormSamples.Samples;
namespace WinFormSamples
{
public partial class FormMapBox : Form
{
private static readonly Dictionary<string, Type> MapDecorationTypes = new Dictionary<string, Type>();
private static bool AddToListView;
public static bool UseDotSpatial
{
get
{
return Session.Instance.CoordinateSystemServices.GetCoordinateSystem(4326) is DotSpatialCoordinateSystem;
}
set
{
if (value == UseDotSpatial) return;
var s = (Session) Session.Instance;
var css = !value
? new CoordinateSystemServices(
new CoordinateSystemFactory(),
new CoordinateTransformationFactory(),
SharpMap.Converters.WellKnownText.SpatialReference.GetAllReferenceSystems())
: new CoordinateSystemServices(
new DotSpatialCoordinateSystemFactory(),
new DotSpatialCoordinateTransformationFactory(),
SharpMap.Converters.WellKnownText.SpatialReference.GetAllReferenceSystems());
s.SetCoordinateSystemServices(css);
}
}
public FormMapBox()
{
AddToListView = false;
InitializeComponent();
mapBox1.ActiveTool = MapBox.Tools.Pan;
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (System.Reflection.Assembly a in loadedAssemblies)
LoadMapDecorationTypes(a);
foreach (var name in MapDecorationTypes.Keys)
{
lvwDecorations.Items.Add(name);
}
AddToListView = true;
AppDomain.CurrentDomain.AssemblyLoad += HandleAssemblyLoad;
pgMapDecoration.SelectedObject = null;
radioButton2.Checked = true;
radioButton_Click(radioButton2, EventArgs.Empty);
}
private void HandleAssemblyLoad(object sender, AssemblyLoadEventArgs args)
{
LoadMapDecorationTypes(args.LoadedAssembly);
}
private void LoadMapDecorationTypes(System.Reflection.Assembly a)
{
var mdtype = typeof(SharpMap.Rendering.Decoration.IMapDecoration);
try
{
foreach (Type type in a.GetTypes())
{
if (type.FullName.StartsWith("SharpMap"))
Console.WriteLine(type.FullName);
if (mdtype.IsAssignableFrom(type))
{
if (!type.IsAbstract)
{
if (AddToListView)
lvwDecorations.Items.Add(new ListViewItem(type.Name));
MapDecorationTypes[type.Name] = type;
}
}
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception)
{
}
#pragma warning restore CA1031 // Do not catch general exception types
}
private void UpdatePropertyGrid()
{
if (pgMap.InvokeRequired)
pgMap.Invoke(new MethodInvoker(() => pgMap.Update()));
else
pgMap.Update();
}
private static void AdjustRotation(LayerCollection lc, float angle)
{
foreach (ILayer layer in lc)
{
if (layer is VectorLayer)
((VectorLayer) layer).Style.SymbolRotation = -angle;
else if (layer is LabelLayer)
((LabelLayer)layer).Style.Rotation = -angle;
}
}
private static string[] GetOpenFileName(string filter)
{
using (var ofd = new OpenFileDialog())
{
ofd.CheckFileExists = true;
ofd.ShowReadOnly = false;
ofd.Multiselect = true;
ofd.Filter = filter;
if (ofd.ShowDialog() == DialogResult.OK)
return ofd.FileNames;
return null;
}
}
private void btnTool_Click(object sender, EventArgs e)
{
var btn = (Button) sender;
IMapTool tool = null;
switch (btn.Name)
{
case "btnTool":
tool = (mapBox1.CustomTool is SampleTool) ? null : new SampleTool(mapBox1);
break;
case "btnTool2":
tool = (mapBox1.CustomTool is MagnifierTool) ? null : new MagnifierTool(mapBox1);
break;
}
var oldCustomTool = mapBox1.CustomTool;
if (oldCustomTool is SampleTool) btnTool.Font = new Font(btn.Font, FontStyle.Regular);
if (oldCustomTool is MagnifierTool) btnTool2.Font = new Font(btn.Font, FontStyle.Regular);
if (oldCustomTool is IDisposable) ((IDisposable) oldCustomTool).Dispose();
mapBox1.CustomTool = tool;
btn.Font = new Font(btn.Font, tool == null ? FontStyle.Regular : FontStyle.Bold);
if (tool == null)
mapBox1.ActiveTool = MapBox.Tools.Pan;
//if (mapBox1.CustomTool == null)
// mapBox1.CustomTool = new SampleTool(mapBox1);
//else
//{
// mapBox1.CustomTool = null;
// mapBox1.ActiveTool = MapBox.Tools.Pan;
//}
}
void mapBox1_MapQueryStarted(object sender, EventArgs e)
{
dataGridView1.DataSource = _queriedData = null;
}
void mapBox1_MapQueryEnded(object sender, EventArgs e)
{
if (_queriedData != null && _queriedData.Tables.Count > 0)
dataGridView1.DataSource = _queriedData.Tables[0];
}
private FeatureDataSet _queriedData;
private void mapBox1_OnMapQueried(FeatureDataTable data)
{
if (_queriedData == null)
_queriedData = new FeatureDataSet();
_queriedData.Tables.Add(data);
}
private void btnCreateTiles_Click(object sender, EventArgs e)
{
if (mapBox1.Map == null)
return;
if (mapBox1.Map.Layers.Count == 0)
return;
using (var f = new FormCreateTilesSample())
{
f.Map = mapBox1.Map;
f.ShowDialog();
}
}
private void radioButton_Click(object sender, EventArgs e)
{
Cursor mic = mapBox1.Cursor;
mapBox1.Cursor = Cursors.WaitCursor;
Cursor = Cursors.WaitCursor;
if (formSqlServerOpts != null)
{
formSqlServerOpts.Close();
formSqlServerOpts = null;
}
try
{
//mapImage.ActiveTool = MapImage.Tools.None;
string text = ((RadioButton)sender).Text;
switch (text)
{
case "Shapefile":
mapBox1.Map = ShapefileSample.InitializeMap(tbAngle.Value);
break;
case "GradientTheme":
mapBox1.Map = GradiantThemeSample.InitializeMap(tbAngle.Value);
break;
case "WFS Client":
mapBox1.Map = WfsSample.InitializeMap(tbAngle.Value);
break;
case "WMS Client":
//mapBox1.Map = TiledWmsSample.InitializeMap();
mapBox1.Map = WmsSample.InitializeMap(tbAngle.Value);
break;
case "OGR - MapInfo":
case "OGR - S-57":
mapBox1.Map = OgrSample.InitializeMap(tbAngle.Value);
break;
case "GDAL - GeoTiff":
case "GDAL - '.DEM'":
case "GDAL - '.ASC'":
case "GDAL - '.VRT'":
mapBox1.Map = GdalSample.InitializeMap(tbAngle.Value);
mapBox1.ActiveTool = MapBox.Tools.Pan;
break;
case "TileLayer - OSM":
case "TileLayer - OSM with XLS":
case "TileLayer - Bing Roads":
case "TileLayer - Bing Aerial":
case "TileLayer - Bing Hybrid":
case "TileLayer - GoogleMap":
case "TileLayer - GoogleSatellite":
case "TileLayer - GoogleTerrain":
case "TileLayer - GoogleLabels":
case "Eniro":
/*
tbAngle.Visible = text.Equals("TileLayer - GoogleLabels");
if (!tbAngle.Visible) tbAngle.Value = 0;
*/
mapBox1.Map = TileLayerSample.InitializeMap(tbAngle.Value);
((RadioButton)sender).Text = (mapBox1.Map.BackgroundLayer.Count > 0)
? ((RadioButton)sender).Text = mapBox1.Map.BackgroundLayer[0].LayerName
: ((RadioButton)sender).Text = mapBox1.Map.Layers[0].LayerName;
break;
case "PostGis":
mapBox1.Map = PostGisSample.InitializeMap(tbAngle.Value);
break;
case "SpatiaLite":
mapBox1.Map = SpatiaLiteSample.InitializeMap(tbAngle.Value);
break;
case "SqlServer":
// create empty map with BruTile basemap
mapBox1.Map = SqlServerSample.InitializeMap(tbAngle.Value);
// check conn string
var connStrBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder(Properties.Settings.Default.SqlServerConnectionString);
if (string.IsNullOrEmpty(connStrBuilder.DataSource) || string.IsNullOrEmpty(connStrBuilder.InitialCatalog))
{
mapBox1.Refresh();
MessageBox.Show("Requires SqlServer connection string to be defined (Project / Settings)",
"Sql Server", MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
// now show SqlServer dialog
formSqlServerOpts = new FormSqlServerOpts()
{
MapBox = mapBox1,
ConnectionString = connStrBuilder.ToString()
};
formSqlServerOpts.Show(this);
break;
case "Oracle":
mapBox1.Map = OracleSample.InitializeMap(tbAngle.Value);
break;
case "shp_TextOnPath":
mapBox1.Map = TextOnPathSample.InitializeMapOrig(tbAngle.Value);
break;
case "GdiImageLayer":
mapBox1.Map = GdiImageLayerSample.InitializeMap(tbAngle.Value);
break;
default:
break;
}
//Add checked Map decorations
foreach (ListViewItem checkedItem in lvwDecorations.CheckedItems)
{
Type mdType;
if (MapDecorationTypes.TryGetValue(checkedItem.Text, out mdType))
{
IMapDecoration md = Activator.CreateInstance(mdType) as IMapDecoration;
mapBox1.Map.Decorations.Add(md);
}
}
mapBox1.Map.Size = Size;
mapBox1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error");
}
finally
{
Cursor = Cursors.Default;
mapBox1.Cursor = mic;
}
}
private void mapImage_ActiveToolChanged(MapBox.Tools tool)
{
UpdatePropertyGrid();
}
private void mapImage_MapCenterChanged(GeoAPI.Geometries.Coordinate center)
{
UpdatePropertyGrid();
}
private void mapImage_MapRefreshed(object sender, EventArgs e)
{
UpdatePropertyGrid();
}
private void mapImage_MapZoomChanged(double zoom)
{
UpdatePropertyGrid();
Console.WriteLine("Current Extents: {0}", mapBox1.Map.Envelope);
}
private void mapImage_MapZooming(double zoom)
{
UpdatePropertyGrid();
}
private void mapImage_SizeChanged(object sender, EventArgs e)
{
mapBox1.Refresh();
}
private void tbAngle_Scroll(object sender, EventArgs e)
{
System.Drawing.Drawing2D.Matrix matrix = new Matrix();
if (tbAngle.Value != 0f)
matrix.RotateAt(tbAngle.Value, new PointF(mapBox1.Width * 0.5f, mapBox1.Height * 0.5f));
mapBox1.Map.MapTransform = matrix;
AdjustRotation(mapBox1.Map.Layers, tbAngle.Value);
AdjustRotation(mapBox1.Map.VariableLayers, tbAngle.Value);
mapBox1.Refresh();
}
private string _gdalConnection = string.Empty;
private void radioButton2_MouseUp(object sender, MouseEventArgs e)
{
var rb = sender as RadioButton;
if (rb == null) return;
if (e.Button != MouseButtons.Right) return;
SharpMap.Map map = null;
switch (rb.Name)
{
case "radioButton2": // ShapeFile
map = Samples.ShapefileSample.InitializeMap(tbAngle.Value, GetOpenFileName("Shapefile|*.shp"));
break;
case "radioButton5": // Ogr
map = Samples.OgrSample.InitializeMap(tbAngle.Value, GetOpenFileName("Ogr Datasource|*.*"));
break;
case "radioButton6": // Gdal
if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
{
string cn = InputBox(
"Please enter connection string.\n" +
"For e.g. PostGis Rasters sth. like 'PG:host=... port=... dbname=... schema=... table=... column=...'",
@"GDAL Raster Layer Connection", _gdalConnection);
if (string.IsNullOrEmpty(cn))
return;
map = Samples.GdalSample.InitializeMap(tbAngle.Value, new []{ cn });
_gdalConnection = cn;
}
else
{
map = Samples.GdalSample.InitializeMap(tbAngle.Value, GetOpenFileName("Gdal Datasource|*.*"));
}
break;
case "radioButton9": // spatialite
map = Samples.SpatiaLiteSample.InitializeMap(tbAngle.Value, GetOpenFileName("SpatiaLite 2|*.db;*.sqlite"));
break;
}
if (map == null)
return;
//Add checked Map decorations
foreach (ListViewItem checkedItem in lvwDecorations.CheckedItems)
{
Type mdType;
if (MapDecorationTypes.TryGetValue(checkedItem.Text, out mdType))
{
IMapDecoration md = Activator.CreateInstance(mdType) as IMapDecoration;
map.Decorations.Add(md);
}
}
mapBox1.Map = map;
}
private void lvwDecorations_ItemChecked(object sender, ItemCheckedEventArgs e)
{
Type mdType;
if (!MapDecorationTypes.TryGetValue(e.Item.Text, out mdType))
return;
if (e.Item.Checked)
{
IMapDecoration ins = Activator.CreateInstance(mdType) as IMapDecoration;
if (ins != null)
{
mapBox1.Map.Decorations.Add(ins);
}
}
else
{
foreach (var item in mapBox1.Map.Decorations)
{
var mdTmpType = item.GetType();
if (mdType.Equals(mdTmpType))
{
mapBox1.Map.Decorations.Remove(item);
break;
}
}
}
e.Item.Selected = true;
mapBox1.Refresh();
}
private void lvwDecorations_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvwDecorations.SelectedItems.Count == 0)
{
pgMapDecoration.SelectedObject = null;
return;
}
var lvwi = (ListViewItem)lvwDecorations.SelectedItems[0];
if (!lvwi.Checked)
{
pgMapDecoration.SelectedObject = null;
pgMapDecoration.Visible = false;
return;
}
Type mdType;
if (MapDecorationTypes.TryGetValue(lvwi.Text, out mdType))
{
foreach (IMapDecoration mapDecoration in mapBox1.Map.Decorations)
{
if (mapDecoration.GetType().Equals(mdType))
{
pgMapDecoration.SelectedObject = mapDecoration;
pgMapDecoration.Visible = true;
return;
}
}
}
pgMapDecoration.SelectedObject = null;
pgMapDecoration.Visible = false;
}
private static string InputBox(string text, string caption, string defaultValue = null)
{
var prompt = new Form
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
MaximizeBox = false,
MinimizeBox = false,
ShowInTaskbar = false,
StartPosition = FormStartPosition.CenterScreen,
Padding = new Padding(5),
Margin = new Padding(10)
};
var textLabel = new TextBox
{
Left = prompt.Margin.Left,
Top = prompt.Margin.Top,
Width = prompt.ClientSize.Width - prompt.Margin.Left - prompt.Margin.Right,
BackColor = SystemColors.Control,
BorderStyle = BorderStyle.None,
Multiline = true,
HideSelection = true,
ReadOnly = true, Enabled = false
};
var size = SizeF.Empty;
using (var g = Graphics.FromHwnd(IntPtr.Zero))
size = g.MeasureString(text, DefaultFont, new SizeF(textLabel.Width, 20 * textLabel.Height));
textLabel.Height = System.Drawing.Size.Ceiling(size).Height;
textLabel.Lines = text.Split('\n');
textLabel.SelectionStart = 0;
textLabel.SelectionLength = 0;
var textBox = new TextBox
{
Left = prompt.Margin.Left,
Top = textLabel.Bottom,
Width = prompt.ClientSize.Width - prompt.Margin.Left - prompt.Margin.Right,
Multiline = true,
Text = defaultValue ?? string.Empty
};
var confirmation = new Button
{
Text = "&OK",
Width = 100,
Left = prompt.ClientSize.Width - prompt.Margin.Right - 100,
Top = textBox.Top + textBox.Height + prompt.Padding.Vertical,
DialogResult = DialogResult.OK
};
prompt.ClientSize = new Size(prompt.ClientSize.Width, confirmation.Bottom + prompt.Padding.Bottom);
prompt.AcceptButton = confirmation;
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
private void FormMapBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{
mapBox1.Invalidate();
mapBox1.Refresh();
//e.SuppressKeyPress = true;
e.Handled = true;
}
}
}
}