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

2014-11-24 07:56:16 · 作者: · 浏览: 4
urn null;
}
}

@SuppressWarnings("unchecked")
public static Collection getMergedLines(
final MultiLineString multiLineString) {
LineMerger merger = new LineMerger();
merger.add(multiLineString);
Collection lineStrings = merger.getMergedLineStrings();
return lineStrings;
}

/**
* Merge two lines that share common coordinates at either the start or end.
* If the lines touch only at their start coordinates, the line2 will be
* reversed and joined before the start of line1. If the tow lines ouch only
* at their end coordinates, the line2 will be reversed and joined after the
* end of line1.
*
* @param line1 The first line.
* @param line2 The second line.
* @return The new line string
*/
public static LineString merge(final LineString line1, final LineString line2) {
CoordinateSequence coordinates = merge(line1.getCoordinateSequence(),
line2.getCoordinateSequence());
GeometryFactory factory = line1.getFactory();
LineString line = factory.createLineString(coordinates);
line.setUserData(line1.getUserData());
return line;
}

private static CoordinateSequence merge(final CoordinateSequence coordinates1,
final CoordinateSequence coordinates2) {
Coordinate[] coordinates = new Coordinate[coordinates1.size()
+ coordinates2.size() - 1];
int numCoords = 0;
Coordinate coordinates1Start = coordinates1.getCoordinate(0);
Coordinate coordinates1End = coordinates1.getCoordinate(coordinates1.size() - 1);
Coordinate coordinates2Start = coordinates2.getCoordinate(0);
Coordinate coordinates2End = coordinates2.getCoordinate(coordinates2.size() - 1);
if (coordinates1Start.equals(coordinates2End)) {
numCoords = addCoordinates(coordinates2, coordinates, numCoords, null);
numCoords = addCoordinates(coordinates1, coordinates, numCoords,
coordinates[numCoords - 1]);
} else if (coordinates2Start.equals(coordinates1End)) {
numCoords = addCoordinates(coordinates1, coordinates, numCoords, null);
numCoords = addCoordinates(coordinates2, coordinates, numCoords,
coordinates[numCoords - 1]);
} else if (coordinates1Start.equals(coordinates2Start)) {
numCoords = addReversedCoordinates(coordinates2, coordinates, numCoords,
null);
numCoords = addCoordinates(coordinates1, coordinates, numCoords,
coordinates[numCoords - 1]);
} else if (coordinates1End.equals(coordinates2End)) {
numCoords = addCoordinates(coordinates1, coordinates, numCoords, null);
numCoords = addReversedCoordinates(coordinates2, coordinates, numCoords,
coordinates[numCoords - 1]);
} else {
throw new IllegalArgumentException("lines don't touch");

}

if (numCoords != coordinates.length) {
Coordinate[] newCoordinates = new Coordinate[numCoords];
System.arraycopy(coordinates, 0, newCoordinates, 0, numCoords);
coordinates = newCoordinates;
}

return CoordinateArraySequenceFactory.instance().create(coordinates);
}

public static int addCoordinates(final CoordinateSequence src, final Coordinate[] dest,
final int startIndex, final Coordinate lastCoordinate) {
Coordinate previousCoordinate = lastCoordinate;
int coordIndex = startIndex;
try {
for (int i = 0; i < src.size(); i++) {
Coordinate coordinate = src.getCoordinate(i);