gt-grid 源码分析

在工作使用到了gt-grid.jar中生成矢量grid,发现在某些长度和宽度的时候,生成的栅格并不能全覆盖图形。

使用的方法:Grids.createSquareGrid

最开始还以为是代码出现bug,后面对生成的grid进行测试发现是由于该方法本身会出现该问题,于是对源码进行了分析发现确实有此问题:

核心代码源码如下:

 while (el.getBounds().getMinY() <= gridBounds.getMaxY()) {
            while (el.getBounds().getMaxX() <= gridBounds.getMaxX()) {
                if (((Envelope) gridBounds).contains(el.getBounds())) {
                    if (gridFeatureBuilder.getCreateFeature(el)) {
                        Map attrMap = new HashMap();
                        gridFeatureBuilder.setAttributes(el, attrMap);

                        if (densify) {
                            fb.set(geomPropName, el.toDenseGeometry(vertexSpacing));
                        } else {
                            fb.set(geomPropName, el.toGeometry());
                        }

                        for (String propName : attrMap.keySet()) {
                            fb.set(propName, attrMap.get(propName));
                        }

                        fc.add(fb.buildFeature(gridFeatureBuilder.getFeatureID(el)));
                    }
                }

                el = getNextXElement(el);
            }

            el0 = getNextYElement(el0);
            el = el0;
        }

其中 if (((Envelope) gridBounds).contains(el.getBounds()))
条件 明确表示出了生成的grid如果超出了范围,则该grid不算。因此出现了我之前的问题。
解决方法:
为保证在某个宽度和长度生成的grid全覆盖
则在传入bound范围时,人为扩大bound范围保证,其中过大的值,如果宽度为1,那么扩大值最好在1-2之间,目前我使用的是2倍于max(宽度,长度)

你可能感兴趣的:(gt-grid 源码分析)