| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package org.locationtech.jts.operation.overlay.snap; |
| 14 |
|
| 15 |
import org.locationtech.jts.geom.Geometry; |
| 16 |
import org.locationtech.jts.operation.overlay.OverlayOp; |
| 17 |
import org.locationtech.jts.precision.CommonBitsRemover; |
| 18 |
|
| 19 |
/** |
| 20 |
* Performs an overlay operation using snapping and enhanced precision |
| 21 |
* to improve the robustness of the result. |
| 22 |
* This class <i>always</i> uses snapping. |
| 23 |
* This is less performant than the standard JTS overlay code, |
| 24 |
* and may even introduce errors which were not present in the original data. |
| 25 |
* For this reason, this class should only be used |
| 26 |
* if the standard overlay code fails to produce a correct result. |
| 27 |
* |
| 28 |
* @author Martin Davis |
| 29 |
* @version 1.7 |
| 30 |
*/ |
| 31 |
public class SnapOverlayOp |
| 32 |
{ |
| 33 |
public static Geometry overlayOp(Geometry g0, Geometry g1, int opCode) |
| 34 |
{ |
| 35 |
SnapOverlayOp op = new SnapOverlayOp(g0, g1); |
| 36 |
return op.getResultGeometry(opCode); |
| 37 |
} |
| 38 |
|
| 39 |
public static Geometry intersection(Geometry g0, Geometry g1) |
| 40 |
{ |
| 41 |
return overlayOp(g0, g1, OverlayOp.INTERSECTION); |
| 42 |
} |
| 43 |
|
| 44 |
public static Geometry union(Geometry g0, Geometry g1) |
| 45 |
{ |
| 46 |
return overlayOp(g0, g1, OverlayOp.UNION); |
| 47 |
} |
| 48 |
|
| 49 |
public static Geometry difference(Geometry g0, Geometry g1) |
| 50 |
{ |
| 51 |
return overlayOp(g0, g1, OverlayOp.DIFFERENCE); |
| 52 |
} |
| 53 |
|
| 54 |
public static Geometry symDifference(Geometry g0, Geometry g1) |
| 55 |
{ |
| 56 |
return overlayOp(g0, g1, OverlayOp.SYMDIFFERENCE); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
private Geometry[] geom = new Geometry[2]; |
| 61 |
private double snapTolerance; |
| 62 |
|
| 63 |
public SnapOverlayOp(Geometry g1, Geometry g2) |
| 64 |
{ |
| 65 |
geom[0] = g1; |
| 66 |
geom[1] = g2; |
| 67 |
computeSnapTolerance(); |
| 68 |
} |
| 69 |
private void computeSnapTolerance() |
| 70 |
{ |
| 71 |
snapTolerance = GeometrySnapper.computeOverlaySnapTolerance(geom[0], geom[1]); |
| 72 |
|
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public Geometry getResultGeometry(int opCode) |
| 77 |
{ |
| 78 |
|
| 79 |
Geometry[] prepGeom = snap(geom); |
| 80 |
Geometry result = OverlayOp.overlayOp(prepGeom[0], prepGeom[1], opCode); |
| 81 |
return prepareResult(result); |
| 82 |
} |
| 83 |
|
| 84 |
private Geometry selfSnap(Geometry geom) |
| 85 |
{ |
| 86 |
GeometrySnapper snapper0 = new GeometrySnapper(geom); |
| 87 |
Geometry snapGeom = snapper0.snapTo(geom, snapTolerance); |
| 88 |
|
| 89 |
|
| 90 |
return snapGeom; |
| 91 |
} |
| 92 |
|
| 93 |
private Geometry[] snap(Geometry[] geom) |
| 94 |
{ |
| 95 |
Geometry[] remGeom = removeCommonBits(geom); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
Geometry[] snapGeom = GeometrySnapper.snap(remGeom[0], remGeom[1], snapTolerance); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
return snapGeom; |
| 111 |
} |
| 112 |
|
| 113 |
private Geometry prepareResult(Geometry geom) |
| 114 |
{ |
| 115 |
cbr.addCommonBits(geom); |
| 116 |
return geom; |
| 117 |
} |
| 118 |
|
| 119 |
private CommonBitsRemover cbr; |
| 120 |
|
| 121 |
private Geometry[] removeCommonBits(Geometry[] geom) |
| 122 |
{ |
| 123 |
cbr = new CommonBitsRemover(); |
| 124 |
cbr.add(geom[0]); |
| 125 |
cbr.add(geom[1]); |
| 126 |
Geometry remGeom[] = new Geometry[2]; |
| 127 |
remGeom[0] = cbr.removeCommonBits(geom[0].copy()); |
| 128 |
remGeom[1] = cbr.removeCommonBits(geom[1].copy()); |
| 129 |
return remGeom; |
| 130 |
} |
| 131 |
|
| 132 |
private void checkValid(Geometry g) |
| 133 |
{ |
| 134 |
if (! g.isValid()) { |
| 135 |
System.out.println("Snapped geometry is invalid"); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|