root / trunk / libraries / libTopology / src / org / gvsig / jts / GeometryCracker.java @ 13591
History | View | Annotate | Download (2.6 KB)
1 |
package org.gvsig.jts; |
---|---|
2 |
|
3 |
import com.vividsolutions.jts.algorithms.SnapCGAlgorithms; |
4 |
import com.vividsolutions.jts.geom.Coordinate; |
5 |
import com.vividsolutions.jts.geom.CoordinateList; |
6 |
import com.vividsolutions.jts.geom.Geometry; |
7 |
import com.vividsolutions.jts.geom.GeometryFactory; |
8 |
import com.vividsolutions.jts.geom.LineSegment; |
9 |
import com.vividsolutions.jts.geom.LineString; |
10 |
|
11 |
/**
|
12 |
* Cracks the geometries of a topology's layers to force the sharing of
|
13 |
* coordinates
|
14 |
*
|
15 |
*/
|
16 |
public class GeometryCracker { |
17 |
|
18 |
private static GeometryFactory geomFactory = new GeometryFactory(); |
19 |
|
20 |
private LineSegment seg = new LineSegment(); |
21 |
private double snapTolerance; |
22 |
|
23 |
public GeometryCracker(double snapTolerance) |
24 |
{ |
25 |
this.snapTolerance = snapTolerance;
|
26 |
} |
27 |
|
28 |
|
29 |
|
30 |
/**
|
31 |
* "Cracks" a geometry a with the points of a given geometry b.
|
32 |
* @param a
|
33 |
* @param b
|
34 |
* @param snapTolerance
|
35 |
* @return
|
36 |
*/
|
37 |
public static Geometry crack(LineString a, LineString b, double snapTolerance) { |
38 |
GeometryCracker cracker = new GeometryCracker(snapTolerance);
|
39 |
Coordinate[] newCoordsA = cracker.crackTo(a.getCoordinates(), b.getCoordinates());
|
40 |
return geomFactory.createLinearRing(newCoordsA);
|
41 |
} |
42 |
|
43 |
|
44 |
/*
|
45 |
* This code is extracted from the class LineStringSnapper of JTS
|
46 |
* */
|
47 |
private Coordinate[] crackTo(Coordinate[] srcPts, Coordinate[] snapPts) { |
48 |
CoordinateList coordList = new CoordinateList(srcPts);
|
49 |
crackSegments(coordList, snapPts); |
50 |
Coordinate[] newPts = coordList.toCoordinateArray();
|
51 |
return newPts;
|
52 |
} |
53 |
|
54 |
private void crackSegments(CoordinateList srcCoords, Coordinate[] snapPts) { |
55 |
int distinctPtCount = snapPts.length;
|
56 |
|
57 |
Coordinate firstPoint = snapPts[0];
|
58 |
Coordinate lastPoint = snapPts[snapPts.length - 1];
|
59 |
if(SnapCGAlgorithms.snapEquals2D(firstPoint, lastPoint, snapTolerance))
|
60 |
distinctPtCount = snapPts.length - 1;
|
61 |
|
62 |
for (int i = 0; i < distinctPtCount; i++) { |
63 |
Coordinate snapPt = snapPts[i]; |
64 |
int index = findSegmentIndexToSnap(snapPt, srcCoords);
|
65 |
if (index >= 0) { |
66 |
srcCoords.add(index + 1, new Coordinate(snapPt), false); |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
|
72 |
private int findSegmentIndexToSnap(Coordinate snapPt, CoordinateList srcCoords) { |
73 |
double minDist = Double.MAX_VALUE; |
74 |
int snapIndex = -1; |
75 |
for (int i = 0; i < srcCoords.size() - 1; i++) { |
76 |
seg.p0 = (Coordinate) srcCoords.get(i); |
77 |
seg.p1 = (Coordinate) srcCoords.get(i + 1);
|
78 |
|
79 |
if(SnapCGAlgorithms.snapEquals2D(seg.p0, snapPt, snapTolerance)
|
80 |
|| SnapCGAlgorithms.snapEquals2D(seg.p1, snapPt, snapTolerance)) |
81 |
return -1; |
82 |
double dist = seg.distance(snapPt);
|
83 |
if (dist < snapTolerance && dist < minDist) {
|
84 |
minDist = dist; |
85 |
snapIndex = i; |
86 |
} |
87 |
} |
88 |
return snapIndex;
|
89 |
} |
90 |
|
91 |
} |