Class PointGeometryUnion

 1 /*
 2  * Copyright (c) 2016 Vivid Solutions.
 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  
13 package org.locationtech.jts.operation.union;
14  
15 import java.util.Set;
16 import java.util.TreeSet;
17  
18 import org.locationtech.jts.algorithm.PointLocator;
19 import org.locationtech.jts.geom.Coordinate;
20 import org.locationtech.jts.geom.CoordinateArrays;
21 import org.locationtech.jts.geom.Geometry;
22 import org.locationtech.jts.geom.GeometryFactory;
23 import org.locationtech.jts.geom.Location;
24 import org.locationtech.jts.geom.Point;
25 import org.locationtech.jts.geom.Puntal;
26 import org.locationtech.jts.geom.util.GeometryCombiner;
27  
28 /**
29  * Computes the union of a {@link Puntal} geometry with 
30  * another arbitrary {@link Geometry}.
31  * Does not copy any component geometries.
32  * 
33  * @author mbdavis
34  *
35  */
36 public class PointGeometryUnion 
37 {
38     public static Geometry union(Puntal pointGeom, Geometry otherGeom)
39     {
40         PointGeometryUnion unioner = new PointGeometryUnion(pointGeom, otherGeom);
41         return unioner.union();
42     }
43     
44     private Geometry pointGeom;
45     private Geometry otherGeom;
46     private GeometryFactory geomFact;
47     
48     public PointGeometryUnion(Puntal pointGeom, Geometry otherGeom)
49     {
50         this.pointGeom = (Geometry) pointGeom;
51         this.otherGeom = otherGeom;
52         geomFact = otherGeom.getFactory();
53     }
54     
55     public Geometry union()
56     {
57         PointLocator locater = new PointLocator();
58         // use a set to eliminate duplicates, as required for union
59         Set exteriorCoords = new TreeSet();
60         
61         for (int i =0 ; i < pointGeom.getNumGeometries(); i++) {
62             Point point = (Point) pointGeom.getGeometryN(i);
63             Coordinate coord = point.getCoordinate();
64             int loc = locater.locate(coord, otherGeom);
65             if (loc == Location.EXTERIOR)
66                 exteriorCoords.add(coord);
67         }
68         
69         // if no points are in exterior, return the other geom
70         if (exteriorCoords.size() == 0)
71             return otherGeom;
72         
73         // make a puntal geometry of appropriate size
74         Geometry ptComp = null;
75         Coordinate[] coords = CoordinateArrays.toCoordinateArray(exteriorCoords);
76         if (coords.length == 1) {
77             ptComp = geomFact.createPoint(coords[0]);
78         }
79         else {
80             ptComp = geomFact.createMultiPointFromCoords(coords);
81         }
82         
83         // add point component to the other geometry
84         return GeometryCombiner.combine(ptComp, otherGeom);
85     }
86 }
87