forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTablePointTests.cs
More file actions
187 lines (157 loc) · 7.34 KB
/
DataTablePointTests.cs
File metadata and controls
187 lines (157 loc) · 7.34 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
// Copyright 2007 - 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.Data;
using NUnit.Framework;
using SharpMap.Data;
using SharpMap.Data.Providers;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
namespace UnitTests.Data.Providers
{
public class DataTablePointTests : ProviderTest
{
internal static DataTable CreateDataTableSource()
{
var source = new DataTable("PointSource", "http://www.codeplex.com/SharpMap/v1/UnitTests");
DataColumn pk;
source.Columns.AddRange(new[]
{
pk = new DataColumn("oid", typeof (uint)),
new DataColumn("name", typeof (string)),
new DataColumn("x", typeof (double)),
new DataColumn("y", typeof (double))
});
var rnd = new Random(17);
for (int i = 0; i < 100; i++)
{
DataRow row = source.NewRow();
row["oid"] = i;
row["name"] = "Feature #" + i;
row["x"] = rnd.NextDouble()*1000;
row["y"] = rnd.NextDouble()*1000;
source.Rows.Add(row);
}
source.PrimaryKey = new[] {pk};
return source;
}
[Test]
public void CreateDataTablePoints()
{
DataTable source = CreateDataTableSource();
DataTablePoint provider = new DataTablePoint(source, "oid", "x", "y");
Assert.IsNotNull(provider);
Assert.AreSame(source, provider.Table);
Assert.AreEqual("oid", provider.ObjectIdColumn);
Assert.AreEqual("x", provider.XColumn);
Assert.AreEqual("y", provider.YColumn);
}
[Test]
public void ExecuteIntersectionQueryReturnsExpectedFeatures()
{
DataTable source = CreateDataTableSource();
DataTablePoint provider = new DataTablePoint(source, "oid", "x", "y");
var query = new Envelope(400, 600, 400, 600);
FeatureDataTable expected = new FeatureDataTable();
expected.TableName = "PointSource";
foreach (DataColumn column in source.Columns)
{
expected.Columns.Add(column.ColumnName, column.DataType);
}
foreach (DataRowView rowView in source.DefaultView)
{
if (query.Contains(new Coordinate((double) rowView["x"], (double) rowView["y"])))
{
expected.ImportRow(rowView.Row);
}
}
FeatureDataSet dataSet = new FeatureDataSet();
provider.ExecuteIntersectionQuery(query, dataSet);
Assert.IsNotNull(dataSet);
Assert.IsNotNull(dataSet.Tables);
Assert.AreEqual(1, dataSet.Tables.Count);
FeatureDataTable actual = dataSet.Tables[0];
Assert.AreEqual(expected.Rows.Count, actual.Rows.Count);
foreach (DataRowView expectedRowView in expected.DefaultView)
{
DataRow[] actualRows = actual.Select("oid = " + expectedRowView["oid"]);
Assert.AreEqual(1, actualRows.Length);
Assert.AreEqual(expectedRowView["oid"], actualRows[0]["oid"]);
Assert.AreEqual(expectedRowView["x"], actualRows[0]["x"]);
Assert.AreEqual(expectedRowView["y"], actualRows[0]["y"]);
}
}
[Test]
public void GetExtentsComputation()
{
DataTable source = CreateDataTableSource();
DataTablePoint provider = new DataTablePoint(source, "oid", "x", "y");
double minX = Double.PositiveInfinity,
minY = Double.PositiveInfinity,
maxX = Double.NegativeInfinity,
maxY = Double.NegativeInfinity;
foreach (DataRowView rowView in source.DefaultView)
{
if (minX > (double) rowView["x"]) minX = (double) rowView["x"];
if (minY > (double) rowView["y"]) minY = (double) rowView["y"];
if (maxX < (double) rowView["x"]) maxX = (double) rowView["x"];
if (maxY < (double) rowView["y"]) maxY = (double) rowView["y"];
}
Envelope expectedBounds = new Envelope(minX, maxX, minY, maxY);
Envelope actualBounds = provider.GetExtents();
Assert.AreEqual(expectedBounds, actualBounds);
}
[Test]
public void GetGeometryByIDReturnsExpectedGeometry()
{
DataTable source = CreateDataTableSource();
DataTablePoint provider = new DataTablePoint(source, "oid", "x", "y");
DataRow row = source.Select("oid = 43")[0];
Point expected = new Point((double) row["x"], (double) row["y"]);
IGeometry actual = provider.GetGeometryByID(43);
Assert.IsNotNull(actual);
Assert.IsTrue(actual is Point);
Assert.AreEqual(expected, actual);
}
[Test]
public void GetFeatureReturnsExpectedFeature()
{
DataTable source = CreateDataTableSource();
DataTablePoint provider = new DataTablePoint(source, "oid", "x", "y");
DataRow row = source.Select("oid = 43")[0];
Point expected = new Point((double)row["x"], (double)row["y"]);
var actual = provider.GetFeature(43);
Assert.That(actual, Is.Not.Null, "actual != null");
Assert.That(actual.ItemArray.Length, Is.EqualTo(4), "actual.ItemArray.Length == 4");
Assert.That(actual[0], Is.EqualTo(row[0]));
Assert.That(actual[1], Is.EqualTo(row[1]));
Assert.That(actual[2], Is.EqualTo(row[2]));
Assert.That(actual[3], Is.EqualTo(row[3]));
Assert.That(actual.Geometry, Is.EqualTo(expected));
}
[Test]
public void OpenAndCloseUpdatesIsOpenPropertyCorrectly()
{
DataTable source = CreateDataTableSource();
DataTablePoint provider = new DataTablePoint(source, "oid", "x", "y");
Assert.AreEqual(false, provider.IsOpen);
provider.Open();
Assert.AreEqual(true, provider.IsOpen);
provider.Close();
Assert.AreEqual(false, provider.IsOpen);
}
}
}