在平时工作中会涉及一些空间范围的操作,比如两个面求交集,求一个多边形的面积等等,一般都会用到JTS。JTS支持点线面的很多操作,关键字为point、linestring、polygon。
JTS没有直接表示圆的关键字,一般用一个多边形表示圆:
1,确定边数
2,画一个环线
3,根据环线生成面
public class JtsTest {
private static GeometryFactory geometryFactory = new GeometryFactory();
public static Polygon createCircle(double x, double y, final double RADIUS) {
final int SIDES = 32; // 确定边数
Coordinate coords[] = new Coordinate[SIDES + 1];
for (int i = 0; i < SIDES; i++) {
double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
double dx = Math.cos(angle) * RADIUS;
double dy = Math.sin(angle) * RADIUS;
coords[i] = new Coordinate((double) x + dx, (double) y + dy);
}
coords[SIDES] = coords[0];
LinearRing ring = geometryFactory.createLinearRing(coords); // 画一个环线
Polygon polygon = geometryFactory.createPolygon(ring, null); // 生成一个面
return polygon;
}
public static void main(String[] args) throws ParseException {
Polygon p