jts-1.13中的bug分析
在使用jts的STRtree时,由于项目需要,需要向索引中添加数据,还会删除数据,在使用STRtree的过程中,发现无法删除数据!代码如下:
STRtree stRtree = new STRtree();
stRtree.insert(envelope, geom1);
stRtree.remove(envelope, geom1);
鉴于对jts的敬畏之心,首先怀疑是自己调用出现了问题,在参考了各种网站后,如:
发现应该是参数没有问题,过程中还调整过参数,如将item传入null,也没有任何效果。
接下来只能读源码了,呜呜呜。。。。。。
STRtree的父类AbstractSTRtree中,remove的实现如下:
protected boolean remove(Object searchBounds, Object item) {
build();
if (this.itemBoundables.isEmpty()) {
Assert.isTrue(this.root.getBounds() == null);
}
if (getIntersectsOp().intersects(this.root.getBounds(), searchBounds)) {
return remove(searchBounds, this.root, item);
}
return false;
}
由于导入的是jar包,没有用源码编译,所以不能调试。那么,只能怀疑这段代码中的各个对象了。
经过各种波折后,发现build()代码中有关联,代码如下:
public void build() {
if (this.built)
return;
this.root = ((this.itemBoundables.isEmpty())
? createNode(0)
: createHigherLevels(this.itemBoundables, -1));
this.itemBoundables = null;
this.built = true;
}
联合remove和build代码,发现,当remove的时候,若没有build(),则会执行:
this.itemBoundables = null;
而在remove的下一行中,如下语句:
if (this.itemBoundables.isEmpty()) {
Assert.isTrue(this.root.getBounds() == null);
}
那么,就会报出空指针异常了。
接下来,下载新版本的jts(jts-1.14),看到remove代码如下:
protected boolean remove(Object searchBounds, Object item) {
build();
if (getIntersectsOp().intersects(this.root.getBounds(), searchBounds)) {
return remove(searchBounds, this.root, item);
}
return false;
}
去掉if assert后果然不再出现问题。
然后去网站上看更新日志,粗略看了下,并没有发现remove相关的故障,应该在其他bug中一起修复的吧。
总结和反思:
不能迷信权威。
show me the code
关注使用jar包的版本更新
提供稳定高效的API,或者给出足够的定位信息