Skip to content

Commit 0b701bb

Browse files
committed
测试geometryFactory为线程安全,故将其调整为单例
1 parent 5070b74 commit 0b701bb

19 files changed

+64
-90
lines changed

src/main/java/org/wowtools/neo4j/rtree/RtreeNearestQuery.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import java.util.List;
1111

12-
import static org.bouncycastle.asn1.x500.style.RFC4519Style.dc;
13-
1412
/**
1513
* @author liuyu
1614
* @date 2020/6/10

src/main/java/org/wowtools/neo4j/rtree/RtreeQuery.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@
1818

1919
import org.locationtech.jts.geom.Coordinate;
2020
import org.locationtech.jts.geom.Geometry;
21-
import org.locationtech.jts.geom.GeometryFactory;
2221
import org.locationtech.jts.geom.Polygon;
23-
import org.locationtech.jts.io.ParseException;
2422
import org.locationtech.jts.io.WKBReader;
25-
import org.locationtech.jts.io.WKBWriter;
2623
import org.locationtech.jts.operation.predicate.RectangleIntersects;
27-
import org.neo4j.graphdb.Direction;
2824
import org.neo4j.graphdb.Node;
29-
import org.neo4j.graphdb.Relationship;
3025
import org.neo4j.graphdb.Transaction;
3126
import org.wowtools.neo4j.rtree.spatial.RTreeIndex;
3227
import org.wowtools.neo4j.rtree.util.GeometryBbox;
3328
import org.wowtools.neo4j.rtree.util.RtreeTraverser;
34-
35-
import java.util.ArrayDeque;
36-
import java.util.Deque;
29+
import org.wowtools.neo4j.rtree.util.Singleton;
3730

3831
import static org.wowtools.neo4j.rtree.util.BboxIntersectUtil.bbox2Geometry;
3932
import static org.wowtools.neo4j.rtree.util.BboxIntersectUtil.bboxIntersect;
@@ -116,9 +109,8 @@ public static class BboxSpatialFilter implements SpatialFilter {
116109
*/
117110
public BboxSpatialFilter(double[] bbox) {
118111
this.bbox = bbox;
119-
GeometryFactory gf = new GeometryFactory();
120112
Coordinate c0 = new Coordinate(bbox[0], bbox[1]);
121-
Polygon bboxPolygon = gf.createPolygon(new Coordinate[]{
113+
Polygon bboxPolygon = Singleton.geometryFactory.createPolygon(new Coordinate[]{
122114
c0,
123115
new Coordinate(bbox[2], bbox[1]),
124116
new Coordinate(bbox[2], bbox[3]),
@@ -205,11 +197,10 @@ public static void queryByGeometryIntersects(Transaction tx, RTreeIndex rTreeInd
205197
* @param visitor 结果访问器
206198
*/
207199
public static void queryByStripGeometryIntersects(Transaction tx, RTreeIndex rTreeIndex, Geometry geometry, NodeVisitor visitor) {
208-
final GeometryFactory gf = new GeometryFactory();
209-
MyObjNodeVisitor myObjNodeVisitor = new MyObjNodeVisitor(rTreeIndex,new GeometryIntersectsSpatialFilter(geometry),visitor);
210-
RtreeTraverser.traverse(tx,rTreeIndex,
200+
MyObjNodeVisitor myObjNodeVisitor = new MyObjNodeVisitor(rTreeIndex, new GeometryIntersectsSpatialFilter(geometry), visitor);
201+
RtreeTraverser.traverse(tx, rTreeIndex,
211202
(rtreeNode, nodeBbox) -> {
212-
Geometry nodeGeo = bbox2Geometry(nodeBbox, gf);
203+
Geometry nodeGeo = bbox2Geometry(nodeBbox);
213204
return nodeGeo.intersects(geometry);
214205
},
215206
myObjNodeVisitor
@@ -226,8 +217,8 @@ public static void queryByStripGeometryIntersects(Transaction tx, RTreeIndex rTr
226217
*/
227218
public static void queryBySpatialFilter(Transaction tx, RTreeIndex rTreeIndex, SpatialFilter spatialFilter, NodeVisitor visitor) {
228219
double[] bbox = spatialFilter.getBbox();
229-
MyObjNodeVisitor myObjNodeVisitor = new MyObjNodeVisitor(rTreeIndex,spatialFilter,visitor);
230-
RtreeTraverser.traverse(tx,rTreeIndex,
220+
MyObjNodeVisitor myObjNodeVisitor = new MyObjNodeVisitor(rTreeIndex, spatialFilter, visitor);
221+
RtreeTraverser.traverse(tx, rTreeIndex,
231222
(rtreeNode, nodeBbox) -> bboxIntersect(bbox, nodeBbox),
232223
myObjNodeVisitor
233224
);

src/main/java/org/wowtools/neo4j/rtree/bigshape/pojo/BigShape.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.wowtools.neo4j.rtree.bigshape.pojo;
22

33
import org.locationtech.jts.geom.Geometry;
4-
import org.locationtech.jts.geom.GeometryFactory;
54
import org.locationtech.jts.geom.Point;
65
import org.locationtech.jts.io.ParseException;
76
import org.neo4j.graphdb.Direction;
@@ -169,7 +168,6 @@ boolean judgeBbox(double[] bbox) {
169168
* 其它geometry相交判断
170169
*/
171170
private static class OtherIntersectsJudge extends IntersectsJudge {
172-
private final GeometryFactory geometryFactory = new GeometryFactory();
173171

174172
public OtherIntersectsJudge(Geometry geometry, int cacheSize) {
175173
super(geometry, cacheSize);
@@ -180,7 +178,7 @@ boolean judgeBbox(double[] bbox) {
180178
if (!BboxIntersectUtil.bboxIntersect(geoBbox, bbox)) {
181179
return false;//bbox不相交的话肯定不会相交
182180
}
183-
return geometry.intersects(BboxIntersectUtil.bbox2Geometry(bbox, geometryFactory));
181+
return geometry.intersects(BboxIntersectUtil.bbox2Geometry(bbox));
184182
}
185183

186184
}

src/main/java/org/wowtools/neo4j/rtree/bigshape/util/GridCuter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.locationtech.jts.geom.*;
44
import org.wowtools.neo4j.rtree.bigshape.pojo.Grid;
55
import org.wowtools.neo4j.rtree.util.GeometryBbox;
6+
import org.wowtools.neo4j.rtree.util.Singleton;
67

78
import java.util.LinkedList;
89
import java.util.List;
@@ -45,7 +46,6 @@ private static List<Grid> _cut(Geometry geometry, int row, int column) {
4546
double height = (bbox.ymax - bbox.ymin) / row;
4647
double xmin = bbox.xmin, ymin, xmax = xmin + width, ymax;
4748
Coordinate c0, c1, c2, c3;//网格的四个顶点,左下角起逆时针
48-
GeometryFactory geometryFactory = new GeometryFactory();
4949
List<Grid> res = new LinkedList<>();
5050
//按从左下角逐行上移、逐列右移的方式遍历所有格子并进行相交分析
5151
do {
@@ -56,7 +56,7 @@ private static List<Grid> _cut(Geometry geometry, int row, int column) {
5656
c2 = new Coordinate(xmax, ymax);
5757
c3 = new Coordinate(xmin, ymax);
5858
do {
59-
Polygon bboxGeometry = geometryFactory.createPolygon(new Coordinate[]{
59+
Polygon bboxGeometry = Singleton.geometryFactory.createPolygon(new Coordinate[]{
6060
c0, c1, c2, c3, c0
6161
});
6262
Geometry intersectionGeometry = geometry.intersection(bboxGeometry);

src/main/java/org/wowtools/neo4j/rtree/nearest/NearestNeighbour.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import org.locationtech.jts.geom.Coordinate;
99
import org.locationtech.jts.geom.Geometry;
10-
import org.locationtech.jts.geom.GeometryFactory;
1110
import org.locationtech.jts.geom.Point;
1211
import org.locationtech.jts.io.WKBReader;
1312
import org.neo4j.graphdb.Direction;
@@ -16,6 +15,7 @@
1615
import org.wowtools.neo4j.rtree.Constant;
1716
import org.wowtools.neo4j.rtree.RtreeNearestQuery;
1817
import org.wowtools.neo4j.rtree.spatial.RTreeIndex;
18+
import org.wowtools.neo4j.rtree.util.Singleton;
1919

2020
import java.util.*;
2121

@@ -37,7 +37,7 @@ public NearestNeighbour(RtreeNearestQuery.NodeFilter filter, int maxHits, Node r
3737
this.x = x;
3838
this.y = y;
3939
this.rTreeIndex = rTreeIndex;
40-
point = new GeometryFactory().createPoint(new Coordinate(x, y));
40+
point = Singleton.geometryFactory.createPoint(new Coordinate(x, y));
4141
}
4242

4343
/**

src/main/java/org/wowtools/neo4j/rtree/util/BboxIntersectUtil.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.locationtech.jts.geom.Coordinate;
44
import org.locationtech.jts.geom.Geometry;
5-
import org.locationtech.jts.geom.GeometryFactory;
65
import org.locationtech.jts.geom.Polygon;
76

87
/**
@@ -48,18 +47,17 @@ public static boolean pointInBbox(double[] bbox, double x, double y) {
4847
/**
4948
* bbo转为geometry
5049
* @param bbox bbox
51-
* @param gf GeometryFactory 为了防止频繁创建而直接传入
5250
* @return geometry
5351
*/
54-
public static Geometry bbox2Geometry(double[] bbox, GeometryFactory gf) {
52+
public static Geometry bbox2Geometry(double[] bbox) {
5553
Coordinate[] shell = new Coordinate[5];
5654
double xmin = bbox[0], ymin = bbox[1], xmax = bbox[2], ymax = bbox[3];
5755
shell[0] = new Coordinate(xmin, ymin);
5856
shell[1] = new Coordinate(xmax, ymin);
5957
shell[2] = new Coordinate(xmax, ymax);
6058
shell[3] = new Coordinate(xmin, ymax);
6159
shell[4] = shell[0];
62-
Polygon bboxGeo = gf.createPolygon(shell);
60+
Polygon bboxGeo = Singleton.geometryFactory.createPolygon(shell);
6361
return bboxGeo;
6462
}
6563

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.wowtools.neo4j.rtree.util;
2+
3+
import org.locationtech.jts.geom.GeometryFactory;
4+
5+
/**
6+
* 单例对象类
7+
* @author liuyu
8+
* @date 2020/11/30
9+
*/
10+
public class Singleton {
11+
public static final GeometryFactory geometryFactory = new GeometryFactory();
12+
13+
}

src/test/java/org/wowtools/neo4j/rtree/CacheTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.neo4j.graphdb.Node;
1111
import org.neo4j.graphdb.Transaction;
1212
import org.wowtools.neo4j.rtree.spatial.RTreeIndex;
13+
import org.wowtools.neo4j.rtree.util.Singleton;
1314

1415
import java.util.HashSet;
1516
import java.util.LinkedList;
@@ -86,7 +87,6 @@ private BuildCell buildIndex(int geoCacheSize) {
8687
}
8788
HashSet<String> intersectWkt = new HashSet<>();
8889
//随机写入测试数据(一堆范围内的三角形)
89-
GeometryFactory gf = new GeometryFactory();
9090
WKBWriter wkbWriter = new WKBWriter();
9191
Random random = new Random(233);
9292
try (Transaction tx = db.beginTx()) {
@@ -97,7 +97,7 @@ private BuildCell buildIndex(int geoCacheSize) {
9797
for (int i1 = 0; i1 < pointNum; i1++) {
9898
coordinates[i1] = new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100);
9999
}
100-
Geometry geo = gf.createLineString(coordinates);
100+
Geometry geo = Singleton.geometryFactory.createLineString(coordinates);
101101
// geo = geo.buffer(1);
102102
byte[] wkb = wkbWriter.write(geo);//转为wkb
103103
node.setProperty(geometryFileName, wkb);//设置空间字段值,必须为wkb格式

src/test/java/org/wowtools/neo4j/rtree/QueryByBboxTest.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.neo4j.graphdb.Node;
1010
import org.neo4j.graphdb.Transaction;
1111
import org.wowtools.neo4j.rtree.spatial.RTreeIndex;
12+
import org.wowtools.neo4j.rtree.util.Singleton;
1213

1314
import java.util.*;
1415

@@ -26,9 +27,8 @@ private void testPoint(GraphDatabaseService db, RTreeIndex rTreeIndex) {
2627
HashSet<String> intersectWkt = new HashSet<>();
2728
RectangleIntersects bboxRectangleIntersects;//用于校验查询结果
2829
{
29-
GeometryFactory gf = new GeometryFactory();
3030
Coordinate c0 = new Coordinate(bbox[0], bbox[1]);
31-
Polygon bboxPolygon = gf.createPolygon(new Coordinate[]{
31+
Polygon bboxPolygon = Singleton.geometryFactory.createPolygon(new Coordinate[]{
3232
c0,
3333
new Coordinate(bbox[2], bbox[1]),
3434
new Coordinate(bbox[2], bbox[3]),
@@ -39,14 +39,13 @@ private void testPoint(GraphDatabaseService db, RTreeIndex rTreeIndex) {
3939
}
4040

4141
//随机写入测试数据
42-
GeometryFactory gf = new GeometryFactory();
4342
WKBWriter wkbWriter = new WKBWriter();
4443
Random random = new Random(233);
4544
try (Transaction tx = db.beginTx()) {
4645
List<Node> sidxList = new LinkedList<>();
4746
for (int i = 0; i < geoNum; i++) {
4847
Node node = tx.createNode(testLabel);//新建节点
49-
Point geo = gf.createPoint(new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100));//构建一个geometry,POINT(x y)
48+
Point geo = Singleton.geometryFactory.createPoint(new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100));//构建一个geometry,POINT(x y)
5049
byte[] wkb = wkbWriter.write(geo);//转为wkb
5150
node.setProperty(geometryFileName, wkb);//设置空间字段值,必须为wkb格式
5251
node.setProperty(wktFileName, geo.toText());//设置其他值(可选)
@@ -84,9 +83,8 @@ private void testLine(GraphDatabaseService db, RTreeIndex rTreeIndex) {
8483
HashSet<String> intersectWkt = new HashSet<>();
8584
RectangleIntersects bboxRectangleIntersects;//用于校验查询结果
8685
{
87-
GeometryFactory gf = new GeometryFactory();
8886
Coordinate c0 = new Coordinate(bbox[0], bbox[1]);
89-
Polygon bboxPolygon = gf.createPolygon(new Coordinate[]{
87+
Polygon bboxPolygon = Singleton.geometryFactory.createPolygon(new Coordinate[]{
9088
c0,
9189
new Coordinate(bbox[2], bbox[1]),
9290
new Coordinate(bbox[2], bbox[3]),
@@ -97,7 +95,6 @@ private void testLine(GraphDatabaseService db, RTreeIndex rTreeIndex) {
9795
}
9896

9997
//随机写入测试数据
100-
GeometryFactory gf = new GeometryFactory();
10198
WKBWriter wkbWriter = new WKBWriter();
10299
Random random = new Random(233);
103100
try (Transaction tx = db.beginTx()) {
@@ -112,7 +109,7 @@ private void testLine(GraphDatabaseService db, RTreeIndex rTreeIndex) {
112109
for (int i1 = 0; i1 < lineNum; i1++) {
113110
coords[i1] = new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100);
114111
}
115-
LineString geo = gf.createLineString(coords);
112+
LineString geo = Singleton.geometryFactory.createLineString(coords);
116113
byte[] wkb = wkbWriter.write(geo);//转为wkb
117114
node.setProperty(geometryFileName, wkb);//设置空间字段值,必须为wkb格式
118115
node.setProperty(wktFileName, geo.toText());//设置其他值(可选)
@@ -148,9 +145,8 @@ private void testPolygon(GraphDatabaseService db, RTreeIndex rTreeIndex) {
148145
HashSet<String> intersectWkt = new HashSet<>();
149146
RectangleIntersects bboxRectangleIntersects;//用于校验查询结果
150147
{
151-
GeometryFactory gf = new GeometryFactory();
152148
Coordinate c0 = new Coordinate(bbox[0], bbox[1]);
153-
Polygon bboxPolygon = gf.createPolygon(new Coordinate[]{
149+
Polygon bboxPolygon = Singleton.geometryFactory.createPolygon(new Coordinate[]{
154150
c0,
155151
new Coordinate(bbox[2], bbox[1]),
156152
new Coordinate(bbox[2], bbox[3]),
@@ -161,7 +157,6 @@ private void testPolygon(GraphDatabaseService db, RTreeIndex rTreeIndex) {
161157
}
162158

163159
//随机写入测试数据
164-
GeometryFactory gf = new GeometryFactory();
165160
WKBWriter wkbWriter = new WKBWriter();
166161
Random random = new Random(233);
167162
try (Transaction tx = db.beginTx()) {
@@ -177,7 +172,7 @@ private void testPolygon(GraphDatabaseService db, RTreeIndex rTreeIndex) {
177172
coords[i1] = new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100);
178173
}
179174
coords[lineNum-1] = coords[0];
180-
Polygon geo = gf.createPolygon(coords);
175+
Polygon geo = Singleton.geometryFactory.createPolygon(coords);
181176
byte[] wkb = wkbWriter.write(geo);//转为wkb
182177
node.setProperty(geometryFileName, wkb);//设置空间字段值,必须为wkb格式
183178
node.setProperty(wktFileName, geo.toText());//设置其他值(可选)

src/test/java/org/wowtools/neo4j/rtree/QueryByGeometryTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.neo4j.graphdb.Node;
1212
import org.neo4j.graphdb.Transaction;
1313
import org.wowtools.neo4j.rtree.spatial.RTreeIndex;
14+
import org.wowtools.neo4j.rtree.util.Singleton;
1415

1516
import java.util.HashSet;
1617
import java.util.LinkedList;
@@ -35,14 +36,13 @@ private void testPoint(GraphDatabaseService db, RTreeIndex rTreeIndex) {
3536
throw new RuntimeException(e);
3637
}
3738
//随机写入测试数据
38-
GeometryFactory gf = new GeometryFactory();
3939
WKBWriter wkbWriter = new WKBWriter();
4040
Random random = new Random(233);
4141
try (Transaction tx = db.beginTx()) {
4242
List<Node> sidxList = new LinkedList<>();
4343
for (int i = 0; i < geoNum; i++) {
4444
Node node = tx.createNode(testLabel);//新建节点
45-
Point geo = gf.createPoint(new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100));//构建一个geometry,POINT(x y)
45+
Point geo = Singleton.geometryFactory.createPoint(new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100));//构建一个geometry,POINT(x y)
4646
byte[] wkb = wkbWriter.write(geo);//转为wkb
4747
node.setProperty(geometryFileName, wkb);//设置空间字段值,必须为wkb格式
4848
node.setProperty(wktFileName, geo.toText());//设置其他值(可选)
@@ -85,7 +85,6 @@ private void testLine(GraphDatabaseService db, RTreeIndex rTreeIndex) {
8585
HashSet<String> intersectWkt = new HashSet<>();
8686

8787
//随机写入测试数据
88-
GeometryFactory gf = new GeometryFactory();
8988
WKBWriter wkbWriter = new WKBWriter();
9089
Random random = new Random(233);
9190
try (Transaction tx = db.beginTx()) {
@@ -100,7 +99,7 @@ private void testLine(GraphDatabaseService db, RTreeIndex rTreeIndex) {
10099
for (int i1 = 0; i1 < lineNum; i1++) {
101100
coords[i1] = new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100);
102101
}
103-
LineString geo = gf.createLineString(coords);
102+
LineString geo = Singleton.geometryFactory.createLineString(coords);
104103
byte[] wkb = wkbWriter.write(geo);//转为wkb
105104
node.setProperty(geometryFileName, wkb);//设置空间字段值,必须为wkb格式
106105
node.setProperty(wktFileName, geo.toText());//设置其他值(可选)
@@ -140,7 +139,6 @@ private void testPolygon(GraphDatabaseService db, RTreeIndex rTreeIndex) {
140139
}
141140
HashSet<String> intersectWkt = new HashSet<>();
142141
//随机写入测试数据
143-
GeometryFactory gf = new GeometryFactory();
144142
WKBWriter wkbWriter = new WKBWriter();
145143
Random random = new Random(233);
146144
try (Transaction tx = db.beginTx()) {
@@ -156,7 +154,7 @@ private void testPolygon(GraphDatabaseService db, RTreeIndex rTreeIndex) {
156154
coords[i1] = new Coordinate(random.nextDouble() * 100, random.nextDouble() * 100);
157155
}
158156
coords[lineNum-1] = coords[0];
159-
Polygon geo = gf.createPolygon(coords);
157+
Polygon geo = Singleton.geometryFactory.createPolygon(coords);
160158
byte[] wkb = wkbWriter.write(geo);//转为wkb
161159
node.setProperty(geometryFileName, wkb);//设置空间字段值,必须为wkb格式
162160
node.setProperty(wktFileName, geo.toText());//设置其他值(可选)

0 commit comments

Comments
 (0)