GDAL关于圆形,多边形的创建以及如何判断点位是否与图形相交
使用gdal计算点与圆相交的条件:
注意:以下方法以WKT格式为准。
1.圆形在GDAL中无法直接通过两个点位信息以及半径描述出来,需要自己通过构建圆形的方式才能拼凑出符合GDAL的图形描述,其中参数graph为图形描述信息
eg:
String graph=”105.4471206665039,28.889448344707493,105.45038223266602,28.88773173093796″
/**
* 获得圆形的图形描述
* @param graph
* @return
*/
protected String getCircle(String graph ){
String[] points=graph.split(“,”);
if (points.length==4){
CirclePoint centerPoint=new CirclePoint();
centerPoint.setLng(Double.parseDouble(points[0]));
centerPoint.setLat(Double.parseDouble(points[1]));
CirclePoint endPoint=new CirclePoint();
endPoint.setLng(Double.parseDouble(points[2]));
endPoint.setLat(Double.parseDouble(points[3]));
centerPoint.setDistance(getRadius(centerPoint,endPoint));
double sin=0.0;double cos=0.0; double x=0.0;double y=0.0;
List<CirclePoint> list= new ArrayList<>();
for (int i = 0; i <100 ; i++) {
sin=Math.sin((6.2831853071795862 * i) / 100.0);
cos = Math.cos((6.2831853071795862 * i) / 100.0);
x = centerPoint.getLng() + (centerPoint.getDistance() * sin);
y = centerPoint.getLat() + (centerPoint.getDistance() * cos);
CirclePoint point =new CirclePoint();
point.setLng(x);
point.setLat(y);
list.add(point);
}
String graphinfo=”POLYGON((“;
for (int i = 0; i < list.size() ; i++) {
if (i<list.size()-1){
CirclePoint circle =list.get(i);
graphinfo+=””+circle.getLng()+” “+circle.getLat()+”,”;
}else if (i==list.size()-1){
CirclePoint circle =list.get(i);
CirclePoint firstCircle=list.get(0);
graphinfo+=””+circle.getLng()+” “+circle.getLat()+”,”+firstCircle.getLng()+” “+firstCircle.getLat()+”))”;
}
}
return graphinfo;
}
return “”;
}
/**
* 获取圆的半径
* @param centerPoint
* @param endPoint
* @return
*/
protected double getRadius(CirclePoint centerPoint,CirclePoint endPoint){
double x1=centerPoint.getLng();
double y1=centerPoint.getLat();
double x2=endPoint.getLng();
double y2=endPoint.getLat();
double radius=Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2));
return radius;
}
2.多边形也需要拼装成符合wkt格式,其中该方法的参数golygon为图形描述信息
eg:
String golygon=”105.43652057647705,28.886371180415154,105.44085502624512,28.8847403973341,105.43368816375732,28.88375334441662“
/**
* 拼装多边形wkt格式
* @param golygon
* @return
*/
protected String dealWithPolygon(String golygon){
String[] points=golygon.split(“,”);
String graph=””;
for (int i = 0; i <points.length ; i++) {
if(i==0){
graph=”POLYGON((“+points[0]+” “;
}else if (i>0&&(i%2==0)){
graph+=points[i]+” “;
}else if(i>0&&(i%2)==1){
if (i!=points.length-1){
graph+=points[i]+”,”;
}else {
graph+=points[i]+”))”;
}
}
}
return graph;
}
3.如何在拼装好WKT格式后使用gdal,调用Geometry的Intersect方法判断是否相交
//根据判断图形是多边形还是圆形来判断多边形 拼装wkt
if (“POLYGON”.equals(graphinfo.getType())){
ploygonStr=dealWithPolygon(graphinfo.getGraph());
}
if (“POINT”.equals(graphinfo.getType())){
ploygonStr=getCircle(graphinfo.getGraph());
}
try {
Geometry ploygon =Geometry.CreateFromWkt(ploygonStr);
if (ployPoint.Intersect(ploygon)){
source.setPassuuid(graphinfo.getUuid());
resultList.add(source);
}
} catch (Exception e) {
e.printStackTrace();
}
转载自:https://blog.csdn.net/weixin_39960755/article/details/81135898