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

2014-11-24 07:56:16 · 作者: · 浏览: 8
ometry);
}

public static void applyPrecisionModel(final Geometry geometry) {
PrecisionModel precisionModel = geometry.getPrecisionModel();
Coordinate[] coordinates = geometry.getCoordinates();
for (int i = 0; i < coordinates.length; i++) {
Coordinate coordinate = coordinates[i];
precisionModel.makePrecise(coordinate);
}

}

public static double getMiddleAngle(final double lastAngle,
final double angle, final int orientation) {
if (orientation == Angle.COUNTERCLOCKWISE) {
if (Double.isNaN(lastAngle)) {
return angle + Angle.PI_OVER_2;
} else if (Double.isNaN(angle)) {
return lastAngle + Angle.PI_OVER_2;
} else {
int turn = Angle.getTurn(lastAngle, angle);
double angleDiff = angle - lastAngle;
if (turn == Angle.CLOCKWISE) {
return lastAngle
- (Angle.PI_TIMES_2 - (Math.PI - Math.abs(angleDiff)) / 2);
} else {
return angle + (Math.PI - Math.abs(angleDiff)) / 2;
}

}
} else {
if (Double.isNaN(lastAngle)) {
return angle - Angle.PI_OVER_2;
} else if (Double.isNaN(angle)) {
return lastAngle - Angle.PI_OVER_2;
} else {
int turn = Angle.getTurn(lastAngle, angle);
double angleDiff = angle - lastAngle;
if (turn == Angle.CLOCKWISE) {
return angle - (Math.PI - Math.abs(angleDiff)) / 2;
} else {
return lastAngle
+ (Angle.PI_TIMES_2 - (Math.PI - Math.abs(angleDiff)) / 2);
}
}
}

}

public static LineString subLineString(final LineString line, final int length) {
CoordinateSequence coords = line.getCoordinateSequence();
CoordinateSequence newCoords = subSequence(coords, 0, length);
GeometryFactory factory = line.getFactory();
return factory.createLineString(newCoords);
}

public static LineString subLineString(final LineString line,
final Coordinate fromCoordinate, final int fromIndex, final int toIndex,
final Coordinate toCoordinate) {
int numCoords = toIndex - fromIndex + 1;
int length = numCoords;
int offset = 0;
if (fromCoordinate != null) {
length++;
offset = 1;
}
if (toCoordinate != null) {
length++;
}
CoordinateSequence coords = line.getCoordinateSequence();
int dimension = coords.getDimension();
PackedCoordinateSequenceFactory doubleFactory = PackedCoordinateSequenceFactory.DOUBLE_FACTORY;
try {
CoordinateSequence newCoords = doubleFactory.create(length, dimension);
if (fromCoordinate != null) {
setCoordinate(newCoords, 0, fromCoordinate);
}
copyCoords(coords, fromIndex, newCoords, offset, numCoords);
if (toCoordinate != null) {
try {
setCoordinate(newCoords, length - 1, toCoordinate);
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
GeometryFactory factory = line.getFactory();
return factory.createLineString(newCoords);
} catch (NegativeArraySizeException e) {
e.printStackTrace();
throw e;
}
}

public static LineString subLineString(final LineString line,
final int length, final Coordinate coordinate) {
CoordinateSequence coords = line.getCoordinateSequence();
int dimension = coords.getDimension();
PackedCoordinateSequenceFactory doubleFactory = PackedCoordinateSequenceFactory.DOUBLE_