⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 geometryfromwkt.cs

📁 Sharp Map 用于制作GIS系统S harp Map 用于制作GIS系统S harp Map 用于制作GIS系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
// 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.Collections.Generic;
using System.Text;
using SharpMap.Geometries;

namespace SharpMap.Converters.WellKnownText
{
	/// <summary>
	///  Converts a Well-known Text representation to a <see cref="SharpMap.Geometries.Geometry"/> instance.
	/// </summary>
	/// <remarks>
	/// <para>The Well-Known Text (WKT) representation of Geometry is designed to exchange geometry data in ASCII form.</para>
	/// Examples of WKT representations of geometry objects are:
	/// <list type="table">
	/// <listheader><term>Geometry </term><description>WKT Representation</description></listheader>
	/// <item><term>A Point</term>
	/// <description>POINT(15 20)<br/> Note that point coordinates are specified with no separating comma.</description></item>
	/// <item><term>A LineString with four points:</term>
	/// <description>LINESTRING(0 0, 10 10, 20 25, 50 60)</description></item>
	/// <item><term>A Polygon with one exterior ring and one interior ring:</term>
	/// <description>POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))</description></item>
	/// <item><term>A MultiPoint with three Point values:</term>
	/// <description>MULTIPOINT(0 0, 20 20, 60 60)</description></item>
	/// <item><term>A MultiLineString with two LineString values:</term>
	/// <description>MULTILINESTRING((10 10, 20 20), (15 15, 30 15))</description></item>
	/// <item><term>A MultiPolygon with two Polygon values:</term>
	/// <description>MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))</description></item>
	/// <item><term>A GeometryCollection consisting of two Point values and one LineString:</term>
	/// <description>GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))</description></item>
	/// </list>
	/// </remarks>
	public class GeometryFromWKT
	{
		/// <summary>
		/// Converts a Well-known text representation to a <see cref="SharpMap.Geometries.Geometry"/>.
		/// </summary>
		/// <param name="wellKnownText">A <see cref="SharpMap.Geometries.Geometry"/> tagged text string ( see the OpenGIS Simple Features Specification.</param>
		/// <returns>Returns a <see cref="SharpMap.Geometries.Geometry"/> specified by wellKnownText.  Throws an exception if there is a parsing problem.</returns>
		public static Geometry Parse(string wellKnownText)
		{
			// throws a parsing exception is there is a problem.
			System.IO.StringReader reader = new System.IO.StringReader(wellKnownText);
			return Parse(reader);
		}

		/// <summary>
		/// Converts a Well-known Text representation to a <see cref="SharpMap.Geometries.Geometry"/>.
		/// </summary>
		/// <param name="reader">A Reader which will return a Geometry Tagged Text
		/// string (see the OpenGIS Simple Features Specification)</param>
		/// <returns>Returns a <see cref="SharpMap.Geometries.Geometry"/> read from StreamReader. 
		/// An exception will be thrown if there is a parsing problem.</returns>
		public static Geometry Parse(System.IO.TextReader reader)
		{
			WktStreamTokenizer tokenizer = new WktStreamTokenizer(reader);

			return ReadGeometryTaggedText(tokenizer);
		}

		/// <summary>
		/// Returns the next array of Coordinates in the stream.
		/// </summary>
		/// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text format.  The
		/// next element returned by the stream should be "(" (the beginning of "(x1 y1, x2 y2, ..., xn yn)" or
		/// "EMPTY".</param>
		/// <returns>The next array of Coordinates in the stream, or an empty array of "EMPTY" is the
		/// next element returned by the stream.</returns>
		private static List<SharpMap.Geometries.Point> GetCoordinates(WktStreamTokenizer tokenizer)
		{
			List<SharpMap.Geometries.Point> coordinates = new List<SharpMap.Geometries.Point>();
			string nextToken = GetNextEmptyOrOpener(tokenizer);
			if (nextToken=="EMPTY")
				return coordinates;

			SharpMap.Geometries.Point externalCoordinate = new SharpMap.Geometries.Point();
			SharpMap.Geometries.Point internalCoordinate = new SharpMap.Geometries.Point();
			externalCoordinate.X = GetNextNumber(tokenizer);
			externalCoordinate.Y = GetNextNumber(tokenizer);
			coordinates.Add(externalCoordinate);
			nextToken = GetNextCloserOrComma(tokenizer);
			while (nextToken==",")
			{
				internalCoordinate = new SharpMap.Geometries.Point();
				internalCoordinate.X = GetNextNumber(tokenizer);
				internalCoordinate.Y = GetNextNumber(tokenizer);
				coordinates.Add(internalCoordinate);
				nextToken = GetNextCloserOrComma(tokenizer);
			}
			return coordinates;
		}


		/// <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>
		/// 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>
		/// 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 ")" in the stream.
		/// </summary>
		/// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text
		/// format. The next token must be ")".</param>
		/// <returns>Returns the next ")" in the stream.</returns>
		/// <remarks>
		/// ParseException is thrown if the next token is not ")".
		/// </remarks>
		private static string GetNextCloser(WktStreamTokenizer tokenizer)
		{

			string nextWord = GetNextWord(tokenizer);
			if (nextWord == ")")
				return nextWord;

			throw new Exception("Expected ')' but encountered '" + nextWord + "'");
		}

		/// <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 "(";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -