forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatiaLiteSample.cs
More file actions
191 lines (169 loc) · 8.66 KB
/
SpatiaLiteSample.cs
File metadata and controls
191 lines (169 loc) · 8.66 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
#if OPENGLSAMPLE
using VectorLayer = SharpMap.Layers.OpenGLVectorLayer;
#else
using VectorLayer = SharpMap.Layers.VectorLayer;
#endif
namespace WinFormSamples.Samples
{
public static class SpatiaLiteSample
{
private static string DataSource
{
get { return @"Data Source=GeoData\SpatiaLite\sample.sqlite"; }
}
public static SharpMap.Map InitializeMap(float angle)
{
//Initialize a new map of size 'imagesize'
SharpMap.Map map = new SharpMap.Map();
//Set up the countries layer
var layCountries = new VectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
layCountries.DataSource = new SharpMap.Data.Providers.SpatiaLite(
DataSource, "countries", "geom", "PK_UID");
//Set fill-style to green
layCountries.Style.Fill = new System.Drawing.SolidBrush(System.Drawing.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
var layRivers = new SharpMap.Layers.VectorLayer("Rivers");
//Set the datasource to a shapefile in the App_data folder
layRivers.DataSource = new SharpMap.Data.Providers.SpatiaLite(
DataSource, "rivers", "geom", "PK_UID");
//Define a blue 3px wide pen
layRivers.Style.Line = new System.Drawing.Pen(System.Drawing.Color.LightBlue, 2);
layRivers.Style.Line.CompoundArray = new[] { 0.2f, 0.8f };
layRivers.Style.Outline = new System.Drawing.Pen(System.Drawing.Color.DarkBlue, 3);
layRivers.Style.EnableOutline = true;
//Set up a cities layer
var layCities = new SharpMap.Layers.VectorLayer("Cities");
//Set the datasource to the spatialite table
layCities.DataSource = new SharpMap.Data.Providers.SpatiaLite(
DataSource, "cities", "geom", "PK_UID");
layCities.Style.SymbolScale = 0.8f;
layCities.MaxVisible = 40;
//Set up a country label layer
SharpMap.Layers.LabelLayer layLabel = new SharpMap.Layers.LabelLayer("Country labels")
{
DataSource = layCountries.DataSource,
LabelColumn = "NAME",
MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest,
LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection,
PriorityColumn = "POPDENS",
Style = new SharpMap.Styles.LabelStyle
{
ForeColor = System.Drawing.Color.White,
Font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 12),
BackColor = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(128, 255, 0, 0)),
HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center,
MaxVisible = 90,
MinVisible = 30,
CollisionDetection = true
}
};
//Set up a city label layer
SharpMap.Layers.LabelLayer layCityLabel = new SharpMap.Layers.LabelLayer("City labels")
{
DataSource = layCities.DataSource,
LabelColumn = "name",
PriorityColumn = "population",
PriorityDelegate = delegate(SharpMap.Data.FeatureDataRow fdr)
{
System.Int32 retVal = 10000000 * ( (System.String)fdr["capital"] == "Y" ? 1 : 0 );
return retVal + System.Convert.ToInt32(fdr["population"]);
},
TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias,
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias,
LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection,
Style = new SharpMap.Styles.LabelStyle
{
ForeColor = System.Drawing.Color.Black,
Font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 11),
MaxVisible = layLabel.MinVisible,
HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Left,
VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Bottom,
Offset = new System.Drawing.PointF(3, 3),
Halo = new System.Drawing.Pen(System.Drawing.Color.Yellow, 2),
CollisionDetection = true
}
};
var layRiverLabels = new SharpMap.Layers.LabelLayer("RiverLabels");
layRiverLabels.DataSource = layRivers.DataSource;
layRiverLabels.LabelColumn = "Name";
layRiverLabels.PriorityDelegate = GetRiverLength;
layRiverLabels.Style = new SharpMap.Styles.LabelStyle
{
Font = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold),
Halo = new System.Drawing.Pen(System.Drawing.Color.Azure, 2),
ForeColor = System.Drawing.Color.DarkCyan,
IgnoreLength = false,
Enabled = true,
CollisionDetection = true,
};
layRiverLabels.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
//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(layRiverLabels);
map.Layers.Add(layLabel);
map.Layers.Add(layCityLabel);
//limit the zoom to 360 degrees width
map.MaximumZoom = 360;
map.BackColor = System.Drawing.Color.LightBlue;
map.ZoomToExtents(); // = 360;
var mat = new System.Drawing.Drawing2D.Matrix();
mat.RotateAt(angle, map.WorldToImage(map.Center));
map.MapTransform = mat;
return map;
}
#if !OPENGLSAMPLE
internal static SharpMap.Map InitializeMap(float angle, string[] filenames)
{
if (filenames == null)
return null;
var map = new SharpMap.Map();
try
{
foreach (var filename in filenames)
{
var connectionString = string.Format("Data Source={0}", filename);
foreach (var provider in SharpMap.Data.Providers.SpatiaLite.GetSpatialTables(connectionString))
{
map.Layers.Add(
new SharpMap.Layers.VectorLayer(
string.Format("{0} - {1}", provider.Table, provider.GeometryColumn), provider) { Style = LayerTools.GetRandomVectorStyle() });
}
}
if (map.Layers.Count > 0)
{
map.ZoomToExtents();
System.Drawing.Drawing2D.Matrix mat = new System.Drawing.Drawing2D.Matrix();
mat.RotateAt(angle, map.WorldToImage(map.Center));
map.MapTransform = mat;
return map;
}
}
catch (System.Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
}
return null;
}
#endif
internal static int GetRiverLength(SharpMap.Data.FeatureDataRow row)
{
if (row == null) return 0;
if (row.Geometry == null)
return 0;
var ml = row.Geometry as GeoAPI.Geometries.IMultiLineString;
if (ml != null)
return System.Convert.ToInt32(ml.Length);
var l = row.Geometry as GeoAPI.Geometries.ILineString;
if (l != null)
return System.Convert.ToInt32(l.Length);
return 0;
}
}
}