}
}
double max = xmax - xmin + ymax - ymin;
// Extend start vertex
Point ptStart = source.getStartPoint();
Point ptStart2 = source.getPointN(1);
// Calculate an extend point
double aStart = Math.atan((ptStart2.getY() - ptStart.getY())
/ (ptStart2.getX() - ptStart.getX()));
double xStart = ptStart.getX() + max
* (ptStart2.getX() > ptStart.getX() ? -1 : 1)
* Math.abs(Math.cos(aStart));
double yStart = ptStart.getY() + max
* (ptStart2.getY() > ptStart.getY() ? -1 : 1)
* Math.abs(Math.sin(aStart));
Point ptStart3 = GeometryUtil.gf().createPoint(
new Coordinate(xStart, yStart));
LineString lStart = GeometryUtil.gf().createLineString(
new Coordinate[] { ptStart.getCoordinate(),
ptStart3.getCoordinate() });
Point nearestCrossStart = null;
double nearestStart = Double.MAX_VALUE;
for (Iterator<LineString> itrTrimExtend = trimExtendToList
.iterator(); itrTrimExtend.hasNext();) {
LineString l = itrTrimExtend.next();
Geometry crossPoints = l.intersection(lStart);
if (crossPoints != null) {
ArrayList<Point> pts = new ArrayList<Point>();
if (crossPoints instanceof Point) {
pts.add((Point) crossPoints);
} else if (crossPoints instanceof MultiPoint) {
MultiPoint mp = (MultiPoint) crossPoints;
for (int i = 0, count = mp.getNumGeometries(); i < count; i++) {
Point pt = (Point) mp.getGeometryN(i);
pts.add(pt);
}
}
for (Iterator<Point> itr = pts.iterator(); itr.hasNext();) {
Point pt = itr.next();
double d = pt.distance(ptStart);
if (d < nearestStart) {
nearestStart = d;
nearestCrossStart = pt;
}
}
}
}
if (nearestCrossStart != null) {
lineCoords.add(0, nearestCrossStart.getCoordinate());
}
// Extend end vertex
Point ptEnd = source.getEndPoint();
Point ptEnd2 = source.getPointN(source.getNumPoints() - 2);
// Calculate an extend point
double aEnd = Math.atan((ptEnd2.getY() - ptEnd.getY())
/ (ptEnd2.getX() - ptEnd.getX()));
double xEnd = ptEnd.getX() + max
* (ptEnd2.getX() > ptEnd.getX() ? -1 : 1)
* Math.abs(Math.cos(aEnd));
double yEnd = ptEnd.getY() + max
* (ptEnd2.getY() > ptEnd.getY() ? -1 : 1)
* Math.abs(Math.sin(aEnd));
Point ptEnd3 = GeometryUtil.gf().createPoint(
new Coordinate(xEnd, yEnd));
LineString lEnd = GeometryUtil.gf().createLineString(
new Coordinate[] { ptEnd.getCoordinate(),
ptEnd3.getCoordinate() });
Point nearestCrossEnd = null;
double nearestEnd = Double.MAX_VALUE;
for (Iterator<LineString> itrTrimExtend = trimExtendToList
.iterator(); itrTrimExtend.hasNext();) {
LineString l = itrTrimExtend.next();
Geometry crossPoints = l.intersection(lEnd);
if (crossPoints != null) {
ArrayList<Point> pts = new ArrayList<Point>();
if (crossPoints instanceof Point) {
pts.add((Point) crossPoints);
} else if (crossPoints instanceof MultiPoint) {
MultiPoint mp = (MultiPoint) crossPoints;
for (int i = 0, count = mp.getNumGeometries(); i < count; i++) {
Point pt = (Point) mp.getGeometryN(i);
pts.add(pt);
}
}
for (Iterator<Point> itr = pts.iterator(); itr.hasNext();) {
Point pt = itr.next();
double d = pt.distance(ptEnd);
if (d < nearestEnd) {
nearestEnd = d;
nearestCrossEnd = pt;
}
}