-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFeatureDataSet.cs
More file actions
345 lines (296 loc) · 12.6 KB
/
FeatureDataSet.cs
File metadata and controls
345 lines (296 loc) · 12.6 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
// Portions copyright 2005 - 2006: Morten Nielsen (www.iter.dk)
// Portions copyright 2006 - 2008: Rory Plaire (codekaizen@gmail.com)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Serialization;
using System.Threading;
using System.Xml;
using System.Xml.Schema;
using GeoAPI.Geometries;
namespace SharpMap.Data
{
/// <summary>
/// Represents an in-memory cache of spatial data.
/// </summary>
/// <remarks>
/// The FeatureDataSet is an extension of System.Data.DataSet, and can be used
/// in much the same way.
/// </remarks>
[Serializable()]
public class FeatureDataSet : DataSet
{
#region Nested types
private delegate void SetDefaultViewManagerDelegate(FeatureDataSet dataSet, FeatureDataViewManager viewManager);
private delegate FeatureDataViewManager GetDefaultViewManagerDelegate(FeatureDataSet dataSet);
#endregion
#region Type fields
private static readonly SetDefaultViewManagerDelegate _setDefaultViewManager;
private static readonly GetDefaultViewManagerDelegate _getDefaultViewManager;
private static Int32 _nameSeries = -1;
#endregion
#region Static constructor
static FeatureDataSet()
{
// Create DefaultViewManager getter method
_getDefaultViewManager = generateGetDefaultViewManagerDelegate();
// Create DefaultViewManager setter method
_setDefaultViewManager = generateSetDefaultViewManagerDelegate();
}
#endregion
#region Instance fields
private FeatureTableCollection _featureTables;
private readonly Object _defaultViewManagerSync = new Object();
private Int32 _defaultViewManagerInitialized = 0;
private readonly IGeometryFactory _geoFactory;
#endregion
#region Object constructors
/// <summary>
/// Initializes a new instance of the FeatureDataSet class.
/// </summary>
public FeatureDataSet(IGeometryFactory geoFactory)
: this(generateName(), geoFactory) { }
/// <summary>
/// Initializes a new instance of the FeatureDataSet class with the given name.
/// </summary>
public FeatureDataSet(String name, IGeometryFactory geoFactory)
{
_geoFactory = geoFactory;
initClass(name);
CollectionChangeEventHandler schemaChangedHandler = schemaChanged;
//this.Tables.CollectionChanged += schemaChangedHandler;
Relations.CollectionChanged += schemaChangedHandler;
}
/// <summary>
/// Initializes a new instance of the FeatureDataSet class.
/// </summary>
/// <param name="info">Serialized info.</param>
/// <param name="context">Remoting context.</param>
protected FeatureDataSet(SerializationInfo info, StreamingContext context)
{
String schemaString = ((String) (info.GetValue("XmlSchema", typeof (String))));
if ((schemaString != null))
{
DataSet ds = new DataSet();
ds.ReadXmlSchema(new XmlTextReader(new StringReader(schemaString)));
if ((ds.Tables["FeatureTable"] != null))
{
Tables.Add(new FeatureDataTable(ds.Tables["FeatureTable"], _geoFactory));
}
DataSetName = ds.DataSetName;
Prefix = ds.Prefix;
Namespace = ds.Namespace;
Locale = ds.Locale;
CaseSensitive = ds.CaseSensitive;
EnforceConstraints = ds.EnforceConstraints;
Merge(ds, false, MissingSchemaAction.Add);
}
else
{
initClass("Unknown");
}
GetSerializationData(info, context);
CollectionChangeEventHandler schemaChangedHandler = schemaChanged;
//Tables.CollectionChanged += schemaChangedHandler;
Relations.CollectionChanged += schemaChangedHandler;
}
#endregion
public new FeatureDataViewManager DefaultViewManager
{
get
{
if (_defaultViewManagerInitialized == 0)
{
lock (_defaultViewManagerSync)
{
if (_defaultViewManagerInitialized == 0)
{
Interlocked.Increment(ref _defaultViewManagerInitialized);
// Read value to initialize base storage field.
DataViewManager temp = base.DefaultViewManager;
// Replace base storage field with subclass instance
_setDefaultViewManager(this, new FeatureDataViewManager(this, true));
// Get rid of initial instance, since we don't need it
temp.Dispose();
}
}
}
return _getDefaultViewManager(this);
}
}
public IExtents Extents
{
get
{
IExtents extents = null;
foreach (FeatureDataTable table in _featureTables)
{
if (extents == null)
{
extents = table.Extents;
}
else
{
extents.ExpandToInclude(table.Extents);
}
}
return extents;
}
}
/// <summary>
/// Gets the collection of tables contained in the FeatureDataSet.
/// </summary>
public new FeatureTableCollection Tables
{
get { return _featureTables; }
}
/// <summary>
/// Copies the structure of the FeatureDataSet,
/// including all FeatureDataTable schemas, relations,
/// and constraints. Does not copy any data.
/// </summary>
/// <returns></returns>
public new FeatureDataSet Clone()
{
FeatureDataSet copy = base.Clone() as FeatureDataSet;
return copy;
}
#region Overrides
/// <summary>
/// Gets a value indicating whether Tables property should be persisted.
/// </summary>
/// <returns></returns>
protected override Boolean ShouldSerializeTables()
{
// TODO: no clue what to do here...
return false;
}
/// <summary>
/// Gets a value indicating whether Relations property should be persisted.
/// </summary>
/// <returns></returns>
protected override Boolean ShouldSerializeRelations()
{
// TODO: no clue what to do here...
return false;
}
/// <summary>
///
/// </summary>
/// <param name="reader"></param>
protected override void ReadXmlSerializable(XmlReader reader)
{
Reset();
DataSet ds = new DataSet();
ds.ReadXml(reader);
//if ((ds.Tables["FeatureTable"] != null))
//{
// this.Tables.Add(new FeatureDataTable(ds.Tables["FeatureTable"]));
//}
DataSetName = ds.DataSetName;
Prefix = ds.Prefix;
Namespace = ds.Namespace;
Locale = ds.Locale;
CaseSensitive = ds.CaseSensitive;
EnforceConstraints = ds.EnforceConstraints;
Merge(ds, false, MissingSchemaAction.Add);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected override XmlSchema GetSchemaSerializable()
{
MemoryStream stream = new MemoryStream();
WriteXmlSchema(new XmlTextWriter(stream, null));
stream.Position = 0;
return XmlSchema.Read(new XmlTextReader(stream), null);
}
#endregion
#region Private static helper methods
private static String generateName()
{
String name = "FeatureDataSet";
Interlocked.Increment(ref _nameSeries);
if (_nameSeries > 0)
{
name += _nameSeries.ToString();
}
return name;
}
private static SetDefaultViewManagerDelegate generateSetDefaultViewManagerDelegate()
{
DynamicMethod set_DefaultViewManagerMethod = new DynamicMethod("set_DefaultViewManager_DynamicMethod",
null,
new Type[]
{
typeof (FeatureDataSet),
typeof (FeatureDataViewManager)
},
typeof (DataSet));
ILGenerator il = set_DefaultViewManagerMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Stfld,
typeof (DataSet).GetField("defaultViewManager", BindingFlags.Instance | BindingFlags.NonPublic));
return set_DefaultViewManagerMethod.CreateDelegate(typeof (SetDefaultViewManagerDelegate))
as SetDefaultViewManagerDelegate;
}
private static GetDefaultViewManagerDelegate generateGetDefaultViewManagerDelegate()
{
DynamicMethod get_DefaultViewManagerMethod = new DynamicMethod("get_DefaultViewManager_DynamicMethod",
typeof (FeatureDataViewManager),
new Type[] {typeof (FeatureDataSet)},
typeof (DataSet));
ILGenerator il = get_DefaultViewManagerMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldfld,
typeof (DataSet).GetField("defaultViewManager", BindingFlags.Instance | BindingFlags.NonPublic));
il.Emit(OpCodes.Ret);
return get_DefaultViewManagerMethod.CreateDelegate(typeof (GetDefaultViewManagerDelegate))
as GetDefaultViewManagerDelegate;
}
#endregion
#region Private helper methods
private void initClass(String name)
{
DataSetName = name;
_featureTables = new FeatureTableCollection(base.Tables);
Prefix = "";
Namespace = "http://www.codeplex.com/SharpMap/Wiki/View.aspx?title=FeatureDataSet";
Locale = new CultureInfo("en-US");
CaseSensitive = false;
EnforceConstraints = true;
}
private Boolean shouldSerializeFeatureTable()
{
return false;
}
private void schemaChanged(Object sender, CollectionChangeEventArgs e)
{
if (e.Action == CollectionChangeAction.Remove)
{
//this.InitVars();
}
}
#endregion
}
}