forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapHelper.cs
More file actions
574 lines (499 loc) · 25.2 KB
/
MapHelper.cs
File metadata and controls
574 lines (499 loc) · 25.2 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
using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Web;
using SharpMap;
using SharpMap.Data.Providers;
using SharpMap.Layers;
using SharpMap.Rendering;
using SharpMap.Rendering.Thematics;
using SharpMap.Styles;
using ColorBlend=SharpMap.Rendering.Thematics.ColorBlend;
using Point=GeoAPI.Geometries.Coordinate;
/// <summary>
/// Summary description for CreateMap
/// </summary>
public class MapHelper
{
static MapHelper()
{
var gss = GeoAPI.GeometryServiceProvider.Instance;
var css = new SharpMap.CoordinateSystems.CoordinateSystemServices(
new ProjNet.CoordinateSystems.CoordinateSystemFactory(System.Text.Encoding.ASCII),
new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory(),
SharpMap.Converters.WellKnownText.SpatialReference.GetAllReferenceSystems());
GeoAPI.GeometryServiceProvider.Instance = gss;
SharpMap.Session.Instance
.SetGeometryServices(gss)
.SetCoordinateSystemServices(css)
.SetCoordinateSystemRepository(css);
}
public static Map InitializeMap(Size size)
{
HttpContext.Current.Trace.Write("Initializing map...");
//Initialize a new map of size 'imagesize'
Map map = new Map(size);
//Set up the countries layer
VectorLayer layCountries = new VectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
layCountries.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\countries.shp"), true);
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.Green);
//Set the polygons to have a black outline
layCountries.Style.Outline = Pens.Black;
layCountries.Style.EnableOutline = true;
layCountries.SRID = 4326;
//Set up a river layer
VectorLayer layRivers = new VectorLayer("Rivers");
//Set the datasource to a shapefile in the App_data folder
layRivers.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\rivers.shp"), true);
//Define a blue 1px wide pen
layRivers.Style.Line = new Pen(Color.Blue, 1);
layRivers.SRID = 4326;
//Set up a river layer
VectorLayer layCities = new VectorLayer("Cities");
//Set the datasource to a shapefile in the App_data folder
layCities.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\cities.shp"), true);
//Define a blue 1px wide pen
//layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));
layCities.Style.SymbolScale = 0.8f;
layCities.MaxVisible = 40;
layCities.SRID = 4326;
//Set up a country label layer
LabelLayer layLabel = new LabelLayer("Country labels");
layLabel.DataSource = layCountries.DataSource;
layLabel.Enabled = true;
layLabel.LabelColumn = "Name";
layLabel.Style = new LabelStyle();
layLabel.Style.ForeColor = Color.White;
layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
layLabel.MaxVisible = 90;
layLabel.MinVisible = 30;
layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
layLabel.SRID = 4326;
layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest;
//Set up a city label layer
LabelLayer layCityLabel = new LabelLayer("City labels");
layCityLabel.DataSource = layCities.DataSource;
layCityLabel.Enabled = true;
layCityLabel.LabelColumn = "Name";
layCityLabel.Style = new LabelStyle();
layCityLabel.Style.ForeColor = Color.Black;
layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
layCityLabel.MaxVisible = layLabel.MinVisible;
layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
layCityLabel.Style.Offset = new PointF(3, 3);
layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
layCityLabel.SRID = 4326;
layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
layCityLabel.Style.CollisionDetection = true;
//Add the layers to the map object.
//The order we add them in are the order they are drawn, so we add the rivers last to put them on top
map.Layers.Add(layCountries);
map.Layers.Add(layRivers);
map.Layers.Add(layCities);
map.Layers.Add(layLabel);
map.Layers.Add(layCityLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = Color.LightBlue;
map.Zoom = 360;
map.Center = new Point(0, 0);
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
public static Map InitializeGradientMap(Size size)
{
//Initialize a new map based on the simple map
Map map = InitializeMap(size);
//Set a gradient theme on the countries layer, based on Population density
//First create two styles that specify min and max styles
//In this case we will just use the default values and override the fill-colors
//using a colorblender. If different line-widths, line- and fill-colors where used
//in the min and max styles, these would automatically get linearly interpolated.
VectorStyle min = new VectorStyle();
VectorStyle max = new VectorStyle();
//Create theme using a density from 0 (min) to 400 (max)
GradientTheme popdens = new GradientTheme("PopDens", 0, 400, min, max);
//We can make more advanced coloring using the ColorBlend'er.
//Setting the FillColorBlend will override any fill-style in the min and max fills.
//In this case we just use the predefined Rainbow colorscale
popdens.FillColorBlend = ColorBlend.Rainbow5;
(map.Layers[0] as VectorLayer).Theme = popdens;
//Lets scale the labels so that big countries have larger texts as well
LabelStyle lblMin = new LabelStyle();
LabelStyle lblMax = new LabelStyle();
lblMin.ForeColor = Color.Black;
lblMin.Font = new Font(FontFamily.GenericSerif, 6);
lblMax.ForeColor = Color.Blue;
lblMax.BackColor = new SolidBrush(Color.FromArgb(128, 255, 255, 255));
lblMin.BackColor = lblMax.BackColor;
lblMax.Font = new Font(FontFamily.GenericSerif, 9);
(map.Layers[3] as LabelLayer).Theme = new GradientTheme("PopDens", 0, 400, lblMin, lblMax);
//Lets scale city icons based on city population
//cities below 1.000.000 gets the smallest symbol, and cities with more than 5.000.000 the largest symbol
VectorStyle citymin = new VectorStyle();
VectorStyle citymax = new VectorStyle();
citymin.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));
citymin.SymbolScale = 0.5f;
citymax.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));
citymax.SymbolScale = 1f;
(map.Layers[2] as VectorLayer).Theme = new GradientTheme("Population", 1000000, 5000000, citymin, citymax);
//Turn off the river layer
map.Layers[1].Enabled = false;
return map;
}
public static WmsLayer GetWmsLayer()
{
string wmsUrl = "http://www2.demis.nl/worldmap/wms.asp";
WmsLayer layWms = new WmsLayer("Demis Map", wmsUrl);
layWms.AddLayer("Bathymetry");
//layWms.AddLayer("Coastlines");
//layWms.AddLayer("Countries");
//layWms.AddLayer("Rivers");
//layWms.AddLayer("Streams");
layWms.AddLayer("Ocean features");
layWms.SetImageFormat(layWms.OutputFormats[0]);
layWms.ContinueOnError = true;
//Skip rendering the WMS Map if the server couldn't be requested (if set to false such an event would crash the app)
layWms.TimeOut = 5000; //Set timeout to 5 seconds
layWms.SRID = 4326;
return layWms;
}
public static Map InitializeWmsMap(Size size)
{
HttpContext.Current.Trace.Write("Initializing Wms map...");
//Initialize a new map of size 'imagesize'
Map map = new Map(size);
WmsLayer layWms = GetWmsLayer();
//Set up the countries layer
VectorLayer layCountries = new VectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
layCountries.DataSource = new ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\countries.shp"), true);
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.Green);
//Set the polygons to have a black outline
layCountries.Style.Outline = Pens.Yellow;
layCountries.Style.EnableOutline = true;
layCountries.SRID = 4326;
//Set up a country label layer
LabelLayer layLabel = new LabelLayer("Country labels");
layLabel.DataSource = layCountries.DataSource;
layLabel.Enabled = true;
layLabel.LabelColumn = "Name";
layLabel.Style = new LabelStyle();
layLabel.Style.ForeColor = Color.White;
layLabel.Style.Font = new Font(FontFamily.GenericSerif, 8);
layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
layLabel.MaxVisible = 90;
layLabel.MinVisible = 30;
layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
layLabel.SRID = 4326;
//Add the layers to the map object.
//The order we add them in are the order they are drawn, so we add the rivers last to put them on top
map.Layers.Add(layWms);
map.Layers.Add(layCountries);
map.Layers.Add(layLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = Color.LightBlue;
map.Zoom = 360;
map.Center = new Point(0, 0);
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
public static Map InitializeMapOgr(Size size)
{
HttpContext.Current.Trace.Write("Initializing map...");
//Initialize a new map of size 'imagesize'
Map map = new Map(size);
//Set up the countries layer
VectorLayer layCountries = new VectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
try
{
layCountries.DataSource =
new Ogr(HttpContext.Current.Server.MapPath(@"~\App_data\MapInfo\countriesMapInfo.tab"));
}
catch (TypeInitializationException ex)
{
if (ex.GetType() == typeof (TypeInitializationException))
throw new Exception(
"Please copy the umanaged dll's into your bin folder from javascript:window.location.href='http://www.codeplex.com/SharpMap/Wiki/View.aspx?title=Extensions';.");
}
catch (Exception ex)
{
throw;
}
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.Green);
//Set the polygons to have a black outline
layCountries.Style.Outline = Pens.Black;
layCountries.Style.EnableOutline = true;
layCountries.SRID = 4326;
//Set up a river layer
VectorLayer layRivers = new VectorLayer("Rivers");
//Set the datasource to a shapefile in the App_data folder
layRivers.DataSource = new Ogr(HttpContext.Current.Server.MapPath(@"~\App_data\MapInfo\riversMapInfo.tab"));
//Define a blue 1px wide pen
layRivers.Style.Line = new Pen(Color.Blue, 1);
layRivers.SRID = 4326;
//Set up a river layer
VectorLayer layCities = new VectorLayer("Cities");
//Set the datasource to a shapefile in the App_data folder
layCities.DataSource = new Ogr(HttpContext.Current.Server.MapPath(@"~\App_data\MapInfo\citiesMapInfo.tab"));
//Define a blue 1px wide pen
//layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));
layCities.Style.SymbolScale = 0.8f;
layCities.MaxVisible = 40;
layCities.SRID = 4326;
//Set up a country label layer
LabelLayer layLabel = new LabelLayer("Country labels");
layLabel.DataSource = layCountries.DataSource;
layLabel.Enabled = true;
layLabel.LabelColumn = "Name";
layLabel.Style = new LabelStyle();
layLabel.Style.ForeColor = Color.White;
layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
layLabel.MaxVisible = 90;
layLabel.MinVisible = 30;
layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
layLabel.SRID = 4326;
layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest;
//Set up a city label layer
LabelLayer layCityLabel = new LabelLayer("City labels");
layCityLabel.DataSource = layCities.DataSource;
layCityLabel.Enabled = true;
layCityLabel.LabelColumn = "Name";
layCityLabel.Style = new LabelStyle();
layCityLabel.Style.ForeColor = Color.Black;
layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
layCityLabel.MaxVisible = layLabel.MinVisible;
layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
layCityLabel.Style.Offset = new PointF(3, 3);
layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
layCityLabel.SRID = 4326;
layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
layCityLabel.Style.CollisionDetection = true;
//Add the layers to the map object.
//The order we add them in are the order they are drawn, so we add the rivers last to put them on top
map.Layers.Add(layCountries);
map.Layers.Add(layRivers);
map.Layers.Add(layCities);
map.Layers.Add(layLabel);
map.Layers.Add(layCityLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = Color.LightBlue;
map.ZoomToExtents(); // = 360;
map.Center = new Point(0, 0);
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
public static Map InitializeMapMsSqlSpatial(Size size)
{
HttpContext.Current.Trace.Write("Initializing map...");
string connectionString = ConfigurationManager.ConnectionStrings["mssqlspatial"].ConnectionString;
//Initialize a new map of size 'imagesize'
Map map = new Map(size);
if ( connectionString.Contains("server=XXXXXXXXXXX" ) )
throw new ArgumentException("You need to adjust the connectionstring to MsSqlSpatial Server");
//Set up the countries layer
VectorLayer layCountries = new VectorLayer("Countries");
//Set the datasource to a new MsSqlSpatialProvider
layCountries.DataSource = new MsSqlSpatial(connectionString, "Countries", "Geometry", "oid");
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.Green);
//Set the polygons to have a black outline
layCountries.Style.Outline = Pens.Black;
layCountries.Style.EnableOutline = true;
layCountries.SRID = 4326;
//Set up a river layer
VectorLayer layRivers = new VectorLayer("Rivers");
//Set the datasource to a new MsSqlSpatialProvider
layRivers.DataSource = new MsSqlSpatial(connectionString, "Rivers", "Geometry", "oid");
//Define a blue 1px wide pen
layRivers.Style.Line = new Pen(Color.Blue, 1);
layRivers.SRID = 4326;
//Set up a river layer
VectorLayer layCities = new VectorLayer("Cities");
//Set the datasource to a new MsSqlSpatialProvider
layCities.DataSource = new MsSqlSpatial(connectionString, "Cities", "Geometry", "oid");
;
//Define a blue 1px wide pen
//layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));
layCities.Style.SymbolScale = 0.8f;
layCities.MaxVisible = 40;
layCities.SRID = 4326;
//Set up a country label layer
LabelLayer layLabel = new LabelLayer("Country labels");
layLabel.DataSource = layCountries.DataSource;
layLabel.Enabled = true;
layLabel.LabelColumn = "Name";
layLabel.Style = new LabelStyle();
layLabel.Style.ForeColor = Color.White;
layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
layLabel.Style.BackColor = new SolidBrush(Color.FromArgb(128, 255, 0, 0));
layLabel.MaxVisible = 90;
layLabel.MinVisible = 30;
layLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center;
layLabel.SRID = 4326;
layLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.Largest;
//Set up a city label layer
LabelLayer layCityLabel = new LabelLayer("City labels");
layCityLabel.DataSource = layCities.DataSource;
layCityLabel.Enabled = true;
layCityLabel.LabelColumn = "Name";
layCityLabel.Style = new LabelStyle();
layCityLabel.Style.ForeColor = Color.Black;
layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
layCityLabel.MaxVisible = layLabel.MinVisible;
layCityLabel.Style.HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left;
layCityLabel.Style.VerticalAlignment = LabelStyle.VerticalAlignmentEnum.Bottom;
layCityLabel.Style.Offset = new PointF(3, 3);
layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
layCityLabel.TextRenderingHint = TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = SmoothingMode.AntiAlias;
layCityLabel.SRID = 4326;
layCityLabel.LabelFilter = LabelCollisionDetection.ThoroughCollisionDetection;
layCityLabel.Style.CollisionDetection = true;
//Add the layers to the map object.
//The order we add them in are the order they are drawn, so we add the rivers last to put them on top
map.Layers.Add(layCountries);
map.Layers.Add(layRivers);
map.Layers.Add(layCities);
map.Layers.Add(layLabel);
map.Layers.Add(layCityLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = Color.LightBlue;
map.Zoom = 360;
map.Center = new Point(0, 0);
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
public static Map InitializeMapPostGis(Size size)
{
HttpContext.Current.Trace.Write("Initializing map...");
string connectionString = ConfigurationManager.ConnectionStrings["postgis"].ConnectionString;
//Initialize a new map of size 'imagesize'
SharpMap.Map map = new SharpMap.Map(size);
//Set up the countries layer
SharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer("Countries");
//Set the datasource to a new MsSqlSpatialProvider
layCountries.DataSource = new SharpMap.Data.Providers.PostGIS(connectionString, "countries", "wkb_geometry", "ogc_fid");
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.Green);
//Set the polygons to have a black outline
layCountries.Style.Outline = System.Drawing.Pens.Black;
layCountries.Style.EnableOutline = true;
//Set up a river layer
SharpMap.Layers.VectorLayer layRivers = new SharpMap.Layers.VectorLayer("Rivers");
//Set the datasource to a new MsSqlSpatialProvider
layRivers.DataSource = new SharpMap.Data.Providers.PostGIS(connectionString, "rivers", "wkb_geometry", "ogc_fid");
//Define a blue 1px wide pen
layRivers.Style.Line = new Pen(Color.Blue, 1);
//Set up a river layer
SharpMap.Layers.VectorLayer layCities = new SharpMap.Layers.VectorLayer("Cities");
//Set the datasource to a new MsSqlSpatialProvider
layCities.DataSource = new SharpMap.Data.Providers.PostGIS(connectionString, "cities", "wkb_geometry", "ogc_fid"); ;
//Define a blue 1px wide pen
//layCities.Style.Symbol = new Bitmap(HttpContext.Current.Server.MapPath(@"~\App_data\icon.png"));
layCities.Style.SymbolScale = 0.8f;
layCities.MaxVisible = 40;
//Set up a country label layer
SharpMap.Layers.LabelLayer layLabel = new SharpMap.Layers.LabelLayer("Country labels");
layLabel.DataSource = layCountries.DataSource;
layLabel.Enabled = true;
layLabel.LabelColumn = "Name";
layLabel.MaxVisible = 90;
layLabel.MinVisible = 30;
layLabel.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;
layLabel.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
layLabel.PriorityColumn = "popdens";
layLabel.Style = new SharpMap.Styles.LabelStyle();
layLabel.Style.ForeColor = Color.White;
layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
layLabel.Style.BackColor = new System.Drawing.SolidBrush(Color.FromArgb(128, 255, 0, 0));
layLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
layLabel.Style.CollisionDetection = true;
//Set up a city label layer
SharpMap.Layers.LabelLayer layCityLabel = new SharpMap.Layers.LabelLayer("City labels");
layCityLabel.DataSource = layCities.DataSource;
layCityLabel.Enabled = true;
layCityLabel.LabelColumn = "name";
layCityLabel.PriorityColumn = "population";
layCityLabel.PriorityDelegate = delegate(SharpMap.Data.FeatureDataRow fdr)
{
Int32 retVal = 10000000 * (Int32)((String)fdr["capital"] == "Y" ? 1 : 0);
return retVal + Convert.ToInt32(fdr["population"]);
};
layCityLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
layCityLabel.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
layCityLabel.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
layCityLabel.Style = new SharpMap.Styles.LabelStyle();
layCityLabel.Style.ForeColor = Color.Black;
layCityLabel.Style.Font = new Font(FontFamily.GenericSerif, 11);
layCityLabel.Style.MaxVisible = layLabel.MinVisible;
layCityLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left;
layCityLabel.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom;
layCityLabel.Style.Offset = new PointF(3, 3);
layCityLabel.Style.Halo = new Pen(Color.Yellow, 2);
layCityLabel.Style.CollisionDetection = true;
//Add the layers to the map object.
//The order we add them in are the order they are drawn, so we add the rivers last to put them on top
map.Layers.Add(layCountries);
map.Layers.Add(layRivers);
map.Layers.Add(layCities);
map.Layers.Add(layLabel);
map.Layers.Add(layCityLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = Color.LightBlue;
map.Zoom = 360;
map.Center = new Point(0, 0);
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
public static Map InitializeMapGdal(Size size)
{
HttpContext.Current.Trace.Write("Initializing map...");
//Initialize a new map of size 'imagesize'
Map map = new Map(size);
//Set up the gdal layer
LayerGroup g = new LayerGroup("OS");
g.SRID = 27700;
//D:\Raster\Ordnance Survey\OS Street View SM\data\sm
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"~\App_Data\Gdal"));
foreach (System.IO.FileInfo fi in di.GetFiles("*.tif"))
{
try
{
SharpMap.Layers.GdalRasterLayer layer =
new SharpMap.Layers.GdalRasterLayer(
fi.Name, HttpContext.Current.Server.MapPath(@"~\App_Data\Gdal\" + fi.Name));
//layer.SRID = 27700;
g.Layers.Add(layer);
}
catch (TypeInitializationException ex)
{
throw new Exception(
"Please copy the umanaged dll's into your bin folder from javascript:window.location.href='http://www.codeplex.com/SharpMap/Wiki/View.aspx?title=Extensions';.");
}
}
map.Layers.Add(g);
map.ZoomToExtents();
HttpContext.Current.Trace.Write("Map initialized");
return map;
}
}