Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper handling of EXTENSION tag #111

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/ProjNet/CoordinateSystems/Projection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ public ProjectionParameter GetParameter(int index)
return _parameters[index];
}

/// <summary>
/// Adds a parameter to the parameter list
/// </summary>
/// <param name="name">The projection parameter name</param>
/// <param name="value">The value</param>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Projection.Parameters.Add(new ProjectionParameter(name, value)); and remove this method

internal void AddParameter(string name, double value)
{
var param = new ProjectionParameter(name, value);
_parameters.Add(param);
}

/// <summary>
/// Gets an named parameter of the projection.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/ProjNet/CoordinateSystems/Projections/MapProjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ public ProjectionParameter GetParameter(string name)
return _Parameters.Find(name);
}

/// <summary>
/// Adds a parameter to the parameter list
/// </summary>
internal void AddParameter(string name, double value)
Copy link
Member Author

@FObermaier FObermaier Dec 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed to get the solution to compile. The parameters need to be present when the class is constructed anyway, so adding parameters afterwards will not do any good.

{
_Parameters.Add(name, value);
}

/// <summary>
///
/// </summary>
Expand Down
190 changes: 118 additions & 72 deletions src/ProjNet/IO/CoordinateSystems/CoordinateSystemWktReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static IUnit ReadUnit(WktStreamTokenizer tokenizer)
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadCloser(bracket);
}
else
else
tokenizer.CheckCloser(bracket);

return new Unit(unitsPerUnit, unitName, authority, authorityCode, string.Empty, string.Empty, string.Empty);
Expand Down Expand Up @@ -192,6 +192,7 @@ private static AxisInfo ReadAxis(WktStreamTokenizer tokenizer)
case "SOUTH": return new AxisInfo(axisName, AxisOrientationEnum.South);
case "UP": return new AxisInfo(axisName, AxisOrientationEnum.Up);
case "WEST": return new AxisInfo(axisName, AxisOrientationEnum.West);
case "UNKNOWN": return new AxisInfo(axisName, AxisOrientationEnum.Other);
default:
throw new ArgumentException("Invalid axis name '" + unitname + "' in WKT");
}
Expand All @@ -206,7 +207,7 @@ private static CoordinateSystem ReadCoordinateSystem(string coordinateSystem, Wk
case "PROJCS":
return ReadProjectedCoordinateSystem(tokenizer);
case "FITTED_CS":
return ReadFittedCoordinateSystem (tokenizer);
return ReadFittedCoordinateSystem(tokenizer);
case "GEOCCS":
return ReadGeocentricCoordinateSystem(tokenizer);
case "COMPD_CS":
Expand Down Expand Up @@ -285,7 +286,7 @@ private static Ellipsoid ReadEllipsoid(WktStreamTokenizer tokenizer)
return ellipsoid;
}

private static IProjection ReadProjection(WktStreamTokenizer tokenizer)
private static Projection ReadProjection(WktStreamTokenizer tokenizer)
{
if (tokenizer.GetStringValue() != "PROJECTION")
tokenizer.ReadToken("PROJECTION");
Expand All @@ -303,34 +304,46 @@ private static IProjection ReadProjection(WktStreamTokenizer tokenizer)
else
tokenizer.CheckCloser(bracket);

tokenizer.ReadToken(",");//,
tokenizer.ReadToken("PARAMETER");
var paramList = new List<ProjectionParameter>();
while (tokenizer.GetStringValue() == "PARAMETER")
{
bracket = tokenizer.ReadOpener();
string paramName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
double paramValue = tokenizer.GetNumericValue();
tokenizer.ReadCloser(bracket);
paramList.Add(new ProjectionParameter(paramName, paramValue));
//tokenizer.ReadToken(",");
//tokenizer.NextToken();
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
{
tokenizer.NextToken();
}
else
{
break;
}
}

var projection = new Projection(projectionName, paramList, projectionName, authority, authorityCode, string.Empty, string.Empty, string.Empty);
return projection;
}

private static void ReadParamater(WktStreamTokenizer tokenizer, Projection projection)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo ReadParameter, argument projection could be of type ICollection<ProjectionParameter>

{
var bracket = tokenizer.ReadOpener();
string paramName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
double paramValue = tokenizer.GetNumericValue();
tokenizer.ReadCloser(bracket);

projection.AddParameter(paramName, paramValue);
}

