/// <summary>
        /// Computes the intersection of the specified <see cref="IGeometry" /> instances.
        /// </summary>
        /// <param name="geometry">The geometry.</param>
        /// <param name="otherGeometry">The other geometry.</param>
        /// <returns>The intersection of the <see cref="IGeometry" /> instances.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// The geometry is null.
        /// or
        /// The other geometry is null.
        /// </exception>
        /// <exception cref="System.ArgumentException">The operation is not supported with the specified geometry types.</exception>
        public static IGeometry Intersection(this IGeometry geometry, IGeometry otherGeometry)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry", "The geometry is null.");
            }
            if (otherGeometry == null)
            {
                throw new ArgumentNullException("otherGeometry", "The other geometry is null.");
            }

            using (IGeometryOverlayOperator op = GetOverlayOperator(geometry))
            {
                return(op.Intersection(geometry, otherGeometry));
            }
        }
Ejemplo n.º 2
0
        public void HalfedgeGeometryOverlayTest()
        {
            IMultiPolygon first = _factory.CreateMultiPolygon(new[]
            {
                _factory.CreatePolygon(
                    _factory.CreatePoint(0, 0),
                    _factory.CreatePoint(10, 0),
                    _factory.CreatePoint(10, 10),
                    _factory.CreatePoint(0, 10)),
                _factory.CreatePolygon(
                    _factory.CreatePoint(10, 0),
                    _factory.CreatePoint(20, 0),
                    _factory.CreatePoint(20, 10),
                    _factory.CreatePoint(10, 10)),
                _factory.CreatePolygon(
                    _factory.CreatePoint(0, 10),
                    _factory.CreatePoint(10, 10),
                    _factory.CreatePoint(10, 20),
                    _factory.CreatePoint(0, 20)),
            });

            IMultiPolygon second = _factory.CreateMultiPolygon(new[]
            {
                _factory.CreatePolygon(
                    _factory.CreatePoint(-5, -5),
                    _factory.CreatePoint(5, -5),
                    _factory.CreatePoint(5, 5),
                    _factory.CreatePoint(-5, 5)),
                _factory.CreatePolygon(
                    _factory.CreatePoint(5, -5),
                    _factory.CreatePoint(15, -5),
                    _factory.CreatePoint(15, 5),
                    _factory.CreatePoint(5, 5)),
                _factory.CreatePolygon(
                    _factory.CreatePoint(-5, -15),
                    _factory.CreatePoint(5, -15),
                    _factory.CreatePoint(5, -5),
                    _factory.CreatePoint(-5, -5)),
            });

            Coordinate[][] expected = new[]
            {
                // ExternalFirst
                new[]
                {
                    new Coordinate(0, 10),
                    new Coordinate(10, 10),
                    new Coordinate(10, 20),
                    new Coordinate(0, 20),
                    new Coordinate(0, 10)
                },
                new[]
                {
                    new Coordinate(0, 5),
                    new Coordinate(5, 5),
                    new Coordinate(10, 5),
                    new Coordinate(10, 10),
                    new Coordinate(0, 10),
                    new Coordinate(0, 5)
                },
                new[]
                {
                    new Coordinate(10, 5),
                    new Coordinate(15, 5),
                    new Coordinate(15, 0),
                    new Coordinate(20, 0),
                    new Coordinate(20, 10),
                    new Coordinate(10, 10),
                    new Coordinate(10, 5)
                },
                // Internal
                new[]
                {
                    new Coordinate(5, 0),
                    new Coordinate(10, 0),
                    new Coordinate(10, 5),
                    new Coordinate(5, 5),
                    new Coordinate(5, 0)
                },
                new[]
                {
                    new Coordinate(10, 0),
                    new Coordinate(15, 0),
                    new Coordinate(15, 5),
                    new Coordinate(10, 5),
                    new Coordinate(10, 0)
                },
                new[]
                {
                    new Coordinate(0, 0),
                    new Coordinate(5, 0),
                    new Coordinate(5, 5),
                    new Coordinate(0, 5),
                    new Coordinate(0, 0)
                },
                // ExternalSecond
                new[]
                {
                    new Coordinate(-5, -15),
                    new Coordinate(5, -15),
                    new Coordinate(5, -5),
                    new Coordinate(-5, -5),
                    new Coordinate(-5, -15)
                },
                new[]
                {
                    new Coordinate(-5, -5),
                    new Coordinate(5, -5),
                    new Coordinate(5, 0),
                    new Coordinate(0, 0),
                    new Coordinate(0, 5),
                    new Coordinate(-5, 5),
                    new Coordinate(-5, -5)
                },
                new[]
                {
                    new Coordinate(5, -5),
                    new Coordinate(15, -5),
                    new Coordinate(15, 0),
                    new Coordinate(10, 0),
                    new Coordinate(5, 0),
                    new Coordinate(5, -5)
                }
            };

            // Difference
            IGeometry result = _operator.Difference(first, second);

            Assert.IsInstanceOf <IGeometryCollection <IPolygon> >(result);

            IGeometryCollection <IPolygon> collection = (IGeometryCollection <IPolygon>)result;

            Assert.AreEqual(new[]
            {
                expected[0],
                expected[1],
                expected[2],
            }.ToRingSet(), collection.Select(polygon => polygon.Shell).ToRingSet());

            // Intersection
            result = _operator.Intersection(first, second);
            Assert.IsInstanceOf <IGeometryCollection <IPolygon> >(result);

            collection = (IGeometryCollection <IPolygon>)result;
            Assert.AreEqual(new[]
            {
                expected[3],
                expected[4],
                expected[5],
            }.ToRingSet(), collection.Select(polygon => polygon.Shell).ToRingSet());

            // Symmetric difference
            result = _operator.SymmetricDifference(first, second);
            Assert.IsInstanceOf <IGeometryCollection <IPolygon> >(result);

            collection = (IGeometryCollection <IPolygon>)result;
            Assert.AreEqual(new[]
            {
                expected[0],
                expected[1],
                expected[2],
                expected[6],
                expected[7],
                expected[8],
            }.ToRingSet(), collection.Select(polygon => polygon.Shell).ToRingSet());

            // Union
            result = _operator.Union(first, second);
            Assert.IsInstanceOf <IGeometryCollection <IPolygon> >(result);

            collection = (IGeometryCollection <IPolygon>)result;
            Assert.AreEqual(expected.ToRingSet(), collection.Select(polygon => polygon.Shell).ToRingSet());
        }