private static List <List <ICoordinate> > readPointsCoordLists(WktStreamTokenizer tokenizer) { List <List <ICoordinate> > result = new List <List <ICoordinate> >(); bool comma = true; while (comma) { tokenizer.ReadToken("("); result.Add(readPointCoordsList(tokenizer)); if (tokenizer.GetStringValue() == ")") { tokenizer.NextToken(); comma = tokenizer.GetStringValue() == ","; } else { throwMissingCloseBracket(tokenizer); } } return(result); }
private static List <ICoordinate> readPointCoordsList(WktStreamTokenizer tokenizer) { List <ICoordinate> points = new List <ICoordinate>(); bool comma = true; while (comma) { points.Add(readPointCoords(tokenizer)); tokenizer.NextToken(); comma = tokenizer.GetStringValue() == ","; } return(points); }
/// <summary> /// Constructs a geometry from its well-known text representation. /// </summary> /// <returns>A constructed geometry</returns> public static IGeometry GeometryFromWKT(string wkt) { StringReader sr = new StringReader(wkt); WktStreamTokenizer tokenizer = new WktStreamTokenizer(sr); tokenizer.NextToken(); switch (tokenizer.GetStringValue().ToUpper()) { case "POINT": return(readPoint(tokenizer)); case "LINESTRING": return(readLineString(tokenizer)); case "MULTILINESTRING": return(readMultiLineString(tokenizer)); case "POLYGON": return(readPolygon(tokenizer)); case "MULTIPOLYGON": return(readMultiPolygon(tokenizer)); case "MULTIPOINT": return(readMultiPoint(tokenizer)); } throw new ArgumentException(string.Format("Unknown geometry \"{0}\"", tokenizer.GetStringValue())); }
/// <summary> /// Constructs a coordinate system object from well-known text. /// </summary> /// <param name="wkt">Well-known text representation of object</param> /// <returns>An object constructed from well-known text</returns> /// <exception cref="System.ArgumentException">Raises when parsing of string fails.</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 recognized.", objectName)); } reader.Close(); return(returnObject); }
private static Polyline readLineString(WktStreamTokenizer tokenizer) { tokenizer.ReadToken("("); List <ICoordinate> points = readPointCoordsList(tokenizer); if (tokenizer.GetStringValue() == ")") { Polyline polyline = new Polyline(); polyline.Paths.Add(new LinePath()); polyline.Paths[0].Vertices = points; return(polyline); } else { throwMissingCloseBracket(tokenizer); } return(null); }
/// <summary> /// Reads an anglular unit. /// </summary> 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); ReadAuthority(tokenizer, ref authority, ref authorityCode); tokenizer.ReadToken("]"); } return(new AngularUnit(unitsPerUnit, unitName, authority, authorityCode, String.Empty, String.Empty, String.Empty)); }
/// <summary> /// Reads am authority and authority code. /// </summary> private static void ReadAuthority(WktStreamTokenizer tokenizer, ref string authority, ref long authorityCode) { if (tokenizer.GetStringValue() != "AUTHORITY") { tokenizer.ReadToken("AUTHORITY"); } tokenizer.ReadToken("["); authority = tokenizer.ReadDoubleQuotedWord(); tokenizer.ReadToken(","); #if (!Silverlight) long.TryParse(tokenizer.ReadDoubleQuotedWord(), NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out authorityCode); #else try { authorityCode = long.Parse(tokenizer.ReadDoubleQuotedWord(), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture.NumberFormat); } catch { } #endif tokenizer.ReadToken("]"); }
private static Polyline readMultiLineString(WktStreamTokenizer tokenizer) { tokenizer.ReadToken("("); List <List <ICoordinate> > lists = readPointsCoordLists(tokenizer); if (tokenizer.GetStringValue() == ")") { Polyline polyline = new Polyline(); foreach (List <ICoordinate> list in lists) { LinePath path = new LinePath(); path.Vertices = list; polyline.Paths.Add(path); } return(polyline); } else { throwMissingCloseBracket(tokenizer); } return(null); }
private static Polygon readPolygon(WktStreamTokenizer tokenizer) { tokenizer.ReadToken("("); List <List <ICoordinate> > lists = readPointsCoordLists(tokenizer); if (tokenizer.GetStringValue() == ")") { Polygon polygon = new Polygon(); foreach (List <ICoordinate> list in lists) { Contour contour = new Contour(); list.RemoveAt(list.Count - 1); contour.Vertices = list; polygon.Contours.Add(contour); } return(polygon); } else { throwMissingCloseBracket(tokenizer); } return(null); }
/// <summary> /// Reads a prime meridian. /// </summary> private static IPrimeMeridian ReadPrimeMeridian(WktStreamTokenizer tokenizer) { 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() == ",") { ReadAuthority(tokenizer, ref authority, ref authorityCode); tokenizer.ReadToken("]"); } 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 IGeometry ReadGeometryTaggedText(WktStreamTokenizer tokenizer) { tokenizer.NextToken(); var type = tokenizer.GetStringValue().ToUpper(); IGeometry geometry; IGeometryFactory 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> /// 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); if (type == TokenType.Word) return token.ToUpper(); if (token == "(") return "("; if (token == ")") return ")"; if (token == ",") return ","; throw new Exception("Not a valid symbol in WKT format."); }
/// <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 + "'"); }