根据面积限制分割几何

根据面积限制分割几何。针对面积大于限制的几何,计算最小外包矩形,使用长边进行等距拆分。拆分后,会再次判断面积,面积不符则再次拆分。 ~~~java

根据面积限制分割几何

针对面积大于限制的几何,计算最小外包矩形,使用长边进行等距拆分。拆分后,会再次判断面积,面积不符则再次拆分。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
     * 根据面积限制,分割几何
     *
     * @param geometryList 几何列表
     * @return {@link List}<{@link Geometry}>
     */
private List<Geometry> splitGeometryByArea(List<Geometry> geometryList, double maxArea) {
    // 判断拆是否有面积大于规定的几何
    boolean anyMatch = geometryList.stream().anyMatch(p -> this.baseMapper.calculateArea(p) * 0.0015 > maxArea);
    // 没有面积大于规定的几何,则直接返回
    if (!anyMatch) {
        return geometryList;
    }

    for (int i = 0; i < geometryList.size(); i++) {
        Geometry geometry = geometryList.get(i);
        Double area = this.baseMapper.calculateArea(geometry);
        if (area * 0.0015 > runnerConfig.getSplitMaxArea()) {
            // 获取外包矩形
            Envelope envelope = geometry.getEnvelopeInternal();
            double height = envelope.getHeight();
            double width = envelope.getWidth();
            double minX = envelope.getMinX();
            double maxX = envelope.getMaxX();
            double minY = envelope.getMinY();
            double maxY = envelope.getMaxY();
            if (height >= width) {
                splitByHeight(geometryList, i, minX, maxX, minY, maxY, height, width);
            } else {
                splitByWidth(geometryList, i, minX, maxX, minY, maxY, height, width);
            }
            i--;
        }
    }
    return geometryList;
}

/**
     * 在垂直方向进行分割
     *
     * @param geometryList 几何列表
     * @param i            我
     * @param minX         风骚女子
     * @param maxX         maxx
     * @param minY         如矿坑
     * @param maxY         maxy
     * @param height       高度
     * @param width        宽度
     */
private void splitByHeight(List<Geometry> geometryList, int i,
                           double minX, double maxX, double minY, double maxY,
                           double height, double width) {
    double h_divide_w = height / width;
    double count = Math.ceil(h_divide_w);
    count = count < 2 ? 2 : count;
    Geometry remove = geometryList.remove(i);
    for (int n = 0; n < count; n++) {
        double y1 = maxY * n / count + minY * (count - n) / count;
        double y2 = maxY * (n + 1) / count + minY * (count - n - 1) / count;
        Envelope tempEnvelope = new Envelope(minX, maxX, y1, y2);
        Geometry tempGeom = JTS.toGeometry(tempEnvelope).intersection(remove);
        geometryList.add(tempGeom);
    }
}

/**
     * 在水平方向上进行分割
     *
     * @param geometryList 几何列表
     * @param i            我
     * @param minX         风骚女子
     * @param maxX         maxx
     * @param minY         如矿坑
     * @param maxY         maxy
     * @param height       高度
     * @param width        宽度
     */
private void splitByWidth(List<Geometry> geometryList, int i,
                          double minX, double maxX, double minY, double maxY,
                          double height, double width) {
    double w_divide_h = width / height;
    double count = Math.ceil(w_divide_h);
    count = count < 2 ? 2 : count;
    Geometry remove = geometryList.remove(i);
    for (int n = 0; n < count; n++) {
        double x1 = maxX * i / count + minX * (count - i) / count;
        double x2 = maxX * (i + 1) / count + minX * (count - i - 1) / count;
        Envelope tempEnvelope = new Envelope(x1, x2, minY, maxY);
        Geometry tempGeom = JTS.toGeometry(tempEnvelope).intersection(remove);
        geometryList.add(tempGeom);
    }
}
Licensed under CC BY-NC-SA 4.0
Gear(夕照)的博客。记录开发、生活,以及一些不足为道的思考……