Skip to content
Navigation Menu
{{ message }}
forked from DMS-Aus/MapManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapControl.cs
More file actions
1505 lines (1374 loc) · 55.8 KB
/
Copy pathMapControl.cs
File metadata and controls
1505 lines (1374 loc) · 55.8 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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using OSGeo.MapServer;
using System.Diagnostics;
using MapLibrary.Properties;
using System.Reflection;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace DMS.MapLibrary
{
/// <summary>
/// Event handler to sign that the cursor is moving.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="x">The x position in map coordinates.</param>
/// <param name="y">The y position in map coordinates.</param>
public delegate void CursorMoveEventHandler(object sender, double x, double y);
/// <summary>
/// User Control to render maps.
/// </summary>
public partial class MapControl : Control, IMapControl
{
// properties
private MapObjectHolder target;
public mapObj map;
private Image mapImage = null;
private InputModes inputMode;
private Rectangle dragRect;
private bool dragging;
private SolidBrush selectionBrush;
private Pen selectionPen;
private double a11, a13, a21, a23;
private int unitPrecision;
private string unitName;
private MS_UNITS mapunits;
private int gap;
private bool border;
private Pen borderPen;
private bool centerMarker;
private Pen centerMarkerPen;
private string drawMessage;
private bool forcePan = false;
private float magnify = 1;
private int mouseX, mouseY;
private GraphicsPath trackPoints;
private bool queryMode = false;
private bool drawQuery = false;
private bool enableRendering = true;
private long lastRenderTime = 0;
private NavigationHistory history = new NavigationHistory();
// public events
/// <summary>
/// The CursorMove event object.
/// </summary>
public event CursorMoveEventHandler CursorMove;
/// <summary>
/// The BeforeRefresh event object.
/// </summary>
public event EventHandler BeforeRefresh;
/// <summary>
/// The AfterRefresh event object.
/// </summary>
public event EventHandler AfterRefresh;
/// <summary>
/// Constructs a new MapControl object.
/// </summary>
public MapControl()
{
InitializeComponent();
InputMode = InputModes.Pan;
dragRect = new Rectangle(0,0,0,0);
dragging = false;
selectionBrush = new SolidBrush(Color.FromArgb(75, Color.Gray));
selectionPen = new Pen(Color.Black,1);
borderPen = new Pen(Color.Black, 1);
border = false;
centerMarkerPen = new Pen(Color.Blue, 1);
centerMarker = false;
drawMessage = "The map hasn't been initialised yet.";
gap = 10;
unitPrecision = 4;
unitName = "";
mapunits = MS_UNITS.MS_METERS;
a11 = a13 = a21 = a23 = 0;
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
}
/// <summary>
/// Enum for The user editing modes (panning or zooming).
/// </summary>
public enum InputModes
{
/// <summary>
/// Panning
/// </summary>
Pan,
/// <summary>
/// Zooming in
/// </summary>
ZoomIn,
/// <summary>
/// Zooming out
/// </summary>
ZoomOut,
/// <summary>
/// Rectangle tracking
/// </summary>
TrackRectangle,
/// <summary>
/// Polygon tracking
/// </summary>
TrackPolygon,
/// <summary>
/// Line tracking
/// </summary>
TrackLine,
/// <summary>
/// Select Item
/// </summary>
Select
}
public bool EnableRendering
{
get
{
return enableRendering;
}
set
{
enableRendering = value;
RefreshView();
}
}
/// <summary>
/// The current user editing mode (panning or zooming).
/// </summary>
public InputModes InputMode
{
get { return inputMode; }
set
{
inputMode = value;
trackPoints = null;
if (!DesignMode)
{
switch (inputMode)
{
case InputModes.Pan:
this.Cursor = new Cursor(new MemoryStream(Resources.GrabberTool));
break;
case InputModes.ZoomIn:
this.Cursor = new Cursor(new MemoryStream(Resources.ZoomInTool));
break;
case InputModes.ZoomOut:
this.Cursor = new Cursor(new MemoryStream(Resources.ZoomOutTool));
break;
case InputModes.Select:
this.Cursor = new Cursor(new MemoryStream(Resources.SelectTool));
break;
case InputModes.TrackRectangle:
this.Cursor = new Cursor(new MemoryStream(Resources.SelectRectTool));
break;
case InputModes.TrackPolygon:
this.Cursor = new Cursor(new MemoryStream(Resources.SelectPolygonTool));
break;
default:
this.Cursor = Cursors.Default;
break;
}
}
}
}
/// <summary>
/// The sample feature border gap in pixels.
/// </summary>
public int Gap
{
get { return gap; }
set
{
gap = value;
}
}
/// <summary>
/// Gets the current map image object.
/// </summary>
public Image MapImage
{
get { return mapImage; }
}
/// <summary>
/// Gets the last rendering time of the map.
/// </summary>
public long LastRenderTime
{
get { return lastRenderTime; }
}
/// <summary>
/// Flag to enable the drawing of a single line border.
/// </summary>
public bool Border
{
get { return border; }
set
{
border = value;
this.RefreshView();
}
}
/// <summary>
/// Flag to enable the drawing of a center marker.
/// </summary>
public bool CenterMarker
{
get { return centerMarker; }
set
{
centerMarker = value;
this.RefreshView();
}
}
/// <summary>
/// Update the pixel-map transformation coefficients.
/// </summary>
private void UpdateTansformations()
{
// update the map to pixel transformation
if (this.Width > 2)
a11 = (map.extent.maxx - map.extent.minx) / this.Width;
else a11 = 0;
a13 = map.extent.minx;
if (this.Height > 2)
a21 = -a11;
else a21 = 0;
a23 = map.extent.maxy;
// update the pixel to map transformation
}
/// <summary>
/// Converts the X coordinate from pixel space to map coordinate space.
/// </summary>
/// <param name="x">Pixel coordinate.</param>
/// <returns>Map coordinate.</returns>
private double Pixel2MapX(double x)
{
return a11 * x + a13;
}
/// <summary>
/// Converts the Y coordinate from pixel space to map coordinate space.
/// </summary>
/// <param name="y">Pixel coordinate.</param>
/// <returns>Map coordinate.</returns>
private double Pixel2MapY(double y)
{
return a21 * y + a23;
}
/// <summary>
/// Converts the MapScript shape object to the GDI+ GraphicsPath.
/// </summary>
/// <param name="feature">The shapeObj to conver.</param>
/// <returns>The converted GraphicsPath object</returns>
public static GraphicsPath ShapeToGraphicsPath(shapeObj feature)
{
GraphicsPath path = new GraphicsPath();
path.Reset();
if (feature != null)
{
lineObj line;
pointObj point;
for (int i = 0; i < feature.numlines; i++)
{
path.StartFigure();
line = feature.get(i);
for (int j = 0; j < line.numpoints; j++)
{
point = line.get(j);
PointF[] pts = { new PointF((float)point.x, (float)point.y) };
path.AddLines(pts);
}
if (feature.type == (int)MS_SHAPE_TYPE.MS_SHAPE_POLYGON) path.CloseFigure();
}
}
return path;
}
/// <summary>
/// Converts the GDI+ GraphicsPath to the MapScript shape object.
/// </summary>
/// <param name="path">The GraphicsPath object to convert.</param>
/// <returns>The converted shapeObj</returns>
public shapeObj GraphicsPathToShape(GraphicsPath path)
{
shapeObj feature = null;
if (path != null)
{
using (GraphicsPathIterator myPathIterator = new GraphicsPathIterator(path))
{
int myStartIndex;
int myEndIndex;
bool myIsClosed;
// get the number of Subpaths.
int numSubpaths = myPathIterator.SubpathCount;
while (myPathIterator.NextSubpath(out myStartIndex, out myEndIndex, out myIsClosed) > 0)
{
lineObj line = new lineObj();
for (int i = myStartIndex; i <= myEndIndex; i++)
{
if (i == myStartIndex ||
(path.PathPoints[i].X != path.PathPoints[i - 1].X &&
path.PathPoints[i].Y != path.PathPoints[i - 1].Y))
line.add(new pointObj(Pixel2MapX(path.PathPoints[i].X), Pixel2MapY(path.PathPoints[i].Y), 0, 0));
}
if (feature == null)
{
if (myIsClosed && line.numpoints > 2)
feature = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_POLYGON);
else
feature = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_LINE);
}
if (line.numpoints >= 2)
feature.add(line);
}
}
}
return feature;
}
/// <summary>
/// Zooming the map by using the specified zoom factor
/// </summary>
/// <param name="zoomfactor">Zoom factor.</param>
public void ZoomOut(double zoomfactor)
{
if (map != null)
{
using (pointObj center = new pointObj(map.width / 2, map.height / 2, 0, 0))
{
map.zoomScale(zoomfactor * map.scaledenom, center, map.width, map.height, map.extent, null);
history.Add(new NavigationHistoryItem(map.extent));
RaiseZoomChanged();
}
this.RefreshView();
}
}
/// <summary>
/// Zooming the map to the specified extent.
/// </summary>
/// <param name="minx">The minx of the extent.</param>
/// <param name="miny">The miny of the extent.</param>
/// <param name="maxx">The maxx of the extent.</param>
/// <param name="maxy">The maxy of the extent.</param>
public void ZoomRectangle(double minx, double miny, double maxx, double maxy)
{
if (map != null && (maxx - minx) > 2 && (maxy-miny) > 2)
{
using (rectObj imgrect = new rectObj(minx, miny, maxx, maxy, 0))
{
// mapscript requires this hack
imgrect.miny = maxy;
imgrect.maxy = miny;
map.zoomRectangle(imgrect, map.width, map.height, map.extent, null);
history.Add(new NavigationHistoryItem(map.extent));
RaiseZoomChanged();
this.RefreshView();
return;
}
}
}
/// <summary>
/// Recenter the map to the specified pixel coordinate.
/// </summary>
/// <param name="imgX">X coordinate in the pixel space.</param>
/// <param name="imgY">Y coordinate in the pixel space.</param>
public void PanTo(int imgX, int imgY)
{
if (map != null)
{
using (pointObj imgpoint = new pointObj(imgX, imgY, 0, 0))
{
map.zoomPoint(1, imgpoint, map.width, map.height, map.extent, null);
history.Add(new NavigationHistoryItem(map.extent));
RaisePositionChanged();
}
this.RefreshView();
}
}
/// <summary>
/// Restore map the extent to the initial values.
/// </summary>
public void SetInitialExtent()
{
if (map != null)
{
history.First();
history.Current.Apply(map);
RaiseZoomChanged();
this.RefreshView();
}
}
public void SetNextExtent()
{
if (map != null && history.HasNext())
{
history.Next();
history.Current.Apply(map);
RaiseZoomChanged();
this.RefreshView();
}
}
public void SetPreviousExtent()
{
if (map != null && history.HasPrevious())
{
history.Previous();
history.Current.Apply(map);
RaiseZoomChanged();
this.RefreshView();
}
}
/// <summary>
/// Get inital extent values.
/// </summary>
public rectObj GetInitialExtent()
{
return history[0].GetExtent();
}
/// <summary>
/// Retrieve the actual zoom width in map coordinates.
/// </summary>
/// <returns>The actual zoom width in map coordinates</returns>
public double GetZoom()
{
if (map != null)
return map.extent.maxx - map.extent.minx;
return 0;
}
/// <summary>
/// Retrieve the current scale.
/// </summary>
/// <returns>The current scale.</returns>
public double GetScale()
{
if (map != null)
return map.scaledenom;
return 0;
}
/// <summary>
/// Get the current unit name.
/// </summary>
/// <returns>The current unit name.</returns>
public string GetUnitName()
{
return unitName;
}
/// <summary>
/// Get the current unit display unit.
/// </summary>
/// <returns>The current unit.</returns>
public MS_UNITS GetMapUnits()
{
return mapunits;
}
/// <summary>
/// Update the unit values according to the mapObj settings.
/// </summary>
public void UpdateUnitValues()
{
mapunits = map.units;
unitPrecision = MapUtils.GetUnitPrecision(map.units);
string newUnit = MapUtils.GetUnitName(mapunits);
if (unitName != newUnit)
{
unitName = newUnit;
RaiseZoomChanged();
}
}
/// <summary>
/// Fires a zoomchanged event
/// </summary>
private void RaiseZoomChanged()
{
double zoom = (map.extent.maxx - map.extent.minx);
if (mapunits != map.units)
zoom = zoom * MapUtils.InchesPerUnit(map.units) / MapUtils.InchesPerUnit(mapunits);
target.RaiseZoomChanged(this, Math.Round(zoom, MapUtils.GetUnitPrecision(mapunits)), map.scaledenom);
}
/// <summary>
/// Fires a positionchanged event
/// </summary>
private void RaisePositionChanged()
{
target.RaisePositionChanged(this, Math.Round((map.extent.maxx + map.extent.minx) / 2, MapUtils.GetUnitPrecision(mapunits)), Math.Round((map.extent.maxy + map.extent.miny) / 2, MapUtils.GetUnitPrecision(mapunits)));
}
/// <summary>
/// Setting up the selection the drawing modes
/// </summary>
/// <param name="bQuery">The desired mode</param>
private void SetSelectionMode(bool bQuery)
{
if (bQuery)
{
drawQuery = true;
queryMode = false;
for (int i = 0; i < map.numlayers; i++)
{
layerObj layer = map.getLayer(i);
if (layer.status != mapscript.MS_OFF)
{
using (resultCacheObj results = layer.getResults())
{
if (results != null && results.numresults > 0)
{
queryMode = true;
// suppress the query drawing for the unsupported types
if (layer.type != MS_LAYER_TYPE.MS_LAYER_POINT &&
layer.type != MS_LAYER_TYPE.MS_LAYER_POLYGON &&
layer.type != MS_LAYER_TYPE.MS_LAYER_LINE &&
layer.type != MS_LAYER_TYPE.MS_LAYER_ANNOTATION &&
layer.type != MS_LAYER_TYPE.MS_LAYER_RASTER)
drawQuery = false;
}
}
}
}
if (!queryMode)
drawQuery = false;
}
else
{
if (queryMode || drawQuery)
{
queryMode = false;
drawQuery = false;
ClearResults();
this.target.RaiseSelectionChanged(this);
this.RefreshView();
}
}
}
/// <summary>
/// Draw the map.
/// </summary>
/// <param name="pe">The argument containing the drawing context to use.</param>
protected override void OnPaint(PaintEventArgs pe)
{
if (mapImage != null)
{
if (dragging)
{
if (inputMode == InputModes.Pan || forcePan)
{
float offsetX = (magnify - 1) * mouseX - magnify * dragRect.Width;
float offsetY = (magnify - 1) * mouseY - magnify * dragRect.Height;
pe.Graphics.DrawImage(mapImage, -offsetX, -offsetY, magnify * mapImage.Width, magnify * mapImage.Height);
}
else if (inputMode == InputModes.ZoomIn || inputMode == InputModes.ZoomOut || inputMode == InputModes.TrackRectangle)
{
// drawing the map image
pe.Graphics.DrawImage(mapImage, 0, 0);
// drawing the rectangle
Rectangle normalizedRectangle =
new Rectangle(
Math.Min(dragRect.X, dragRect.X + dragRect.Width),
Math.Min(dragRect.Y, dragRect.Y + dragRect.Height),
Math.Abs(dragRect.Width),
Math.Abs(dragRect.Height));
pe.Graphics.FillRectangle(selectionBrush, normalizedRectangle);
pe.Graphics.DrawRectangle(selectionPen, normalizedRectangle);
}
}
else
{
float offsetX = (magnify - 1) * mouseX;
float offsetY = (magnify - 1) * mouseY;
// drawing the map image
pe.Graphics.DrawImage(mapImage, -offsetX, -offsetY, magnify * mapImage.Width, magnify * mapImage.Height);
if (inputMode == InputModes.TrackPolygon || inputMode == InputModes.TrackLine)
{
if (trackPoints != null && trackPoints.PointCount >= 2)
{
if (inputMode == InputModes.TrackPolygon)
pe.Graphics.FillPath(selectionBrush, trackPoints);
pe.Graphics.DrawPath(selectionPen, trackPoints);
}
}
}
}
if (drawMessage != null)
{
using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
RectangleF rectF1 = new RectangleF(0, 0, this.Width, this.Height);
pe.Graphics.DrawString(drawMessage, font1, Brushes.Blue, rectF1);
}
}
if (border)
{
// implement additional drawing if needed
pe.Graphics.DrawRectangle(borderPen, new Rectangle(0,0, this.Width-1, this.Height-1));
}
if (centerMarker)
{
float centerX = ((float)this.Width - 1) / 2;
float centerY = ((float)this.Height - 1) / 2;
pe.Graphics.DrawLine(centerMarkerPen, centerX - 10, centerY, centerX + 10, centerY);
pe.Graphics.DrawLine(centerMarkerPen, centerX, centerY - 10, centerX, centerY + 10);
}
}
/// <summary>
/// Adding a preview feature to the layer according to the layer type. This method is used to initialize the preview controls.
/// </summary>
/// <param name="srcLayer">The source layer the felature should be created in.</param>
/// <param name="numvalues">The number of the feature attributes to be created.</param>
private void AddSampleFeature(layerObj srcLayer, int numvalues)
{
if (srcLayer.type == MS_LAYER_TYPE.MS_LAYER_LINE)
{
using (shapeObj shape = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_LINE))
{
// adding a horizontal line sample
using (lineObj line = new lineObj())
{
using (pointObj point = new pointObj(this.gap, this.Height / 2, 0, 0))
{
line.add(point);
point.setXY(this.Width - this.gap, this.Height / 2, 0);
line.add(point);
}
shape.add(line);
}
if (numvalues > 0)
shape.initValues(numvalues);
srcLayer.addFeature(shape);
}
}
else if (srcLayer.type == MS_LAYER_TYPE.MS_LAYER_POLYGON)
{
using (shapeObj shape = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_POLYGON))
{
// adding a rectangle sample
using (lineObj line = new lineObj())
{
using (pointObj point = new pointObj(this.gap, this.gap, 0, 0))
{
line.add(point);
point.setXY(this.Width - this.gap, this.gap, 0);
line.add(point);
point.setXY(this.Width - this.gap, this.Height - this.gap, 0);
line.add(point);
point.setXY(this.gap, this.Height - this.gap, 0);
line.add(point);
point.setXY(this.gap, this.gap, 0);
line.add(point);
}
shape.add(line);
}
if (numvalues > 0)
shape.initValues(numvalues);
srcLayer.addFeature(shape);
}
}
else
{
using (shapeObj shape = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_POINT))
{
// adding a point sample
using (lineObj line = new lineObj())
{
using (pointObj point = new pointObj(this.Width / 2, this.Height / 2, 0, 0))
{
line.add(point);
}
shape.add(line);
}
if (numvalues > 0)
shape.initValues(numvalues);
srcLayer.addFeature(shape);
}
}
}
/// <summary>
/// Create a default layer for creating a preview to another layer
/// </summary>
/// <param name="originalMap">The original map.</param>
/// <param name="originalLayer">The original layer.</param>
/// <returns>The created layer object.</returns>
private layerObj InitializeDefaultLayer(mapObj originalMap, layerObj originalLayer)
{
// create a new map object
map = new mapObj(null);
map.units = MS_UNITS.MS_PIXELS;
map.setExtent(0, 0, this.Width, this.Height);
map.width = this.Width;
map.height = this.Height;
outputFormatObj format = originalMap.outputformat;
if (map.getOutputFormatByName(format.name) == null)
map.appendOutputFormat(format);
map.selectOutputFormat(originalMap.imagetype);
// copy symbolset
for (int i = 1; i < originalMap.symbolset.numsymbols; i++)
{
symbolObj origsym = originalMap.symbolset.getSymbol(i);
map.symbolset.appendSymbol(MapUtils.CloneSymbol(origsym));
}
// copy the fontset
string key = null;
while ((key = originalMap.fontset.fonts.nextKey(key)) != null)
map.fontset.fonts.set(key, originalMap.fontset.fonts.get(key, ""));
// setting a default font
//map.fontset.fonts.set("",
// originalMap.fontset.fonts.get(originalMap.fontset.fonts.nextKey(null),""));
// insert a new layer
layerObj layer = new layerObj(map);
if (originalLayer != null)
{
// the chart type doesn't support having as single class in it
if (originalLayer.type == MS_LAYER_TYPE.MS_LAYER_CHART)
layer.type = MS_LAYER_TYPE.MS_LAYER_POLYGON;
else
layer.type = originalLayer.type;
originalLayer.open();
// add the sample feature to the layer
AddSampleFeature(layer, originalLayer.numitems);
if (originalLayer.getResults() == null)
originalLayer.close(); // close only is no query results
}
else
{
layer.type = MS_LAYER_TYPE.MS_LAYER_ANNOTATION;
// add the sample feature to the layer
AddSampleFeature(layer, 0);
}
layer.status = mapscript.MS_ON;
return layer;
}
/// <summary>
/// Creating a sample (preview) based on a classObj, styleObj or labelObj.
/// </summary>
/// <param name="original">The wrapper holding the original object.</param>
private void CreateSampleMap(MapObjectHolder original)
{
MapObjectHolder originalMap = null;
MapObjectHolder originalLayer = null;
MapObjectHolder originalClass = null;
// create a sample map to render a preview of the given object
if (original.GetType() == typeof(classObj))
{
// tracking down the whole object tree
originalLayer = original.GetParent();
if (originalLayer != null)
originalMap = originalLayer.GetParent();
// creating a new compatible map object
if (originalMap != null)
{
layerObj layer = InitializeDefaultLayer(originalMap, originalLayer);
layer.insertClass(((classObj)original).clone(), -1);
// bindings are not supported with sample maps
classObj classobj = layer.getClass(0);
for (int i = 0; i < classobj.numstyles; i++)
StyleBindingController.RemoveAllBindings(classobj.getStyle(i));
for (int i = 0; i < classobj.numlabels; i++)
LabelBindingController.RemoveAllBindings(classobj.getLabel(i));
classobj.setText("Sample text");
classobj.setExpression(""); // remove expression to have the class shown
this.target = new MapObjectHolder(classobj, original.GetParent());
}
}
else if (original.GetType() == typeof(styleObj))
{
// tracking down the whole object tree
if (original.GetParent().GetType() == typeof(labelObj))
originalClass = original.GetParent().GetParent();
else
originalClass = original.GetParent();
if (originalClass != null)
originalLayer = originalClass.GetParent();
if (originalLayer != null)
originalMap = originalLayer.GetParent();
// creating a new compatible map object
if (originalMap != null)
{
layerObj layer = InitializeDefaultLayer(originalMap, originalLayer);
classObj classobj = new classObj(layer);
classobj.name = MapUtils.GetClassName(layer);
styleObj style;
if (original.GetParent().GetType() == typeof(labelObj))
{
classobj.addLabel(new labelObj());
labelObj label = classobj.getLabel(classobj.numlabels - 1);
MapUtils.SetDefaultLabel(label, layer.map);
label.insertStyle(((styleObj)original).clone(), -1);
style = label.getStyle(0);
}
else
{
classobj.insertStyle(((styleObj)original).clone(), -1);
style = classobj.getStyle(0);
}
// bindings are not supported with sample maps
StyleBindingController.RemoveAllBindings(style);
classobj.setText("Sample text");
this.target = new MapObjectHolder(style, original.GetParent());
}
}
else if (original.GetType() == typeof(labelObj))
{
// tracking down the whole object tree
originalClass = original.GetParent();
if (originalClass != null)
{
if (originalClass.GetType() == typeof(classObj))
{
originalLayer = originalClass.GetParent();
if (originalLayer != null)
originalMap = originalLayer.GetParent();
}
else if (originalClass.GetType() == typeof(scalebarObj))
{
originalMap = originalClass.GetParent();
}
}
// creating a new compatible map object
if (originalMap != null)
{
layerObj layer = InitializeDefaultLayer(originalMap, originalLayer);
classObj classobj = new classObj(layer);
classobj.name = MapUtils.GetClassName(layer);
labelObj label = new labelObj();
if (originalClass.GetType() == typeof(classObj))
{
// copy settings
label.updateFromString(((labelObj)original).convertToString());
}
classobj.addLabel(label);
this.target = new MapObjectHolder(layer.getClass(0).getLabel(0), original.GetParent());
}
}
else
throw new Exception("Invalid target type: " + original.GetType());
}
#region IMapControl Members
/// <summary>
/// Refresh the controls according to the underlying object.
/// </summary>
public void RefreshView()
{
timerRefresh.Enabled = false;
if (!enableRendering)
{
drawMessage = "Rendering is disabled.";
this.Refresh();
return;
}
if (BeforeRefresh != null)
BeforeRefresh(this, null);
// setting up the size of the map image
mapImage = null;
if (map != null)
{
if (this.Height > 2 && this.Width > 2 &&
map.extent.maxx > map.extent.minx && map.extent.maxy > map.extent.miny)
{
if (map.width != this.Width || map.height != this.Height)
{
map.height = this.Height;
map.width = this.Width;
map.setExtent(map.extent.minx, map.extent.miny, map.extent.maxx, map.extent.maxy);
RaiseZoomChanged();
}
UpdateTansformations();
using (outputFormatObj format = map.outputformat)
{
string imageType = null;
if ((format.renderer != mapscript.MS_RENDER_WITH_AGG &&
format.renderer != mapscript.MS_RENDER_WITH_CAIRO_RASTER)
|| string.Compare(format.mimetype.Trim(), "image/vnd.wap.wbmp", true) == 0
|| string.Compare(format.mimetype.Trim(), "image/tiff", true) == 0)
{
// falling back to the png type in case of the esoteric types
imageType = map.imagetype;
map.selectOutputFormat("png");
drawMessage = "MapManager cannot display maps in the output format " + imageType + ", rendering with png instead.";
}
else
{
drawMessage = null;
}
Cursor cur = this.Cursor;
try
{
this.Cursor = Cursors.WaitCursor;
if (drawQuery)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (imageObj image = map.drawQuery())
{
if (mapImage == null || mapImage.Width != image.width || mapImage.Height != image.height)
{
mapImage = new Bitmap(image.width, image.height, PixelFormat.Format32bppRgb);
}
Bitmap bitmap = (Bitmap)mapImage;
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, image.width, image.height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
try
{
image.getRawPixels(bitmapData.Scan0);
}
finally
{
bitmap.UnlockBits(bitmapData);
}
//byte[] img = image.getBytes();
//using (MemoryStream ms = new MemoryStream(img))
//{
// mapImage = Image.FromStream(ms);
You can’t perform that action at this time.
