-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShapeDisplay.cs
1086 lines (944 loc) · 41.4 KB
/
ShapeDisplay.cs
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
// Filename: ShapeDisplay.cs
// Description: A helper class for importing ESRI shapefiles and
// creating/displaying shapes on a WPF canvas.
// Comments: Uses the classes from ShapeFile.cs.
// 2007-01-29 nschan Initial revision.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace kazOilMap
{
/// <summary>
/// The GeometryType enumeration defines geometry types that
/// can be used when creating WPF shapes. Choosing a stream
/// geometry type can improve rendering performance.
/// </summary>
public enum GeometryType
{
/// <summary>
/// Use path geometry containing path figures.
/// </summary>
UsePathGeometry,
/// <summary>
/// Use StreamGeometry with StreamGeometryContext class
/// to specify drawing instructions.
/// </summary>
UseStreamGeometry,
/// <summary>
/// Same as UseStreamGeometry except that the figures
/// will be unstroked for greater performance (borders
/// won't be displayed for the shapes).
/// </summary>
UseStreamGeometryNotStroked
}
/// <summary>
/// ShapeDisplay is a helper class for importing ESRI shapefiles
/// and creating/displaying shapes on a WPF canvas. During the
/// import step, a progress window is displayed. This is implemented
/// using the WPF single-threaded programming model, which allows
/// tasks to be executed by a Dispatcher instance while keeping the
/// UI responsive.
/// </summary>
public class ShapeDisplay
{
#region Delegates
/// <summary>
/// Defines the prototype for a method that reads a block
/// of shapefile records. Such a method is intended to be
/// executed by the Dispatcher.
/// </summary>
/// <param name="info">Shapefile read information.</param>
public delegate void ReadNextPrototype(ShapeFileReadInfo info);
/// <summary>
/// Defines the prototype for a method that creates and displays
/// a set of WPF shapes. Such a method is intended to be
/// executed by the Dispatcher.
/// </summary>
/// <param name="info">Shapefile read information.</param>
public delegate void DisplayNextPrototype(ShapeFileReadInfo info);
#endregion Delegates
#region Constants
private const int readShapesBlockingFactor = 50;
private const int displayShapesBlockingFactor = 10;
private const string baseLonLatText = "Lon/Lat: ";
#endregion Constants
#region Private fields
// UI components.
private Window owner;
private Canvas canvas;
private Dispatcher dispatcher;
private ProgressWindow progressWindow;
// Used during reading of a shapefile.
private bool isReadingShapeFile;
private bool cancelReadShapeFile;
private int wpfShapeCount;
// Used during creation of WPF shapes.
private List<Shape> shapeList = new List<Shape>();
private GeometryType geometryType = GeometryType.UseStreamGeometryNotStroked;
// Transformation from lon/lat to canvas coordinates.
private TransformGroup shapeTransform;
// Combined view transformation (zoom and pan).
private TransformGroup viewTransform = new TransformGroup();
private ScaleTransform zoomTransform = new ScaleTransform();
private TranslateTransform panTransform = new TranslateTransform();
// For coloring of WPF shapes.
private Brush[] shapeBrushes;
private Random rand = new Random(379013);
private Brush strokeBrush = new SolidColorBrush(Color.FromArgb(150, 150, 150, 150));
// For panning operations.
private bool isPanningEnabled = true;
Point prevMouseLocation;
bool isMouseDragging;
private double panTolerance = 1;
// Displaying lon/lat coordinates on the canvas.
bool isDisplayLonLatEnabled = true;
Label lonLatLabel = new Label();
#endregion Private fields
#region Constructor
/// <summary>
/// Constructor for the ShapeDisplay class.
/// </summary>
/// <param name="owner">Window that acts as an owner to child windows.</param>
/// <param name="canvas">The canvas on which to create WPF shapes.</param>
public ShapeDisplay(Window owner, Canvas canvas)
{
// Keep reference to a Window to act as the owner for a progress window.
if ( owner == null )
throw new ArgumentNullException("owner");
this.owner = owner;
// Keep reference to the canvas and add mouse event handlers
// for implementing panning.
if ( canvas == null )
throw new ArgumentNullException("canvas");
this.canvas = canvas;
this.canvas.MouseEnter += new MouseEventHandler(canvas_MouseEnter);
this.canvas.MouseDown += new System.Windows.Input.MouseButtonEventHandler(canvas_MouseDown);
this.canvas.MouseMove += new System.Windows.Input.MouseEventHandler(canvas_MouseMove);
this.canvas.MouseUp += new System.Windows.Input.MouseButtonEventHandler(canvas_MouseUp);
this.canvas.MouseLeave += new MouseEventHandler(canvas_MouseLeave);
// Keep reference to the dispatcher for task execution.
this.dispatcher = this.canvas.Dispatcher;
// Add the zoom and pan transforms to the view transform.
this.viewTransform.Children.Add(this.zoomTransform);
this.viewTransform.Children.Add(this.panTransform);
// Configure the lon/lat label.
this.lonLatLabel.Opacity = 0.70;
}
#endregion Constructor
#region Properties
/// <summary>
/// Indicates if a shapefile read operation is in progress.
/// </summary>
public bool IsReadingShapeFile
{
get { return this.isReadingShapeFile; }
}
/// <summary>
/// Indicates if we can perform a zoom operation. This is true
/// the shape transform has been set (meaning at least one shapefile
/// has been loaded).
/// </summary>
public bool CanZoom
{
get
{
return (this.shapeTransform != null);
}
}
/// <summary>
/// Indicates if panning is enabled or not. This
/// applies to both mouse and keyboard panning.
/// </summary>
public bool IsPanningEnabled
{
get { return this.isPanningEnabled; }
set { this.isPanningEnabled = value; }
}
/// <summary>
/// Indicates if display of lon/lat coordinates
/// is enabled or not.
/// </summary>
public bool IsDisplayLonLatEnabled
{
get { return this.isDisplayLonLatEnabled; }
set
{
if ( this.isDisplayLonLatEnabled != value )
{
this.isDisplayLonLatEnabled = value;
if ( this.isDisplayLonLatEnabled )
this.DisplayLonLatDefault();
else
this.canvas.Children.Remove(this.lonLatLabel);
}
}
}
/// <summary>
/// Specifies the geometry type to use when creating WPF shapes.
/// </summary>
public GeometryType GeometryType
{
get { return this.geometryType; }
set { this.geometryType = value; }
}
#endregion Properties
#region Public methods
/// <summary>
/// Read shapes and attributes from the given shapefile.
/// </summary>
/// <param name="fileName">Full pathname of a shapefile.</param>
public void ReadShapeFile(string fileName)
{
this.isReadingShapeFile = true;
this.cancelReadShapeFile = false;
// Create an object to store shapefile info during the read.
ShapeFileReadInfo info = new ShapeFileReadInfo();
info.FileName = fileName;
info.ShapeFile = new ShapeFile();
info.Stream = null;
info.NumberOfBytesRead = 0;
info.RecordIndex = 0;
try
{
// Read the File Header first.
info.Stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
info.ShapeFile.ReadShapeFileHeader(info.Stream);
info.NumberOfBytesRead = ShapeFileHeader.Length;
// Schedule the first read of shape file records using the dispatcher.
this.dispatcher.BeginInvoke(DispatcherPriority.Normal, new ReadNextPrototype(this.ReadNextShapeRecord), info);
}
catch (IOException ex)
{
this.EndReadShapeFile(info);
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// Request the current shapefile read operation to be cancelled.
/// </summary>
public void CancelReadShapeFile()
{
this.cancelReadShapeFile = true;
this.HideProgress();
}
/// <summary>
/// Reset the canvas.
/// </summary>
public void ResetCanvas()
{
// End reading of the shapefile.
this.EndReadShapeFile();
// Clear the canvas.
this.canvas.Children.Clear();
this.wpfShapeCount = 0;
// Reset transformations.
this.panTransform.X = 0;
this.panTransform.Y = 0;
this.zoomTransform.ScaleX = 1;
this.zoomTransform.ScaleY = 1;
this.shapeTransform = null;
}
/// <summary>
/// Perform a zoom operation about the current center
/// of the canvas.
/// </summary>
/// <param name="zoomFactor">Zoom multiplication factor (1, 2, 4, etc).</param>
public void Zoom(double zoomFactor)
{
// Compute the coordinates of the center of the canvas
// in terms of pre-view transformation values. We do this
// by applying the inverse of the view transform.
Point canvasCenter = new Point(this.canvas.ActualWidth / 2, this.canvas.ActualHeight / 2);
canvasCenter = this.viewTransform.Inverse.Transform(canvasCenter);
// Temporarily reset the panning transformation.
this.panTransform.X = 0;
this.panTransform.Y = 0;
// Set the new zoom transformation scale factors.
this.zoomTransform.ScaleX = zoomFactor;
this.zoomTransform.ScaleY = zoomFactor;
// Apply the updated view transform to the canvas center.
// This gives us the updated location of the center point
// on the canvas. By differencing this with the desired
// center of the canvas, we can determine the ideal panning
// transformation parameters.
Point canvasLocation = this.viewTransform.Transform(canvasCenter);
this.panTransform.X = this.canvas.ActualWidth / 2 - canvasLocation.X;
this.panTransform.Y = this.canvas.ActualHeight / 2 - canvasLocation.Y;
}
/// <summary>
/// Perform a panning operation given X and Y factor values
/// which can be thought of as a fraction of the canvas actual
/// width or height.
/// </summary>
/// <param name="factorX">Fraction of canvas actual width to pan horizontally.</param>
/// <param name="factorY">Fraction of canvas actual height to pan vertically.</param>
public void Pan(double factorX, double factorY)
{
if ( !this.isPanningEnabled )
return;
this.panTransform.X += (factorX * this.canvas.ActualWidth);
this.panTransform.Y += (factorY * this.canvas.ActualHeight);
}
/// <summary>
/// Save the owner object (the main window) to XAML.
/// This method may take a long time to run if there
/// are many objects on the canvas (shapefile > 1 Mb).
/// </summary>
/// <param name="stream">Output stream for writing the XAML.</param>
public void SaveToXaml(Stream stream)
{
System.Windows.Markup.XamlWriter.Save(this.owner, stream);
}
#endregion Public methods
#region Transformations
/// <summary>
/// Computes a transformation so that the shapefile geometry
/// will maximize the available space on the canvas and be
/// perfectly centered as well.
/// </summary>
/// <param name="info">Shapefile information.</param>
/// <returns>A transformation object.</returns>
private TransformGroup CreateShapeTransform(ShapeFileReadInfo info)
{
// Bounding box for the shapefile.
double xmin = info.ShapeFile.FileHeader.XMin;
double xmax = info.ShapeFile.FileHeader.XMax;
double ymin = info.ShapeFile.FileHeader.YMin;
double ymax = info.ShapeFile.FileHeader.YMax;
// Width and height of the bounding box.
double width = Math.Abs(xmax - xmin);
double height = Math.Abs(ymax - ymin);
// Aspect ratio of the bounding box.
double aspectRatio = width / height;
// Aspect ratio of the canvas.
double canvasRatio = this.canvas.ActualWidth / this.canvas.ActualHeight;
// Compute a scale factor so that the shapefile geometry
// will maximize the space used on the canvas while still
// maintaining its aspect ratio.
double scaleFactor = 1.0;
if ( aspectRatio < canvasRatio )
scaleFactor = this.canvas.ActualHeight / height;
else
scaleFactor = this.canvas.ActualWidth / width;
// Compute the scale transformation. Note that we flip
// the Y-values because the lon/lat grid is like a cartesian
// coordinate system where Y-values increase upwards.
ScaleTransform xformScale = new ScaleTransform(scaleFactor, -scaleFactor);
// Compute the translate transformation so that the shapefile
// geometry will be centered on the canvas.
TranslateTransform xformTrans = new TranslateTransform();
xformTrans.X = (this.canvas.ActualWidth - (xmin + xmax) * scaleFactor) / 2;
xformTrans.Y = (this.canvas.ActualHeight + (ymin + ymax) * scaleFactor) / 2;
// Add the two transforms to a transform group.
TransformGroup xformGroup = new TransformGroup();
xformGroup.Children.Add(xformScale);
xformGroup.Children.Add(xformTrans);
return xformGroup;
}
#endregion Transformations
#region Brushes for gradient coloring
/// <summary>
/// Create a set of linear gradient brushes which we can use
/// as a random pool for assignment to WPF shapes. A higher
/// gradient factor results in a stronger gradient effect.
/// </summary>
/// <param name="gradientFactor">Gradient factor from 0 to 1.</param>
/// <param name="gradientAngle">Direction of gradient in degrees.</param>
private void CreateShapeBrushes(double gradientFactor, double gradientAngle)
{
// Pick a set of base colors for the brushes.
Color[] colors = new Color[] {
Colors.Crimson, Colors.ForestGreen, Colors.RoyalBlue,
Colors.Navy, Colors.DarkSeaGreen, Colors.LightSlateGray,
Colors.DarkKhaki, Colors.Olive, Colors.Indigo, Colors.Violet };
// Create one brush per color.
this.shapeBrushes = new Brush[colors.Length];
for (int i = 0; i < this.shapeBrushes.Length; i++)
{
this.shapeBrushes[i] = new LinearGradientBrush(ShapeDisplay.GetAdjustedColor(colors[i], gradientFactor), colors[i], gradientAngle);
}
}
/// <summary>
/// Given an input color, return an adjusted color using a
/// factor value which ranges from 0 to 1. The larger the factor,
/// the lighter the adjusted color. A factor of 0 means no adjustment
/// to the input color.
/// </summary>
/// <remarks>
/// Note that the alpha component of the input color is not adjusted.
/// </remarks>
/// <param name="inColor">Input color.</param>
/// <param name="factor">Color adjustment factor, from 0 to 1.</param>
/// <returns>An adjusted color value.</returns>
private static Color GetAdjustedColor(Color inColor, double factor)
{
int red = inColor.R + (int)((255-inColor.R) * factor);
red = Math.Max(0, red);
red = Math.Min(255, red);
int green = inColor.G + (int)((255-inColor.G) * factor);
green = Math.Max(0, green);
green = Math.Min(255, green);
int blue = inColor.B + (int)((255-inColor.B) * factor);
blue = Math.Max(0, blue);
blue = Math.Min(255, blue);
return Color.FromArgb(inColor.A, (byte)red, (byte)green, (byte)blue);
}
/// <summary>
/// Get the next brush that can be used to fill a WPF shape.
/// </summary>
/// <returns>A randomly selected brush.</returns>
private Brush GetRandomShapeBrush()
{
int index = this.rand.Next() % this.shapeBrushes.Length;
return this.shapeBrushes[index];
}
#endregion Brushes for gradient coloring
#region Reading ESRI shapes
/// <summary>
/// Read a block of shape file records and possibly schedule
/// the next read with the dispatcher.
/// </summary>
/// <param name="info">Shapefile read information.</param>
private void ReadNextShapeRecord(ShapeFileReadInfo info)
{
if ( this.cancelReadShapeFile )
return;
try
{
// Read a block of shape records.
for (int i = 0; i < ShapeDisplay.readShapesBlockingFactor; i++)
{
ShapeFileRecord record = info.ShapeFile.ReadShapeFileRecord(info.Stream);
info.NumberOfBytesRead += (4 + record.ContentLength) * 2;
}
}
catch (FileFormatException ex)
{
this.EndReadShapeFile(info);
MessageBox.Show(ex.Message);
return;
}
catch (IOException)
{
// Display the end progress (100 percent).
this.ShowProgress("Reading shapefile...", 100);
// Read attributes from the associated dBASE file.
this.ReadDbaseAttributes(info);
// Display shapes on the canvas.
if ( info.ShapeFile.Records.Count > 0 )
this.DisplayShapes(info);
else
this.EndReadShapeFile(info);
return;
}
// Display the current progress.
double progressValue = info.NumberOfBytesRead * 100.0 / (info.ShapeFile.FileHeader.FileLength * 2);
progressValue = Math.Min(100, progressValue);
this.ShowProgress("Reading shapefile...", progressValue);
// Schedule the next read at Background priority.
this.dispatcher.BeginInvoke(DispatcherPriority.Background, new ReadNextPrototype(this.ReadNextShapeRecord), info);
}
/// <summary>
/// Perform some cleanup at the end of reading a shapefile.
/// </summary>
private void EndReadShapeFile()
{
this.HideProgress();
this.isReadingShapeFile = false;
this.cancelReadShapeFile = true;
}
/// <summary>
/// Perform some cleanup at the end of reading a shapefile.
/// </summary>
/// <param name="info">Shapefile read information.</param>
private void EndReadShapeFile(ShapeFileReadInfo info)
{
if ( info != null && info.Stream != null )
{
info.Stream.Close();
info.Stream.Dispose();
info.Stream = null;
}
this.EndReadShapeFile();
}
/// <summary>
/// Read dBASE file attributes.
/// </summary>
/// <param name="info">Shapefile read information.</param>
private void ReadDbaseAttributes(ShapeFileReadInfo info)
{
// Read attributes from the associated dBASE file.
try
{
string dbaseFile = info.FileName.Replace(".shp", ".dbf");
dbaseFile = dbaseFile.Replace(".SHP", ".DBF");
info.ShapeFile.ReadAttributes(dbaseFile);
}
catch (OleDbException ex)
{
// Note: An exception will occur if the filename of the dBASE
// file does not follow 8.3 naming conventions. In this case,
// you must use its short (MS-DOS) filename.
MessageBox.Show(ex.Message);
// Activate the window.
this.owner.Activate();
}
}
#endregion Reading ESRI shapes
#region Creating / displaying WPF shapes
/// <summary>
/// Create a WPF shape given a shapefile record.
/// </summary>
/// <param name="shapeName">The name of the WPF shape.</param>
/// <param name="record">Shapefile record.</param>
/// <returns>The created WPF shape.</returns>
private Shape CreateWPFShape(string shapeName, ShapeFileRecord record)
{
// Create a new geometry.
Geometry geometry;
if ( this.geometryType == GeometryType.UsePathGeometry )
geometry = this.CreatePathGeometry(record);
else
geometry = this.CreateStreamGeometry(record);
// Transform the geometry based on current zoom and pan settings.
geometry.Transform = this.viewTransform;
// Create a new WPF Path.
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
// Assign the geometry to the path and set its name.
path.Data = geometry;
path.Name = shapeName;
// Set path properties.
path.StrokeThickness = 0.5;
if ( record.ShapeType == (int)ShapeType.Polygon )
{
path.Stroke = this.strokeBrush;
path.Fill = this.GetRandomShapeBrush();
}
else
{
path.Stroke = Brushes.DimGray;
}
// Return the created WPF shape.
return path;
}
/// <summary>
/// Create a PathGeometry given a shapefile record.
/// </summary>
/// <param name="record">Shapefile record.</param>
/// <returns>A PathGeometry instance.</returns>
private Geometry CreatePathGeometry(ShapeFileRecord record)
{
// Create a new geometry.
PathGeometry geometry = new PathGeometry();
// Add figures to the geometry.
for (int i = 0; i < record.NumberOfParts; i++)
{
// Create a new path figure.
PathFigure figure = new PathFigure();
// Determine the starting index and the end index
// into the points array that defines the figure.
int start = record.Parts[i];
int end;
if (record.NumberOfParts > 1 && i != (record.NumberOfParts - 1))
end = record.Parts[i + 1];
else
end = record.NumberOfPoints;
// Add line segments to the figure.
for (int j = start; j < end; j++)
{
System.Windows.Point pt = record.Points[j];
// Transform from lon/lat to canvas coordinates.
pt = this.shapeTransform.Transform(pt);
if (j == start)
figure.StartPoint = pt;
else
figure.Segments.Add(new LineSegment(pt, true));
}
// Add the new figure to the geometry.
geometry.Figures.Add(figure);
}
// Return the created path geometry.
return geometry;
}
/// <summary>
/// Create a StreamGeometry given a shapefile record.
/// </summary>
/// <param name="record">Shapefile record.</param>
/// <returns>A StreamGeometry instance.</returns>
private Geometry CreateStreamGeometry(ShapeFileRecord record)
{
// Create a new stream geometry.
StreamGeometry geometry = new StreamGeometry();
// Obtain the stream geometry context for drawing each part.
using (StreamGeometryContext ctx = geometry.Open())
{
// Draw figures.
for (int i = 0; i < record.NumberOfParts; i++)
{
// Determine the starting index and the end index
// into the points array that defines the figure.
int start = record.Parts[i];
int end;
if (record.NumberOfParts > 1 && i != (record.NumberOfParts - 1))
end = record.Parts[i + 1];
else
end = record.NumberOfPoints;
// Draw the figure.
for (int j = start; j < end; j++)
{
System.Windows.Point pt = record.Points[j];
// Transform from lon/lat to canvas coordinates.
pt = this.shapeTransform.Transform(pt);
// Decide if the line segments are stroked or not. For the
// PolyLine type it must be stroked.
bool isStroked = (record.ShapeType == (int)ShapeType.PolyLine) || !(this.geometryType == GeometryType.UseStreamGeometryNotStroked);
// Register the drawing instruction.
if (j == start)
ctx.BeginFigure(pt, true, false);
else
ctx.LineTo(pt, isStroked, true);
}
}
}
// Return the created stream geometry.
return geometry;
}
/// <summary>
/// Create a WPF shape to represent a shapefile point or
/// multipoint record.
/// </summary>
/// <param name="shapeName">The name of the WPF shape.</param>
/// <param name="record">Shapefile record.</param>
/// <returns>The created WPF shape.</returns>
private Shape CreateWPFPoint(string shapeName, ShapeFileRecord record)
{
// Create a new geometry.
GeometryGroup geometry = new GeometryGroup();
// Add ellipse geometries to the group.
foreach (Point pt in record.Points)
{
// Create a new ellipse geometry.
EllipseGeometry ellipseGeo = new EllipseGeometry();
// Transform center point of the ellipse from lon/lat to
// canvas coordinates.
ellipseGeo.Center = this.shapeTransform.Transform(pt);
// Set the size of the ellipse.
ellipseGeo.RadiusX = 0.1;
ellipseGeo.RadiusY = 0.1;
// Add the ellipse to the geometry group.
geometry.Children.Add(ellipseGeo);
}
// Transform the geometry based on current zoom and pan settings.
geometry.Transform = this.viewTransform;
// Add the geometry to a new Path and set path properties.
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
path.Data = geometry;
path.Name = shapeName;
path.Fill = Brushes.Crimson;
path.StrokeThickness = 1;
path.Stroke = Brushes.DimGray;
// Return the created WPF shape.
return path;
}
/// <summary>
/// Begin creating and displaying WPF shapes on the canvas.
/// </summary>
private void DisplayShapes(ShapeFileReadInfo info)
{
// Create shape brushes.
if ( this.shapeBrushes == null )
this.CreateShapeBrushes(0.40, 45);
// Set up the transformation for WPF shapes.
if ( this.shapeTransform == null )
this.shapeTransform = this.CreateShapeTransform(info);
// Schedule display of the first block of shapefile records
// using the dispatcher.
this.dispatcher.BeginInvoke(DispatcherPriority.Normal, new DisplayNextPrototype(this.DisplayNextShapeRecord), info);
}
/// <summary>
/// Display a block of shape records as WPF shapes, and schedule
/// the next display with the dispatcher if needed.
/// </summary>
/// <param name="info">Shapefile read information.</param>
private void DisplayNextShapeRecord(ShapeFileReadInfo info)
{
if ( this.cancelReadShapeFile )
return;
// Create a block of WPF shapes and add them to the shape list.
this.shapeList.Clear();
int index = info.RecordIndex;
for( ; index < (info.RecordIndex + ShapeDisplay.displayShapesBlockingFactor); index++)
{
if ( index >= info.ShapeFile.Records.Count )
break;
ShapeFileRecord record = info.ShapeFile.Records[index];
// Set the name of the WPF shape.
++this.wpfShapeCount;
string shapeName = String.Format(System.Globalization.CultureInfo.InvariantCulture, "Shape{0}", this.wpfShapeCount);
// Create the WPF shape.
Shape shape;
if ( record.NumberOfParts == 0 )
shape = this.CreateWPFPoint(shapeName, record);
else
shape = this.CreateWPFShape(shapeName, record);
// Set a tooltip for the shape that displays up to 5 attribute values.
shape.ToolTip = shape.Name;
if ( record.Attributes != null )
{
string attr = String.Empty;
for (int i = 0; i < Math.Min(5, record.Attributes.ItemArray.GetLength(0)); i++)
{
attr += (", " + record.Attributes[i].ToString());
}
shape.ToolTip += attr;
}
// Add the shape to the shape list.
this.shapeList.Add(shape);
// If the record just processed is very large, then don't process
// any further records.
if ( record.Points.Count > 5000 )
{
++index;
break;
}
}
// Set the record index to read next (as part of the
// next dispatched task).
info.RecordIndex = index;
// Add the newly created WPF shapes to the canvas.
foreach (Shape shape in this.shapeList)
{
this.canvas.Children.Add(shape);
}
this.shapeList.Clear();
// Display the current progress.
double progressValue = (index * 100.0) / info.ShapeFile.Records.Count;
progressValue = Math.Min(100, progressValue);
this.ShowProgress("Creating WPF shapes...", progressValue);
// See if we need to dispatch another display operation.
if ( index < info.ShapeFile.Records.Count )
{
// Schedule the next display at Background priority.
this.dispatcher.BeginInvoke(DispatcherPriority.Background, new DisplayNextPrototype(this.DisplayNextShapeRecord), info);
}
else
{
// End the progress.
this.ShowProgress("Creating WPF shapes...", 100);
this.EndReadShapeFile(info);
}
}
#endregion Creating / displaying WPF shapes
#region Displaying progress
/// <summary>
/// Show the progress window with the given progress value.
/// </summary>
/// <param name="progressText">Progress text to display.</param>
/// <param name="progressValue">Progress value from 0 to 100 percent.</param>
private void ShowProgress(string progressText, double progressValue)
{
if ( this.progressWindow == null )
{
// Create a new progress window.
this.progressWindow = new ProgressWindow();
this.progressWindow.Owner = this.owner;
this.progressWindow.Cancel += new CancelEventHandler(this.progressWindow_Cancel);
this.progressWindow.Closed += new System.EventHandler(this.progressWindow_Closed);
this.progressWindow.Title = "Import Shapefile";
}
// Show the progress window with new progress text and value.
this.progressWindow.ProgressText = progressText;
this.progressWindow.ProgressValue = progressValue;
this.progressWindow.Show();
}
/// <summary>
/// Hide the progress window.
/// </summary>
private void HideProgress()
{
if ( this.progressWindow != null )
{
this.progressWindow.Close();
}
}
/// <summary>
/// Handle the Cancel button being pressed in the progress window.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void progressWindow_Cancel(object sender, CancelEventArgs e)
{
this.EndReadShapeFile();
}
/// <summary>
/// Handle closing of the progress window.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void progressWindow_Closed(object sender, EventArgs e)
{
this.progressWindow = null;
}
#endregion Displaying progress
#region Lon/lat coordinates
/// <summary>
/// Given a canvas position, convert it to a longitude
/// and latitude coordinate.
/// </summary>
/// <param name="canvasPosition">A canvas position or location.</param>
/// <returns>The corresponding point in lon/lat coordinates.</returns>
private Point GetLonLatCoordinates(Point canvasPosition)
{
// Apply the inverse of the view transformation.
Point p1 = this.viewTransform.Inverse.Transform(canvasPosition);
// Apply the inverse of the shape transformation.
if ( this.shapeTransform != null )
{
Point p2 = this.shapeTransform.Inverse.Transform(p1);
return p2;
}
return p1;
}
/// <summary>
/// Given a lon/lat coordinate, determine the corresponding
/// display text.
/// </summary>
/// <param name="lonLat">A lon/lat coordinate.</param>
/// <returns>Formatted lon/lat display text.</returns>
private static string GetLonLatDisplayText(Point lonLat)
{
string text = ShapeDisplay.baseLonLatText;
if ( lonLat.X < -180 || lonLat.X > 180 || lonLat.Y < -90 || lonLat.Y > 90 )
text += "n/a";
else
text += String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0.00}, {1:0.00}", lonLat.X, lonLat.Y);
return text;
}
/// <summary>
/// Given a canvas position, display the corresponding
/// lon/lat coordinates on the canvas.
/// </summary>
/// <param name="canvasPosition">A canvas position or location.</param>
private void DisplayLonLatCoord(Point canvasPosition)
{
// Remove the lon/lat label from the canvas first.
this.canvas.Children.Remove(this.lonLatLabel);
if ( this.shapeTransform != null )
{
// Convert from canvas position to lon/lat coordinates.
Point lonLat = this.GetLonLatCoordinates(canvasPosition);
// Convert lon/lat value to a display string.
this.lonLatLabel.Content = ShapeDisplay.GetLonLatDisplayText(lonLat);
// Add the label back to the canvas. This ensures that
// it will appear on top of all other canvas elements.
this.canvas.Children.Add(this.lonLatLabel);
}
}
/// <summary>
/// Display some default text in the lon/lat label.
/// </summary>
private void DisplayLonLatDefault()
{
// Remove the lon/lat label from the canvas first.
this.canvas.Children.Remove(this.lonLatLabel);
// Set the default text to display.
this.lonLatLabel.Content = ShapeDisplay.baseLonLatText + "n/a";
// Add the label back to the canvas. This ensures that
// it will appear on top of all other canvas elements.
this.canvas.Children.Add(this.lonLatLabel);
}
#endregion Lon/lat coordinates
#region Mouse handlers
/// <summary>
/// Handle the MouseEnter event for the canvas.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event arguments.</param>
private void canvas_MouseEnter(object sender, MouseEventArgs e)