Skip to content

Commit

Permalink
Add and wire ReadGeocentricCoordinateSystem
Browse files Browse the repository at this point in the history
* add unit test

Closes #32
  • Loading branch information
FObermaier committed Mar 7, 2019
1 parent 5b9c5c1 commit 8863dc7
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
83 changes: 83 additions & 0 deletions ProjNet.Tests/WKT/WKTCoordSysParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GeoAPI.CoordinateSystems;
using GeoAPI.CoordinateSystems.Transformations;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;

Expand Down Expand Up @@ -323,5 +324,87 @@ public void ParseFittedCoordinateSystemWkt ()
Assert.AreEqual ("EPSG", fcs.BaseCoordinateSystem.Authority);
Assert.AreEqual (31467, fcs.BaseCoordinateSystem.AuthorityCode);
}

[Test]
public void TestGeocentricCoordinateSystem()
{
var fac = new CoordinateSystemFactory();
IGeocentricCoordinateSystem fcs = null;

string wkt = "GEOCCS[\"TUREF\", " +
"DATUM[\"Turkish_National_Reference_Frame\", SPHEROID[\"GRS 1980\", 6378137, 298.257222101, AUTHORITY[\"EPSG\", \"7019\"]], AUTHORITY[\"EPSG\", \"1057\"]], " +
"PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], " +
"UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], " +
"AXIS[\"Geocentric X\", OTHER], AXIS[\"Geocentric Y\", OTHER], AXIS[\"Geocentric Z\", NORTH], " +
"AUTHORITY[\"EPSG\", \"5250\"]]";

try
{
fcs = fac.CreateFromWkt(wkt) as IGeocentricCoordinateSystem;
}
catch (Exception ex)
{
Assert.Fail("Could not create fitted coordinate system from:\r\n" + wkt + "\r\n" + ex.Message);
}

Assert.That(fcs, Is.Not.Null);
Assert.That(CheckInfo(fcs, "TUREF", "EPSG", 5250L));
Assert.That(CheckHorizontalDatum(fcs.HorizontalDatum, "Turkish_National_Reference_Frame", "EPSG", 1057L), Is.True);
Assert.That(CheckEllipsoid(fcs.HorizontalDatum.Ellipsoid, "GRS 1980", 6378137, 298.257222101, "EPSG", 7019), Is.True);
Assert.That(CheckPrimem(fcs.PrimeMeridian, "Greenwich", 0, "EPSG", 8901L), Is.True);
Assert.That(CheckUnit(fcs.PrimeMeridian.AngularUnit, "degree", null, null, null), Is.True);
Assert.That(CheckUnit(fcs.LinearUnit, "metre", 1, "EPSG", 9001L), Is.True);

Assert.That(fcs.Authority, Is.EqualTo("EPSG"));
Assert.That(fcs.AuthorityCode, Is.EqualTo(5250L));
}

private bool CheckPrimem(IPrimeMeridian primeMeridian, string name, double? longitude, string authority, long? code)
{
Assert.That(primeMeridian, Is.Not.Null);
Assert.That(CheckInfo(primeMeridian, name, authority, code));
Assert.That(primeMeridian.Longitude, Is.EqualTo(longitude));
return true;
}

private static bool CheckUnit(IUnit unit, string name, double? value, string authority, long? code)
{
Assert.That(unit, Is.Not.Null);
Assert.That(CheckInfo(unit, name, authority, code));
if (!value.HasValue) return true;
if (unit is ILinearUnit lunit)
Assert.That(lunit.MetersPerUnit, Is.EqualTo(value));
else if (unit is IAngularUnit aunit)
Assert.That(aunit.RadiansPerUnit, Is.EqualTo(value));
return true;
}

