forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineSymbolizerTest.cs
More file actions
413 lines (347 loc) · 17.9 KB
/
LineSymbolizerTest.cs
File metadata and controls
413 lines (347 loc) · 17.9 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
namespace ExampleCodeSnippets
{
/// <summary>
/// A (simple) street direction symbolizer
/// </summary>
public class StreetDirectionSymbolizer : SharpMap.Rendering.Symbolizer.BaseSymbolizer,
SharpMap.Rendering.Symbolizer.ILineSymbolizer
{
/// <summary>
/// Creates an instance of this class
/// </summary>
public StreetDirectionSymbolizer()
{
RepeatInterval = 500;
ArrowLength = 100;
ArrowPen = new System.Drawing.Pen(System.Drawing.Color.Black, 2)
{
EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor
};
}
/// <summary>
/// Method to place the street direction symbols
/// </summary>
/// <param name="map"></param>
/// <param name="lineString"></param>
/// <param name="graphics"></param>
private void OnRenderInternal(SharpMap.MapViewport map, GeoAPI.Geometries.ILineString lineString,
System.Drawing.Graphics graphics)
{
var length = lineString.Length;
var lil = new NetTopologySuite.LinearReferencing.LengthIndexedLine(lineString);
if (length < RepeatInterval + ArrowLength)
{
var start = System.Math.Max(0, (length - ArrowLength)/2);
var end = System.Math.Min(length, (length + ArrowLength)/2);
var arrow = (GeoAPI.Geometries.ILineString) lil.ExtractLine(start, end);
RenderArrow(map, graphics, arrow);
return;
}
var numArrows = (int) ((lineString.Length - ArrowLength)/RepeatInterval);
var offset = (lineString.Length - numArrows*RepeatInterval - ArrowLength)*0.5;
while (offset + ArrowLength < lineString.Length)
{
var arrow = (GeoAPI.Geometries.ILineString) lil.ExtractLine(offset, offset + ArrowLength);
RenderArrow(map, graphics, arrow);
offset += RepeatInterval;
}
}
/// <summary>
/// Method to render the arrow
/// </summary>
/// <param name="map">The map</param>
/// <param name="graphics">The graphics object</param>
/// <param name="arrow">The arrow</param>
private void RenderArrow(SharpMap.MapViewport map, System.Drawing.Graphics graphics, GeoAPI.Geometries.ILineString arrow)
{
var pts = new System.Drawing.PointF[arrow.Coordinates.Length];
for (var i = 0; i < pts.Length; i++)
pts[i] = map.WorldToImage(arrow.GetCoordinateN(i));
graphics.DrawLines(ArrowPen, pts);
}
/// <summary>
/// Gets or sets a value indicating at which distances a street direction marker should be drawn.
/// </summary>
public double RepeatInterval { get; set; }
/// <summary>
/// Gets or sets a value indicating the length of the street direction arrow.
/// </summary>
public double ArrowLength { get; set; }
/// <summary>
/// Gets or sets a value indicating the pen to use when drawing the marker
/// </summary>
public System.Drawing.Pen ArrowPen { get; set; }
public override void Begin(System.Drawing.Graphics g, SharpMap.MapViewport map, int aproximateNumberOfGeometries)
{
base.Begin(g, map, aproximateNumberOfGeometries);
//Adjust Arrow cap
var size = (float) (ArrowLength/5/map.PixelWidth);
ArrowPen.CustomEndCap = new System.Drawing.Drawing2D.AdjustableArrowCap(size, size);
}
public void Render(SharpMap.MapViewport map, GeoAPI.Geometries.ILineal geometry, System.Drawing.Graphics graphics)
{
if (geometry is GeoAPI.Geometries.IMultiLineString)
{
var mls = (GeoAPI.Geometries.IMultiLineString) geometry;
for (var i = 0; i < mls.Count; i++)
{
OnRenderInternal(map, (GeoAPI.Geometries.ILineString) mls.GetGeometryN(i), graphics);
}
return;
}
OnRenderInternal(map, (GeoAPI.Geometries.ILineString) geometry, graphics);
}
protected override void ReleaseManagedResources()
{
base.ReleaseManagedResources();
ArrowPen.Dispose();
}
public override object Clone()
{
var res = (StreetDirectionSymbolizer) MemberwiseClone();
res.ArrowPen = new System.Drawing.Pen(((System.Drawing.SolidBrush) ArrowPen.Brush).Color, ArrowPen.Width);
return res;
}
}
public class LineSymbolizerTest
{
[NUnit.Framework.OneTimeSetUp]
public void OneTimeSetUp()
{
GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance;
}
[NUnit.Framework.Test]
public void TestBasicLineSymbolizer()
{
if (!System.IO.File.Exists("d:\\daten\\GeoFabrik\\roads.shp"))
throw new NUnit.Framework.IgnoreException("");
var p = new SharpMap.Data.Providers.ShapeFile(@"d:\\daten\\GeoFabrik\\roads.shp", false);
var l = new SharpMap.Layers.VectorLayer("roads", p);
//l.Style.Outline = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 5);
l.Style.Line = new System.Drawing.Pen(System.Drawing.Color.Gold, 1);
l.Style.EnableOutline = false;
var m = new SharpMap.Map(new System.Drawing.Size(1440,1080)) {BackColor = System.Drawing.Color.Cornsilk};
m.Layers.Add(l);
m.ZoomToExtents();
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering old method: {0}ms", sw.ElapsedMilliseconds));
sw.Reset();
sw.Start();
var bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering old method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("NDSRoads1.bmp");
var cls = new SharpMap.Rendering.Symbolizer.CachedLineSymbolizer();
//cls.LineSymbolizeHandlers.Add(new SharpMap.Rendering.Symbolizer.PlainLineSymbolizeHandler { Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 5) });
cls.LineSymbolizeHandlers.Add(new SharpMap.Rendering.Symbolizer.PlainLineSymbolizeHandler { Line = new System.Drawing.Pen(System.Drawing.Color.Gold, 1) });
l.Style.LineSymbolizer = cls;
sw.Reset(); sw.Start();
bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("NDSRoads2.bmp");
}
[NUnit.Framework.Test]
public void TestStreetDirectionSymbolizer()
{
if (!System.IO.File.Exists("d:\\daten\\GeoFabrik\\Aurich\\roads.shp"))
throw new NUnit.Framework.IgnoreException("");
var p = new SharpMap.Data.Providers.ShapeFile(@"d:\\daten\\GeoFabrik\\Aurich\\roads.shp", false);
var l = new SharpMap.Layers.VectorLayer("roads", p);
l.Style.Outline = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 7);
l.Style.Line = new System.Drawing.Pen(System.Drawing.Color.Gold, 3);
l.Style.EnableOutline = true;
var sd = new SharpMap.Layers.Symbolizer.LinealVectorLayer("streetd", p, new StreetDirectionSymbolizer()
{
ArrowLength = 100, RepeatInterval = 500
});
var m = new SharpMap.Map(new System.Drawing.Size(1440, 1080)) { BackColor = System.Drawing.Color.Cornsilk };
m.Layers.Add(l);
m.Layers.Add(sd);
m.ZoomToExtents();
m.Zoom *= 0.3;
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
var bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("NDSRoadsSD.bmp");
}
[NUnit.Framework.Test]
public void TestWarpedLineSymbolizer()
{
if (!System.IO.File.Exists("d:\\daten\\GeoFabrik\\Aurich\\roads.shp"))
throw new NUnit.Framework.IgnoreException("");
var p = new SharpMap.Data.Providers.ShapeFile(@"d:\\daten\\GeoFabrik\\Aurich\\roads.shp", false);
var l = new SharpMap.Layers.VectorLayer("roads", p);
var cls = new SharpMap.Rendering.Symbolizer.CachedLineSymbolizer();
cls.LineSymbolizeHandlers.Add(new SharpMap.Rendering.Symbolizer.PlainLineSymbolizeHandler
{Line = new System.Drawing.Pen(System.Drawing.Color.Gold, 2)});
var wls = new SharpMap.Rendering.Symbolizer.WarpedLineSymbolizeHander
{
Pattern =
SharpMap.Rendering.Symbolizer.WarpedLineSymbolizer.
GetGreaterSeries(3, 3),
Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 1)
, Interval = 20
};
cls.LineSymbolizeHandlers.Add(wls);
l.Style.LineSymbolizer = cls;
var m = new SharpMap.Map(new System.Drawing.Size(720, 540)) {BackColor = System.Drawing.Color.Cornsilk};
m.Layers.Add(l);
m.ZoomToExtents();
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
var bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("AurichRoads1.bmp");
cls.LineSymbolizeHandlers[1] = new SharpMap.Rendering.Symbolizer.WarpedLineSymbolizeHander
{
Pattern =
SharpMap.Rendering.Symbolizer.WarpedLineSymbolizer.
GetTriangle(4, 0),
Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 1),
Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Firebrick)
,Interval = 10
};
sw.Reset(); sw.Start();
bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("AurichRoads2-0.bmp");
cls.LineSymbolizeHandlers[1] = new SharpMap.Rendering.Symbolizer.WarpedLineSymbolizeHander
{
Pattern =
SharpMap.Rendering.Symbolizer.WarpedLineSymbolizer.
GetTriangle(4, 1),
Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 1),
Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Firebrick)
,
Interval = 10
};
sw.Reset(); sw.Start();
bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("AurichRoads2-1.bmp");
cls.LineSymbolizeHandlers[1] = new SharpMap.Rendering.Symbolizer.WarpedLineSymbolizeHander
{
Pattern =
SharpMap.Rendering.Symbolizer.WarpedLineSymbolizer.
GetTriangle(4, 2),
Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 1),
Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Firebrick)
,
Interval = 10
};
sw.Reset(); sw.Start();
bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("AurichRoads2-2.bmp");
cls.LineSymbolizeHandlers[1] = new SharpMap.Rendering.Symbolizer.WarpedLineSymbolizeHander
{
Pattern =
SharpMap.Rendering.Symbolizer.WarpedLineSymbolizer.
GetTriangle(4, 3),
Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 1),
Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Firebrick)
,
Interval = 10
};
sw.Reset(); sw.Start();
bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("AurichRoads2-3.bmp");
//cls.LineSymbolizeHandlers[0] = cls.LineSymbolizeHandlers[1];
cls.LineSymbolizeHandlers[1] = new SharpMap.Rendering.Symbolizer.WarpedLineSymbolizeHander
{
Pattern =
SharpMap.Rendering.Symbolizer.WarpedLineSymbolizer.GetZigZag(4, 4),
Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 1),
//Fill = new System.Drawing.SolidBrush(System.Drawing.Color.Firebrick)
};
sw.Reset(); sw.Start();
bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("AurichRoads3.bmp");
}
[NUnit.Framework.Test]
public void TestCachedLineSymbolizerInTheme()
{
if (!System.IO.File.Exists("d:\\daten\\GeoFabrik\\Aurich\\roads.shp"))
throw new NUnit.Framework.IgnoreException("");
var p = new SharpMap.Data.Providers.ShapeFile(@"d:\\daten\\GeoFabrik\\Aurich\\roads.shp", false);
var l = new SharpMap.Layers.VectorLayer("roads", p);
var theme = new ClsTheme(l.Style);
l.Theme = theme;
var m = new SharpMap.Map(new System.Drawing.Size(720, 540)) { BackColor = System.Drawing.Color.Cornsilk };
m.Layers.Add(l);
m.ZoomToExtents();
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
var bmp = m.GetMap();
sw.Stop();
System.Console.WriteLine(string.Format("Rendering new method: {0}ms", sw.ElapsedMilliseconds));
bmp.Save("AurichRoads1Theme.bmp");
}
internal class ClsTheme : SharpMap.Rendering.Thematics.ITheme
{
private SharpMap.Styles.VectorStyle _style;
public ClsTheme(SharpMap.Styles.VectorStyle style)
{
_style = style;
}
#region Implementation of ITheme
/// <summary>
/// Returns the style based on a feature
/// </summary>
/// <param name="attribute">Set of attribute values to calculate the <see cref="SharpMap.Styles.IStyle"/> from</param>
/// <returns>The style</returns>
public SharpMap.Styles.IStyle GetStyle(SharpMap.Data.FeatureDataRow attribute)
{
var res = _style;
var cls = new SharpMap.Rendering.Symbolizer.CachedLineSymbolizer();
cls.LineSymbolizeHandlers.Add(new SharpMap.Rendering.Symbolizer.PlainLineSymbolizeHandler { Line = new System.Drawing.Pen(System.Drawing.Color.Gold, 2) });
var wls = new SharpMap.Rendering.Symbolizer.WarpedLineSymbolizeHander
{
Pattern =
SharpMap.Rendering.Symbolizer.WarpedLineSymbolizer.
GetGreaterSeries(3, 3),
Line = new System.Drawing.Pen(System.Drawing.Color.Firebrick, 1)
,
Interval = 20
};
cls.LineSymbolizeHandlers.Add(wls);
cls.ImmediateMode = true;
res.LineSymbolizer = cls;
return res;
}
#endregion
}
[NUnit.Framework.Test]
public void TestTransformation()
{
var m = new SharpMap.Map(new System.Drawing.Size(640, 320));
var l = new SharpMap.Layers.Symbolizer.PuntalVectorLayer("l",
new SharpMap.Data.Providers.GeometryProvider(m.Factory.CreatePoint(new GeoAPI.Geometries.Coordinate(0, 51.478885))),
SharpMap.Rendering.Symbolizer.PathPointSymbolizer.CreateCircle(System.Drawing.Pens.Aquamarine, System.Drawing.Brushes.BurlyWood, 24));
var ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
l.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
l.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
m.Layers.Add(new SharpMap.Layers.TileLayer(BruTile.Predefined.KnownTileSources.Create(),"b"));
m.Layers.Add(l);
var e = new GeoAPI.Geometries.Envelope(-0.02, 0.02, 51.478885 - 0.01, 51.478885 + 0.01);
e = GeoAPI.CoordinateSystems.Transformations.GeometryTransform.TransformBox(e,
l.CoordinateTransformation.MathTransform);
m.ZoomToBox(e);
m.GetMap().Save("Greenwich.png", System.Drawing.Imaging.ImageFormat.Png);
}
}
}