private static void ReadExtension(WktStreamTokenizer tokenizer)
{
var bracket = tokenizer.ReadOpener();
string paramName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
if (paramName == "PROJ4")
{
string paramValue = tokenizer.ReadDoubleQuotedWord();
}
else
{
System.Diagnostics.Debug.WriteLine("What are you?");
}

tokenizer.ReadCloser(bracket);

System.Diagnostics.Debug.WriteLine("Now what do we do with you?");
}



private static ProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStreamTokenizer tokenizer)
{
/*PROJCS[
Expand All @@ -356,42 +369,64 @@ private static ProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStream
*/
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.ReadToken("GEOGCS");
var geographicCS = ReadGeographicCoordinateSystem(tokenizer);
tokenizer.ReadToken(",");
tokenizer.ReadToken("PROJECTION");
var projection = ReadProjection(tokenizer);
var unit = ReadLinearUnit(tokenizer);
tokenizer.NextToken();

GeographicCoordinateSystem geographicCS = null;
Projection projection = null;
LinearUnit unit = null;
var axisInfo = new List<AxisInfo>(2);
string authority = string.Empty;
long authorityCode = -1;

tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
while (tokenizer.GetStringValue() == ",")
{
tokenizer.NextToken();
while (tokenizer.GetStringValue() == "AXIS")
{
axisInfo.Add(ReadAxis(tokenizer));
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
}
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
if (tokenizer.GetStringValue() == "AUTHORITY")
string nextToken = tokenizer.GetStringValue();
switch (nextToken)
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadCloser(bracket);
case "AUTHORITY":
tokenizer.ReadAuthority(out authority, out authorityCode);
break;
case "AXIS":
axisInfo.Add(ReadAxis(tokenizer));
break;
case "GEOGCS":
geographicCS = ReadGeographicCoordinateSystem(tokenizer);
break;
case "PARAMETER":
ReadParamater(tokenizer, projection);
break;
case "PROJECTION":
projection = ReadProjection(tokenizer);
break;
case "UNIT":
unit = ReadLinearUnit(tokenizer);
break;
case "EXTENSION":
ReadExtension(tokenizer);
break;
default:
throw new ArgumentException("Token not found for {0]", nextToken);
}

tokenizer.NextToken();
}
//This is default axis values if not specified.

// we should be at the end already!?
tokenizer.CheckCloser(bracket);

if (axisInfo.Count == 0)
{
axisInfo.Add(new AxisInfo("X", AxisOrientationEnum.East));
axisInfo.Add(new AxisInfo("Y", AxisOrientationEnum.North));
}
var projectedCS = new ProjectedCoordinateSystem(geographicCS.HorizontalDatum, geographicCS, unit as LinearUnit, projection, axisInfo, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);
return projectedCS;

if (geographicCS != null)
{
return new ProjectedCoordinateSystem(geographicCS.HorizontalDatum, geographicCS, unit as LinearUnit, projection, axisInfo, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);
}

return null;
}

private static VerticalCoordinateSystem ReadVerticalCoordinateSystem(WktStreamTokenizer tokenizer)
Expand Down Expand Up @@ -451,10 +486,10 @@ private static CompoundCoordinateSystem ReadCompoundCoordinateSystem(WktStreamTo
long authorityCode = -1;
tokenizer.NextToken();

if ( tokenizer.GetStringValue() == ",")
if (tokenizer.GetStringValue() == ",")
{
tokenizer.NextToken();
if(tokenizer.GetStringValue() == "AUTHORITY")
if (tokenizer.GetStringValue() == "AUTHORITY")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
}
Expand Down Expand Up @@ -541,23 +576,27 @@ private static GeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStre
long authorityCode = -1;
tokenizer.NextToken();
var info = new List<AxisInfo>(2);
if (tokenizer.GetStringValue() == ",")
while (tokenizer.GetStringValue() == ",")
{
tokenizer.NextToken();
while (tokenizer.GetStringValue() == "AXIS")
string nextToken = tokenizer.GetStringValue();
if (nextToken == "AXIS")
{
info.Add(ReadAxis(tokenizer));
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
}
if (tokenizer.GetStringValue() == ",") tokenizer.NextToken();
if (tokenizer.GetStringValue() == "AUTHORITY")
else if (nextToken == "AUTHORITY")
{
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadCloser(bracket);
}

tokenizer.NextToken();
}

//We should be closed already
tokenizer.CheckCloser(bracket);
//if (tokenizer.GetStringValue() != "]")
// tokenizer.ReadCloser(bracket);

//This is default axis values if not specified.
if (info.Count == 0)
{
Expand All @@ -575,6 +614,8 @@ private static HorizontalDatum ReadHorizontalDatum(WktStreamTokenizer tokenizer)
Wgs84ConversionInfo wgsInfo = null;
string authority = string.Empty;
long authorityCode = -1;
string extension = string.Empty;
string extensionMethod = string.Empty;

var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord();
Expand All @@ -595,6 +636,11 @@ private static HorizontalDatum ReadHorizontalDatum(WktStreamTokenizer tokenizer)
tokenizer.ReadAuthority(out authority, out authorityCode);
tokenizer.ReadCloser(bracket);
}
else if (tokenizer.GetStringValue() == "EXTENSION")
{
tokenizer.ReadExtension(out extension, out extensionMethod);
tokenizer.ReadCloser(bracket);
}
}
// make an assumption about the datum type.
var horizontalDatum = new HorizontalDatum(ellipsoid, wgsInfo, DatumType.HD_Geocentric, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);
Expand All @@ -612,7 +658,7 @@ private static VerticalDatum ReadVerticalDatum(WktStreamTokenizer tokenizer)
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
var datumType = (DatumType) tokenizer.GetNumericValue();
var datumType = (DatumType)tokenizer.GetNumericValue();
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
{
Expand All @@ -623,7 +669,7 @@ private static VerticalDatum ReadVerticalDatum(WktStreamTokenizer tokenizer)
tokenizer.ReadCloser(bracket);
}
}
var verticalDatum = new VerticalDatum( datumType, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);
var verticalDatum = new VerticalDatum(datumType, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);

