Skip to content

Commit

Permalink
* Allow round brackets
Browse files Browse the repository at this point in the history
refers to #94
  • Loading branch information
FObermaier committed Aug 17, 2021
1 parent ce85ea4 commit 2972eee
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PropertyGroup Label="Version numbers">
<!-- MAJOR, MINOR, and PATCH are defined according to SemVer 2.0.0. -->
<NtsMajorVersion>2</NtsMajorVersion>
<NtsMinorVersion>0</NtsMinorVersion>
<NtsMinorVersion>1</NtsMinorVersion>
<NtsPatchVersion>0</NtsPatchVersion>

<NtsBuildTimestamp>$([System.DateTime]::UtcNow.Ticks)</NtsBuildTimestamp>
Expand Down
80 changes: 52 additions & 28 deletions src/ProjNet/IO/CoordinateSystems/CoordinateSystemWktReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static IInfo Parse(string wkt)
/// <returns>An object that implements the IUnit interface.</returns>
private static IUnit ReadUnit(WktStreamTokenizer tokenizer)
{
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string unitName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
Expand All @@ -107,8 +107,11 @@ private static IUnit ReadUnit(WktStreamTokenizer tokenizer)
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
else
tokenizer.CheckCloser(bracket);

return new Unit(unitsPerUnit, unitName, authority, authorityCode, string.Empty, string.Empty, string.Empty);
}
/// <summary>
Expand All @@ -118,7 +121,8 @@ private static IUnit ReadUnit(WktStreamTokenizer tokenizer)
/// <returns>An object that implements the IUnit interface.</returns>
private static LinearUnit ReadLinearUnit(WktStreamTokenizer tokenizer)
{
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();

string unitName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
Expand All @@ -129,8 +133,11 @@ private static LinearUnit ReadLinearUnit(WktStreamTokenizer tokenizer)
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
else
tokenizer.CheckCloser(bracket);

return new LinearUnit(unitsPerUnit, unitName, authority, authorityCode, string.Empty, string.Empty, string.Empty);
}
/// <summary>
Expand All @@ -140,7 +147,8 @@ private static LinearUnit ReadLinearUnit(WktStreamTokenizer tokenizer)
/// <returns>An object that implements the IUnit interface.</returns>
private static AngularUnit ReadAngularUnit(WktStreamTokenizer tokenizer)
{
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();

string unitName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
Expand All @@ -151,7 +159,11 @@ private static AngularUnit ReadAngularUnit(WktStreamTokenizer tokenizer)
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
else
{
tokenizer.CheckCloser(bracket);
}
return new AngularUnit(unitsPerUnit, unitName, authority, authorityCode, string.Empty, string.Empty, string.Empty);
}
Expand All @@ -165,12 +177,12 @@ private static AxisInfo ReadAxis(WktStreamTokenizer tokenizer)
{
if (tokenizer.GetStringValue() != "AXIS")
tokenizer.ReadToken("AXIS");
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string axisName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
string unitname = tokenizer.GetStringValue();
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
switch (unitname.ToUpperInvariant())
{
case "DOWN": return new AxisInfo(axisName, AxisOrientationEnum.Down);
Expand Down Expand Up @@ -210,7 +222,7 @@ private static CoordinateSystem ReadCoordinateSystem(string coordinateSystem, Wk
private static Wgs84ConversionInfo ReadWGS84ConversionInfo(WktStreamTokenizer tokenizer)
{
//TOWGS84[0,0,0,0,0,0,0]
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
var info = new Wgs84ConversionInfo();
tokenizer.NextToken();
info.Dx = tokenizer.GetNumericValue();
Expand Down Expand Up @@ -244,14 +256,14 @@ private static Wgs84ConversionInfo ReadWGS84ConversionInfo(WktStreamTokenizer to
}
}
if (tokenizer.GetStringValue() != "]")
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
return info;
}

private static Ellipsoid ReadEllipsoid(WktStreamTokenizer tokenizer)
{
//SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]]
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
Expand All @@ -265,7 +277,7 @@ private static Ellipsoid ReadEllipsoid(WktStreamTokenizer tokenizer)
if (tokenizer.GetStringValue() == ",") //Read authority
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
var ellipsoid = new Ellipsoid(majorAxis, 0.0, e, true, LinearUnit.Metre, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);
return ellipsoid;
Expand All @@ -275,7 +287,7 @@ private static IProjection ReadProjection(WktStreamTokenizer tokenizer)
{
if (tokenizer.GetStringValue() != "PROJECTION")
tokenizer.ReadToken("PROJECTION");
tokenizer.ReadToken("[");//[
var bracket = tokenizer.ReadOpener();
string projectionName = tokenizer.ReadDoubleQuotedWord();
string authority = string.Empty;
long authorityCode = -1L;
Expand All @@ -284,20 +296,22 @@ private static IProjection ReadProjection(WktStreamTokenizer tokenizer)
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
else
tokenizer.CheckCloser(bracket);

tokenizer.ReadToken(",");//,
tokenizer.ReadToken("PARAMETER");
var paramList = new List<ProjectionParameter>();
while (tokenizer.GetStringValue() == "PARAMETER")
{
tokenizer.ReadToken("[");
bracket = tokenizer.ReadOpener();
string paramName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
double paramValue = tokenizer.GetNumericValue();
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
paramList.Add(new ProjectionParameter(paramName, paramValue));
//tokenizer.ReadToken(",");
//tokenizer.NextToken();
Expand Down Expand Up @@ -338,7 +352,7 @@ private static ProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStream
AUTHORITY["EPSG","27700"]
]
*/
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.ReadToken("GEOGCS");
Expand All @@ -356,7 +370,10 @@ private static ProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStream
switch (tokenizer.GetStringValue())
{
case ",":
break;
case "]":
case ")":
tokenizer.CheckCloser(bracket);
break;
case "PROJECTION":
projection = ReadProjection(tokenizer);
Expand All @@ -372,7 +389,7 @@ private static ProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStream
break;
case "AUTHORITY":
tokenizer.ReadAuthority(out authority, out authorityCode);
//tokenizer.ReadToken("]");
//tokenizer.ReadCloser(bracket);
break;
}
ct = tokenizer.NextToken();
Expand All @@ -394,7 +411,7 @@ private static GeocentricCoordinateSystem ReadGeocentricCoordinateSystem(WktStre
* GEOCCS["<name>", <datum>, <prime meridian>, <linear unit> {,<axis>, <axis>, <axis>} {,<authority>}]
*/

tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.ReadToken("DATUM");
Expand Down Expand Up @@ -424,7 +441,7 @@ private static GeocentricCoordinateSystem ReadGeocentricCoordinateSystem(WktStre
if (tokenizer.GetStringValue() == "AUTHORITY")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
}

Expand All @@ -451,7 +468,7 @@ private static GeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStre
AUTHORITY["EPSG","4277"]
]
*/
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.ReadToken("DATUM");
Expand Down Expand Up @@ -480,7 +497,7 @@ private static GeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStre
if (tokenizer.GetStringValue() == "AUTHORITY")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
}

Expand All @@ -502,7 +519,7 @@ private static HorizontalDatum ReadHorizontalDatum(WktStreamTokenizer tokenizer)
string authority = string.Empty;
long authorityCode = -1;

tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.ReadToken("SPHEROID");
Expand All @@ -519,7 +536,7 @@ private static HorizontalDatum ReadHorizontalDatum(WktStreamTokenizer tokenizer)
else if (tokenizer.GetStringValue() == "AUTHORITY")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
}
// make an assumption about the datum type.
Expand All @@ -531,7 +548,7 @@ private static HorizontalDatum ReadHorizontalDatum(WktStreamTokenizer tokenizer)
private static PrimeMeridian ReadPrimeMeridian(WktStreamTokenizer tokenizer)
{
//PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]]
tokenizer.ReadToken("[");
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
Expand All @@ -543,8 +560,11 @@ private static PrimeMeridian ReadPrimeMeridian(WktStreamTokenizer tokenizer)
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadToken("]");
tokenizer.ReadCloser(bracket);
}
else
tokenizer.CheckCloser(bracket);

// make an assumption about the Angular units - degrees.
var primeMeridian = new PrimeMeridian(longitude, AngularUnit.Degrees, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);

Expand Down Expand Up @@ -572,7 +592,7 @@ private static FittedCoordinateSystem ReadFittedCoordinateSystem (WktStreamToken
AUTHORITY["CUSTOM","12345"]
]
*/
tokenizer.ReadToken ("[");
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord ();
tokenizer.ReadToken (",");
tokenizer.ReadToken ("PARAM_MT");
Expand All @@ -590,11 +610,15 @@ private static FittedCoordinateSystem ReadFittedCoordinateSystem (WktStreamToken
switch (tokenizer.GetStringValue ())
{
case ",":
break;
case "]":
case ")":
tokenizer.CheckCloser(bracket);

break;
case "AUTHORITY":
tokenizer.ReadAuthority (out authority, out authorityCode);
//tokenizer.ReadToken("]");
//tokenizer.ReadCloser(bracket);
break;
}
ct = tokenizer.NextToken ();
Expand Down
62 changes: 60 additions & 2 deletions src/ProjNet/IO/CoordinateSystems/WKTStreamTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,64 @@ public string ReadDoubleQuotedWord()
return word;
}

/// <summary>
/// Reads an opener
/// </summary>
/// <param name="expectedBracket">The expected bracket type.</param>
/// <returns>The bracket type encountered</returns>
public WktBracket ReadOpener(WktBracket expectedBracket = WktBracket.DontCare)
{
NextToken();
string stringValue = GetStringValue();
if (stringValue == "[")
{
if (expectedBracket == WktBracket.Square || expectedBracket == WktBracket.DontCare)
return WktBracket.Square;
}
else if (stringValue == "(")
{
if (expectedBracket == WktBracket.Round || expectedBracket == WktBracket.DontCare)
return WktBracket.Round;
}

string expectedToken = expectedBracket == WktBracket.Square ? "[" : "(";
string s = string.Format(_nfi, "Expecting ('{3}') but got a '{0}' at line {1} column {2}.", stringValue, LineNumber, Column, expectedToken);
throw new ArgumentException(s);
}

/// <summary>
/// Reads an closer
/// </summary>
/// <param name="expectedBracket">The expected bracket type.</param>
public void ReadCloser(WktBracket expectedBracket)
{
NextToken();
CheckCloser(expectedBracket);
}

/// <summary>
/// Checks if the current token is a closer of expected type.
/// </summary>
/// <param name="expectedBracket">The expected bracket type.</param>
public void CheckCloser(WktBracket expectedBracket)
{
string stringValue = GetStringValue();
if (stringValue == "]")
{
if (expectedBracket == WktBracket.Square || expectedBracket == WktBracket.DontCare)
return;
}
else if (stringValue == ")")
{
if (expectedBracket == WktBracket.Round || expectedBracket == WktBracket.DontCare)
return;
}

string expectedToken = expectedBracket == WktBracket.Square ? "]" : ")";
string s = string.Format(_nfi, "Expecting ('{3}') but got a '{0}' at line {1} column {2}.", stringValue, LineNumber, Column, expectedToken);
throw new ArgumentException(s);
}

/// <summary>
/// Reads the authority and authority code.
/// </summary>
Expand All @@ -101,15 +159,15 @@ public void ReadAuthority(out string authority, out long authorityCode)
//AUTHORITY["EPGS","9102"]]
if (GetStringValue() != "AUTHORITY")
ReadToken("AUTHORITY");
ReadToken("[");
var bracket = ReadOpener();
authority = ReadDoubleQuotedWord();
ReadToken(",");
NextToken();
if (GetTokenType() == TokenType.Number)
authorityCode = (long) GetNumericValue();
else
long.TryParse(ReadDoubleQuotedWord(), NumberStyles.Any, _nfi, out authorityCode);
ReadToken("]");
ReadCloser(bracket);
}
}
}
8 changes: 8 additions & 0 deletions src/ProjNet/ProjNET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<RootNamespace>ProjNet</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<EnableApiCompat>true</EnableApiCompat>
</PropertyGroup>

<PropertyGroup Label="Assembly Info">
Expand All @@ -28,4 +29,11 @@ Proj.NET performs point-to-point coordinate conversions between geodetic coordin
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
</ItemGroup>

<ItemGroup Condition=" '$(EnableApiCompat)' == 'true' ">
<PackageReference Include="Microsoft.DotNet.ApiCompat" Version="6.0.0-beta.21159.11" PrivateAssets="All" />
<PackageDownload Include="ProjNet" Version="[2.0.0]" PrivateAssets="All" />

<ResolvedMatchingContract Include="$(NugetPackageRoot)projnet\2.0.0\lib\netstandard2.0\ProjNet.dll" />
</ItemGroup>

</Project>
Loading

0 comments on commit 2972eee

Please sign in to comment.