判定两个几何对象空间相交,非实际相交,考虑容差值

/**
     * 判定两个几何对象空间相交,非实际相交,考虑容差值
     *
     * @param geom1
     * @param geom2
     * @param tolerance
     * @return true 重叠,false 不重叠
     */
    public static boolean checkRangeOverlap(Geometry geom1, Geometry geom2, double tolerance) {
        if (geom1.intersects(geom2)) {
            Geometry geometry = geom1.intersection(geom2);
            Geometry buffer = geometry.buffer(3);
            Geometry geometry1 = buffer.intersection(geom1);
            Geometry geometry2 = buffer.intersection(geom2);
            geometry1 = synchrozValue(geometry1, geom1);
            geometry2 = synchrozValue(geometry2, geom2);
            return isNotInterchange(geometry1, geometry2, tolerance);
        } else {
            return false;
        }
    }

    /**
     * 同步z值
     *
     * @param nozgeom
     * @param haszgeom
     */
    public static Geometry synchrozValue(Geometry nozgeom, Geometry haszgeom) {
        if (haszgeom == null) {
            return nozgeom;
        }
        Coordinate[] nozcoordinates = nozgeom.getCoordinates();
        for (Coordinate coordinate : nozcoordinates) {
            Double zzValue = coordinate.getZ();
            if (zzValue.isNaN()) {
                Coordinate nearestCoordinate = getNearestCoordinate(haszgeom, coordinate);
                if (null != nearestCoordinate) {
                    Double zz = nearestCoordinate.getZ();
                    if (!zz.isNaN()) {
                        coordinate.setZ(zz);
                    } else {
                        coordinate.setZ(0.0001);
                    }
                }
            }
        }
        return nozgeom;
    }

    /**
     * 获取最近的点
     *
     * @param haszgeom
     * @param coordinate
     * @return
     */
    private static Coordinate getNearestCoordinate(Geometry haszgeom, Coordinate coordinate) {
        double maxDis = Double.MAX_VALUE;
        Coordinate[] haszcoordinates = haszgeom.getCoordinates();
        Coordinate tempCoord = null;
        for (Coordinate haszcoordinate : haszcoordinates) {
            Double zzvalue = haszcoordinate.getZ();
            if (!zzvalue.isNaN()) {
                double dis = haszcoordinate.distance(coordinate);
                if (dis < maxDis) {
                    maxDis = dis;
                    tempCoord = haszcoordinate;
                }
            }
        }
        return tempCoord;
    }

    /**
     * @param geom1
     * @param geom2
     * @param tolerance
     * @return
     */
    public static boolean isNotInterchange(Geometry geom1, Geometry geom2, double tolerance) {
        double min1 = getMinZ(geom1);
        double max1 = getMaxZ(geom1);
        double min2 = getMinZ(geom2);
        double max2 = getMaxZ(geom2);
        if (max1 > max2) {
            return min1 - max2 <= tolerance;
        } else {
            return min2 - max1 <= tolerance;
        }
    }

    public static double getMaxZ(Geometry geometry) {
        return Arrays.stream(getCoordinates(geometry)).mapToDouble(Coordinate::getZ).max().orElse(1D);
    }

    public static double getMinZ(Geometry geometry) {
        return Arrays.stream(getCoordinates(geometry)).mapToDouble(Coordinate::getZ).min().orElse(0D);
    }

    private static Coordinate[] getCoordinates(Geometry geometry) {
        Coordinate[] coordinates = geometry.getCoordinates();
        if (geometry instanceof Polygon) {
            return Arrays.copyOf(coordinates, coordinates.length - 1);
        } else {
            return coordinates;
        }
    }

    public static void main(String[] args) throws Exception {
        int SRID_MERCATOR = 3857;
        GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), SRID_MERCATOR);
        WKTReader reader = new WKTReader(geometryFactory);
        LineString lineString = (LineString) reader.read("LINESTRING(121.44679536701773 31.1608426593518 14.89559997, 121.44670229800522 31.161342944083394 14.19027644)");
        lineString.setSRID(SRID_MERCATOR);
        Polygon polygon = (Polygon) reader.read("POLYGON((121.44672467606036 31.160848053543837 14.753499999,  121.4467909085093 31.160860108945435 14.88559998, 121.446779106079 31.16091131445206 14.849600027, 121.44671262379684 31.16090080174292 14.744085496527873,  121.44672467606036 31.160848053543837 14.753499999))");
        polygon.setSRID(SRID_MERCATOR);
        Geometry g2 = geometryFactory.createGeometry(polygon);

        boolean insert = checkRangeOverlap(lineString, g2, 1);
        System.out.println(insert);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值