| 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.geom.TopologyException; |
| 17 |
import org.locationtech.jts.operation.overlay.OverlayOp; |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Performs an overlay operation using snapping and enhanced precision |
| 22 |
* to improve the robustness of the result. |
| 23 |
* This class only uses snapping |
| 24 |
* if an error is detected when running the standard JTS overlay code. |
| 25 |
* Errors detected include thrown exceptions |
| 26 |
* (in particular, {@link TopologyException}) |
| 27 |
* and invalid overlay computations. |
| 28 |
* |
| 29 |
* @author Martin Davis |
| 30 |
* @version 1.7 |
| 31 |
*/ |
| 32 |
public class SnapIfNeededOverlayOp |
| 33 |
{ |
| 34 |
public static Geometry overlayOp(Geometry g0, Geometry g1, int opCode) |
| 35 |
{ |
| 36 |
SnapIfNeededOverlayOp op = new SnapIfNeededOverlayOp(g0, g1); |
| 37 |
return op.getResultGeometry(opCode); |
| 38 |
} |
| 39 |
|
| 40 |
public static Geometry intersection(Geometry g0, Geometry g1) |
| 41 |
{ |
| 42 |
return overlayOp(g0, g1, OverlayOp.INTERSECTION); |
| 43 |
} |
| 44 |
|
| 45 |
public static Geometry union(Geometry g0, Geometry g1) |
| 46 |
{ |
| 47 |
return overlayOp(g0, g1, OverlayOp.UNION); |
| 48 |
} |
| 49 |
|
| 50 |
public static Geometry difference(Geometry g0, Geometry g1) |
| 51 |
{ |
| 52 |
return overlayOp(g0, g1, OverlayOp.DIFFERENCE); |
| 53 |
} |
| 54 |
|
| 55 |
public static Geometry symDifference(Geometry g0, Geometry g1) |
| 56 |
{ |
| 57 |
return overlayOp(g0, g1, OverlayOp.SYMDIFFERENCE); |
| 58 |
} |
| 59 |
|
| 60 |
private Geometry[] geom = new Geometry[2]; |
| 61 |
|
| 62 |
public SnapIfNeededOverlayOp(Geometry g1, Geometry g2) |
| 63 |
{ |
| 64 |
geom[0] = g1; |
| 65 |
geom[1] = g2; |
| 66 |
} |
| 67 |
|
| 68 |
public Geometry getResultGeometry(int opCode) |
| 69 |
{ |
| 70 |
Geometry result = null; |
| 71 |
boolean isSuccess = false; |
| 72 |
RuntimeException savedException = null; |
| 73 |
try { |
| 74 |
|
| 75 |
result = OverlayOp.overlayOp(geom[0], geom[1], opCode); |
| 76 |
boolean isValid = true; |
| 77 |
|
| 78 |
|
| 79 |
if (isValid) |
| 80 |
isSuccess = true; |
| 81 |
} |
| 82 |
catch (RuntimeException ex) { |
| 83 |
savedException = ex; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
} |
| 91 |
if (! isSuccess) { |
| 92 |
|
| 93 |
|
| 94 |
try { |
| 95 |
result = SnapOverlayOp.overlayOp(geom[0], geom[1], opCode); |
| 96 |
} |
| 97 |
catch (RuntimeException ex) { |
| 98 |
throw savedException; |
| 99 |
} |
| 100 |
} |
| 101 |
return result; |
| 102 |
} |
| 103 |
} |
| 104 |
|