开源社区里利用JTS库进行空间处理的代码参考(八)

2014-11-24 07:56:16 · 作者: · 浏览: 10
s oriented counter-clockwise.
*/
public static boolean isCCW(final CoordinateSequence ring) {
// # of points without closing endpoint
int nPts = ring.size() - 1;

// find highest point
Coordinate hiPt = ring.getCoordinate(0);
int hiIndex = 0;
for (int i = 1; i <= nPts; i++) {
Coordinate p = ring.getCoordinate(i);
if (p.y > hiPt.y) {
hiPt = p;
hiIndex = i;
}
}

// find distinct point before highest point
int iPrev = hiIndex;
do {
iPrev = iPrev - 1;
if (iPrev < 0) {
iPrev = nPts;
}
} while (ring.getCoordinate(iPrev).equals2D(hiPt) && iPrev != hiIndex);

// find distinct point after highest point
int iNext = hiIndex;
do {
iNext = (iNext + 1) % nPts;
} while (ring.getCoordinate(iNext).equals2D(hiPt) && iNext != hiIndex);

Coordinate prev = ring.getCoordinate(iPrev);
Coordinate next = ring.getCoordinate(iNext);

/**
* This check catches cases where the ring contains an A-B-A configuration
* of points. This can happen if the ring does not contain 3 distinct points
* (including the case where the input array has fewer than 4 elements), or
* it contains coincident line segments.
*/
if (prev.equals2D(hiPt) || next.equals2D(hiPt) || prev.equals2D(next)) {
return false;
}

int disc = CGAlgorithms.computeOrientation(prev, hiPt, next);

/**
* If disc is exactly 0, lines are collinear. There are two possible cases:
* (1) the lines lie along the x axis in opposite directions (2) the lines
* lie on top of one another (1) is handled by checking if next is left of
* prev ==> CCW (2) will never happen if the ring is valid, so don't check
* for it (Might want to assert this)
*/
boolean isCCW = false;
if (disc == 0) {
// poly is CCW if prev x is right of next x
isCCW = (prev.x > next.x);
} else {
// if area is positive, points are ordered CCW
isCCW = (disc > 0);
}
return isCCW;
}

public static void addElevation(final Coordinate coordinate, final LineString line) {
CoordinateSequence coordinates = line.getCoordinateSequence();
Coordinate previousCoordinate = coordinates.getCoordinate(0);
for (int i = 1; i < coordinates.size(); i++) {
Coordinate currentCoordinate = coordinates.getCoordinate(i);
LineSegment3D segment = new LineSegment3D(previousCoordinate,
currentCoordinate);
if (segment.distance(coordinate) < 1) {
PrecisionModel precisionModel = line.getFactory().getPrecisionModel();
addElevation(precisionModel, coordinate, segment);
return;
}
previousCoordinate = currentCoordinate;
}

}

public static Geometry difference2DZ(final LineString line, final Geometry geometry) {
Geometry difference = line.difference(geometry);
if (difference instanceof LineString) {
LineString lineDiff = (LineString)difference;
Coordinate c0 = lineDiff.getCoordinateN(0);
if (Double.isNaN(c0.z)) {
addElevation(c0, line);
}
Coordinate cN = lineDiff.getCoordinateN(lineDiff.getNumPoints() - 1);
if (Double.isNaN(cN.z)) {
addElevation(cN, line);
}

}
difference.setUserData(line.getUserData());
return difference;
}

/**
* Change to a floating precision model to calculate the intersection. This
* reduces the chance of lines being returned instead of points where there is
* a sharp angle
*
* @param line1
* @param line2
* @return
*/
public stati