PageRenderTime 462ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/nettopologysuite/NetTopologySuite/IO/GML2/GMLWriter.cs

https://github.com/maxm/osmuy
C# | 368 lines | 222 code | 25 blank | 121 comment | 47 complexity | b3f93455530007a66299b26c0d1cce33 MD5 | raw file
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Xml;
  5. using GeoAPI.Geometries;
  6. using GisSharpBlog.NetTopologySuite.Utilities;
  7. namespace GisSharpBlog.NetTopologySuite.IO.GML2
  8. {
  9. /// <summary>
  10. /// Writes the GML representation of the features of NetTopologySuite model.
  11. /// Uses GML 2.1.1 <c>Geometry.xsd</c> schema for base for features.
  12. /// <remarks>
  13. /// Thanks to <see href="http//www.codeplex.com/Wiki/View.aspx?ProjectName=MsSqlSpatial">rstuven</see> for improvements :)
  14. /// </remarks>
  15. /// </summary>
  16. public class GMLWriter
  17. {
  18. private const int InitValue = 150;
  19. private const int CoordSize = 200;
  20. /// <summary>
  21. /// Formatter for double values of coordinates
  22. /// </summary>
  23. protected static NumberFormatInfo NumberFormatter
  24. {
  25. get { return Global.GetNfi(); }
  26. }
  27. /// <summary>
  28. /// Returns an <c>XmlReader</c> with feature informations.
  29. /// Use <c>XmlDocument.Load(XmlReader)</c> for obtain a <c>XmlDocument</c> to work.
  30. /// </summary>
  31. /// <param name="geometry"></param>
  32. /// <returns></returns>
  33. public XmlReader Write(IGeometry geometry)
  34. {
  35. byte[] data = GetBytes(geometry);
  36. using (Stream stream = new MemoryStream(data))
  37. Write(geometry, stream);
  38. Stream outStream = new MemoryStream(data);
  39. return new XmlTextReader(outStream);
  40. }
  41. /// <summary>
  42. /// Writes a GML feature into a generic <c>Stream</c>, such a <c>FileStream</c> or other streams.
  43. /// </summary>
  44. /// <param name="geometry"></param>
  45. /// <param name="stream"></param>
  46. public void Write(IGeometry geometry, Stream stream)
  47. {
  48. XmlTextWriter writer = new XmlTextWriter(stream, null);
  49. writer.Namespaces = true;
  50. writer.WriteStartElement(GMLElements.gmlPrefix, "GML", GMLElements.gmlNS);
  51. writer.Formatting = Formatting.Indented;
  52. Write(geometry, writer);
  53. writer.WriteEndElement();
  54. writer.Close();
  55. }
  56. /// <summary>
  57. ///
  58. /// </summary>
  59. /// <param name="coordinate"></param>
  60. /// <param name="writer"></param>
  61. protected void Write(ICoordinate coordinate, XmlTextWriter writer)
  62. {
  63. writer.WriteStartElement(GMLElements.gmlPrefix, "coord", GMLElements.gmlNS);
  64. writer.WriteElementString(GMLElements.gmlPrefix, "X", GMLElements.gmlNS, coordinate.X.ToString("g", NumberFormatter));
  65. writer.WriteElementString(GMLElements.gmlPrefix, "Y", GMLElements.gmlNS, coordinate.Y.ToString("g", NumberFormatter));
  66. writer.WriteEndElement();
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. /// <param name="coordinates"></param>
  72. /// <param name="writer"></param>
  73. protected void Write(ICoordinate[] coordinates, XmlTextWriter writer)
  74. {
  75. foreach (ICoordinate coord in coordinates)
  76. Write(coord, writer);
  77. }
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. /// <param name="geometry"></param>
  82. /// <param name="writer"></param>
  83. protected void Write(IGeometry geometry, XmlTextWriter writer)
  84. {
  85. if (geometry is IPoint)
  86. Write(geometry as IPoint, writer);
  87. else if (geometry is ILineString)
  88. Write(geometry as ILineString, writer);
  89. else if (geometry is IPolygon)
  90. Write(geometry as IPolygon, writer);
  91. else if (geometry is IMultiPoint)
  92. Write(geometry as IMultiPoint, writer);
  93. else if (geometry is IMultiLineString)
  94. Write(geometry as IMultiLineString, writer);
  95. else if (geometry is IMultiPolygon)
  96. Write(geometry as IMultiPolygon, writer);
  97. else if (geometry is IGeometryCollection)
  98. Write(geometry as IGeometryCollection, writer);
  99. else throw new ArgumentException("Geometry not recognized: " + geometry.ToString());
  100. }
  101. /// <summary>
  102. ///
  103. /// </summary>
  104. /// <param name="point"></param>
  105. /// <param name="writer"></param>
  106. protected void Write(IPoint point, XmlTextWriter writer)
  107. {
  108. writer.WriteStartElement("Point", GMLElements.gmlNS);
  109. Write(point.Coordinate, writer);
  110. writer.WriteEndElement();
  111. }
  112. /// <summary>
  113. ///
  114. /// </summary>
  115. /// <param name="lineString"></param>
  116. /// <param name="writer"></param>
  117. protected void Write(ILineString lineString, XmlTextWriter writer)
  118. {
  119. writer.WriteStartElement("LineString", GMLElements.gmlNS);
  120. Write(lineString.Coordinates, writer);
  121. writer.WriteEndElement();
  122. }
  123. /// <summary>
  124. ///
  125. /// </summary>
  126. /// <param name="linearRing"></param>
  127. /// <param name="writer"></param>
  128. protected void Write(ILinearRing linearRing, XmlTextWriter writer)
  129. {
  130. writer.WriteStartElement("LinearRing", GMLElements.gmlNS);
  131. Write(linearRing.Coordinates, writer);
  132. writer.WriteEndElement();
  133. }
  134. /// <summary>
  135. ///
  136. /// </summary>
  137. /// <param name="polygon"></param>
  138. /// <param name="writer"></param>
  139. protected void Write(IPolygon polygon, XmlTextWriter writer)
  140. {
  141. writer.WriteStartElement("Polygon", GMLElements.gmlNS);
  142. writer.WriteStartElement("outerBoundaryIs", GMLElements.gmlNS);
  143. Write(polygon.ExteriorRing as ILinearRing, writer);
  144. writer.WriteEndElement();
  145. for (int i = 0; i < polygon.NumInteriorRings; i++)
  146. {
  147. writer.WriteStartElement("innerBoundaryIs", GMLElements.gmlNS);
  148. Write(polygon.InteriorRings[i] as ILinearRing, writer);
  149. writer.WriteEndElement();
  150. }
  151. writer.WriteEndElement();
  152. }
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. /// <param name="multiPoint"></param>
  157. /// <param name="writer"></param>
  158. protected void Write(IMultiPoint multiPoint, XmlTextWriter writer)
  159. {
  160. writer.WriteStartElement("MultiPoint", GMLElements.gmlNS);
  161. for (int i = 0; i < multiPoint.NumGeometries; i++)
  162. {
  163. writer.WriteStartElement("pointMember", GMLElements.gmlNS);
  164. Write(multiPoint.Geometries[i] as IPoint, writer);
  165. writer.WriteEndElement();
  166. }
  167. writer.WriteEndElement();
  168. }
  169. /// <summary>
  170. ///
  171. /// </summary>
  172. /// <param name="multiLineString"></param>
  173. /// <param name="writer"></param>
  174. protected void Write(IMultiLineString multiLineString, XmlTextWriter writer)
  175. {
  176. writer.WriteStartElement("MultiLineString", GMLElements.gmlNS);
  177. for (int i = 0; i < multiLineString.NumGeometries; i++)
  178. {
  179. writer.WriteStartElement("lineStringMember", GMLElements.gmlNS);
  180. Write(multiLineString.Geometries[i] as ILineString, writer);
  181. writer.WriteEndElement();
  182. }
  183. writer.WriteEndElement();
  184. }
  185. /// <summary>
  186. ///
  187. /// </summary>
  188. /// <param name="multiPolygon"></param>
  189. /// <param name="writer"></param>
  190. protected void Write(IMultiPolygon multiPolygon, XmlTextWriter writer)
  191. {
  192. writer.WriteStartElement("MultiPolygon", GMLElements.gmlNS);
  193. for (int i = 0; i < multiPolygon.NumGeometries; i++)
  194. {
  195. writer.WriteStartElement("polygonMember", GMLElements.gmlNS);
  196. Write(multiPolygon.Geometries[i] as IPolygon, writer);
  197. writer.WriteEndElement();
  198. }
  199. writer.WriteEndElement();
  200. }
  201. /// <summary>
  202. ///
  203. /// </summary>
  204. /// <param name="geometryCollection"></param>
  205. /// <param name="writer"></param>
  206. protected void Write(IGeometryCollection geometryCollection, XmlTextWriter writer)
  207. {
  208. writer.WriteStartElement("MultiGeometry", GMLElements.gmlNS);
  209. for (int i = 0; i < geometryCollection.NumGeometries; i++)
  210. {
  211. writer.WriteStartElement("geometryMember", GMLElements.gmlNS);
  212. Write(geometryCollection.Geometries[i] as IGeometry, writer);
  213. writer.WriteEndElement();
  214. }
  215. writer.WriteEndElement();
  216. }
  217. /// <summary>
  218. /// Sets corrent length for Byte Stream.
  219. /// </summary>
  220. /// <param name="geometry"></param>
  221. /// <returns></returns>
  222. protected byte[] GetBytes(IGeometry geometry)
  223. {
  224. if (geometry is IPoint)
  225. return new byte[SetByteStreamLength(geometry as IPoint)];
  226. else if (geometry is ILineString)
  227. return new byte[SetByteStreamLength(geometry as ILineString)];
  228. else if (geometry is IPolygon)
  229. return new byte[SetByteStreamLength(geometry as IPolygon)];
  230. else if (geometry is IMultiPoint)
  231. return new byte[SetByteStreamLength(geometry as IMultiPoint)];
  232. else if (geometry is IMultiLineString)
  233. return new byte[SetByteStreamLength(geometry as IMultiLineString)];
  234. else if (geometry is IMultiPolygon)
  235. return new byte[SetByteStreamLength(geometry as IMultiPolygon)];
  236. else if (geometry is IGeometryCollection)
  237. return new byte[SetByteStreamLength(geometry as IGeometryCollection)];
  238. else throw new ArgumentException("ShouldNeverReachHere");
  239. }
  240. /// <summary>
  241. /// Sets corrent length for Byte Stream.
  242. /// </summary>
  243. /// <param name="geometry"></param>
  244. /// <returns></returns>
  245. protected int SetByteStreamLength(IGeometry geometry)
  246. {
  247. if (geometry is IPoint)
  248. return SetByteStreamLength(geometry as IPoint);
  249. else if (geometry is ILineString)
  250. return SetByteStreamLength(geometry as ILineString);
  251. else if (geometry is IPolygon)
  252. return SetByteStreamLength(geometry as IPolygon);
  253. else if (geometry is IMultiPoint)
  254. return SetByteStreamLength(geometry as IMultiPoint);
  255. else if (geometry is IMultiLineString)
  256. return SetByteStreamLength(geometry as IMultiLineString);
  257. else if (geometry is IMultiPolygon)
  258. return SetByteStreamLength(geometry as IMultiPolygon);
  259. else if (geometry is IGeometryCollection)
  260. return SetByteStreamLength(geometry as IGeometryCollection);
  261. else throw new ArgumentException("ShouldNeverReachHere");
  262. }
  263. /// <summary>
  264. ///
  265. /// </summary>
  266. /// <param name="geometryCollection"></param>
  267. /// <returns></returns>
  268. protected int SetByteStreamLength(IGeometryCollection geometryCollection)
  269. {
  270. int count = InitValue;
  271. foreach (IGeometry g in geometryCollection.Geometries)
  272. count += SetByteStreamLength(g);
  273. return count;
  274. }
  275. /// <summary>
  276. ///
  277. /// </summary>
  278. /// <param name="multiPolygon"></param>
  279. /// <returns></returns>
  280. protected int SetByteStreamLength(IMultiPolygon multiPolygon)
  281. {
  282. int count = InitValue;
  283. foreach (IPolygon p in multiPolygon.Geometries)
  284. count += SetByteStreamLength(p);
  285. return count;
  286. }
  287. /// <summary>
  288. ///
  289. /// </summary>
  290. /// <param name="multiLineString"></param>
  291. /// <returns></returns>
  292. protected int SetByteStreamLength(IMultiLineString multiLineString)
  293. {
  294. int count = InitValue;
  295. foreach (ILineString ls in multiLineString.Geometries)
  296. count += SetByteStreamLength(ls);
  297. return count;
  298. }
  299. /// <summary>
  300. ///
  301. /// </summary>
  302. /// <param name="multiPoint"></param>
  303. /// <returns></returns>
  304. protected int SetByteStreamLength(IMultiPoint multiPoint)
  305. {
  306. int count = InitValue;
  307. foreach (IPoint p in multiPoint.Geometries)
  308. count += SetByteStreamLength(p);
  309. return count;
  310. }
  311. /// <summary>
  312. ///
  313. /// </summary>
  314. /// <param name="polygon"></param>
  315. /// <returns></returns>
  316. protected int SetByteStreamLength(IPolygon polygon)
  317. {
  318. int count = InitValue;
  319. count += polygon.NumPoints * CoordSize;
  320. return count;
  321. }
  322. /// <summary>
  323. ///
  324. /// </summary>
  325. /// <param name="lineString"></param>
  326. /// <returns></returns>
  327. protected int SetByteStreamLength(ILineString lineString)
  328. {
  329. int count = InitValue;
  330. count += lineString.NumPoints * CoordSize;
  331. return count;
  332. }
  333. /// <summary>
  334. ///
  335. /// </summary>
  336. /// <param name="point"></param>
  337. /// <returns></returns>
  338. protected int SetByteStreamLength(IPoint point)
  339. {
  340. return InitValue + CoordSize;
  341. }
  342. }
  343. }