public void GeometryDistanceAlgorithmDistancePointsTest() { BasicPoint first = new BasicPoint(new Coordinate(0, 10)); BasicPoint second = new BasicPoint(new Coordinate(10, 10)); GeometryDistanceAlgorithm.Distance(first, second).ShouldBe(10); first = new BasicPoint(new Coordinate(10, 10)); second = new BasicPoint(new Coordinate(10, 0)); GeometryDistanceAlgorithm.Distance(first, second).ShouldBe(10); first = new BasicPoint(new Coordinate(67, 75)); second = new BasicPoint(new Coordinate(17, 89)); GeometryDistanceAlgorithm.Distance(first, second).ShouldBe(51.92, 0.01); // same point GeometryDistanceAlgorithm.Distance(first, first).ShouldBe(0); // same coordinate second = new BasicPoint(first.Coordinate); GeometryDistanceAlgorithm.Distance(first, second).ShouldBe(0); // undefined coordinate second = new BasicPoint(Coordinate.Undefined); GeometryDistanceAlgorithm.Distance(first, second).ShouldBe(Double.NaN); }
public void MTreeAddTest() { MTree <IPoint> tree = new MTree <IPoint>(2, 3, (x, y) => GeometryDistanceAlgorithm.Distance(x, y)); tree.Add(this.geometries[0]); tree.NumberOfDataItems.ShouldBe(1); tree.Add(this.geometries[1]); tree.NumberOfDataItems.ShouldBe(2); tree.Add(this.geometries[2]); tree.NumberOfDataItems.ShouldBe(3); tree.Add(this.geometries[3]); tree.NumberOfDataItems.ShouldBe(4); for (int i = 4; i < this.geometries.Count; i++) { tree.Add(this.geometries[i]); } tree.NumberOfDataItems.ShouldBe(this.geometries.Count); tree = new MTree <IPoint>(2, 3, (x, y) => GeometryDistanceAlgorithm.Distance(x, y)); tree.Add(this.geometries); tree.NumberOfDataItems.ShouldBe(this.geometries.Count); Should.Throw <ArgumentNullException>(() => tree.Add((IPoint)null)); Should.Throw <ArgumentNullException>(() => tree.Add((IEnumerable <IPoint>)null)); // try to insert item already in the tree Should.Throw <ArgumentException>(() => tree.Add(this.geometries[0])); }
public void SetUp() { this.factory = new GeometryFactory(); this.geometries = new List <IPoint>(Enumerable.Range(1, 1000).Select(value => this.factory.CreatePoint(value, value, value))); this.tree = new MTree <IPoint>((x, y) => GeometryDistanceAlgorithm.Distance(x, y)); this.tree.Add(this.geometries); }
public void MTreeSearchTest() { MTree <IPoint> tree = new MTree <IPoint>(2, 3, (x, y) => GeometryDistanceAlgorithm.Distance(x, y)); tree.Search(this.geometries[0]).ShouldBeEmpty(); IPoint[] points = new IPoint[9]; for (Int32 i = 0; i < 9; i++) { points[i] = this.factory.CreatePoint(i, 0); tree.Add(points[i]); } // search without limits should return all items in the tree tree.Search(this.factory.CreatePoint(2, 0)).Count().ShouldBe(9); // search returns the correct data items in the correct order with correct distance information List <ResultItem <IPoint> > results = new List <ResultItem <IPoint> >(tree.Search(this.factory.CreatePoint(8, 0))); List <IPoint> expectedResults = new List <IPoint>(points.Reverse()); for (Int32 i = 0; i < points.Length; i++) { results[i].Item.ShouldBe(expectedResults[i]); results[i].Distance.ShouldBe(i); } // search with a given radius should return only the elements within that radius tree.Search(this.factory.CreatePoint(8, 0), 1.0D).Count().ShouldBe(2); tree.Search(this.factory.CreatePoint(7, 0), 1.0D).Count().ShouldBe(3); tree.Search(this.factory.CreatePoint(9, 0), 1.0D).Count().ShouldBe(1); tree.Search(this.factory.CreatePoint(10, 0), 1.0D).Count().ShouldBe(0); // search with a given limit should return only that amount of elements tree.Search(this.factory.CreatePoint(2, 0), 0).Count().ShouldBe(0); tree.Search(this.factory.CreatePoint(2, 0), 1).Count().ShouldBe(1); tree.Search(this.factory.CreatePoint(2, 0), 5).Count().ShouldBe(5); tree.Search(this.factory.CreatePoint(2, 0), 10).Count().ShouldBe(9); // search with both radius and limit where radius contains less elements than limit tree.Search(this.factory.CreatePoint(2, 0), 1.1D, 5).Count().ShouldBe(3); // search with both radius and limit where radius contains more elements than limit tree.Search(this.factory.CreatePoint(2, 0), 1.1D, 2).Count().ShouldBe(2); // errors Should.Throw <ArgumentNullException>(() => tree.Search(null)); }
public void GeometryDistanceAlgorithmDistancePolygonsTest() { // point outside polygon BasicPoint point = new BasicPoint(new Coordinate(1, 4)); BasicPolygon polygon = new BasicPolygon(new[] { new Coordinate(0, 0), new Coordinate(2, 0), new Coordinate(2, 2), new Coordinate(0, 2) }); GeometryDistanceAlgorithm.Distance(point, polygon).ShouldBe(2); // point on polygon shell point = new BasicPoint(new Coordinate(2, 1)); polygon = new BasicPolygon(new[] { new Coordinate(0, 0), new Coordinate(2, 0), new Coordinate(2, 2), new Coordinate(0, 2) }); GeometryDistanceAlgorithm.Distance(point, polygon).ShouldBe(0); // point in polygon point = new BasicPoint(new Coordinate(1, 1)); polygon = new BasicPolygon(new[] { new Coordinate(0, 0), new Coordinate(2, 0), new Coordinate(2, 2), new Coordinate(0, 2) }); GeometryDistanceAlgorithm.Distance(point, polygon).ShouldBe(0); }
public void MTreeConstructorTest() { MTree <IPoint> tree = new MTree <IPoint>((x, y) => GeometryDistanceAlgorithm.Distance(x, y)); tree.NumberOfDataItems.ShouldBe(0); tree.MinChildren.ShouldBe(50); tree.MaxChildren.ShouldBe(101); tree = new MTree <IPoint>(2, 4, (x, y) => GeometryDistanceAlgorithm.Distance(x, y)); tree.NumberOfDataItems.ShouldBe(0); tree.MinChildren.ShouldBe(2); tree.MaxChildren.ShouldBe(4); tree = new MTree <IPoint>(10, 100, (x, y) => GeometryDistanceAlgorithm.Distance(x, y)); tree.NumberOfDataItems.ShouldBe(0); tree.MinChildren.ShouldBe(10); tree.MaxChildren.ShouldBe(100); Should.Throw <ArgumentOutOfRangeException>(() => new MTree <IPoint>(-10, 1, (x, y) => GeometryDistanceAlgorithm.Distance(x, y))); Should.Throw <ArgumentOutOfRangeException>(() => new MTree <IPoint>(0, 1, (x, y) => GeometryDistanceAlgorithm.Distance(x, y))); Should.Throw <ArgumentOutOfRangeException>(() => new MTree <IPoint>(1, 1, (x, y) => GeometryDistanceAlgorithm.Distance(x, y))); Should.Throw <ArgumentNullException>(() => new MTree <IPoint>(3, 5, null)); }
public void GeometryDistanceAlgorithmDistanceLineStringsTest() { // parallel line strings BasicLineString firstLineString = new BasicLineString(new[] { new Coordinate(2, 2), new Coordinate(2, -2) }); BasicLineString secondLineString = new BasicLineString(new[] { new Coordinate(4, 2), new Coordinate(4, -2) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(2); firstLineString = new BasicLineString(new[] { new Coordinate(0, 0), new Coordinate(2, 2) }); secondLineString = new BasicLineString(new[] { new Coordinate(0, 2), new Coordinate(2, 4) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(Math.Sqrt(2), 0.001); firstLineString = new BasicLineString(new[] { new Coordinate(2, 2), new Coordinate(2, -2), new Coordinate(10, -2), new Coordinate(10, 2) }); secondLineString = new BasicLineString(new[] { new Coordinate(2, 24), new Coordinate(2, 22), new Coordinate(10, 22), new Coordinate(10, 24) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(20); // one line is skewed towards the other firstLineString = new BasicLineString(new[] { new Coordinate(2, 2), new Coordinate(2, -2) }); secondLineString = new BasicLineString(new Coordinate[] { new Coordinate(4, 2), new Coordinate(3, -2) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(1); // a line and a line string, where the line string's middle point is the closest to the middle of the line firstLineString = new BasicLineString(new[] { new Coordinate(2, 2), new Coordinate(2, -2) }); secondLineString = new BasicLineString(new[] { new Coordinate(4, 2), new Coordinate(3, 0), new Coordinate(7, -2) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(1); // intersecting lines firstLineString = new BasicLineString(new[] { new Coordinate(0, 0), new Coordinate(2, 2) }); secondLineString = new BasicLineString(new[] { new Coordinate(0, 2), new Coordinate(2, 0) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(0); firstLineString = new BasicLineString(new[] { new Coordinate(0, 0), new Coordinate(1, 1) }); secondLineString = new BasicLineString(new[] { new Coordinate(0, 1), new Coordinate(1, 0) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(0); firstLineString = new BasicLineString(new[] { new Coordinate(0, 0), new Coordinate(1, 1) }); secondLineString = new BasicLineString(new[] { new Coordinate(1, 1), new Coordinate(2, 2) }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(0); // point on line BasicPoint point = new BasicPoint(new Coordinate(1, 1)); secondLineString = new BasicLineString(new[] { new Coordinate(0, 0), new Coordinate(2, 2) }); GeometryDistanceAlgorithm.Distance(point, secondLineString).ShouldBe(0); point = new BasicPoint(new Coordinate(1.5, 1.5)); secondLineString = new BasicLineString(new[] { new Coordinate(0, 0), new Coordinate(1, 1), new Coordinate(2, 2) }); GeometryDistanceAlgorithm.Distance(point, secondLineString).ShouldBe(0); // empty line strings secondLineString = new BasicLineString(new Coordinate[] { }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(Double.MaxValue); firstLineString = new BasicLineString(new Coordinate[] { }); secondLineString = new BasicLineString(new Coordinate[] { }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(Double.MaxValue); // invalid line secondLineString = new BasicLineString(new Coordinate[] { new Coordinate(0, 0), Coordinate.Undefined }); GeometryDistanceAlgorithm.Distance(firstLineString, secondLineString).ShouldBe(Double.MaxValue); }