forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerTools.cs
More file actions
280 lines (236 loc) · 12.5 KB
/
LayerTools.cs
File metadata and controls
280 lines (236 loc) · 12.5 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
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using SharpMap.Layers;
using System.Drawing;
using SharpMap.Styles;
#if DotSpatialProjections
using DotSpatial.Projections;
#else
using GeoAPI.CoordinateSystems.Transformations;
using ProjNet.CoordinateSystems.Transformations;
using GeoAPI.CoordinateSystems;
using ProjNet.CoordinateSystems;
#endif
namespace WinFormSamples
{
static class LayerTools
{
private static ICoordinateTransformation dhdn2towgs84;
private static ICoordinateTransformation wgs84toGoogle;
private static ICoordinateTransformation googletowgs84;
/// <summary>
/// Wgs84 to Google Mercator Coordinate Transformation
/// </summary>
public static ICoordinateTransformation Wgs84toGoogleMercator
{
get
{
if (wgs84toGoogle == null)
{
#if DotSpatialProjections
wgs84toGoogle = new CoordinateTransformation()
{
Source = KnownCoordinateSystems.Geographic.World.WGS1984,
Target = KnownCoordinateSystems.Projected.World.WebMercator
};
#else
CoordinateSystemFactory csFac = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
IGeographicCoordinateSystem wgs84 = csFac.CreateGeographicCoordinateSystem(
"WGS 84", AngularUnit.Degrees, HorizontalDatum.WGS84, PrimeMeridian.Greenwich,
new AxisInfo("north", AxisOrientationEnum.North), new AxisInfo("east", AxisOrientationEnum.East));
List<ProjectionParameter> parameters = new List<ProjectionParameter>();
parameters.Add(new ProjectionParameter("semi_major", 6378137.0));
parameters.Add(new ProjectionParameter("semi_minor", 6378137.0));
parameters.Add(new ProjectionParameter("latitude_of_origin", 0.0));
parameters.Add(new ProjectionParameter("central_meridian", 0.0));
parameters.Add(new ProjectionParameter("scale_factor", 1.0));
parameters.Add(new ProjectionParameter("false_easting", 0.0));
parameters.Add(new ProjectionParameter("false_northing", 0.0));
IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);
IProjectedCoordinateSystem epsg900913 = csFac.CreateProjectedCoordinateSystem(
"Google Mercator", wgs84, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East),
new AxisInfo("North", AxisOrientationEnum.North));
((CoordinateSystem)epsg900913).DefaultEnvelope = new [] { -20037508.342789, -20037508.342789, 20037508.342789, 20037508.342789 };
wgs84toGoogle = ctFac.CreateFromCoordinateSystems(wgs84, epsg900913);
#endif
}
return wgs84toGoogle;
}
}
/// <summary>
/// Wgs84 to Google Mercator Coordinate Transformation
/// </summary>
public static ICoordinateTransformation Dhdn2ToWgs84
{
get
{
if (dhdn2towgs84 == null)
{
#if DotSpatialProjections
var piSource = ProjectionInfo.FromEpsgCode(31466);
wgs84toGoogle = new CoordinateTransformation()
{
Target = KnownCoordinateSystems.Geographic.World.WGS1984,
Source = piSource
};
#else
CoordinateSystemFactory csFac = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
IGeographicCoordinateSystem wgs84 = csFac.CreateGeographicCoordinateSystem(
"WGS 84", AngularUnit.Degrees, HorizontalDatum.WGS84, PrimeMeridian.Greenwich,
new AxisInfo("north", AxisOrientationEnum.North), new AxisInfo("east", AxisOrientationEnum.East));
List<ProjectionParameter> parameters = new List<ProjectionParameter>();
parameters.Add(new ProjectionParameter("semi_major", 6378137.0));
parameters.Add(new ProjectionParameter("semi_minor", 6378137.0));
parameters.Add(new ProjectionParameter("latitude_of_origin", 0.0));
parameters.Add(new ProjectionParameter("central_meridian", 0.0));
parameters.Add(new ProjectionParameter("scale_factor", 1.0));
parameters.Add(new ProjectionParameter("false_easting", 0.0));
parameters.Add(new ProjectionParameter("false_northing", 0.0));
IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);
IProjectedCoordinateSystem epsg900913 = csFac.CreateProjectedCoordinateSystem(
"Google Mercator", wgs84, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East),
new AxisInfo("North", AxisOrientationEnum.North));
dhdn2towgs84 = ctFac.CreateFromCoordinateSystems(wgs84, epsg900913);
#endif
}
return dhdn2towgs84;
}
}
public static ICoordinateTransformation GoogleMercatorToWgs84
{
get
{
if (dhdn2towgs84 == null)
{
#if DotSpatialProjections
googletowgs84 = new CoordinateTransformation()
{
Source = KnownCoordinateSystems.Projected.World.WebMercator,
Target = KnownCoordinateSystems.Geographic.World.WGS1984
};
#else
CoordinateSystemFactory csFac = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
IGeographicCoordinateSystem wgs84 = csFac.CreateGeographicCoordinateSystem(
"WGS 84", AngularUnit.Degrees, HorizontalDatum.WGS84, PrimeMeridian.Greenwich,
new AxisInfo("north", AxisOrientationEnum.North), new AxisInfo("east", AxisOrientationEnum.East));
List<ProjectionParameter> parameters = new List<ProjectionParameter>();
parameters.Add(new ProjectionParameter("semi_major", 6378137.0));
parameters.Add(new ProjectionParameter("semi_minor", 6378137.0));
parameters.Add(new ProjectionParameter("latitude_of_origin", 0.0));
parameters.Add(new ProjectionParameter("central_meridian", 0.0));
parameters.Add(new ProjectionParameter("scale_factor", 1.0));
parameters.Add(new ProjectionParameter("false_easting", 0.0));
parameters.Add(new ProjectionParameter("false_northing", 0.0));
IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);
IProjectedCoordinateSystem epsg900913 = csFac.CreateProjectedCoordinateSystem(
"Google Mercator", wgs84, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East),
new AxisInfo("North", AxisOrientationEnum.North));
googletowgs84 = ctFac.CreateFromCoordinateSystems(epsg900913, wgs84);
#endif
}
return googletowgs84;
}
}
public static LabelLayer CreateLabelLayer(VectorLayer originalLayer, string labelColumnName)
{
SharpMap.Layers.LabelLayer labelLayer = new SharpMap.Layers.LabelLayer(originalLayer.LayerName + ":Labels");
labelLayer.DataSource = originalLayer.DataSource;
labelLayer.LabelColumn = labelColumnName;
labelLayer.Style.CollisionDetection = true;
labelLayer.Style.CollisionBuffer = new SizeF(10F, 10F);
labelLayer.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
labelLayer.Style.Offset = new PointF(0, -5F);
labelLayer.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter;
labelLayer.Style.Font = new Font(FontFamily.GenericSansSerif, 12);
labelLayer.MaxVisible = originalLayer.MaxVisible;
labelLayer.MinVisible = originalLayer.MinVisible;
labelLayer.Style.Halo = new Pen(Color.White, 2);
labelLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
labelLayer.CoordinateTransformation = originalLayer.CoordinateTransformation;
return labelLayer;
}
private static readonly Random Rnd = new Random();
public static Color GetRandomColor()
{
return Color.FromArgb(Rnd.Next(0, 127), Rnd.Next(0, 255), Rnd.Next(0,255), Rnd.Next(0,255));
}
public static Boolean GetRandomBoolean()
{
return Rnd.Next(0, 2) == 1;
}
public static Brush GetRandomBrush()
{
return new SolidBrush(GetRandomColor());
}
public static Pen GetRandomPen()
{
return GetRandomPen(1, 4);
}
/// <summary>
/// Generates a random symbol.
/// </summary>
/// <returns></returns>
public static Bitmap GetRandomSymbol()
{
var s = Rnd.Next(4, 12);
var f = new Font("WingDings", 2*s, GraphicsUnit.Pixel);
var bmp = new Bitmap(2*s + 2, 2*s + 2, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
g.DrawString(new string(Convert.ToChar((byte) Rnd.Next(15, 127)), 1), f, GetRandomBrush(), new PointF(s, s) ,
new StringFormat
{Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center});
}
return bmp;
}
public static SharpMap.Styles.VectorStyle GetRandomVectorStyle()
{
return new SharpMap.Styles.VectorStyle
{
EnableOutline = GetRandomBoolean(),
Outline = GetRandomPen(3, 7),
Line = GetRandomPen(1, 4),
Fill = GetRandomBrush(),
Symbol = GetRandomSymbol(),
//SymbolRotation = Rnd.Next(0, 359),
};
}
private static Pen GetRandomPen(int min, int max)
{
var p = new Pen(GetRandomColor(), Rnd.Next(min, max));
p.LineJoin = LineJoin.MiterClipped;
p.MiterLimit = 2f;
return p;
}
public static SharpMap.Map GetMapForProviders(IEnumerable<SharpMap.Data.Providers.IProvider> providers)
{
var m = new SharpMap.Map();
foreach (var provider in providers)
{
var l = new VectorLayer(provider.ConnectionID, provider)
{
Style = GetRandomVectorStyle()
};
m.Layers.Add(l);
var ll = new LabelLayer("Label " + provider.ConnectionID);
ll.DataSource = provider;
provider.Open();
var f = provider.GetFeatureByOid(1);
provider.Close();
ll.LabelColumn = f.Factory.AttributesDefinition[1].AttributeName;
ll.Style.CollisionDetection = false;
ll.Style.IgnoreLength = true;
m.Layers.Add(ll);
}
m.ZoomToExtents();
return m;
}
}
}