Skip to content

Commit 1f3b73d

Browse files
committed
Improve unit tests, update SRID.csv
1 parent 8863dc7 commit 1f3b73d

10 files changed

+5649
-2899
lines changed

ProjNet.Tests/CoordinateSystemServicesTest.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using NUnit.Framework;
99
using ProjNet.CoordinateSystems;
1010
using ProjNet.CoordinateSystems.Transformations;
11+
using ProjNET.Tests;
1112

1213
namespace ProjNet
1314
{
@@ -59,15 +60,15 @@ public void TestConstructorLoadCsv(string csvPath)
5960
private static IEnumerable<KeyValuePair<int, string>> LoadCsv(string csvPath)
6061
{
6162

62-
Console.WriteLine("Reading '{0}'.", csvPath);
63+
Console.WriteLine("Reading '{0}'.", csvPath ?? "SRID.csv from resources stream");
6364
var sw = new Stopwatch();
6465
sw.Start();
6566

66-
foreach (var sridWkt in UnitTests.SRIDReader.GetSrids())
67+
foreach (var sridWkt in SRIDReader.GetSrids())
6768
yield return new KeyValuePair<int, string>(sridWkt.WktId, sridWkt.Wkt);
6869

6970
sw.Stop();
70-
Console.WriteLine("Read '{1}' in {0:N0}ms", sw.ElapsedMilliseconds, csvPath);
71+
Console.WriteLine("Read '{1}' in {0:N0}ms", sw.ElapsedMilliseconds, csvPath ?? "SRID.csv from resources stream");
7172
}
7273

7374
private static IEnumerable<KeyValuePair<int, string>> LoadXml(string xmlPath)

ProjNet.Tests/CoordinateTransformTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using NUnit.Framework;
88
using ProjNet.CoordinateSystems;
99
using ProjNet.CoordinateSystems.Transformations;
10+
using ProjNET.Tests;
1011

1112
namespace ProjNet.UnitTests
1213
{

ProjNet.Tests/ProjNET.Tests.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
<PackageReference Include="GeoAPI.CoordinateSystems" Version="$(GeoAPIPackageReferenceVersion)" />
2020

2121
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
22+
23+
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
2224
<PackageReference Include="NUnit" Version="3.10.1" />
2325
<PackageReference Include="Npgsql" Version="4.0.5" />
2426
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />

ProjNet.Tests/SRID.csv

+5,265-2,654
Large diffs are not rendered by default.

ProjNet.Tests/SRIDReader.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.IO;
34
using System.Reflection;
45
using System.Text;
56
using GeoAPI.CoordinateSystems;
67
using ProjNet.CoordinateSystems;
78

8-
namespace ProjNet.UnitTests
9+
namespace ProjNET.Tests
910
{
1011
internal class SRIDReader
1112
{
13+
private static readonly Lazy<ICoordinateSystemFactory> CoordinateSystemFactory =
14+
new Lazy<ICoordinateSystemFactory>(() => new CoordinateSystemFactory());
15+
1216
public struct WktString {
1317
/// <summary>
1418
/// Well-known ID
@@ -26,7 +30,7 @@ public struct WktString {
2630
/// <returns>Enumerator</returns>
2731
public static IEnumerable<WktString> GetSrids(string filename = null)
2832
{
29-
Stream stream = string.IsNullOrWhiteSpace(filename)
33+
var stream = string.IsNullOrWhiteSpace(filename)
3034
? Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjNET.Tests.SRID.csv")
3135
: File.OpenRead(filename);
3236

@@ -49,17 +53,19 @@ public static IEnumerable<WktString> GetSrids(string filename = null)
4953
}
5054
}
5155
}
56+
5257
/// <summary>
5358
/// Gets a coordinate system from the SRID.csv file
5459
/// </summary>
5560
/// <param name="id">EPSG ID</param>
56-
/// <returns>Coordinate system, or null if SRID was not found.</returns>
57-
public static ICoordinateSystem GetCSbyID(int id)
61+
/// <param name="file">(optional) path to CSV File with WKT definitions.</param>
62+
/// <returns>Coordinate system, or <value>null</value> if no entry with <paramref name="id"/> was not found.</returns>
63+
public static ICoordinateSystem GetCSbyID(int id, string file = null)
5864
{
59-
ICoordinateSystemFactory factory = new CoordinateSystemFactory();
60-
foreach (var wkt in GetSrids(null))
65+
//ICoordinateSystemFactory factory = new CoordinateSystemFactory();
66+
foreach (var wkt in GetSrids(file))
6167
if (wkt.WktId == id)
62-
return factory.CreateFromWkt(wkt.Wkt);
68+
return CoordinateSystemFactory.Value.CreateFromWkt(wkt.Wkt);
6369
return null;
6470
}
6571
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
using System;
22
using System.Data;
3+
using System.IO;
4+
using GeoAPI.CoordinateSystems;
5+
using Newtonsoft.Json;
6+
using Newtonsoft.Json.Linq;
37
using Npgsql;
48
using NUnit.Framework;
5-
using ProjNet.Converters.WellKnownText;
69
using ProjNet.CoordinateSystems;
710

811
namespace ProjNet.UnitTests.Converters.WKT
912
{
1013
[TestFixture]
1114
public class SpatialRefSysTableParser
1215
{
13-
private const string ConnectionString =
14-
"Host=localhost;Port=5432;Database=postgis2;uid=postgres;pwd=1.Kennwort";
16+
private static string _connectionString;
1517

16-
[Test, Ignore("Run only if you have a PostGis server and have corrected the ConnectionString")]
17-
public void Test()
18+
private static readonly Lazy<ICoordinateSystemFactory> CoordinateSystemFactory =
19+
new Lazy<ICoordinateSystemFactory>(() => new CoordinateSystemFactory());
20+
21+
[Test]
22+
public void TestParsePostgisDefinitions()
1823
{
24+
if (string.IsNullOrWhiteSpace(ConnectionString))
25+
throw new IgnoreException("No Connection string provided or provided connection string invalid.");
1926

2027
using (NpgsqlConnection cn = new NpgsqlConnection(ConnectionString))
2128
{
@@ -26,19 +33,20 @@ public void Test()
2633
int counted = 0;
2734
int failed = 0;
2835
int tested = 0;
29-
using (NpgsqlDataReader r = cmd.ExecuteReader(CommandBehavior.CloseConnection))
36+
using (var r = cmd.ExecuteReader(CommandBehavior.CloseConnection))
3037
{
3138
if (r != null)
3239
{
3340
while (r.Read())
3441
{
3542
counted++;
43+
int srid = r.GetInt32(0);
3644
string srtext = r.GetString(1);
37-
if (!string.IsNullOrEmpty(srtext))
38-
{
39-
tested++;
40-
if (!TestParse(r.GetInt32(0), srtext)) failed++;
41-
}
45+
if (string.IsNullOrEmpty(srtext)) continue;
46+
if (srtext.StartsWith("COMPD_CS")) continue;
47+
48+
tested++;
49+
if (!TestParse(srid, srtext)) failed++;
4250
}
4351
}
4452
}
@@ -49,12 +57,76 @@ public void Test()
4957

5058
}
5159

60+
[Test]//, Ignore("Only run this if you want a new SRID.csv file")]
61+
public void TestCreateSridCsv()
62+
{
63+
if (string.IsNullOrWhiteSpace(ConnectionString))
64+
throw new IgnoreException("No Connection string provided or provided connection string invalid.");
65+
66+
if (File.Exists("SRID.csv")) File.Delete("SRID.csv");
67+
68+
using (var sw = new StreamWriter(File.OpenWrite("SRID.csv")))
69+
using (var cn = new NpgsqlConnection(ConnectionString))
70+
{
71+
cn.Open();
72+
var cm = cn.CreateCommand();
73+
cm.CommandText = "SELECT \"srid\", \"srtext\" FROM \"public\".\"spatial_ref_sys\" ORDER BY srid;";
74+
using (var dr = cm.ExecuteReader(CommandBehavior.SequentialAccess))
75+
{
76+
while (dr.Read())
77+
{
78+
int srid = dr.GetInt32(0);
79+
string srtext = dr.GetString(1);
80+
switch (srtext.Substring(0, srtext.IndexOf("[")))
81+
{
82+
case "PROJCS":
83+
case "GEOGCS":
84+
case "GEOCCS":
85+
sw.WriteLine($"{srid};{srtext}");
86+
break;
87+
}
88+
}
89+
}
90+
cm.Dispose();
91+
}
92+
}
93+
94+
private static string ConnectionString
95+
{
96+
get
97+
{
98+
if (!string.IsNullOrWhiteSpace(_connectionString))
99+
return _connectionString;
100+
101+
if (!File.Exists("appsettings.json"))
102+
return null;
103+
104+
JToken token = null;
105+
using (var jtr = new Newtonsoft.Json.JsonTextReader(new StreamReader("appsettings.json")))
106+
token = JToken.ReadFrom(jtr);
107+
108+
var connectionString = (string)token["ConnectionString"];
109+
try
110+
{
111+
using (var cn = new NpgsqlConnection(connectionString))
112+
cn.Open();
113+
}
114+
catch (Exception)
115+
{
116+
return null;
117+
}
118+
119+
_connectionString = connectionString;
120+
return _connectionString;
121+
122+
}
123+
}
124+
52125
private static bool TestParse(int srid, string srtext)
53126
{
54127
try
55128
{
56-
CoordinateSystemFactory factory = new CoordinateSystemFactory();
57-
factory.CreateFromWkt(srtext);
129+
CoordinateSystemFactory.Value.CreateFromWkt(srtext);
58130
//CoordinateSystemWktReader.Parse(srtext);
59131
return true;
60132
}
@@ -65,36 +137,6 @@ private static bool TestParse(int srid, string srtext)
65137
}
66138
}
67139

68-
[Test]
69-
public void TestSrOrg()
70-
{
71-
Assert.IsTrue(TestParse(1,
72-
"PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"Popular Visualisation CRS\",DATUM[\"Popular_Visualisation_Datum\",SPHEROID[\"Popular Visualisation Sphere\",6378137,0,AUTHORITY[\"EPSG\",\"7059\"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY[\"EPSG\",\"6055\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4055\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],AUTHORITY[\"EPSG\",\"3785\"],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]"));
73-
}
74140

75-
[Test]
76-
public void TestProjNetIssues()
77-
{
78-
Assert.IsTrue(TestParse(1,
79-
"PROJCS[\"International_Terrestrial_Reference_Frame_1992Lambert_Conformal_Conic_2SP\"," +
80-
"GEOGCS[\"GCS_International_Terrestrial_Reference_Frame_1992\"," +
81-
"DATUM[\"International_Terrestrial_Reference_Frame_1992\"," +
82-
"SPHEROID[\"GRS_1980\",6378137,298.257222101]," +
83-
"TOWGS84[0,0,0,0,0,0,0]]," +
84-
"PRIMEM[\"Greenwich\",0]," +
85-
"UNIT[\"Degree\",0.0174532925199433]]," +
86-
"PROJECTION[\"Lambert_Conformal_Conic_2SP\",AUTHORITY[\"EPSG\",\"9802\"]]," +
87-
"PARAMETER[\"Central_Meridian\",-102]," +
88-
"PARAMETER[\"Latitude_Of_Origin\",12]," +
89-
"PARAMETER[\"False_Easting\",2500000]," +
90-
"PARAMETER[\"False_Northing\",0]," +
91-
"PARAMETER[\"Standard_Parallel_1\",17.5]," +
92-
"PARAMETER[\"Standard_Parallel_2\",29.5]," +
93-
"PARAMETER[\"Scale_Factor\",1]," +
94-
"UNIT[\"Meter\",1,AUTHORITY[\"EPSG\",\"9001\"]]]"));
95-
96-
Assert.IsTrue(TestParse(2,
97-
"PROJCS[\"Google Maps Global Mercator\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_2SP\"],PARAMETER[\"standard_parallel_1\",0],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",0],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1],EXTENSION[\"PROJ4\",\"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs\"],AUTHORITY[\"EPSG\",\"900913\"]]"));
98-
}
99141
}
100142
}

0 commit comments

Comments
 (0)