| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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 |
|
| 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 |
|
| 70 |
if (exteriorCoords.size() == 0) |
| 71 |
return otherGeom; |
| 72 |
|
| 73 |
|
| 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 |
|
| 84 |
return GeometryCombiner.combine(ptComp, otherGeom); |
| 85 |
} |
| 86 |
} |
| 87 |
|