Skip to content

Commit 8863dc7

Browse files
committed
Add and wire ReadGeocentricCoordinateSystem
* add unit test Closes #32
1 parent 5b9c5c1 commit 8863dc7

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

ProjNet.Tests/WKT/WKTCoordSysParserTests.cs

+83
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GeoAPI.CoordinateSystems;
66
using GeoAPI.CoordinateSystems.Transformations;
77
using NUnit.Framework;
8+
using NUnit.Framework.Constraints;
89
using ProjNet.CoordinateSystems;
910
using ProjNet.CoordinateSystems.Transformations;
1011

@@ -323,5 +324,87 @@ public void ParseFittedCoordinateSystemWkt ()
323324
Assert.AreEqual ("EPSG", fcs.BaseCoordinateSystem.Authority);
324325
Assert.AreEqual (31467, fcs.BaseCoordinateSystem.AuthorityCode);
325326
}
327+
328+
[Test]
329+
public void TestGeocentricCoordinateSystem()
330+
{
331+
var fac = new CoordinateSystemFactory();
332+
IGeocentricCoordinateSystem fcs = null;
333+
334+
string wkt = "GEOCCS[\"TUREF\", " +
335+
"DATUM[\"Turkish_National_Reference_Frame\", SPHEROID[\"GRS 1980\", 6378137, 298.257222101, AUTHORITY[\"EPSG\", \"7019\"]], AUTHORITY[\"EPSG\", \"1057\"]], " +
336+
"PRIMEM[\"Greenwich\", 0, AUTHORITY[\"EPSG\", \"8901\"]], " +
337+
"UNIT[\"metre\", 1, AUTHORITY[\"EPSG\", \"9001\"]], " +
338+
"AXIS[\"Geocentric X\", OTHER], AXIS[\"Geocentric Y\", OTHER], AXIS[\"Geocentric Z\", NORTH], " +
339+
"AUTHORITY[\"EPSG\", \"5250\"]]";
340+
341+
try
342+
{
343+
fcs = fac.CreateFromWkt(wkt) as IGeocentricCoordinateSystem;
344+
}
345+
catch (Exception ex)
346+
{
347+
Assert.Fail("Could not create fitted coordinate system from:\r\n" + wkt + "\r\n" + ex.Message);
348+
}
349+
350+
Assert.That(fcs, Is.Not.Null);
351+
Assert.That(CheckInfo(fcs, "TUREF", "EPSG", 5250L));
352+
Assert.That(CheckHorizontalDatum(fcs.HorizontalDatum, "Turkish_National_Reference_Frame", "EPSG", 1057L), Is.True);
353+
Assert.That(CheckEllipsoid(fcs.HorizontalDatum.Ellipsoid, "GRS 1980", 6378137, 298.257222101, "EPSG", 7019), Is.True);
354+
Assert.That(CheckPrimem(fcs.PrimeMeridian, "Greenwich", 0, "EPSG", 8901L), Is.True);
355+
Assert.That(CheckUnit(fcs.PrimeMeridian.AngularUnit, "degree", null, null, null), Is.True);
356+
Assert.That(CheckUnit(fcs.LinearUnit, "metre", 1, "EPSG", 9001L), Is.True);
357+
358+
Assert.That(fcs.Authority, Is.EqualTo("EPSG"));
359+
Assert.That(fcs.AuthorityCode, Is.EqualTo(5250L));
360+
}
361+
362+
private bool CheckPrimem(IPrimeMeridian primeMeridian, string name, double? longitude, string authority, long? code)
363+
{
364+
Assert.That(primeMeridian, Is.Not.Null);
365+
Assert.That(CheckInfo(primeMeridian, name, authority, code));
366+
Assert.That(primeMeridian.Longitude, Is.EqualTo(longitude));
367+
return true;
368+
}
369+
370+
private static bool CheckUnit(IUnit unit, string name, double? value, string authority, long? code)
371+
{
372+
Assert.That(unit, Is.Not.Null);
373+
Assert.That(CheckInfo(unit, name, authority, code));
374+
if (!value.HasValue) return true;
375+
if (unit is ILinearUnit lunit)
376+
Assert.That(lunit.MetersPerUnit, Is.EqualTo(value));
377+
else if (unit is IAngularUnit aunit)
378+
Assert.That(aunit.RadiansPerUnit, Is.EqualTo(value));
379+
return true;
380+
}
381+
382+
private static bool CheckEllipsoid(IEllipsoid ellipsoid, string name, double? semiMajor, double? inverseFlattening, string authority, long? code)
383+
{
384+
Assert.That(ellipsoid, Is.Not.Null);
385+
Assert.That(CheckInfo(ellipsoid, name, authority, code));
386+
if (semiMajor.HasValue) Assert.That(ellipsoid.SemiMajorAxis, Is.EqualTo(semiMajor));
387+
if (inverseFlattening.HasValue) Assert.That(ellipsoid.InverseFlattening, Is.EqualTo(inverseFlattening));
388+
389+
return true;
390+
}
391+
392+
private static bool CheckHorizontalDatum(IHorizontalDatum datum, string name, string authority, long? code)
393+
{
394+
Assert.That(datum, Is.Not.Null);
395+
Assert.That(CheckInfo(datum, name,authority, code), Is.True);
396+
397+
return true;
398+
}
399+
400+
private static bool CheckInfo(IInfo info, string name, string authority, long? code)
401+
{
402+
Assert.That(info, Is.Not.Null);
403+
if (!string.IsNullOrEmpty(name)) Assert.That(info.Name, Is.EqualTo(name));
404+
if (!string.IsNullOrEmpty(authority)) Assert.That(info.Authority, Is.EqualTo(authority));
405+
if (code.HasValue) Assert.That(info.AuthorityCode, Is.EqualTo(code));
406+
407+
return true;
408+
}
326409
}
327410
}

