📄 coordinatesystemwktreader.cs
字号:
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// SOURCECODE IS MODIFIED FROM ANOTHER WORK AND IS ORIGINALLY BASED ON GeoTools.NET:
/*
* Copyright (C) 2002 Urban Science Applications, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
using System;
using System.IO;
using System.Collections.Generic;
using SharpMap.CoordinateSystems;
namespace SharpMap.Converters.WellKnownText
{
/// <summary>
/// Creates an object based on the supplied Well Known Text (WKT).
/// </summary>
public class CoordinateSystemWktReader
{
/// <summary>
/// Reads and parses a WKT-formatted projection string.
/// </summary>
/// <param name="wkt">String containing WKT.</param>
/// <returns>Object representation of the WKT.</returns>
/// <exception cref="System.ArgumentException">If a token is not recognised.</exception>
public static IInfo Parse(string wkt)
{
IInfo returnObject = null;
StringReader reader = new StringReader(wkt);
WktStreamTokenizer tokenizer = new WktStreamTokenizer(reader);
tokenizer.NextToken();
string objectName = tokenizer.GetStringValue();
switch (objectName)
{
case "UNIT":
returnObject = ReadUnit(tokenizer);
break;
//case "VERT_DATUM":
// IVerticalDatum verticalDatum = ReadVerticalDatum(tokenizer);
// returnObject = verticalDatum;
// break;
case "SPHEROID":
returnObject = ReadEllipsoid(tokenizer);
break;
case "DATUM":
returnObject = ReadHorizontalDatum(tokenizer); ;
break;
case "PRIMEM":
returnObject = ReadPrimeMeridian(tokenizer);
break;
case "VERT_CS":
case "GEOGCS":
case "PROJCS":
case "COMPD_CS":
case "GEOCCS":
case "FITTED_CS":
case "LOCAL_CS":
returnObject = ReadCoordinateSystem(wkt, tokenizer);
break;
default:
throw new ArgumentException(String.Format("'{0'} is not recongnized.", objectName));
}
reader.Close();
return returnObject;
}
/// <summary>
/// Returns a IUnit given a piece of WKT.
/// </summary>
/// <param name="tokenizer">WktStreamTokenizer that has the WKT.</param>
/// <returns>An object that implements the IUnit interface.</returns>
private static IUnit ReadUnit(WktStreamTokenizer tokenizer)
{
tokenizer.ReadToken("[");
string unitName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
double unitsPerUnit = tokenizer.GetNumericValue();
string authority = String.Empty;
long authorityCode = -1;
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(ref authority, ref authorityCode);
tokenizer.ReadToken("]");
}
return new Unit(unitsPerUnit, unitName, authority, authorityCode, String.Empty, String.Empty, String.Empty);
}
/// <summary>
/// Returns a <see cref="LinearUnit"/> given a piece of WKT.
/// </summary>
/// <param name="tokenizer">WktStreamTokenizer that has the WKT.</param>
/// <returns>An object that implements the IUnit interface.</returns>
private static ILinearUnit ReadLinearUnit(WktStreamTokenizer tokenizer)
{
tokenizer.ReadToken("[");
string unitName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
double unitsPerUnit = tokenizer.GetNumericValue();
string authority = String.Empty;
long authorityCode = -1;
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(ref authority, ref authorityCode);
tokenizer.ReadToken("]");
}
return new LinearUnit(unitsPerUnit, unitName, authority, authorityCode, String.Empty, String.Empty, String.Empty);
}
/// <summary>
/// Returns a <see cref="AngularUnit"/> given a piece of WKT.
/// </summary>
/// <param name="tokenizer">WktStreamTokenizer that has the WKT.</param>
/// <returns>An object that implements the IUnit interface.</returns>
private static IAngularUnit ReadAngularUnit(WktStreamTokenizer tokenizer)
{
tokenizer.ReadToken("[");
string unitName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
double unitsPerUnit = tokenizer.GetNumericValue();
string authority = String.Empty;
long authorityCode = -1;
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
{
tokenizer.ReadAuthority(ref authority, ref authorityCode);
tokenizer.ReadToken("]");
}
return new AngularUnit(unitsPerUnit, unitName, authority, authorityCode, String.Empty, String.Empty, String.Empty);
}
/// <summary>
/// Returns a <see cref="AxisInfo"/> given a piece of WKT.
/// </summary>
/// <param name="tokenizer">WktStreamTokenizer that has the WKT.</param>
/// <returns>An AxisInfo object.</returns>
private static AxisInfo ReadAxis(WktStreamTokenizer tokenizer)
{
if (tokenizer.GetStringValue() != "AXIS")
tokenizer.ReadToken("AXIS");
tokenizer.ReadToken("[");
string axisName = tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
string unitname = tokenizer.GetStringValue();
tokenizer.ReadToken("]");
switch (unitname.ToUpper())
{
case "DOWN": return new AxisInfo(axisName, AxisOrientationEnum.Down);
case "EAST": return new AxisInfo(axisName, AxisOrientationEnum.East);
case "NORTH": return new AxisInfo(axisName, AxisOrientationEnum.North);
case "OTHER": return new AxisInfo(axisName, AxisOrientationEnum.Other);
case "SOUTH": return new AxisInfo(axisName, AxisOrientationEnum.South);
case "UP": return new AxisInfo(axisName, AxisOrientationEnum.Up);
case "WEST": return new AxisInfo(axisName, AxisOrientationEnum.West);
default:
throw new ArgumentException("Invalid axis name '" + unitname + "' in WKT");
}
}
/// <summary>
///
/// </summary>
/// <param name="coordinateSystem"></param>
/// <param name="tokenizer"></param>
/// <returns></returns>
private static ICoordinateSystem ReadCoordinateSystem(string coordinateSystem, WktStreamTokenizer tokenizer)
{
switch (tokenizer.GetStringValue())
{
case "GEOGCS":
return ReadGeographicCoordinateSystem(tokenizer);
case "PROJCS":
return ReadProjectedCoordinateSystem(tokenizer);
case "COMPD_CS":
/* ICompoundCoordinateSystem compoundCS = ReadCompoundCoordinateSystem(tokenizer);
returnCS = compoundCS;
break;*/
case "VERT_CS":
/* IVerticalCoordinateSystem verticalCS = ReadVerticalCoordinateSystem(tokenizer);
returnCS = verticalCS;
break;*/
case "GEOCCS":
case "FITTED_CS":
case "LOCAL_CS":
throw new NotSupportedException(String.Format("{0} coordinate system is not supported.", coordinateSystem));
default:
throw new InvalidOperationException(String.Format("{0} coordinate system is not recognized.", coordinateSystem));
}
}
/// <summary>
/// Reads either 3, 6 or 7 parameter Bursa-Wolf values from TOWGS84 token
/// </summary>
/// <param name="tokenizer"></param>
/// <returns></returns>
private static Wgs84ConversionInfo ReadWGS84ConversionInfo(WktStreamTokenizer tokenizer)
{
//TOWGS84[0,0,0,0,0,0,0]
tokenizer.ReadToken("[");
Wgs84ConversionInfo info = new Wgs84ConversionInfo();
tokenizer.NextToken();
info.Dx = tokenizer.GetNumericValue();
tokenizer.ReadToken(",");
tokenizer.NextToken();
info.Dy = tokenizer.GetNumericValue();
tokenizer.ReadToken(",");
tokenizer.NextToken();
info.Dz = tokenizer.GetNumericValue();
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
{
tokenizer.NextToken();
info.Ex = tokenizer.GetNumericValue();
tokenizer.ReadToken(",");
tokenizer.NextToken();
info.Ey = tokenizer.GetNumericValue();
tokenizer.ReadToken(",");
tokenizer.NextToken();
info.Ez = tokenizer.GetNumericValue();
tokenizer.NextToken();
if (tokenizer.GetStringValue() == ",")
{
tokenizer.NextToken();
info.Ppm = tokenizer.GetNumericValue();
}
}
if (tokenizer.GetStringValue() != "]")
tokenizer.ReadToken("]");
return info;
}
/*
/// <summary>
///
/// </summary>
/// <param name="tokenizer"></param>
/// <returns></returns>
private static ICompoundCoordinateSystem ReadCompoundCoordinateSystem(WktStreamTokenizer tokenizer)
{
//COMPD_CS[
//"OSGB36 / British National Grid + ODN",
//PROJCS[]
//VERT_CS[]
//AUTHORITY["EPSG","7405"]
//]
tokenizer.ReadToken("[");
string name=tokenizer.ReadDoubleQuotedWord();
tokenizer.ReadToken(",");
tokenizer.NextToken();
string headCSCode = tokenizer.GetStringValue();
ICoordinateSystem headCS = ReadCoordinateSystem(headCSCode,tokenizer);
tokenizer.ReadToken(",");
tokenizer.NextToken();
string tailCSCode = tokenizer.GetStringValue();
ICoordinateSystem tailCS = ReadCoordinateSystem(tailCSCode,tokenizer);
tokenizer.ReadToken(",");
string authority=String.Empty;
string authorityCode=String.Empty;
tokenizer.ReadAuthority(ref authority, ref authorityCode);
tokenizer.ReadToken("]");
ICompoundCoordinateSystem compoundCS = new CompoundCoordinateSystem(headCS,tailCS,String.Empty,authority,authorityCode,name,String.Empty,String.Empty);
return compoundCS;
}*/
/// <summary>
///
/// </summary>
/// <param name="tokenizer"></param>
/// <returns></returns>
private static IEllipsoid ReadEllipsoid(WktStreamTokenizer tokenizer)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -