1 /*
2  * Copyright (c) 2019 Martin Davis.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
8  * and the Eclipse Distribution License is available at
9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.operation.overlayng;
13 
14 import static org.locationtech.jts.operation.overlayng.OverlayNG.UNION;
15 
16 import java.util.Collection;
17 
18 import org.locationtech.jts.geom.Geometry;
19 import org.locationtech.jts.geom.GeometryFactory;
20 import org.locationtech.jts.geom.PrecisionModel;
21 import org.locationtech.jts.operation.union.UnaryUnionOp;
22 import org.locationtech.jts.operation.union.UnionStrategy;
23 
24 /**
25  * Unions a geometry or collection of geometries in an
26  * efficient way, using {@link OverlayNG}
27  * to ensure robust computation.
28  * <p>
29  * This class is most useful for performing UnaryUnion using 
30  * a fixed-precision model. 
31  * For unary union using floating precision,  
32  * {@link OverlayNGRobust#union(Geometry)} should be used.
33  *
34  * @author Martin Davis
35  * @see OverlayNGRobust
36  *
37  */
38 public class UnaryUnionNG {
39 
40   /**
41    * Unions a geometry (which is often a collection)
42    * using a given precision model.
43    *
44    * @param geom the geometry to union
45    * @param pm the precision model to use
46    * @return the union of the geometry
47    */
union(Geometry geom, PrecisionModel pm)48   public static Geometry union(Geometry geom, PrecisionModel pm) {
49     UnaryUnionOp op = new UnaryUnionOp(geom);
50     op.setUnionFunction( createUnionStrategy(pm) );
51     return op.union();
52   }
53 
54   /**
55    * Unions a collection of geometries
56    * using a given precision model.
57    *
58    * @param geoms the collection of geometries to union
59    * @param pm the precision model to use
60    * @return the union of the geometries
61    */
union(Collection<Geometry> geoms, PrecisionModel pm)62   public static Geometry union(Collection<Geometry> geoms, PrecisionModel pm) {
63     UnaryUnionOp op = new UnaryUnionOp(geoms);
64     op.setUnionFunction( createUnionStrategy(pm) );
65     return op.union();
66   }
67 
68   /**
69    * Unions a collection of geometries
70    * using a given precision model.
71    *
72    * @param geoms the collection of geometries to union
73    * @param geomFact the geometry factory to use
74    * @param pm the precision model to use
75    * @return the union of the geometries
76    */
union(Collection<Geometry> geoms, GeometryFactory geomFact, PrecisionModel pm)77   public static Geometry union(Collection<Geometry> geoms, GeometryFactory geomFact, PrecisionModel pm) {
78     UnaryUnionOp op = new UnaryUnionOp(geoms, geomFact);
79     op.setUnionFunction( createUnionStrategy(pm) );
80     return op.union();
81   }
82 
createUnionStrategy(PrecisionModel pm)83   private static UnionStrategy createUnionStrategy(PrecisionModel pm) {
84     UnionStrategy unionSRFun = new UnionStrategy() {
85 
86       public Geometry union(Geometry g0, Geometry g1) {
87         return OverlayNG.overlay(g0, g1, UNION, pm);
88       }
89 
90       @Override
91       public boolean isFloatingPrecision() {
92          return OverlayUtil.isFloating(pm);
93       }
94 
95     };
96     return unionSRFun;
97   }
98 
UnaryUnionNG()99   private UnaryUnionNG() {
100     // no instantiation for now
101   }
102 }
103