ProjNet/IO/CoordinateSystems/CoordinateSystemWktReader.cs

+55-1
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,10 @@ private static ICoordinateSystem ReadCoordinateSystem(string coordinateSystem, W
206206
return ReadProjectedCoordinateSystem(tokenizer);
207207
case "FITTED_CS":
208208
return ReadFittedCoordinateSystem (tokenizer);
209+
case "GEOCCS":
210+
return ReadGeocentricCoordinateSystem(tokenizer);
209211
case "COMPD_CS":
210212
case "VERT_CS":
211-
case "GEOCCS":
212213
case "LOCAL_CS":
213214
throw new NotSupportedException(String.Format("{0} coordinate system is not supported.", coordinateSystem));
214215
default:
@@ -389,6 +390,58 @@ private static IProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStrea
389390
return projectedCS;
390391
}
391392

393+
private static IGeocentricCoordinateSystem ReadGeocentricCoordinateSystem(WktStreamTokenizer tokenizer)
394+
{
395+
/*
396+
* GEOCCS["<name>", <datum>, <prime meridian>, <linear unit> {,<axis>, <axis>, <axis>} {,<authority>}]
397+
*/
398+
399+
tokenizer.ReadToken("[");
400+
string name = tokenizer.ReadDoubleQuotedWord();
401+
tokenizer.ReadToken(",");
402+
tokenizer.ReadToken("DATUM");
403+
var horizontalDatum = ReadHorizontalDatum(tokenizer);
404+
tokenizer.ReadToken(",");
405+
tokenizer.ReadToken("PRIMEM");
406+
var primeMeridian = ReadPrimeMeridian(tokenizer);
407+
tokenizer.ReadToken(",");
408+
tokenizer.ReadToken("UNIT");
409+
var linearUnit = ReadLinearUnit(tokenizer);
410+
411+
string authority = String.Empty;
412+
long authorityCode = -1;
413+
tokenizer.NextToken();
414+
415+
var info = new List<AxisInfo>(3);
416+
if (tokenizer.GetStringValue() == ",")
417+
{
418+
tokenizer.NextToken();
419+
while (tokenizer.GetStringValue() == "AXIS")
420+
{
421+
info.Add(ReadAxis(tokenizer));
422+
tokenizer.NextToken();
423+
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
424+
}
425+
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
426+
if (tokenizer.GetStringValue() == "AUTHORITY")
427+
{
428+
tokenizer.ReadAuthority(ref authority, ref authorityCode);
429+
tokenizer.ReadToken("]");
430+
}
431+
}
432+
433+
//This is default axis values if not specified.
434+
if (info.Count == 0)
435+
{
436+
info.Add(new AxisInfo("Geocentric X", AxisOrientationEnum.Other));
437+
info.Add(new AxisInfo("Geocentric Y", AxisOrientationEnum.Other));
438+
info.Add(new AxisInfo("Geocentric Z", AxisOrientationEnum.North));
439+
}
440+
441+
return new GeocentricCoordinateSystem(horizontalDatum, linearUnit, primeMeridian, info, name, authority, authorityCode,
442+
string.Empty, string.Empty, string.Empty);
443+
}
444+
392445
private static IGeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStreamTokenizer tokenizer)
393446
{
394447
/*
@@ -432,6 +485,7 @@ private static IGeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStr
432485
tokenizer.ReadToken("]");
433486
}
434487
}
488+
435489
//This is default axis values if not specified.
436490
if (info.Count == 0)
437491
{

0 commit comments

Comments
 (0)