/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IProjection ReadProjection(WktStreamTokenizer tokenizer) { //tokenizer.NextToken();// PROJECTION tokenizer.ReadToken("PROJECTION"); tokenizer.ReadToken("["); //[ string projectionName = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken("]"); //] tokenizer.ReadToken(","); //, tokenizer.ReadToken("PARAMETER"); List <ProjectionParameter> paramList = new List <ProjectionParameter>(); while (tokenizer.GetStringValue() == "PARAMETER") { tokenizer.ReadToken("["); string paramName = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.NextToken(); double paramValue = tokenizer.GetNumericValue(); tokenizer.ReadToken("]"); tokenizer.ReadToken(","); paramList.Add(new ProjectionParameter(paramName, paramValue)); tokenizer.NextToken(); } string authority = String.Empty; long authorityCode = -1; IProjection projection = new Projection(projectionName, paramList, projectionName, authority, authorityCode, String.Empty, String.Empty, string.Empty); return(projection); }
/* * /// <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) { //SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]] tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.NextToken(); double majorAxis = tokenizer.GetNumericValue(); tokenizer.ReadToken(","); tokenizer.NextToken(); double e = tokenizer.GetNumericValue(); // //tokenizer.ReadToken(","); tokenizer.NextToken(); string authority = String.Empty; long authorityCode = -1; if (tokenizer.GetStringValue() == ",") //Read authority { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } IEllipsoid ellipsoid = new Ellipsoid(majorAxis, 0.0, e, true, LinearUnit.Metre, name, authority, authorityCode, String.Empty, string.Empty, string.Empty); return(ellipsoid); }
/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IHorizontalDatum ReadHorizontalDatum(WktStreamTokenizer tokenizer) { //DATUM["OSGB 1936",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]]TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6277"]] Wgs84ConversionInfo wgsInfo = null; string authority = String.Empty; long authorityCode = -1; tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.ReadToken("SPHEROID"); IEllipsoid ellipsoid = ReadEllipsoid(tokenizer); tokenizer.NextToken(); while (tokenizer.GetStringValue() == ",") { tokenizer.NextToken(); if (tokenizer.GetStringValue() == "TOWGS84") { wgsInfo = ReadWGS84ConversionInfo(tokenizer); tokenizer.NextToken(); } else if (tokenizer.GetStringValue() == "AUTHORITY") { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } } // make an assumption about the datum type. IHorizontalDatum horizontalDatum = new HorizontalDatum(ellipsoid, wgsInfo, DatumType.HD_Geocentric, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return(horizontalDatum); }
/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IGeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStreamTokenizer tokenizer) { /* * GEOGCS["OSGB 1936", * DATUM["OSGB 1936",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]]TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6277"]] * PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]] * AXIS["Geodetic latitude","NORTH"] * AXIS["Geodetic longitude","EAST"] * AUTHORITY["EPSG","4277"] * ] */ tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.ReadToken("DATUM"); IHorizontalDatum horizontalDatum = ReadHorizontalDatum(tokenizer); tokenizer.ReadToken(","); tokenizer.ReadToken("PRIMEM"); IPrimeMeridian primeMeridian = ReadPrimeMeridian(tokenizer); tokenizer.ReadToken(","); tokenizer.ReadToken("UNIT"); IAngularUnit angularUnit = ReadAngularUnit(tokenizer); string authority = String.Empty; long authorityCode = -1; tokenizer.NextToken(); List <AxisInfo> info = new List <AxisInfo>(2); if (tokenizer.GetStringValue() == ",") { tokenizer.NextToken(); while (tokenizer.GetStringValue() == "AXIS") { info.Add(ReadAxis(tokenizer)); 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("Lon", AxisOrientationEnum.East)); info.Add(new AxisInfo("Lat", AxisOrientationEnum.North)); } IGeographicCoordinateSystem geographicCS = new GeographicCoordinateSystem(angularUnit, horizontalDatum, primeMeridian, info, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return(geographicCS); }
/// <summary> /// Returns the next word in the stream as uppercase text. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next token must be a word.</param> /// <returns>Returns the next word in the stream as uppercase text.</returns> /// <remarks> /// Exception is thrown if the next token is not a word. /// </remarks> private static string GetNextWord(WktStreamTokenizer tokenizer) { TokenType type = tokenizer.NextToken(); string token = tokenizer.GetStringValue(); if (type == TokenType.Number) { throw new Exception("Expected a number but got " + token); } else if (type == TokenType.Word) { return(token.ToUpper()); } else if (token == "(") { return("("); } else if (token == ")") { return(")"); } else if (token == ",") { return(","); } throw new Exception("Not a valid symbol in WKT format."); }
/// <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> /// 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 the next ")" or "," in the stream. /// </summary> /// <param name="tokenizer">tokenizer over a stream of text in Well-known Text /// format. The next token must be ")" or ",".</param> /// <returns>Returns the next ")" or "," in the stream.</returns> /// <remarks> /// ParseException is thrown if the next token is not ")" or ",". /// </remarks> private static string GetNextCloserOrComma(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); string nextWord = tokenizer.GetStringValue(); if (nextWord == "," || nextWord == ")") { return(nextWord); } throw new Exception("Expected ')' or ',' but encountered '" + nextWord + "'"); }
/// <summary> /// Returns the next "EMPTY" or "(" in the stream as uppercase text. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next token must be "EMPTY" or "(".</param> /// <returns>the next "EMPTY" or "(" in the stream as uppercase /// text.</returns> /// <remarks> /// ParseException is thrown if the next token is not "EMPTY" or "(". /// </remarks> private static string GetNextEmptyOrOpener(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); string nextWord = tokenizer.GetStringValue(); if (nextWord == "EMPTY" || nextWord == "(") { return(nextWord); } throw new Exception("Expected 'EMPTY' or '(' but encountered '" + nextWord + "'"); }
/// <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 IPrimeMeridian ReadPrimeMeridian(WktStreamTokenizer tokenizer) { //PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]] tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.NextToken(); double longitude = tokenizer.GetNumericValue(); tokenizer.NextToken(); string authority = String.Empty; long authorityCode = -1; if (tokenizer.GetStringValue() == ",") { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } // make an assumption about the Angular units - degrees. IPrimeMeridian primeMeridian = new PrimeMeridian(longitude, AngularUnit.Degrees, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return(primeMeridian); }
/// <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> /// Creates a Geometry using the next token in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <Geometry Tagged Text>.</param> /// <returns>Returns a Geometry specified by the next token in the stream.</returns> /// <remarks> /// Exception is thrown if the coordinates used to create a Polygon /// shell and holes do not form closed linestrings, or if an unexpected /// token is encountered. /// </remarks> private static Geometry ReadGeometryTaggedText(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); var type = tokenizer.GetStringValue().ToUpper(); Geometry geometry; GeometryFactory factory = new GeometryFactory(); switch (type) { case "POINT": geometry = ReadPointText(tokenizer, factory); break; case "LINESTRING": geometry = ReadLineStringText(tokenizer, factory); break; case "MultiPoint": geometry = ReadMultiPointText(tokenizer, factory); break; case "MultiLineString": geometry = ReadMultiLineStringText(tokenizer, factory); break; case "POLYGON": geometry = ReadPolygonText(tokenizer, factory); break; case "MultiPolygon": geometry = ReadMultiPolygonText(tokenizer, factory); break; case "GEOMETRYCOLLECTION": geometry = ReadGeometryCollectionText(tokenizer, factory); break; default: throw new Exception(String.Format(Map.NumberFormatEnUs, "Geometrytype '{0}' is not supported.", type)); } return(geometry); }
/// <summary> /// Creates a Geometry using the next token in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <Geometry Tagged Text>.</param> /// <returns>Returns a Geometry specified by the next token in the stream.</returns> /// <remarks> /// Exception is thrown if the coordinates used to create a Polygon /// shell and holes do not form closed linestrings, or if an unexpected /// token is encountered. /// </remarks> private static Geometry ReadGeometryTaggedText(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); string type = tokenizer.GetStringValue().ToUpper(); Geometry geometry = null; switch (type) { case "POINT": geometry = ReadPointText(tokenizer); break; case "LINESTRING": geometry = ReadLineStringText(tokenizer); break; case "MULTIPOINT": geometry = ReadMultiPointText(tokenizer); break; case "MULTILINESTRING": geometry = ReadMultiLineStringText(tokenizer); break; case "POLYGON": geometry = ReadPolygonText(tokenizer); break; case "MULTIPOLYGON": geometry = ReadMultiPolygonText(tokenizer); break; case "GEOMETRYCOLLECTION": geometry = ReadGeometryCollectionText(tokenizer); break; default: throw new Exception(String.Format(SharpMap.Map.numberFormat_EnUS, "Geometrytype '{0}' is not supported.", type)); } return(geometry); }
/// <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 the next number in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known text format. The next token /// must be a number.</param> /// <returns>Returns the next number in the stream.</returns> /// <remarks> /// ParseException is thrown if the next token is not a number. /// </remarks> private static double GetNextNumber(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); return(tokenizer.GetNumericValue()); }
/// <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> /// 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 the next word in the stream as uppercase text. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next token must be a word.</param> /// <returns>Returns the next word in the stream as uppercase text.</returns> /// <remarks> /// Exception is thrown if the next token is not a word. /// </remarks> private static string GetNextWord(WktStreamTokenizer tokenizer) { TokenType type = tokenizer.NextToken(); string token = tokenizer.GetStringValue(); if (type == TokenType.Number) throw new Exception("Expected a number but got " + token); else if (type == TokenType.Word) return token.ToUpper(); else if (token == "(") return "("; else if (token == ")") return ")"; else if (token == ",") return ","; throw new Exception("Not a valid symbol in WKT format."); }
/// <summary> /// Returns the next "EMPTY" or "(" in the stream as uppercase text. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next token must be "EMPTY" or "(".</param> /// <returns>the next "EMPTY" or "(" in the stream as uppercase /// text.</returns> /// <remarks> /// ParseException is thrown if the next token is not "EMPTY" or "(". /// </remarks> private static string GetNextEmptyOrOpener(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); string nextWord = tokenizer.GetStringValue(); if (nextWord == "EMPTY" || nextWord == "(") return nextWord; throw new Exception("Expected 'EMPTY' or '(' but encountered '" + nextWord + "'"); }
/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStreamTokenizer tokenizer) { /*PROJCS[ * "OSGB 1936 / British National Grid", * GEOGCS[ * "OSGB 1936", * DATUM[...] * PRIMEM[...] * AXIS["Geodetic latitude","NORTH"] * AXIS["Geodetic longitude","EAST"] * AUTHORITY["EPSG","4277"] * ], * PROJECTION["Transverse Mercator"], * PARAMETER["latitude_of_natural_origin",49], * PARAMETER["longitude_of_natural_origin",-2], * PARAMETER["scale_factor_at_natural_origin",0.999601272], * PARAMETER["false_easting",400000], * PARAMETER["false_northing",-100000], * AXIS["Easting","EAST"], * AXIS["Northing","NORTH"], * AUTHORITY["EPSG","27700"] * ] */ tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.ReadToken("GEOGCS"); IGeographicCoordinateSystem geographicCS = ReadGeographicCoordinateSystem(tokenizer); tokenizer.ReadToken(","); IProjection projection = ReadProjection(tokenizer); IUnit unit = ReadLinearUnit(tokenizer); string authority = String.Empty; long authorityCode = -1; tokenizer.NextToken(); List <AxisInfo> axes = new List <AxisInfo>(2); if (tokenizer.GetStringValue() == ",") { tokenizer.NextToken(); while (tokenizer.GetStringValue() == "AXIS") { axes.Add(ReadAxis(tokenizer)); tokenizer.NextToken(); } if (tokenizer.GetStringValue() == "AUTHORITY") { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } } //This is default axis values if not specified. if (axes.Count == 0) { axes.Add(new AxisInfo("X", AxisOrientationEnum.East)); axes.Add(new AxisInfo("Y", AxisOrientationEnum.North)); } IProjectedCoordinateSystem projectedCS = new ProjectedCoordinateSystem(geographicCS.HorizontalDatum, geographicCS, unit as LinearUnit, projection, axes, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return(projectedCS); }
/* /// <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) { //SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]] tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.NextToken(); double majorAxis = tokenizer.GetNumericValue(); tokenizer.ReadToken(","); tokenizer.NextToken(); double e = tokenizer.GetNumericValue(); // //tokenizer.ReadToken(","); tokenizer.NextToken(); string authority = String.Empty; long authorityCode = -1; if (tokenizer.GetStringValue() == ",") //Read authority { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } IEllipsoid ellipsoid = new Ellipsoid(majorAxis, 0.0, e, true, LinearUnit.Metre, name, authority, authorityCode, String.Empty, string.Empty, string.Empty); return ellipsoid; }
/// <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="tokenizer"></param> /// <returns></returns> private static IGeographicCoordinateSystem ReadGeographicCoordinateSystem(WktStreamTokenizer tokenizer) { /* GEOGCS["OSGB 1936", DATUM["OSGB 1936",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]]TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6277"]] PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]] AXIS["Geodetic latitude","NORTH"] AXIS["Geodetic longitude","EAST"] AUTHORITY["EPSG","4277"] ] */ tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.ReadToken("DATUM"); IHorizontalDatum horizontalDatum = ReadHorizontalDatum(tokenizer); tokenizer.ReadToken(","); tokenizer.ReadToken("PRIMEM"); IPrimeMeridian primeMeridian = ReadPrimeMeridian(tokenizer); tokenizer.ReadToken(","); tokenizer.ReadToken("UNIT"); IAngularUnit angularUnit = ReadAngularUnit(tokenizer); string authority = String.Empty; long authorityCode = -1; tokenizer.NextToken(); List<AxisInfo> info = new List<AxisInfo>(2); if (tokenizer.GetStringValue() == ",") { tokenizer.NextToken(); while (tokenizer.GetStringValue() == "AXIS") { info.Add(ReadAxis(tokenizer)); 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("Lon", AxisOrientationEnum.East)); info.Add(new AxisInfo("Lat", AxisOrientationEnum.North)); } IGeographicCoordinateSystem geographicCS = new GeographicCoordinateSystem(angularUnit, horizontalDatum, primeMeridian, info, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return geographicCS; }
/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IProjection ReadProjection(WktStreamTokenizer tokenizer) { //tokenizer.NextToken();// PROJECTION tokenizer.ReadToken("PROJECTION"); tokenizer.ReadToken("[");//[ string projectionName = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken("]");//] tokenizer.ReadToken(",");//, tokenizer.ReadToken("PARAMETER"); List<ProjectionParameter> paramList = new List<ProjectionParameter>(); while (tokenizer.GetStringValue() == "PARAMETER") { tokenizer.ReadToken("["); string paramName = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.NextToken(); double paramValue = tokenizer.GetNumericValue(); tokenizer.ReadToken("]"); tokenizer.ReadToken(","); paramList.Add(new ProjectionParameter(paramName, paramValue)); tokenizer.NextToken(); } string authority = String.Empty; long authorityCode = -1; IProjection projection = new Projection(projectionName, paramList, projectionName, authority, authorityCode, String.Empty, String.Empty, string.Empty); return projection; }
/// <summary> /// Returns the next number in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known text format. The next token /// must be a number.</param> /// <returns>Returns the next number in the stream.</returns> /// <remarks> /// ParseException is thrown if the next token is not a number. /// </remarks> private static double GetNextNumber(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); return tokenizer.GetNumericValue(); }
/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IHorizontalDatum ReadHorizontalDatum(WktStreamTokenizer tokenizer) { //DATUM["OSGB 1936",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]]TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6277"]] Wgs84ConversionInfo wgsInfo = null; string authority = String.Empty; long authorityCode = -1; tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.ReadToken("SPHEROID"); IEllipsoid ellipsoid = ReadEllipsoid(tokenizer); tokenizer.NextToken(); while (tokenizer.GetStringValue() == ",") { tokenizer.NextToken(); if (tokenizer.GetStringValue() == "TOWGS84") { wgsInfo = ReadWGS84ConversionInfo(tokenizer); tokenizer.NextToken(); } else if (tokenizer.GetStringValue() == "AUTHORITY") { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } } // make an assumption about the datum type. IHorizontalDatum horizontalDatum = new HorizontalDatum(ellipsoid, wgsInfo, DatumType.HD_Geocentric, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return horizontalDatum; }
/// <summary> /// Returns the next ")" or "," in the stream. /// </summary> /// <param name="tokenizer">tokenizer over a stream of text in Well-known Text /// format. The next token must be ")" or ",".</param> /// <returns>Returns the next ")" or "," in the stream.</returns> /// <remarks> /// ParseException is thrown if the next token is not ")" or ",". /// </remarks> private static string GetNextCloserOrComma(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); string nextWord = tokenizer.GetStringValue(); if (nextWord == "," || nextWord == ")") { return nextWord; } throw new Exception("Expected ')' or ',' but encountered '" + nextWord + "'"); }
/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IPrimeMeridian ReadPrimeMeridian(WktStreamTokenizer tokenizer) { //PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]] tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.NextToken(); double longitude = tokenizer.GetNumericValue(); tokenizer.NextToken(); string authority = String.Empty; long authorityCode = -1; if (tokenizer.GetStringValue() == ",") { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } // make an assumption about the Angular units - degrees. IPrimeMeridian primeMeridian = new PrimeMeridian(longitude, AngularUnit.Degrees, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return primeMeridian; }
/// <summary> /// Creates a Geometry using the next token in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <Geometry Tagged Text>.</param> /// <returns>Returns a Geometry specified by the next token in the stream.</returns> /// <remarks> /// Exception is thrown if the coordinates used to create a Polygon /// shell and holes do not form closed linestrings, or if an unexpected /// token is encountered. /// </remarks> private static Geometry ReadGeometryTaggedText(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); string type = tokenizer.GetStringValue().ToUpper(); Geometry geometry = null; switch (type) { case "POINT": geometry = ReadPointText(tokenizer); break; case "LINESTRING": geometry = ReadLineStringText(tokenizer); break; case "MULTIPOINT": geometry = ReadMultiPointText(tokenizer); break; case "MULTILINESTRING": geometry = ReadMultiLineStringText(tokenizer); break; case "POLYGON": geometry = ReadPolygonText(tokenizer); break; case "MULTIPOLYGON": geometry = ReadMultiPolygonText(tokenizer); break; case "GEOMETRYCOLLECTION": geometry = ReadGeometryCollectionText(tokenizer); break; default: throw new Exception(String.Format(CultureInfo.InvariantCulture, "Geometrytype '{0}' is not supported.", type)); } return geometry; }
/// <summary> /// /// </summary> /// <param name="tokenizer"></param> /// <returns></returns> private static IProjectedCoordinateSystem ReadProjectedCoordinateSystem(WktStreamTokenizer tokenizer) { /*PROJCS[ "OSGB 1936 / British National Grid", GEOGCS[ "OSGB 1936", DATUM[...] PRIMEM[...] AXIS["Geodetic latitude","NORTH"] AXIS["Geodetic longitude","EAST"] AUTHORITY["EPSG","4277"] ], PROJECTION["Transverse Mercator"], PARAMETER["latitude_of_natural_origin",49], PARAMETER["longitude_of_natural_origin",-2], PARAMETER["scale_factor_at_natural_origin",0.999601272], PARAMETER["false_easting",400000], PARAMETER["false_northing",-100000], AXIS["Easting","EAST"], AXIS["Northing","NORTH"], AUTHORITY["EPSG","27700"] ] */ tokenizer.ReadToken("["); string name = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); tokenizer.ReadToken("GEOGCS"); IGeographicCoordinateSystem geographicCS = ReadGeographicCoordinateSystem(tokenizer); tokenizer.ReadToken(","); IProjection projection = ReadProjection(tokenizer); IUnit unit = ReadLinearUnit(tokenizer); string authority = String.Empty; long authorityCode = -1; tokenizer.NextToken(); List<AxisInfo> axes = new List<AxisInfo>(2); if (tokenizer.GetStringValue() == ",") { tokenizer.NextToken(); while (tokenizer.GetStringValue() == "AXIS") { axes.Add(ReadAxis(tokenizer)); tokenizer.NextToken(); } if (tokenizer.GetStringValue() == "AUTHORITY") { tokenizer.ReadAuthority(ref authority, ref authorityCode); tokenizer.ReadToken("]"); } } //This is default axis values if not specified. if (axes.Count == 0) { axes.Add(new AxisInfo("X", AxisOrientationEnum.East)); axes.Add(new AxisInfo("Y", AxisOrientationEnum.North)); } IProjectedCoordinateSystem projectedCS = new ProjectedCoordinateSystem(geographicCS.HorizontalDatum, geographicCS, unit as LinearUnit, projection, axes, name, authority, authorityCode, String.Empty, String.Empty, String.Empty); return projectedCS; }