forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapSerialization.cs
More file actions
150 lines (132 loc) · 5.73 KB
/
MapSerialization.cs
File metadata and controls
150 lines (132 loc) · 5.73 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Net;
using System.Xml.Serialization;
using SharpMap.Serialization.Model;
namespace SharpMap.Serialization
{
public static class MapSerialization
{
/// <summary>
/// Parses a Map from a MapDocument
/// </summary>
/// <param name="s">Instance of <see cref="Stream"/></param>
/// <returns>Map Document accodring to the MapDocument</returns>
public static Map LoadMapFromStream(Stream s)
{
XmlSerializer ser = new XmlSerializer(typeof(MapDefinition));
MapDefinition md = (MapDefinition)ser.Deserialize(s);
Map m = new Map();
if (md.Extent != null)
m.ZoomToBox(new GeoAPI.Geometries.Envelope(md.Extent.Xmin, md.Extent.Xmax, md.Extent.Ymin, md.Extent.Ymax));
if (!string.IsNullOrEmpty(md.BackGroundColor))
{
m.BackColor = ColorTranslator.FromHtml(md.BackGroundColor);
}
m.SRID = md.SRID;
foreach (var l in md.Layers)
{
SharpMap.Layers.ILayer lay = null;
//WMSLayer?
if (l is WmsLayer)
{
ICredentials cred = null;
if (!string.IsNullOrEmpty((l as WmsLayer).WmsUser))
cred = new NetworkCredential((l as WmsLayer).WmsUser, (l as WmsLayer).WmsPassword);
SharpMap.Layers.WmsLayer wmsl = new Layers.WmsLayer(l.Name, (l as WmsLayer).OnlineURL, TimeSpan.MaxValue, WebRequest.DefaultWebProxy, cred);
if ((l as WmsLayer).WmsLayers != null)
{
string[] layers = (l as WmsLayer).WmsLayers.Split(',');
foreach (var wl in layers)
{
wmsl.AddLayer(wl);
}
}
else
{
wmsl.AddChildLayers(wmsl.RootLayer,true);
}
lay = wmsl;
}
//And some simple tiled layers
else if (l is OsmLayer)
{
var ol = (OsmLayer) l;
BruTile.Predefined.KnownTileSource kts;
if (!Enum.TryParse(ol.KnownTileSource, out kts))
kts = BruTile.Predefined.KnownTileSource.OpenStreetMap;
if (ol.Async)
lay = new Layers.TileAsyncLayer(BruTile.Predefined.KnownTileSources.Create(kts, ol.ApiKey), l.Name);
else
lay = new Layers.TileLayer(BruTile.Predefined.KnownTileSources.Create(kts, ol.ApiKey), l.Name);
}
/*
else if (l is GoogleLayer)
{
lay = new Layers.TileLayer(new BruTile.Web.GoogleTileSource(BruTile.Web.GoogleMapType.GoogleMap), l.Name);
}
else if (l is GoogleSatLayer)
{
lay = new Layers.TileLayer(new BruTile.Web.GoogleTileSource(BruTile.Web.GoogleMapType.GoogleSatellite), l.Name);
}
else if (l is GoogleTerrainLayer)
{
lay = new Layers.TileLayer(new BruTile.Web.GoogleTileSource(BruTile.Web.GoogleMapType.GoogleTerrain), l.Name);
}
*/
if (lay != null)
{
lay.MinVisible = l.MinVisible;
lay.MaxVisible = l.MaxVisible;
m.Layers.Add(lay);
}
}
return m;
}
public static void SaveMapToStream(Map m, Stream s)
{
MapDefinition md = new MapDefinition();
md.Extent = new Extent()
{
Xmin = m.Envelope.MinX,
Xmax = m.Envelope.MaxX,
Ymin = m.Envelope.MinY,
Ymax = m.Envelope.MaxY
};
md.BackGroundColor = ColorTranslator.ToHtml(m.BackColor);
md.SRID = m.SRID;
List<MapLayer> layers = new List<MapLayer>();
foreach (var layer in m.Layers)
{
MapLayer ml = null;
if (layer is SharpMap.Layers.VectorLayer)
{
}
else if (layer is SharpMap.Layers.WmsLayer)
{
WmsLayer sl = new WmsLayer();
sl.OnlineURL = (layer as SharpMap.Layers.WmsLayer).CapabilitiesUrl;
sl.WmsLayers = string.Join(",", (layer as SharpMap.Layers.WmsLayer).LayerList.ToArray());
if ((layer as SharpMap.Layers.WmsLayer).Credentials is NetworkCredential)
{
sl.WmsUser = ((layer as SharpMap.Layers.WmsLayer).Credentials as NetworkCredential).UserName;
sl.WmsPassword = ((layer as SharpMap.Layers.WmsLayer).Credentials as NetworkCredential).Password;
}
ml = sl;
}
ml.MinVisible = layer.MinVisible;
ml.MaxVisible = layer.MaxVisible;
ml.Name = layer.LayerName;
if (ml != null)
layers.Add(ml);
}
md.Layers = layers.ToArray();
XmlSerializer serializer = new XmlSerializer(typeof(MapDefinition));
serializer.Serialize(s, md);
}
}
}