/**
* Copyright (c) 2009-2011, chunquedong(YangJiandong)
*
* This file is part of ChunMap project
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
*
* History:
* 2010-05-05 Jed Young Creation
*/
package chunmap.model.operate.buffer;
import chunmap.model.elem.Line;
import chunmap.model.elem.LineSegment;
import chunmap.model.geom.LineString;
import chunmap.model.geom.Ring;
import chunmap.model.geom.Polygon;
import chunmap.model.operate.overlay.OverLayOp;
import chunmap.model.coord.CoordSeqEditor;
import chunmap.model.coord.Coordinate2D;
import chunmap.model.coord.CPoint;
/**
* @author chunquedong
*
*/
public class LineBuffer {
public Polygon singleLineNoCapBuffer(LineSegment sl, double distance) {
if (distance <= 0) {
throw new IllegalArgumentException(
"very little distance in singleLineNoCapBuffer");
}
Line l = sl.toLine();
double a = Math.atan(l.getVerticalK());// 角度
double dx = distance * Math.cos(a);
double dy = distance * Math.sin(a);
CPoint p1 = new Coordinate2D(sl.getStartPoint().getX() - dx, sl.getStartPoint()
.getY()
- dy);
CPoint p2 = new Coordinate2D(sl.getStartPoint().getX() + dx, sl.getStartPoint()
.getY()
+ dy);
CPoint p3 = new Coordinate2D(sl.getEndPoint().getX() + dx, sl.getEndPoint()
.getY()
+ dy);
CPoint p4 = new Coordinate2D(sl.getEndPoint().getX() - dx, sl.getEndPoint()
.getY()
- dy);
CoordSeqEditor ls = new CoordSeqEditor();
ls.addPointToLast(p1);
ls.addPointToLast(p2);
ls.addPointToLast(p3);
ls.addPointToLast(p4);
ls.addPointToLast(p1);
Polygon pg = new Polygon(new Ring(ls.toCoordinateSeq()));
return pg;
}
public Polygon lineStringBuffer(LineString ls, double distance) {
if (distance <= 0) {
throw new IllegalArgumentException(
"very little distance in singleLineNoCapBuffer");
}
OverLayOp op = new OverLayOp();
PointBuffer pb = new PointBuffer();
// 开始点的缓冲
Polygon pg = pb.createBuffer(ls.firstPoint(), distance);
for (int i = 0, n = ls.size() - 1; i < n; i++) {
LineSegment lseg = ls.getLineSegment(i);
// 线的缓冲区
Polygon lineBuffer = singleLineNoCapBuffer(lseg, distance);
CPoint point = ls.getPoint(i + 1);
// 结尾点的缓冲
Polygon pointBuffer = pb.createBuffer(point, distance);
pg = op.computeUnion(lineBuffer, pg).getGeometry(0);
// debug
pg.isValid();
pg = op.computeUnion(pointBuffer, pg).getGeometry(0);
// debug
pg.isValid();
}
return pg;
}
}