forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServer2008.cs
More file actions
182 lines (155 loc) · 8.3 KB
/
SqlServer2008.cs
File metadata and controls
182 lines (155 loc) · 8.3 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
using System;
using GeoAPI.Geometries;
using NUnit.Framework;
using Microsoft.SqlServer.Types;
using SharpMap.Converters.SqlServer2008SpatialObjects;
using SharpMap.Converters.WellKnownText;
using SharpMap.Geometries;
using SharpMap.Data.Providers;
namespace UnitTests.Converters
{
[TestFixture]
#if LINUX
[Ignore("Requires SqlServerSpatial")]
#endif
public class SqlServer2008
{
private const string Point = "POINT (20.564 46.3493254)";
private const string Multipoint = "MULTIPOINT (20.564 46.3493254, 45 32, 23 54)";
private const string Linestring = "LINESTRING (20 20, 20 30, 30 30, 30 20, 40 20)";
private const string MultiLinestring =
"MULTILINESTRING ((10 10, 40 50), (20 20, 30 20), (21 21, 50 20, 50 60, 21 21))";
private const string Polygon = "POLYGON ((20 20, 20 30, 30 30, 30 20, 20 20), (21 21, 21 29, 29 29, 29 21, 21 21))";
private const string MultiPolygon = "MULTIPOLYGON (((20 20, 20 30, 30 30, 30 20, 20 20)), ((41 41, 41 49, 49 49, 49 41, 41 41)))";
[OneTimeSetUp]
public void SetupFixture()
{
GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
//SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
}
private IGeometry ToSqlServerAndBack(IGeometry gIn, SqlServerSpatialObjectType spatialType)
{
Assert.That(gIn, Is.Not.Null);
//Assert.That(gIn.SRID, Is.EqualTo(-1));
//gIn.SRID = 0;
Assert.That(gIn.SRID, Is.GreaterThan(0));
switch (spatialType)
{
case SqlServerSpatialObjectType.Geography:
SqlGeography sqlGeography = SqlGeographyConverter.ToSqlGeography(gIn);
//if (gIn is NetTopologySuite.Geometries.Polygon || gIn is NetTopologySuite.Geometries.MultiPolygon)
//{
// sqlGeography.ReorientObject().MakeValid();
//}
return SqlGeographyConverter.ToSharpMapGeometry(sqlGeography, new NetTopologySuite.Geometries.GeometryFactory());
default:
SqlGeometry sqlGeometry = SqlGeometryConverter.ToSqlGeometry(gIn);
return SqlGeometryConverter.ToSharpMapGeometry(sqlGeometry, new NetTopologySuite.Geometries.GeometryFactory());
}
}
[NUnit.Framework.TestCase(SqlServerSpatialObjectType.Geometry)]
[NUnit.Framework.TestCase(SqlServerSpatialObjectType.Geography)]
public void ConvertAndBack(SqlServerSpatialObjectType spatialType)
{
int srid = 4326;
//Prepare data
var gPn = GeometryFromWKT.Parse(Point);
var gMp = GeometryFromWKT.Parse(Multipoint);
var gLi = GeometryFromWKT.Parse(Linestring);
var gML = GeometryFromWKT.Parse(MultiLinestring);
var gPl = GeometryFromWKT.Parse(Polygon);
var gMPol = GeometryFromWKT.Parse(MultiPolygon);
// Geography requires valid SRID
gPn.SRID = srid;
gMp.SRID = srid;
gLi.SRID = srid;
gML.SRID = srid;
gPl.SRID = srid;
gMPol.SRID = srid;
var comparison = new Comparison<IGeometry>((u, v) => u.EqualsExact(v) ? 0 : 1);
Assert.That(ToSqlServerAndBack(gPn, spatialType), Is.EqualTo(gPn).Using(comparison));
Assert.That(ToSqlServerAndBack(gMp, spatialType), Is.EqualTo(gMp).Using(comparison));
Assert.That(ToSqlServerAndBack(gLi, spatialType), Is.EqualTo(gLi).Using(comparison));
Assert.That(ToSqlServerAndBack(gML, spatialType), Is.EqualTo(gML).Using(comparison));
Assert.That(ToSqlServerAndBack(gPl, spatialType), Is.EqualTo(gPl).Using(comparison));
Assert.That(ToSqlServerAndBack(gMPol, spatialType), Is.EqualTo(gMPol).Using(comparison));
}
[NUnit.Framework.TestCase(SqlServerSpatialObjectType.Geometry, 30)]
[NUnit.Framework.TestCase(SqlServerSpatialObjectType.Geography, 1111120)]
public void Operations(SqlServerSpatialObjectType spatialType, double bufferDist)
{
int srid = (spatialType == SqlServerSpatialObjectType.Geometry ? 0 : 4326);
//Prepare data
var gPn = GeometryFromWKT.Parse(Point);
gPn.SRID = srid;
var gMp = GeometryFromWKT.Parse(Multipoint);
gMp.SRID = srid;
var gLi = GeometryFromWKT.Parse(Linestring);
gLi.SRID = srid;
var gML = GeometryFromWKT.Parse(MultiLinestring);
gML.SRID = srid;
var gPl = GeometryFromWKT.Parse(Polygon);
gPl.SRID = srid;
System.Diagnostics.Trace.WriteLine(spatialType.ToString());
if (spatialType == SqlServerSpatialObjectType.Geography)
System.Diagnostics.Trace.WriteLine("SqlServer syntax (STGeomFromText does not require .ReorientObject()): SELECT geography::STGeomFromText(' insert WKT '), 4326)");
var gPnBuffer30 = SpatialOperationsEx.Buffer(gPn, bufferDist, spatialType);
System.Diagnostics.Trace.WriteLine(gPnBuffer30.ToString());
var gPnBuffer30IntersectiongPl = SpatialOperationsEx.Intersection(gPnBuffer30, gPl, spatialType);
System.Diagnostics.Trace.WriteLine(gPnBuffer30IntersectiongPl.ToString());
var gUnion = SpatialOperationsEx.Union(gPn, spatialType, gMp, gML, gLi, gPl);
System.Diagnostics.Trace.WriteLine(gUnion.ToString());
}
[Test]
public void FailingConversionGeom()
{
//Prepare data
const string invalidMultiPolygon = "MULTIPOLYGON (((20 20, 20 30, 30 30, 30 20, 20 20)), ((21 21, 21 29, 29 29, 29 21, 21 21)))";
var gMP = GeometryFromWKT.Parse(invalidMultiPolygon);
gMP.SRID = 4326;
Assert.Throws<SqlGeometryConverterException>(() => gMP = ToSqlServerAndBack(gMP, SqlServerSpatialObjectType.Geometry));
}
[Test]
public void FailingConversionGeog()
{
//Prepare data
const string invalidMultiPolygon = "MULTIPOLYGON (((20 20, 20 30, 30 30, 30 20, 20 20)), ((21 21, 21 29, 29 29, 29 21, 21 21)))";
var gMP = GeometryFromWKT.Parse(invalidMultiPolygon);
gMP.SRID = 4326;
Assert.Throws<SqlGeographyConverterException>(() => gMP = ToSqlServerAndBack(gMP, SqlServerSpatialObjectType.Geography));
}
[NUnit.Framework.TestCase(SqlServerSpatialObjectType.Geometry)]
[NUnit.Framework.TestCase(SqlServerSpatialObjectType.Geography)]
public void TestShapeFile(SqlServerSpatialObjectType spatialType)
{
using (var p = new ShapeFile(TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"), true))
{
p.Open();
var env = p.GetExtents();
if (spatialType == SqlServerSpatialObjectType.Geography && (env.MaxY > 90 || env.MaxY < -90))
Assert.Ignore("Test file Y values exceed valid latitudes");
for (uint i = 0; i < p.GetFeatureCount(); i++)
{
var fdr = p.GetFeature(i);
if (fdr.Geometry == null)
continue;
try
{
fdr.Geometry.SRID = 4326;
var res = ToSqlServerAndBack(fdr.Geometry, spatialType);
Assert.AreEqual(fdr.Geometry, res);
System.Diagnostics.Trace.WriteLine(string.Format("Feature {0} ({1}) converted!", i, fdr[0]));
}
catch (SqlGeometryConverterException)
{
System.Diagnostics.Trace.WriteLine(string.Format("Feature {0} ({1}) conversion failed!", i, fdr[0]));
}
catch (SqlGeographyConverterException)
{
System.Diagnostics.Trace.WriteLine(string.Format("Feature {0} ({1}) conversion failed!", i, fdr[0]));
}
}
}
}
}
}