return verticalDatum;
}
Expand Down Expand Up @@ -654,7 +700,7 @@ private static PrimeMeridian ReadPrimeMeridian(WktStreamTokenizer tokenizer)
return primeMeridian;
}

private static FittedCoordinateSystem ReadFittedCoordinateSystem (WktStreamTokenizer tokenizer)
private static FittedCoordinateSystem ReadFittedCoordinateSystem(WktStreamTokenizer tokenizer)
{
/*
FITTED_CS[
Expand All @@ -676,21 +722,21 @@ private static FittedCoordinateSystem ReadFittedCoordinateSystem (WktStreamToken
]
*/
var bracket = tokenizer.ReadOpener();
string name = tokenizer.ReadDoubleQuotedWord ();
tokenizer.ReadToken (",");
tokenizer.ReadToken ("PARAM_MT");
var toBaseTransform = MathTransformWktReader.ReadMathTransform (tokenizer);
tokenizer.ReadToken (",");
tokenizer.NextToken ();
var baseCS = ReadCoordinateSystem (null, tokenizer);
string name = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.ReadToken("PARAM_MT");
var toBaseTransform = MathTransformWktReader.ReadMathTransform(tokenizer);
tokenizer.ReadToken(",");
tokenizer.NextToken();
var baseCS = ReadCoordinateSystem(null, tokenizer);

string authority = string.Empty;
long authorityCode = -1;

var ct = tokenizer.NextToken ();
var ct = tokenizer.NextToken();
while (ct != TokenType.Eol && ct != TokenType.Eof)
{
switch (tokenizer.GetStringValue ())
switch (tokenizer.GetStringValue())
{
case ",":
break;
Expand All @@ -700,14 +746,14 @@ private static FittedCoordinateSystem ReadFittedCoordinateSystem (WktStreamToken

break;
case "AUTHORITY":
tokenizer.ReadAuthority (out authority, out authorityCode);
tokenizer.ReadAuthority(out authority, out authorityCode);
//tokenizer.ReadCloser(bracket);
break;
}
ct = tokenizer.NextToken ();
ct = tokenizer.NextToken();
}

var fittedCS = new FittedCoordinateSystem (baseCS, toBaseTransform, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);
var fittedCS = new FittedCoordinateSystem(baseCS, toBaseTransform, name, authority, authorityCode, string.Empty, string.Empty, string.Empty);
return fittedCS;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/ProjNet/IO/CoordinateSystems/WKTStreamTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,18 @@ public void ReadAuthority(out string authority, out long authorityCode)
long.TryParse(ReadDoubleQuotedWord(), NumberStyles.Any, _nfi, out authorityCode);
ReadCloser(bracket);
}

public void ReadExtension(out string extension, out string extensionMethod)
{
//EXTENSION["PROJ4_GRIDS","CA61_003.gsb"]
if (GetStringValue() != "EXTENSION")
ReadToken("EXTENSION");
var bracket = ReadOpener();
extension = ReadDoubleQuotedWord();
ReadToken(",");
NextToken();
extensionMethod = ReadDoubleQuotedWord();
ReadCloser(bracket);
}
}
}
Loading