FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SharpMap/Examples/WinFormSamples/Samples/ShapefileSample.cs at main · SharpMap/SharpMap · GitHub
SharpMap
SharpMap
Repository navigation
Code
Issues
42
(42)
Pull requests
6
(6)
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
SharpMap
/
Examples
/
WinFormSamples
/
Samples
/
ShapefileSample.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
428 lines (381 loc) · 20.6 KB
Breadcrumbs
SharpMap
/
Examples
/
WinFormSamples
/
Samples
/
ShapefileSample.cs
Copy path
File metadata and controls
428 lines (381 loc) · 20.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
.
Collections
.
Generic
;
using
System
.
Drawing
;
using
System
.
Drawing
.
Drawing2D
;
using
System
.
Drawing
.
Text
;
using
SharpMap
;
using
SharpMap
.
Data
;
using
SharpMap
.
Data
.
Providers
;
using
SharpMap
.
Layers
;
using
SharpMap
.
Rendering
;
using
SharpMap
.
Rendering
.
Symbolizer
;
using
SharpMap
.
Rendering
.
Thematics
;
using
SharpMap
.
Styles
;
using
Point
=
GeoAPI
.
Geometries
.
Coordinate
;
namespace
WinFormSamples
.
Samples
{
public
static
partial
class
ShapefileSample
{
private
static
int
_mapId
=
0
;
public
static
Map
InitializeMap
(
float
angle
)
{
switch
(
_mapId
)
{
case
0
:
_mapId
++
;
return
InitializeMapOrig
(
angle
)
;
case
1
:
_mapId
++
;
return
InitializeMapOsm
(
angle
)
;
case
2
:
_mapId
++
;
return
InitializeMapOsm
(
angle
)
;
//return InitializeMapSharpDX(angle);
case
3
:
_mapId
-=
3
;
return
InitializeMapWithSymbolizerLayers
(
angle
)
;
default
:
_mapId
=
0
;
return
InitializeMapOrig
(
angle
)
;
}
}
private
static
Map
InitializeMapWithSymbolizerLayers
(
float
angle
)
{
//Initialize a new map of size 'imagesize'
Map
map
=
new
Map
(
)
;
//Set up the countries layer
var
layCountries
=
new
SharpMap
.
Layers
.
Symbolizer
.
PolygonalVectorLayer
(
"Countries"
,
new
ShapeFile
(
"GeoData/World/countries.shp"
,
true
)
,
new
BasicPolygonSymbolizer
{
Fill
=
new
SolidBrush
(
Color
.
Green
)
,
Outline
=
Pens
.
Black
,
}
)
{
SRID
=
4326
}
;
//Set up a river layer
var
symbolizer
=
new
CachedLineSymbolizer
(
)
;
symbolizer
.
LineSymbolizeHandlers
.
AddRange
(
new
[
]
{
new
PlainLineSymbolizeHandler
{
Line
=
new
Pen
(
Color
.
Blue
,
3
)
{
LineJoin
=
LineJoin
.
Round
}
}
,
new
PlainLineSymbolizeHandler
{
Line
=
new
Pen
(
Color
.
Aqua
,
1
)
}
,
}
)
;
var
layRivers
=
new
SharpMap
.
Layers
.
Symbolizer
.
LinealVectorLayer
(
"Rivers"
)
{
//Set the datasource to a shapefile in the App_data folder
DataSource
=
new
ShapeFile
(
"GeoData/World/rivers.shp"
,
true
)
,
//Define a blue 2px wide pen
Symbolizer
=
symbolizer
,
SRID
=
4326
}
;
//Set up a cities layer
var
layCities
=
new
SharpMap
.
Layers
.
Symbolizer
.
PuntalVectorLayer
(
"Cities"
)
{
//Set the datasource to a shapefile in the App_data folder
DataSource
=
new
ShapeFile
(
"GeoData/World/cities.shp"
,
true
)
,
Symbolizer
=
new
RasterPointSymbolizer
(
)
{
Scale
=
0.8f
}
,
MaxVisible
=
40
}
;
//Set up a country label layer
var
layLabel
=
new
LabelLayer
(
"Country labels"
)
{
DataSource
=
layCountries
.
DataSource
,
Enabled
=
true
,
LabelColumn
=
"Name"
,
LabelFilter
=
LabelCollisionDetection
.
QuickAccurateCollisionDetectionMethod
,
Style
=
new
LabelStyle
{
ForeColor
=
Color
.
White
,
Font
=
new
Font
(
FontFamily
.
GenericSerif
,
12
)
,
BackColor
=
new
SolidBrush
(
Color
.
FromArgb
(
128
,
255
,
0
,
0
)
)
,
HorizontalAlignment
=
LabelStyle
.
HorizontalAlignmentEnum
.
Center
}
,
MaxVisible
=
90
,
MinVisible
=
30
,
SRID
=
4326
,
MultipartGeometryBehaviour
=
LabelLayer
.
MultipartGeometryBehaviourEnum
.
Largest
,
}
;
//Set up a city label layer
var
layCityLabel
=
new
LabelLayer
(
"City labels"
)
{
DataSource
=
layCities
.
DataSource
,
Enabled
=
true
,
LabelColumn
=
"Name"
,
TextRenderingHint
=
TextRenderingHint
.
AntiAlias
,
SmoothingMode
=
SmoothingMode
.
AntiAlias
,
SRID
=
4326
,
LabelFilter
=
LabelCollisionDetection
.
QuickAccurateCollisionDetectionMethod
,
Style
=
new
LabelStyle
{
ForeColor
=
Color
.
Black
,
Font
=
new
Font
(
FontFamily
.
GenericSerif
,
11
)
,
HorizontalAlignment
=
LabelStyle
.
HorizontalAlignmentEnum
.
Left
,
VerticalAlignment
=
LabelStyle
.
VerticalAlignmentEnum
.
Bottom
,
Offset
=
new
PointF
(
3
,
3
)
,
CollisionDetection
=
true
,
Halo
=
new
Pen
(
Color
.
Yellow
,
2
)
}
,
MaxVisible
=
layLabel
.
MinVisible
,
}
;
//Setup River label
var
layRiverLabel
=
new
LabelLayer
(
"River labels"
)
{
DataSource
=
layRivers
.
DataSource
,
Enabled
=
true
,
LabelColumn
=
"Name"
,
TextRenderingHint
=
TextRenderingHint
.
AntiAlias
,
SmoothingMode
=
SmoothingMode
.
AntiAlias
,
SRID
=
4326
,
LabelFilter
=
LabelCollisionDetection
.
QuickAccurateCollisionDetectionMethod
,
MultipartGeometryBehaviour
=
LabelLayer
.
MultipartGeometryBehaviourEnum
.
All
,
Style
=
new
LabelStyle
{
ForeColor
=
Color
.
DarkBlue
,
Font
=
new
Font
(
FontFamily
.
GenericSansSerif
,
11
)
,
HorizontalAlignment
=
LabelStyle
.
HorizontalAlignmentEnum
.
Center
,
VerticalAlignment
=
LabelStyle
.
VerticalAlignmentEnum
.
Middle
,
CollisionDetection
=
true
,
Halo
=
new
Pen
(
Color
.
Azure
,
2
)
,
IgnoreLength
=
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
(
layRiverLabel
)
;
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
)
;
Matrix
mat
=
new
Matrix
(
)
;
mat
.
RotateAt
(
angle
,
map
.
WorldToImage
(
map
.
Center
)
)
;
map
.
MapTransform
=
mat
;
return
map
;
}
private
static
Map
InitializeMapOrig
(
float
angle
)
{
//Initialize a new map of size 'imagesize'
Map
map
=
new
Map
(
)
;
map
.
SRID
=
4326
;
//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
(
"GeoData/World/countries.shp"
,
true
)
;
//Set fill-style to green
layCountries
.
Style
.
Fill
=
new
SolidBrush
(
Color
.
FromArgb
(
64
,
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
(
"GeoData/World/rivers.shp"
,
true
)
;
//Define a blue 1px wide pen
layRivers
.
Style
.
Line
=
new
Pen
(
Color
.
Blue
,
1
)
;
layRivers
.
SRID
=
4326
;
//Set up a cities layer
VectorLayer
layCities
=
new
VectorLayer
(
"Cities"
)
;
//Set the datasource to a shapefile in the App_data folder
layCities
.
DataSource
=
new
ShapeFile
(
"GeoData/World/cities.shp"
,
true
)
;
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
;
layLabel
.
LabelFilter
=
LabelCollisionDetection
.
ThoroughCollisionDetection
;
layLabel
.
Style
.
CollisionDetection
=
true
;
layLabel
.
LabelPositionDelegate
=
fdr
=>
fdr
.
Geometry
.
InteriorPoint
.
Coordinate
;
layLabel
.
PriorityColumn
=
"POPDENS"
;
//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
;
layCityLabel
.
PriorityColumn
=
"POPULATION"
;
layCityLabel
.
Theme
=
new
GradientTheme
(
layCityLabel
.
PriorityColumn
,
250000
,
5000000
,
new
LabelStyle
{
MaxVisible
=
10
,
CollisionBuffer
=
new
Size
(
0
,
0
)
,
CollisionDetection
=
true
,
Enabled
=
true
,
ForeColor
=
Color
.
LightSlateGray
,
Halo
=
new
Pen
(
Color
.
Silver
,
1
)
,
HorizontalAlignment
=
LabelStyle
.
HorizontalAlignmentEnum
.
Center
,
VerticalAlignment
=
LabelStyle
.
VerticalAlignmentEnum
.
Middle
,
Font
=
new
Font
(
GenericFontFamilies
.
SansSerif
.
ToString
(
)
,
8f
,
FontStyle
.
Regular
)
}
,
new
LabelStyle
{
MaxVisible
=
layLabel
.
MinVisible
,
CollisionBuffer
=
new
Size
(
3
,
3
)
,
CollisionDetection
=
true
,
Enabled
=
true
,
ForeColor
=
Color
.
LightSlateGray
,
Halo
=
new
Pen
(
Color
.
Silver
,
5
)
,
HorizontalAlignment
=
LabelStyle
.
HorizontalAlignmentEnum
.
Center
,
VerticalAlignment
=
LabelStyle
.
VerticalAlignmentEnum
.
Middle
,
Font
=
new
Font
(
GenericFontFamilies
.
SansSerif
.
ToString
(
)
,
16f
,
FontStyle
.
Bold
)
}
)
;
bool
ignoreLength
=
false
;
var
layRiverLabel
=
new
LabelLayer
(
"River labels"
)
{
DataSource
=
layRivers
.
DataSource
,
Enabled
=
true
,
LabelColumn
=
"Name"
,
TextRenderingHint
=
TextRenderingHint
.
AntiAlias
,
SmoothingMode
=
SmoothingMode
.
AntiAlias
,
SRID
=
4326
,
LabelFilter
=
LabelCollisionDetection
.
QuickAccurateCollisionDetectionMethod
,
MultipartGeometryBehaviour
=
LabelLayer
.
MultipartGeometryBehaviourEnum
.
CommonCenter
,
Style
=
new
LabelStyle
{
ForeColor
=
Color
.
DarkBlue
,
Font
=
new
Font
(
FontFamily
.
GenericSansSerif
,
11
)
,
HorizontalAlignment
=
LabelStyle
.
HorizontalAlignmentEnum
.
Center
,
VerticalAlignment
=
LabelStyle
.
VerticalAlignmentEnum
.
Middle
,
CollisionDetection
=
true
,
Halo
=
new
Pen
(
Color
.
Azure
,
2
)
,
IgnoreLength
=
ignoreLength
,
Offset
=
new
PointF
(
0
,
-
10
)
}
,
}
;
//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.BackgroundLayer.Add(AsyncLayerProxyLayer.Create(layCountries));
map
.
Layers
.
Add
(
layCountries
)
;
map
.
Layers
.
Add
(
layRivers
)
;
map
.
Layers
.
Add
(
layCities
)
;
map
.
Layers
.
Add
(
layLabel
)
;
map
.
Layers
.
Add
(
layCityLabel
)
;
map
.
Layers
.
Add
(
layRiverLabel
)
;
//limit the zoom to 360 degrees width
map
.
MaximumZoom
=
360
;
map
.
BackColor
=
Color
.
LightBlue
;
map
.
Zoom
=
360
;
map
.
Center
=
new
Point
(
0
,
0
)
;
Matrix
mat
=
new
Matrix
(
)
;
mat
.
RotateAt
(
angle
,
map
.
WorldToImage
(
map
.
Center
)
)
;
map
.
MapTransform
=
mat
;
return
map
;
}
/*
private static Map InitializeMapSharpDX(float angle)
{
//Initialize a new map of size 'imagesize'
Map map = new Map();
//Set up the countries layer
VectorLayer layCountries = new SharpDXVectorLayer("Countries");
//Set the datasource to a shapefile in the App_data folder
layCountries.DataSource = new ShapeFile("GeoData/World/countries.shp", true);
//Set fill-style to green
layCountries.Style.Fill = new SolidBrush(Color.FromArgb(64, Color.LimeGreen));
//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 SharpDXVectorLayer("Rivers");
//Set the datasource to a shapefile in the App_data folder
layRivers.DataSource = new ShapeFile("GeoData/World/rivers.shp", true);
//Define a blue 1px wide pen
layRivers.Style.Line = new Pen(Color.Blue, 1);
layRivers.SRID = 4326;
//Set up a cities layer
VectorLayer layCities = new SharpDXVectorLayer("Cities");
//Set the datasource to a shapefile in the App_data folder
layCities.DataSource = new ShapeFile("GeoData/World/cities.shp", true);
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.BackgroundLayer.Add(AsyncLayerProxyLayer.Create(layCountries));
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);
Matrix mat = new Matrix();
mat.RotateAt(angle, map.WorldToImage(map.Center));
map.MapTransform = mat;
return map;
}
*/
internal
static
Map
InitializeMap
(
float
angle
,
string
[
]
filenames
)
{
if
(
filenames
==
null
)
return
null
;
var
providers
=
new
SharpMap
.
Data
.
Providers
.
ShapeFile
[
filenames
.
Length
]
;
for
(
int
i
=
0
;
i
<
filenames
.
Length
;
i
++
)
{
providers
[
i
]
=
new
ShapeFile
(
filenames
[
i
]
,
true
,
false
)
;
providers
[
i
]
.
Open
(
)
;
}
var
map
=
LayerTools
.
GetMapForProviders
(
providers
)
;
Matrix
mat
=
new
Matrix
(
)
;
mat
.
RotateAt
(
angle
,
map
.
WorldToImage
(
map
.
Center
)
)
;
map
.
MapTransform
=
mat
;
return
map
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL