1.缓冲区(Buffer):计算具有给定宽度的几何图形周围的缓冲区几何图形。
/**
* 几何图形指定距离(扩大或者缩小)后的几何图形
*
* @param a
* @param distance
* @return
*/
public Geometry bufferGeometry(Geometry a, double distance) {
return a.buffer(distance);
}
2, equals:两个几何对象是否重叠
/**
* 两个几何对象是否完全重叠
* @return
* @throws ParseException
*/
public void equalsGeometry() throws ParseException{
WKTReader reader = new WKTReader( geometryFactory );
String wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
String wkt2 = ""LINESTRING(3 0, 1 0)";
LineString geometry1 = (LineString) reader.read(wkt1);
LineString geometry2 = (LineString) reader.read(wkt2);
boolean b1 = geometry1.equals(geometry2);//false
wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
wkt2 = ""LINESTRING(10 0, 0 0)";
geometry1 = (LineString) reader.read(wkt1);
geometry2 = (LineString) reader.read(wkt2);
boolean b1 = geometry1.equals(geometry2);//true
}
3:Crosses:几何对象是否交叉(两个对象共享一些点,但不是所有的内部点)
/**
* 两个几何对象是否交叉
* @return
* @throws ParseException
*/
public void crossGeometry() throws ParseException{
GeometryFactory geometryFactory = new GeometryFactory();
//情景1
WKTReader reader = new WKTReader(geometryFactory);
String wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
String wkt2 = "LINESTRING(3 0, 1 0)";
LineString geometry1 = (LineString) reader.read(wkt1);
LineString geometry2 = (LineString) reader.read(wkt2);
boolean b1 = geometry1.crosses(geometry2);//false
System.out.println(b1);
//情景2
wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
wkt2 = "LINESTRING(3 0, 1 1)";
geometry1 = (LineString) reader.read(wkt1);
geometry2 = (LineString) reader.read(wkt2);
boolean b2 = geometry1.crosses(geometry2);//false
System.out.println(b2);
//情景3
wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
wkt2 = "LINESTRING(4 0, -1 -1)";
geometry1 = (LineString) reader.read(wkt1);
geometry2 = (LineString) reader.read(wkt2);
boolean b3 = geometry1.crosses(geometry2);//false
System.out.println(b3);
//情景4
wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
wkt2 = "LINESTRING(0 4, 3 0, -1 -1)";
geometry1 = (LineString) reader.read(wkt1);
geometry2 = (LineString) reader.read(wkt2);
boolean b4 = geometry1.crosses(geometry2);//true
System.out.println(b4);
}
4,intersects :两个对象是否空间相交(至少有一个点)
/**
* 至少一个公共点(相交)
* @return
* @throws ParseException
*/
public boolean intersectsGeo() throws ParseException{
GeometryFactory geometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(geometryFactory);
//情景1
String wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
String wkt2 = "LINESTRING(3 0, 1 0)";
LineString geometry1 = (LineString) reader.read(wkt1);
LineString geometry2 = (LineString) reader.read(wkt2);
boolean b1 = geometry1.intersects(geometry2);//true
System.out.println(b1);
//情景2
wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
wkt2 = "LINESTRING(3 0, 1 1)";
geometry1 = (LineString) reader.read(wkt1);
geometry2 = (LineString) reader.read(wkt2);
boolean b2 = geometry1.intersects(geometry2);//true
System.out.println(b2);
//情景3
wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
wkt2 = "LINESTRING(4 0, -1 -1)";
geometry1 = (LineString) reader.read(wkt1);
geometry2 = (LineString) reader.read(wkt2);
boolean b3 = geometry1.intersects(geometry2);//true
System.out.println(b3);
//情景4
wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
wkt2 = "LINESTRING(0 4, 3 0, -1 -1)";
geometry1 = (LineString) reader.read(wkt1);
geometry2 = (LineString) reader.read(wkt2);
boolean b4 = geometry1.intersects(geometry2);//true
System.out.println(b4);
}
5,within: 判断当前几何图形是否在指定几何图形内。
/**
* 判断点point(x,y)是否在geometry表示的Polygon中
* @param x
* @param y
* @param geometry wkt格式
* @return
*/
public boolean withinGeo(double x,double y,String geometry) throws ParseException {
Coordinate coord = new Coordinate(x,y);
Point point = geometryFactory.createPoint( coord );
WKTReader reader = new WKTReader( geometryFactory );
Polygon polygon = (Polygon) reader.read(geometry);
return point.within(polygon);
}
/**
* lineString是否在Polygon中
* @return
*/
public boolean withinGeo(LineString lineString, Geometry polygon) throws ParseException {
return lineString.within(polygon);
}
6, disjoint: 几何形状没有共有的点(相邻)
/**
* 几何对象没有交点(相邻)
* @return
* @throws ParseException
*/
public boolean disjointGeo() throws ParseException{
GeometryFactory geometryFactory = new GeometryFactory();
//情景1
WKTReader reader = new WKTReader(geometryFactory);
LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)");
System.out.println(geometry1.disjoint(geometry2));//true
//情景2
geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
geometry2 = (LineString) reader.read("LINESTRING(0 1, -1 -1)");
System.out.println(geometry1.disjoint(geometry2));//true
//情景3
geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
geometry2 = (LineString) reader.read("LINESTRING(0 0, -1 -1)");
System.out.println(geometry1.disjoint(geometry2));//false
}