API文档
http://shengshifeiyang.gitee.io/geotools-learning/
/**
* 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中
* @param x
* @param y
* @param geometry wkt格式 POLYGON((0 0, 10 0, 10 10, 0 10,0 0))
* @return
*/
public static boolean withinGeo(double x,double y,String geometry) throws ParseException {
GeometryFactory geometryFactory = new GeometryFactory();
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);
}
public static void main(String[] args) throws Exception {
//请自行搜素了解wkt格式
//wktPoly 1.点位要有顺序,需要按顺序画成封闭区域;2.最后一个点位和第一个点位相同,完成闭环
String wktPoly = "POLYGON ((121 29, 121 30, 122 30, 122 29,121 29))";
String wktPoint = "POINT (121.664564 29.971667)";
GeometryFactory geometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(geometryFactory);
Geometry point = reader.read(wktPoint);
Geometry poly = reader.read(wktPoly);
boolean ex = poly.contains(point);
System.out.println("ex = " + ex);
List<String> list = new ArrayList<>();
list.add("121 29");
list.add("121 30");
list.add("122 30");
list.add("122 29");
String polygon = "POLYGON (("+ StringUtils.join(list,",")+","+list.get(0)+"))";
System.out.println(polygon);
String point1 = "POINT ("+"121.664564"+" "+"29.971667"+")";
boolean b = withinGeo(121.664564, 29.971667, polygon);
System.out.println(b);
}