JTS Geometry.intersects()方法产生无限循环

时间:2015-07-08 20:15:35

标签: java infinite-loop jts

我正在尝试使用JTS库检查Multipolygon是否与列表中的哪个Polygon相交。但是,该方法陷入了无限循环,并以堆空间错误结束。

该方法适用于显然不相交的对。当出现可能相交的第一对时,无限循环开始。

以下是两个有问题的Geometry对象的坐标序列,(GeoJson格式):

编辑1 - 添加代码:

组合区:

[ [ [ [ 24.899993263111575, 60.168590212519007, 0.0 ], [ 24.8921845577913, 60.170930189360085, 0.0 ], [ 24.906727352467691, 60.177129472862902, 0.0 ], [ 24.912750145776748, 60.178210672366426, 0.0 ], [ 24.915824674917332, 60.1759297188066, 0.0 ], [ 24.916581969097017, 60.175298383750082, 0.0 ], [ 24.921993683436096, 60.17592332951083, 0.0 ], [ 24.924298430649159, 60.176277074203171, 0.0 ], [ 24.925777801589089, 60.176578177900481, 0.0 ], [ 24.927069958749666, 60.176812746915481, 0.0 ], [ 24.934606301659603, 60.17770118209701, 0.0 ], [ 24.93829154797028, 60.17943631088842, 0.0 ], [ 24.943101975190991, 60.179746422785151, 0.0 ], [ 24.946157646983945, 60.177893553297686, 0.0 ], [ 24.949063929828164, 60.176872818864446, 0.0 ], [ 24.948884500413151, 60.176763003129317, 0.0 ], [ 24.947435273641268, 60.175810147107796, 0.0 ], [ 24.947645652172458, 60.174545608314318, 0.0 ], [ 24.953922745030216, 60.17465838879788, 0.0 ], [ 24.952114223411215, 60.172614801031834, 0.0 ], [ 24.952535692362105, 60.169038476544031, 0.0 ], [ 24.954301749734213, 60.168843140472724, 0.0 ], [ 24.954352635736257, 60.167875977512914, 0.0 ], [ 24.954382959437606, 60.167722794336058, 0.0 ], [ 24.94618172965189, 60.167514740158367, 0.0 ], [ 24.94537429422477, 60.167437294026875, 0.0 ], [ 24.946805477127789, 60.166445009469363, 0.0 ], [ 24.946239227186837, 60.166289851133151, 0.0 ], [ 24.94605715808019, 60.166369202625063, 0.0 ], [ 24.944895457595461, 60.165955522827154, 0.0 ], [ 24.943796685615666, 60.166732851647168, 0.0 ], [ 24.943804264808218, 60.16695638982231, 0.0 ], [ 24.94339656134855, 60.167013101789991, 0.0 ], [ 24.943005307041311, 60.1672873863494, 0.0 ], [ 24.942815762910648, 60.167233708186423, 0.0 ], [ 24.942650545385295, 60.167130925185603, 0.0 ], [ 24.9407231754085, 60.166436365685314, 0.0 ], [ 24.940722537913366, 60.166342333123318, 0.0 ], [ 24.940730039071539, 60.166232085046666, 0.0 ], [ 24.94054008676806, 60.166146432407373, 0.0 ], [ 24.940689883918264, 60.166032286309964, 0.0 ], [ 24.938850589801866, 60.165384512580708, 0.0 ], [ 24.938374768557253, 60.165445980685028, 0.0 ], [ 24.938302460409481, 60.165202578885371, 0.0 ], [ 24.937168766625486, 60.1648185467416, 0.0 ], [ 24.936325303890971, 60.164523435250963, 0.0 ], [ 24.935515249176746, 60.165055276075314, 0.0 ], [ 24.935162131372266, 60.165067012171299, 0.0 ], [ 24.935243484405692, 60.165243301611966, 0.0 ], [ 24.934771879418953, 60.165604942333808, 0.0 ], [ 24.935697179525107, 60.165921540021522, 0.0 ], [ 24.935678441330882, 60.16607022900051, 0.0 ], [ 24.93562630412751, 60.166223958343842, 0.0 ], [ 24.93432941740414, 60.16639906762866, 0.0 ], [ 24.934086151809726, 60.16643747924013, 0.0 ], [ 24.933996000970303, 60.166879775928571, 0.0 ], [ 24.933494220741714, 60.166503500060202, 0.0 ], [ 24.930639585658206, 60.166879026400828, 0.0 ], [ 24.930477360716871, 60.167021165775722, 0.0 ], [ 24.93012338167825, 60.166940112911924, 0.0 ], [ 24.926990522351048, 60.167324416451343, 0.0 ], [ 24.926831203850465, 60.167593482122442, 0.0 ], [ 24.92584998697993, 60.167800919963199, 0.0 ], [ 24.925440947757817, 60.167842026167428, 0.0 ], [ 24.925464402293027, 60.167636210840456, 0.0 ], [ 24.925057233085131, 60.167426343568081, 0.0 ], [ 24.924638431368617, 60.167314445885701, 0.0 ], [ 24.924653932101634, 60.166777681068048, 0.0 ], [ 24.924239526771945, 60.166656330235163, 0.0 ], [ 24.923909713383374, 60.166769702593491, 0.0 ], [ 24.919742746576386, 60.166669946668634, 0.0 ], [ 24.919486254229472, 60.168947489628216, 0.0 ], [ 24.917865586081348, 60.169052961010145, 0.0 ], [ 24.913389466202162, 60.169206955531649, 0.0 ], [ 24.908339577064975, 60.169298848862468, 0.0 ], [ 24.899993263111575, 60.168590212519007, 0.0 ] ], [ [ 24.936135087425775, 60.173864750028308, 0.0 ], [ 24.935822882124054, 60.173818038029538, 0.0 ], [ 24.935698898369246, 60.173449814747812, 0.0 ], [ 24.935042425364173, 60.172863964505616, 0.0 ], [ 24.934700282784398, 60.172293513337216, 0.0 ], [ 24.934235985164971, 60.171993255866646, 0.0 ], [ 24.93458616422809, 60.171768472366658, 0.0 ], [ 24.935119957047309, 60.171965193243153, 0.0 ], [ 24.935650390363076, 60.171879257705569, 0.0 ], [ 24.936025502093653, 60.171926942487744, 0.0 ], [ 24.936234782914752, 60.171605759312015, 0.0 ], [ 24.937662268667879, 60.172287296440999, 0.0 ], [ 24.936135087425775, 60.173864750028308, 0.0 ] ] ] ]

