Skip to content

Commit

Permalink
Improve unit tests, update SRID.csv
Browse files Browse the repository at this point in the history
  • Loading branch information
FObermaier committed Mar 8, 2019
1 parent 8863dc7 commit 1f3b73d
Show file tree
Hide file tree
Showing 10 changed files with 5,649 additions and 2,899 deletions.
7 changes: 4 additions & 3 deletions ProjNet.Tests/CoordinateSystemServicesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using NUnit.Framework;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using ProjNET.Tests;

namespace ProjNet
{
Expand Down Expand Up @@ -59,15 +60,15 @@ public void TestConstructorLoadCsv(string csvPath)
private static IEnumerable<KeyValuePair<int, string>> LoadCsv(string csvPath)
{

Console.WriteLine("Reading '{0}'.", csvPath);
Console.WriteLine("Reading '{0}'.", csvPath ?? "SRID.csv from resources stream");
var sw = new Stopwatch();
sw.Start();

foreach (var sridWkt in UnitTests.SRIDReader.GetSrids())
foreach (var sridWkt in SRIDReader.GetSrids())
yield return new KeyValuePair<int, string>(sridWkt.WktId, sridWkt.Wkt);

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

private static IEnumerable<KeyValuePair<int, string>> LoadXml(string xmlPath)
Expand Down
1 change: 1 addition & 0 deletions ProjNet.Tests/CoordinateTransformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NUnit.Framework;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using ProjNET.Tests;

namespace ProjNet.UnitTests
{
Expand Down
2 changes: 2 additions & 0 deletions ProjNet.Tests/ProjNET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<PackageReference Include="GeoAPI.CoordinateSystems" Version="$(GeoAPIPackageReferenceVersion)" />

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />

<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="NUnit" Version="3.10.1" />
<PackageReference Include="Npgsql" Version="4.0.5" />
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" />
Expand Down
7,919 changes: 5,265 additions & 2,654 deletions ProjNet.Tests/SRID.csv

Large diffs are not rendered by default.

20 changes: 13 additions & 7 deletions ProjNet.Tests/SRIDReader.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using GeoAPI.CoordinateSystems;
using ProjNet.CoordinateSystems;

namespace ProjNet.UnitTests
namespace ProjNET.Tests
{
internal class SRIDReader
{
private static readonly Lazy<ICoordinateSystemFactory> CoordinateSystemFactory =
new Lazy<ICoordinateSystemFactory>(() => new CoordinateSystemFactory());

public struct WktString {
/// <summary>
/// Well-known ID
Expand All @@ -26,7 +30,7 @@ public struct WktString {
/// <returns>Enumerator</returns>
public static IEnumerable<WktString> GetSrids(string filename = null)
{
Stream stream = string.IsNullOrWhiteSpace(filename)
var stream = string.IsNullOrWhiteSpace(filename)
? Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjNET.Tests.SRID.csv")
: File.OpenRead(filename);

Expand All @@ -49,17 +53,19 @@ public static IEnumerable<WktString> GetSrids(string filename = null)
}
}
}

/// <summary>
/// Gets a coordinate system from the SRID.csv file
/// </summary>
/// <param name="id">EPSG ID</param>
/// <returns>Coordinate system, or null if SRID was not found.</returns>
public static ICoordinateSystem GetCSbyID(int id)
/// <param name="file">(optional) path to CSV File with WKT definitions.</param>
/// <returns>Coordinate system, or <value>null</value> if no entry with <paramref name="id"/> was not found.</returns>
public static ICoordinateSystem GetCSbyID(int id, string file = null)
{
ICoordinateSystemFactory factory = new CoordinateSystemFactory();
foreach (var wkt in GetSrids(null))
//ICoordinateSystemFactory factory = new CoordinateSystemFactory();
foreach (var wkt in GetSrids(file))
if (wkt.WktId == id)
return factory.CreateFromWkt(wkt.Wkt);
return CoordinateSystemFactory.Value.CreateFromWkt(wkt.Wkt);
return null;
}
}
Expand Down
128 changes: 85 additions & 43 deletions ProjNet.Tests/WKT/PostGisSpatialRefSysTableParserTest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
using System;
using System.Data;
using System.IO;
using GeoAPI.CoordinateSystems;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Npgsql;
using NUnit.Framework;
using ProjNet.Converters.WellKnownText;
using ProjNet.CoordinateSystems;

namespace ProjNet.UnitTests.Converters.WKT
{
[TestFixture]
public class SpatialRefSysTableParser
{
private const string ConnectionString =
"Host=localhost;Port=5432;Database=postgis2;uid=postgres;pwd=1.Kennwort";
private static string _connectionString;

[Test, Ignore("Run only if you have a PostGis server and have corrected the ConnectionString")]
public void Test()
private static readonly Lazy<ICoordinateSystemFactory> CoordinateSystemFactory =
new Lazy<ICoordinateSystemFactory>(() => new CoordinateSystemFactory());

[Test]
public void TestParsePostgisDefinitions()
{
if (string.IsNullOrWhiteSpace(ConnectionString))
throw new IgnoreException("No Connection string provided or provided connection string invalid.");

using (NpgsqlConnection cn = new NpgsqlConnection(ConnectionString))
{
Expand All @@ -26,19 +33,20 @@ public void Test()
int counted = 0;
int failed = 0;
int tested = 0;
using (NpgsqlDataReader r = cmd.ExecuteReader(CommandBehavior.CloseConnection))
using (var r = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
if (r != null)
{
while (r.Read())
{
counted++;
int srid = r.GetInt32(0);
string srtext = r.GetString(1);
if (!string.IsNullOrEmpty(srtext))
{
tested++;
if (!TestParse(r.GetInt32(0), srtext)) failed++;
}
if (string.IsNullOrEmpty(srtext)) continue;
if (srtext.StartsWith("COMPD_CS")) continue;

tested++;
if (!TestParse(srid, srtext)) failed++;
}
}
}
Expand All @@ -49,12 +57,76 @@ public void Test()

}

[Test]//, Ignore("Only run this if you want a new SRID.csv file")]
public void TestCreateSridCsv()
{
if (string.IsNullOrWhiteSpace(ConnectionString))
throw new IgnoreException("No Connection string provided or provided connection string invalid.");

if (File.Exists("SRID.csv")) File.Delete("SRID.csv");

using (var sw = new StreamWriter(File.OpenWrite("SRID.csv")))
using (var cn = new NpgsqlConnection(ConnectionString))
{
cn.Open();
var cm = cn.CreateCommand();
cm.CommandText = "SELECT \"srid\", \"srtext\" FROM \"public\".\"spatial_ref_sys\" ORDER BY srid;";
using (var dr = cm.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (dr.Read())
{
int srid = dr.GetInt32(0);
string srtext = dr.GetString(1);
switch (srtext.Substring(0, srtext.IndexOf("[")))
{
case "PROJCS":
case "GEOGCS":
case "GEOCCS":
sw.WriteLine($"{srid};{srtext}");
break;
}
}
}
cm.Dispose();
}
}

private static string ConnectionString
{
get
{
if (!string.IsNullOrWhiteSpace(_connectionString))
return _connectionString;

if (!File.Exists("appsettings.json"))
return null;

JToken token = null;
using (var jtr = new Newtonsoft.Json.JsonTextReader(new StreamReader("appsettings.json")))
token = JToken.ReadFrom(jtr);

var connectionString = (string)token["ConnectionString"];
try
{
using (var cn = new NpgsqlConnection(connectionString))
cn.Open();
}
catch (Exception)
{
return null;
}

_connectionString = connectionString;
return _connectionString;

}
}

private static bool TestParse(int srid, string srtext)
{
try
{
CoordinateSystemFactory factory = new CoordinateSystemFactory();
factory.CreateFromWkt(srtext);
CoordinateSystemFactory.Value.CreateFromWkt(srtext);
//CoordinateSystemWktReader.Parse(srtext);
return true;
}
Expand All @@ -65,36 +137,6 @@ private static bool TestParse(int srid, string srtext)
}
}

[Test]
public void TestSrOrg()
{
Assert.IsTrue(TestParse(1,
"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]]"));
}

[Test]
public void TestProjNetIssues()
{
Assert.IsTrue(TestParse(1,
"PROJCS[\"International_Terrestrial_Reference_Frame_1992Lambert_Conformal_Conic_2SP\"," +
"GEOGCS[\"GCS_International_Terrestrial_Reference_Frame_1992\"," +
"DATUM[\"International_Terrestrial_Reference_Frame_1992\"," +
"SPHEROID[\"GRS_1980\",6378137,298.257222101]," +
"TOWGS84[0,0,0,0,0,0,0]]," +
"PRIMEM[\"Greenwich\",0]," +
"UNIT[\"Degree\",0.0174532925199433]]," +
"PROJECTION[\"Lambert_Conformal_Conic_2SP\",AUTHORITY[\"EPSG\",\"9802\"]]," +
"PARAMETER[\"Central_Meridian\",-102]," +
"PARAMETER[\"Latitude_Of_Origin\",12]," +
"PARAMETER[\"False_Easting\",2500000]," +
"PARAMETER[\"False_Northing\",0]," +
"PARAMETER[\"Standard_Parallel_1\",17.5]," +
"PARAMETER[\"Standard_Parallel_2\",29.5]," +
"PARAMETER[\"Scale_Factor\",1]," +
"UNIT[\"Meter\",1,AUTHORITY[\"EPSG\",\"9001\"]]]"));

Assert.IsTrue(TestParse(2,
"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\"]]"));
}
}
}
Loading

0 comments on commit 1f3b73d

Please sign in to comment.