-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDataConverter.cs
More file actions
72 lines (61 loc) · 2.88 KB
/
DataConverter.cs
File metadata and controls
72 lines (61 loc) · 2.88 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
/*
* This file is part of SharpMap.Demo.FormatConverter
* SharpMap.Demo.FormatConverter is free software © 2008 Newgrove Consultants Limited,
* http://www.newgrove.com; you can redistribute it and/or modify it under the terms
* of the current GNU Lesser General Public License (LGPL) as published by and
* available from the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA: http://fsf.org/
* This program is distributed without any warranty;
* without even the implied warranty of merchantability or fitness for purpose.
* See the GNU Lesser General Public License for the full details.
*
* Author: John Diss 2008
*
*/
using System;
using System.Data;
using GeoAPI.Geometries;
namespace SharpMap.Data
{
public class DataConverter<TSource, TTarget>
{
private readonly Func<TSource, TTarget> _conversionDelegate;
private readonly int _oidColumnIndex;
private readonly FeatureDataTable<TTarget> _target;
public DataConverter(IFeatureDataRecord source, int oidColumnIndex, IGeometryFactory geometryFactory)
: this(
delegate(TSource o) { return (TTarget) Convert.ChangeType(o, typeof (TTarget)); }, source, oidColumnIndex, geometryFactory)
{
}
public DataConverter(Func<TSource, TTarget> conversionDlgt, IFeatureDataRecord source, int oidColumnIndex, IGeometryFactory geometryFactory)
{
_conversionDelegate = conversionDlgt;
_oidColumnIndex = oidColumnIndex;
string oidColName = source.GetName(oidColumnIndex);
FeatureDataTable<TTarget> trgt = new FeatureDataTable<TTarget>("SchemaTable", oidColName,
geometryFactory);
for (int i = 0; i < source.FieldCount; i++)
if (i != oidColumnIndex)
trgt.Columns.Add(new DataColumn(source.GetName(i), source.GetFieldType(i)));
_target = trgt;
}
public FeatureDataTable<TTarget> Model
{
get { return _target; }
}
public FeatureDataRow<TTarget> ConvertRow(IFeatureDataRecord source)
{
if (typeof(TSource) == typeof(TTarget) && source is FeatureDataRow)
return source as FeatureDataRow<TTarget>;
TTarget oid = _conversionDelegate((TSource)source.GetOid());
FeatureDataRow<TTarget> row = _target.NewRow(oid);
object[] vals = new object[source.FieldCount];
vals[_oidColumnIndex] = oid;
source.GetValues(vals);
row.ItemArray = vals;
//row.Geometry = source.Geometry.IsValid ? source.Geometry : source.Geometry.Buffer(0.0);
row.Geometry = source.Geometry;
return row;
}
}
}