private static bool CheckEllipsoid(IEllipsoid ellipsoid, string name, double? semiMajor, double? inverseFlattening, string authority, long? code)
{
Assert.That(ellipsoid, Is.Not.Null);
Assert.That(CheckInfo(ellipsoid, name, authority, code));
if (semiMajor.HasValue) Assert.That(ellipsoid.SemiMajorAxis, Is.EqualTo(semiMajor));
if (inverseFlattening.HasValue) Assert.That(ellipsoid.InverseFlattening, Is.EqualTo(inverseFlattening));

return true;
}

private static bool CheckHorizontalDatum(IHorizontalDatum datum, string name, string authority, long? code)
{
Assert.That(datum, Is.Not.Null);
Assert.That(CheckInfo(datum, name,authority, code), Is.True);

return true;
}

private static bool CheckInfo(IInfo info, string name, string authority, long? code)
{
Assert.That(info, Is.Not.Null);
if (!string.IsNullOrEmpty(name)) Assert.That(info.Name, Is.EqualTo(name));
if (!string.IsNullOrEmpty(authority)) Assert.That(info.Authority, Is.EqualTo(authority));
if (code.HasValue) Assert.That(info.AuthorityCode, Is.EqualTo(code));

return true;
}
}
}
56 changes: 55 additions & 1 deletion ProjNet/IO/CoordinateSystems/CoordinateSystemWktReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,10 @@ private static ICoordinateSystem ReadCoordinateSystem(string coordinateSystem, W
return ReadProjectedCoordinateSystem(tokenizer);
case "FITTED_CS":
return ReadFittedCoordinateSystem (tokenizer);
case "GEOCCS":
return ReadGeocentricCoordinateSystem(tokenizer);
case "COMPD_CS":
case "VERT_CS":
case "GEOCCS":
case "LOCAL_CS":
throw new NotSupportedException(String.Format("{0} coordinate system is not supported.", coordinateSystem));
default:
Expand Down Expand Up @@ -389,6 +390,58 @@ private static IProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStrea
return projectedCS;
}

private static IGeocentricCoordinateSystem ReadGeocentricCoordinateSystem(WktStreamTokenizer tokenizer)
{
/*
* GEOCCS["<name>", <datum>, <prime meridian>, <linear unit> {,<axis>, <axis>, <axis>} {,<authority>}]
*/

tokenizer.ReadToken("[");
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.ReadToken("DATUM");
var horizontalDatum = ReadHorizontalDatum(tokenizer);
tokenizer.ReadToken(",");
tokenizer.ReadToken("PRIMEM");
var primeMeridian = ReadPrimeMeridian(tokenizer);
tokenizer.ReadToken(",");
tokenizer.ReadToken("UNIT");
var linearUnit = ReadLinearUnit(tokenizer);

string authority = String.Empty;
long authorityCode = -1;
tokenizer.NextToken();

var info = new List<AxisInfo>(3);
if (tokenizer.GetStringValue() == ",")
{
tokenizer.NextToken();
while (tokenizer.GetStringValue() == "AXIS")
{
info.Add(ReadAxis(tokenizer));
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
}
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
if (tokenizer.GetStringValue() == "AUTHORITY")
{
tokenizer.ReadAuthority(ref authority, ref authorityCode);
tokenizer.ReadToken("]");
}
}

//This is default axis values if not specified.
if (info.Count == 0)
{
info.Add(new AxisInfo("Geocentric X", AxisOrientationEnum.Other));
info.Add(new AxisInfo("Geocentric Y", AxisOrientationEnum.Other));
info.Add(new AxisInfo("Geocentric Z", AxisOrientationEnum.North));
}

return new GeocentricCoordinateSystem(horizontalDatum, linearUnit, primeMeridian, info, name, authority, authorityCode,
string.Empty, string.Empty, string.Empty);
}

private static IGeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStreamTokenizer tokenizer)
{
/*
Expand Down Expand Up @@ -432,6 +485,7 @@ private static IGeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStr
tokenizer.ReadToken("]");
}
}

//This is default axis values if not specified.
if (info.Count == 0)
{
Expand Down

0 comments on commit 8863dc7

Please sign in to comment.