查询与输入线段相交的所有管线
<select id="getPipelinesByWkt" parameterType="java.lang.String" resultType="java.lang.String" >
select pipeline_wkt from jjlbsdb.t_gis_pipeline
where st_intersects(geom,st_geomfromtext(#{wkt},4550))
</select>
/**
* 求与输入线相交管线的交点集合
* @param wkt
* @return
*/
public List<double[]> getIntersectResByPolyline(String wkt) {
Polyline polyline = (Polyline) GeometryEngine.geometryFromWkt(wkt, WktExportFlags.wktExportDefaults, Geometry.Type.Polyline);
List<String> intersectPipelines = completionRateAnalysisMapper.getPipelinesByWkt(wkt);
List<double[]> resList=new ArrayList<>();
for (int i = 0; i < intersectPipelines.size(); i++) {
Polyline intersectPipeline = (Polyline) GeometryEngine.geometryFromWkt(intersectPipelines.get(i), WktExportFlags.wktExportDefaults, Geometry.Type.Polyline);
List<double[]> interPoints= intersectLinesToPoints( polyline.getCoordinates2D(),intersectPipeline.getCoordinates2D());
resList.addAll(interPoints);
}
return resList;
}
/**
*计算两条多线段之间的交点集合
* @param polyline
* @param interPolyline
* @return
*/
public List<double[]> intersectLinesToPoints(Point2D[] polyline, Point2D[] interPolyline) {
List<double[]> result=new ArrayList<>();
for (int j = 0; j < polyline.length - 1; j++) {
Point2D[] tempLine = {polyline[j], polyline[j + 1]};
for (int i = 0; i < interPolyline.length - 1; i++) {
Point2D[] interLine = {interPolyline[i], interPolyline[i + 1]};
double[] point=getPolyLinesIntersection(tempLine,interLine);
if(point!=null){
result.add(point);
}
}
}
if (result.size()>0){
return result;
}
else {
return null;
}
}
/**
* 计算两个线段间的交点
* @param line 线段1
* @param interLine 线段2
* @return
*/
public double[] getPolyLinesIntersection(Point2D[] line, Point2D[] interLine) {
double[] result;
double line_start_X = line[0].x;
double line_start_Y = line[0].y;
double line_end_X = line[1].x;
double line_end_Y = line[1].y;
double interLine_start_X = interLine[0].x;
double interLine_start_Y = interLine[0].y;
double interLine_end_X = interLine[1].x;
double interLine_end_Y = interLine[1].y;
/** 1 解线性方程组, 求线段交点. **/
// 如果分母为0 则平行或共线, 不相交
double denominator = (line_end_Y - line_start_Y) * (interLine_end_X - interLine_start_X) - (line_start_X - line_end_X) * (interLine_start_Y - interLine_end_Y);
if (Math.abs(denominator) < 1e-20) {
return null;
}
// 线段所在直线的交点坐标 (x , y)
double x = ((line_end_X - line_start_X) * (interLine_end_X - interLine_start_X) * (interLine_start_Y - line_start_Y)
+ (line_end_Y - line_start_Y) * (interLine_end_X - interLine_start_X) * line_start_X
- (interLine_end_Y - interLine_start_Y) * (line_end_X - line_start_X) * interLine_start_X) / denominator;
double y = -((line_end_Y - line_start_Y) * (interLine_end_Y - interLine_start_Y) * (interLine_start_X - line_start_X)
+ (line_end_X - line_start_X) * (interLine_end_Y - interLine_start_Y) * line_start_Y
- (interLine_end_X - interLine_start_X) * (line_end_Y - line_start_Y) * interLine_start_Y) / denominator;
/** 2 判断交点是否在两条线段上 **/
double valid_line_x = (x - line_start_X) * (x - line_end_X);
double valid_line_y = (y - line_start_Y) * (y - line_end_Y);
double valid_interLine_x = (x - interLine_start_X) * (x - interLine_end_X);
double valid_interLine_y = (y - interLine_start_Y) * (y - interLine_end_Y);
if ((valid_line_x <= 0) && (valid_line_y <= 0) && (valid_interLine_x <= 0) && (valid_interLine_y <= 0)) {
result = new double[]{x, y};
return result;
} else {
return null;
}
}
求输入线段与其相交管线的所有交点集合代码.
最新推荐文章于 2024-08-21 10:15:52 发布