PageRenderTime 161ms CodeModel.GetById 52ms RepoModel.GetById 16ms app.codeStats 0ms

/src/NHibernate.Spatial/src/Tests.NHibernate.Spatial/ProjectionsFixture.cs

https://bitbucket.org/dabide/nhcontrib
C# | 169 lines | 132 code | 36 blank | 1 comment | 0 complexity | 62dbe8b4358e66e57b8a2261af268b13 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Apache-2.0, LGPL-3.0, LGPL-2.1
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using GeoAPI.Geometries;
  5. using NetTopologySuite.Geometries;
  6. using NHibernate;
  7. using NHibernate.Criterion;
  8. using NHibernate.Linq;
  9. using NHibernate.Spatial.Criterion;
  10. using NHibernate.Spatial.Linq;
  11. using NUnit.Framework;
  12. using Tests.NHibernate.Spatial.Model;
  13. namespace Tests.NHibernate.Spatial
  14. {
  15. public abstract class ProjectionsFixture : AbstractFixture
  16. {
  17. protected override Type[] Mappings
  18. {
  19. get
  20. {
  21. return new Type[] {
  22. typeof(County)
  23. };
  24. }
  25. }
  26. private ISession session;
  27. protected override void OnSetUp()
  28. {
  29. session = sessions.OpenSession();
  30. session.Save(new County("aaaa", "AA", Wkt.Read("POLYGON((1 0, 2 0, 2 1, 1 1, 1 0))")));
  31. session.Save(new County("bbbb", "BB", Wkt.Read("POLYGON((1 1, 2 1, 2 2, 1 2, 1 1))")));
  32. session.Save(new County("cccc", "BB", Wkt.Read("POLYGON((2 1, 3 1, 3 2, 2 2, 2 1))")));
  33. session.Save(new County("dddd", "AA", Wkt.Read("POLYGON((2 0, 3 0, 3 1, 2 1, 2 0))")));
  34. session.Flush();
  35. }
  36. protected override void OnTearDown()
  37. {
  38. DeleteMappings(session);
  39. session.Close();
  40. }
  41. [Test]
  42. public void CountAndUnion()
  43. {
  44. IList results = session.CreateCriteria(typeof(County))
  45. .SetProjection(Projections.ProjectionList()
  46. .Add(Projections.RowCount())
  47. .Add(SpatialProjections.Union("Boundaries"))
  48. )
  49. .List();
  50. Assert.AreEqual(1, results.Count);
  51. object[] result = (object[])results[0];
  52. IGeometry expected = Wkt.Read("POLYGON((1 0, 1 1, 1 2, 2 2, 3 2, 3 1, 3 0, 2 0, 1 0))");
  53. IGeometry aggregated = (IGeometry)result[1];
  54. Assert.AreEqual(4, result[0]);
  55. Assert.IsTrue(expected.Equals(aggregated));
  56. }
  57. [Test]
  58. public void CountAndUnionByState()
  59. {
  60. IList results = session.CreateCriteria(typeof(County))
  61. .AddOrder(Order.Asc("State"))
  62. .SetProjection(Projections.ProjectionList()
  63. .Add(Projections.GroupProperty("State"))
  64. .Add(Projections.RowCount())
  65. .Add(SpatialProjections.Union("Boundaries"))
  66. )
  67. .List();
  68. CountAndUnionByState(results);
  69. }
  70. [Test]
  71. public void CountAndUnionByStateLambda()
  72. {
  73. var results = session.QueryOver<County>()
  74. .Select(
  75. Projections.ProjectionList()
  76. .Add(Projections.Group<County>(o => o.State))
  77. .Add(Projections.RowCount())
  78. .Add(SpatialProjections.Union<County>(o => o.Boundaries)))
  79. .OrderBy(o => o.State).Asc
  80. .List<object[]>();
  81. CountAndUnionByState((IList)results);
  82. }
  83. private static void CountAndUnionByState(IList results)
  84. {
  85. Assert.AreEqual(2, results.Count);
  86. object[] resultAA = (object[])results[0];
  87. object[] resultBB = (object[])results[1];
  88. int countAA = (int)resultAA[1];
  89. int countBB = (int)resultBB[1];
  90. IGeometry aggregatedAA = (IGeometry)resultAA[2];
  91. IGeometry aggregatedBB = (IGeometry)resultBB[2];
  92. IGeometry expectedAA = Wkt.Read("POLYGON((1 0, 1 1, 3 1, 3 0, 1 0))");
  93. IGeometry expectedBB = Wkt.Read("POLYGON((1 1, 1 2, 3 2, 3 1, 1 1))");
  94. Assert.AreEqual(2, countAA);
  95. Assert.AreEqual(2, countBB);
  96. Assert.IsTrue(expectedAA.Equals(aggregatedAA));
  97. Assert.IsTrue(expectedBB.Equals(aggregatedBB));
  98. }
  99. [Test]
  100. public void EnvelopeAll()
  101. {
  102. IList results = session.CreateCriteria(typeof(County))
  103. .SetProjection(SpatialProjections.Envelope("Boundaries"))
  104. .List();
  105. Assert.AreEqual(1, results.Count);
  106. IGeometry aggregated = (IGeometry)results[0];
  107. IEnvelope expected = new Envelope(1, 3, 0, 2);
  108. Assert.IsTrue(expected.Equals(aggregated.EnvelopeInternal));
  109. }
  110. [Test]
  111. public void CollectAll()
  112. {
  113. IList results = session.CreateCriteria(typeof(County))
  114. .SetProjection(SpatialProjections.Collect("Boundaries"))
  115. .List();
  116. Assert.AreEqual(1, results.Count);
  117. IGeometry aggregated = (IGeometry)results[0];
  118. Assert.AreEqual(4, aggregated.NumGeometries);
  119. //Assert.AreEqual("GEOMETRYCOLLECTION", aggregated.GeometryType);
  120. }
  121. [Test]
  122. public void IntersectionAll()
  123. {
  124. IList results = session.CreateCriteria(typeof(County))
  125. .SetProjection(SpatialProjections.Intersection("Boundaries"))
  126. .List();
  127. Assert.AreEqual(1, results.Count);
  128. IGeometry aggregated = (IGeometry)results[0];
  129. IGeometry expected = new Point(2, 1);
  130. Assert.IsTrue(expected.Equals(aggregated));
  131. }
  132. }
  133. }