多边形:

[ [ [ 24.94068318546399, 60.17336319841633 ], [ 24.93618043398334, 60.17329314372988 ], [ 24.936039655930326, 60.17553644290522 ], [ 24.940542714024545, 60.17560650391693 ], [ 24.94068318546399, 60.17336319841633 ] ] ]

以下是用于生成Polygon的代码:

    public Polygon toPolygonJTS(GeometryFactory factory,
        LinearRing polygonRingHolder, ArrayList<LinearRing> holeHolder,
        ArrayList<Coordinate> coordinateHolder) {
    if (!this.jtsDrawn) {
        boolean isHole = false;
        // list of points stored in array list
        for (ArrayList<ArrayList<Double>> linearRing : this.geometry
                .getCoordinates()) {
            for (ArrayList<Double> point : linearRing) {
                // add the point to array
                coordinateHolder.add(new Coordinate(point.get(0), point
                        .get(1)));
            }
            if (!isHole) {
                // create a LinearRing for the polygon
                polygonRingHolder = new LinearRing(
                        new CoordinateArraySequence(
                                coordinateHolder
                                        .toArray(new Coordinate[coordinateHolder
                                                .size()])), factory);
            } else {
                // add LinearRing to a list of polygons specifying holes
                holeHolder.add(new LinearRing(new CoordinateArraySequence(
                        coordinateHolder
                                .toArray(new Coordinate[coordinateHolder
                                        .size()])), factory));
            }
            // clear coordinate holder for the next loop
            coordinateHolder.clear();
        }
        // create a polygon and store in object property
        this.polygon = new Polygon(polygonRingHolder,
                holeHolder.toArray(new LinearRing[holeHolder.size()]),
                factory);
    }
    return this.polygon;
}

对于MultiPolygon:

    public MultiPolygon toMultiPolygonJTS(GeometryFactory factory,
        ArrayList<Polygon> polygonList, LinearRing polygonRingHolder,
        ArrayList<LinearRing> holeHolder,
        ArrayList<Coordinate> coordinateHolder) {
    if (!this.jtsDrawn) {
        boolean isHole = false;
        for (ArrayList<ArrayList<ArrayList<Double>>> polygon : this.geometry.coordinates) {
            for (ArrayList<ArrayList<Double>> linearRing : polygon) {
                for (ArrayList<Double> point : linearRing) {
                    coordinateHolder.add(new Coordinate(new Coordinate(((Double) (point.get(0) * 100000)).intValue(), ((Double) (point
                        .get(1) * 100000)).intValue())));
                }
                if (!isHole) {
                    polygonRingHolder = new LinearRing(
                            new CoordinateArraySequence(
                                    coordinateHolder
                                            .toArray(new Coordinate[coordinateHolder
                                                    .size()])), factory);
                } else {
                    holeHolder
                            .add(new LinearRing(
                                    new CoordinateArraySequence(
                                            coordinateHolder
                                                    .toArray(new Coordinate[coordinateHolder
                                                            .size()])),
                                    factory));
                }
                coordinateHolder.clear();
            }
            // create a polygon and add to a list
            polygonList.add(new Polygon(polygonRingHolder, holeHolder
                    .toArray(new LinearRing[holeHolder.size()]), factory));
        }
        // create multipolygon from list of polygon, and store in object
        this.multiPolygon = new MultiPolygon(
                polygonList.toArray(new Polygon[polygonList.size()]),
                factory);
    }
    return this.multiPolygon;
}

检查两个Geometrys是否相交:

    public boolean collidesJTS(MapSquarePolygonFeature mapsquare, GeometryFactory factory,
        ArrayList<Polygon> polygonList, LinearRing polygonRingHolder,
        ArrayList<LinearRing> holeHolder,
        ArrayList<Coordinate> coordinateHolder) {
    return this.multiPolygon.intersects(mapsquare.toPolygonJTS(factory, polygonRingHolder, holeHolder, coordinateHolder));
}

编辑2 如果我创建两个几何图形的距离,代码运行良好,例如:将Polygon的x和y乘以10。

0 个答案:

没有答案