-
Notifications
You must be signed in to change notification settings - Fork 192
/
SpatialProceduresTest.java
1339 lines (1212 loc) · 60.5 KB
/
SpatialProceduresTest.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Spatial.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.gis.spatial.procedures;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.neo4j.gis.spatial.Constants.LABEL_LAYER;
import static org.neo4j.gis.spatial.Constants.PROP_GEOMENCODER;
import static org.neo4j.gis.spatial.Constants.PROP_GEOMENCODER_CONFIG;
import static org.neo4j.gis.spatial.Constants.PROP_LAYER;
import static org.neo4j.gis.spatial.Constants.PROP_LAYER_CLASS;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.neo4j.exceptions.KernelException;
import org.neo4j.gis.spatial.AbstractApiTest;
import org.neo4j.gis.spatial.Layer;
import org.neo4j.gis.spatial.SpatialDatabaseService;
import org.neo4j.gis.spatial.SpatialRelationshipTypes;
import org.neo4j.gis.spatial.functions.SpatialFunctions;
import org.neo4j.gis.spatial.index.IndexManager;
import org.neo4j.gis.spatial.utilities.ReferenceNodes;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.spatial.Geometry;
import org.neo4j.graphdb.spatial.Point;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.procedure.GlobalProcedures;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
public class SpatialProceduresTest extends AbstractApiTest {
@Override
protected void registerApiProceduresAndFunctions() throws KernelException {
registerProceduresAndFunctions(SpatialProcedures.class);
registerProceduresAndFunctions(SpatialFunctions.class);
}
public static void testCall(GraphDatabaseService db, String call, Consumer<Map<String, Object>> consumer) {
testCall(db, call, null, consumer);
}
public static void testCall(GraphDatabaseService db, String call, Map<String, Object> params,
Consumer<Map<String, Object>> consumer) {
testCall(db, call, params, consumer, true);
}
public static void testCallFails(GraphDatabaseService db, String call, Map<String, Object> params, String error) {
try {
testResult(db, call, params, (res) -> {
while (res.hasNext()) {
res.next();
}
});
fail("Expected an exception containing '" + error + "', but no exception was thrown");
} catch (Exception e) {
assertTrue(e.getMessage().contains(error));
}
}
public static void testCall(GraphDatabaseService db, String call, Map<String, Object> params,
Consumer<Map<String, Object>> consumer, boolean onlyOne) {
testResult(db, call, params, (res) -> {
assertTrue(res.hasNext(), "Expect at least one result but got none: " + call);
Map<String, Object> row = res.next();
consumer.accept(row);
if (onlyOne) {
assertFalse(res.hasNext(), "Expected only one result, but there are more");
}
});
}
public static void testCallCount(GraphDatabaseService db, String call, Map<String, Object> params, int count) {
testResult(db, call, params, (res) -> {
int numLeft = count;
while (numLeft > 0) {
assertTrue(res.hasNext(),
"Expected " + count + " results but found only " + (count - numLeft));
res.next();
numLeft--;
}
assertFalse(res.hasNext(), "Expected " + count + " results but there are more");
});
}
public static void testResult(GraphDatabaseService db, String call, Consumer<Result> resultConsumer) {
testResult(db, call, null, resultConsumer);
}
public static void testResult(GraphDatabaseService db, String call, Map<String, Object> params,
Consumer<Result> resultConsumer) {
try (Transaction tx = db.beginTx()) {
Map<String, Object> p = (params == null) ? Map.of() : params;
resultConsumer.accept(tx.execute(call, p));
tx.commit();
}
}
public static void registerProceduresAndFunctions(GraphDatabaseService db, Class<?> procedure)
throws KernelException {
GlobalProcedures procedures = ((GraphDatabaseAPI) db).getDependencyResolver()
.resolveDependency(GlobalProcedures.class);
procedures.registerProcedure(procedure);
procedures.registerFunction(procedure);
}
private static Layer makeLayerOfVariousTypes(SpatialDatabaseService spatial, Transaction tx, String name,
int index) {
switch (index % 3) {
case 0:
return spatial.getOrCreateSimplePointLayer(tx, name, SpatialDatabaseService.RTREE_INDEX_NAME, "x", "y");
case 1:
return spatial.getOrCreateNativePointLayer(tx, name, SpatialDatabaseService.RTREE_INDEX_NAME,
"location");
default:
return spatial.getOrCreateDefaultLayer(tx, name);
}
}
private void makeOldSpatialModel(Transaction tx, String... layers) {
KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
SpatialDatabaseService spatial = new SpatialDatabaseService(
new IndexManager((GraphDatabaseAPI) db, ktx.securityContext()));
ArrayList<Node> layerNodes = new ArrayList<>();
int index = 0;
// First create a set of layers
for (String name : layers) {
Layer layer = makeLayerOfVariousTypes(spatial, tx, name, index);
layerNodes.add(layer.getLayerNode(tx));
index++;
}
// Then downgrade to old format, without label and with reference node and relationships
Node root = ReferenceNodes.createDeprecatedReferenceNode(tx, "spatial_root");
for (Node node : layerNodes) {
node.removeLabel(LABEL_LAYER);
root.createRelationshipTo(node, SpatialRelationshipTypes.LAYER);
}
}
@Test
public void old_spatial_model_throws_errors() {
try (Transaction tx = db.beginTx()) {
makeOldSpatialModel(tx, "layer1", "layer2", "layer3");
tx.commit();
}
testCallFails(db, "CALL spatial.layers", null,
"Old reference node exists - please upgrade the spatial database to the new format");
}
@Test
public void old_spatial_model_can_be_upgraded() {
try (Transaction tx = db.beginTx()) {
makeOldSpatialModel(tx, "layer1", "layer2", "layer3");
tx.commit();
}
testCallFails(db, "CALL spatial.layers", null,
"Old reference node exists - please upgrade the spatial database to the new format");
testCallCount(db, "CALL spatial.upgrade", null, 3);
testCallCount(db, "CALL spatial.layers", null, 3);
}
@Test
public void add_node_to_non_existing_layer() {
execute("CALL spatial.addPointLayer('some_name')");
Node node = createNode("CREATE (n:Point {latitude:60.1,longitude:15.2}) RETURN n", "n");
testCallFails(db, "CALL spatial.addNode.byId('wrong_name',$nodeId)", Map.of("nodeId", node.getElementId()),
"No such layer 'wrong_name'");
}
@Test
public void add_node_point_layer() {
execute("CALL spatial.addPointLayer('points')");
executeWrite("CREATE (n:Point {latitude:60.1,longitude:15.2})");
Node node = createNode("MATCH (n:Point) WITH n CALL spatial.addNode('points',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.bbox('points',{longitude:15.0,latitude:60.0},{longitude:15.3, latitude:60.2})",
r -> assertEquals(node, r.get("node")));
testCall(db, "CALL spatial.withinDistance('points',{longitude:15.0,latitude:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_node_and_search_bbox_and_distance() {
execute("CALL spatial.addPointLayerXY('geom','lon','lat')");
Node node = createNode(
"CREATE (n:Node {lat:60.1,lon:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.bbox('geom',{lon:15.0,lat:60.0},{lon:15.3, lat:60.2})",
r -> assertEquals(node, r.get("node")));
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
// This tests issue https://github.com/neo4j-contrib/spatial/issues/298
public void add_node_point_layer_and_search_multiple_points_precision() {
execute("CALL spatial.addPointLayer('bar')");
execute("create (n:Point) set n={latitude: 52.2029252, longitude: 0.0905302} with n call spatial.addNode('bar', n) yield node return node");
execute("create (n:Point) set n={latitude: 52.202925, longitude: 0.090530} with n call spatial.addNode('bar', n) yield node return node");
// long countLow = execute("call spatial.withinDistance('bar', {latitude:52.202925,longitude:0.0905302}, 100) YIELD node RETURN node");
// assertThat("Expected two nodes when using low precision", countLow, equalTo(2L));
long countHigh = execute(
"call spatial.withinDistance('bar', {latitude:52.2029252,longitude:0.0905302}, 100) YIELD node RETURN node");
MatcherAssert.assertThat("Expected two nodes when using high precision", countHigh, equalTo(2L));
}
@Test
public void add_node_and_search_bbox_and_distance_geohash() {
execute("CALL spatial.addPointLayerGeohash('geom')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.bbox('geom',{lon:15.0,lat:60.0},{lon:15.3, lat:60.2})",
r -> assertEquals(node, r.get("node")));
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_node_and_search_bbox_and_distance_zorder() {
execute("CALL spatial.addPointLayerZOrder('geom')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.bbox('geom',{lon:15.0,lat:60.0},{lon:15.3, lat:60.2})",
r -> assertEquals(node, r.get("node")));
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_node_and_search_bbox_and_distance_hilbert() {
execute("CALL spatial.addPointLayerHilbert('geom')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.bbox('geom',{lon:15.0,lat:60.0},{lon:15.3, lat:60.2})",
r -> assertEquals(node, r.get("node")));
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
// This tests issue https://github.com/neo4j-contrib/spatial/issues/298
public void add_node_point_layer_and_search_multiple_points_precision_geohash() {
execute("CALL spatial.addPointLayerGeohash('bar')");
execute("create (n:Point) set n={latitude: 52.2029252, longitude: 0.0905302} with n call spatial.addNode('bar', n) yield node return node");
execute("create (n:Point) set n={latitude: 52.202925, longitude: 0.090530} with n call spatial.addNode('bar', n) yield node return node");
// long countLow = execute("call spatial.withinDistance('bar', {latitude:52.202925,longitude:0.0905302}, 100) YIELD node RETURN node");
// assertEquals("Expected two nodes when using low precision", countLow, equalTo(2L));
long countHigh = execute(
"call spatial.withinDistance('bar', {latitude:52.2029252,longitude:0.0905302}, 100) YIELD node RETURN node");
assertEquals(2L, countHigh, "Expected two nodes when using high precision");
}
//
// Testing interaction between Neo4j Spatial and the Neo4j 3.0 Point type (point() and distance() functions)
//
@Test
public void create_point_and_distance() {
double distance = (Double) executeObject(
"WITH point({latitude: 5.0, longitude: 4.0}) as geometry RETURN point.distance(geometry, point({latitude: 5.0, longitude: 4.0})) as distance",
"distance");
System.out.println(distance);
}
@Test
public void create_point_and_return() {
Object geometry = executeObject("RETURN point({latitude: 5.0, longitude: 4.0}) as geometry", "geometry");
assertInstanceOf(Geometry.class, geometry, "Should be Geometry type");
}
@Test
public void create_node_decode_to_geometry() {
execute("CALL spatial.addWKTLayer('geom','geom')");
Object geometry = executeObject(
"CREATE (n:Node {geom:'POINT(4.0 5.0)'}) RETURN spatial.decodeGeometry('geom',n) AS geometry",
"geometry");
assertInstanceOf(Geometry.class, geometry, "Should be Geometry type");
}
@Test
// TODO: Currently this only works for point geometries because Neo4k 3.4 can only return Point geometries from procedures
public void create_node_and_convert_to_geometry() {
execute("CALL spatial.addWKTLayer('geom','geom')");
Geometry geom = (Geometry) executeObject(
"CREATE (n:Node {geom:'POINT(4.0 5.0)'}) RETURN spatial.decodeGeometry('geom',n) AS geometry",
"geometry");
double distance = (Double) executeObject("RETURN point.distance($geom, point({y: 6.0, x: 4.0})) as distance",
Map.of("geom", geom), "distance");
MatcherAssert.assertThat("Expected the cartesian distance of 1.0", distance, closeTo(1.0, 0.00001));
}
@Test
public void create_a_pointlayer_with_x_and_y() {
testCall(db, "CALL spatial.addPointLayerXY('geom','lon','lat')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
}
@Test
public void create_a_pointlayer_with_config() {
testCall(db, "CALL spatial.addPointLayerWithConfig('geom','lon:lat')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
}
@Test
public void create_a_pointlayer_with_config_on_existing_wkt_layer() {
execute("CALL spatial.addWKTLayer('geom','wkt')");
try {
testCall(db, "CALL spatial.addPointLayerWithConfig('geom','lon:lat')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
fail("Expected exception to be thrown");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Cannot create existing layer"));
}
}
@Test
public void create_a_pointlayer_with_config_on_existing_osm_layer() {
execute("CALL spatial.addLayer('geom','OSM','')");
try {
testCall(db, "CALL spatial.addPointLayerWithConfig('geom','lon:lat')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
fail("Expected exception to be thrown");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Cannot create existing layer"));
}
}
@Test
public void create_a_pointlayer_with_rtree() {
testCall(db, "CALL spatial.addPointLayer('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
}
@Test
public void create_a_pointlayer_with_geohash() {
testCall(db, "CALL spatial.addPointLayerGeohash('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
}
@Test
public void create_a_pointlayer_with_zorder() {
testCall(db, "CALL spatial.addPointLayerZOrder('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
}
@Test
public void create_a_pointlayer_with_hilbert() {
testCall(db, "CALL spatial.addPointLayerHilbert('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
}
@Test
public void create_and_delete_a_pointlayer_with_rtree() {
testCall(db, "CALL spatial.addPointLayer('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
testCallCount(db, "CALL spatial.layers()", null, 1);
execute("CALL spatial.removeLayer('geom')");
testCallCount(db, "CALL spatial.layers()", null, 0);
}
@Test
public void create_and_delete_a_pointlayer_with_geohash() {
testCall(db, "CALL spatial.addPointLayerGeohash('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
testCallCount(db, "CALL spatial.layers()", null, 1);
execute("CALL spatial.removeLayer('geom')");
testCallCount(db, "CALL spatial.layers()", null, 0);
}
@Test
public void create_and_delete_a_pointlayer_with_zorder() {
testCall(db, "CALL spatial.addPointLayerZOrder('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
testCallCount(db, "CALL spatial.layers()", null, 1);
execute("CALL spatial.removeLayer('geom')");
testCallCount(db, "CALL spatial.layers()", null, 0);
}
@Test
public void create_and_delete_a_pointlayer_with_hilbert() {
testCall(db, "CALL spatial.addPointLayerHilbert('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
testCallCount(db, "CALL spatial.layers()", null, 1);
execute("CALL spatial.removeLayer('geom')");
testCallCount(db, "CALL spatial.layers()", null, 0);
}
@Test
public void create_a_simple_pointlayer_using_named_encoder() {
testCall(db, "CALL spatial.addLayerWithEncoder('geom','SimplePointEncoder','')", (r) -> {
Node node = dump((Node) r.get("node"));
assertEquals("geom", node.getProperty("layer"));
assertEquals("org.neo4j.gis.spatial.encoders.SimplePointEncoder",
node.getProperty("geomencoder"));
assertEquals("org.neo4j.gis.spatial.SimplePointLayer", node.getProperty("layer_class"));
assertFalse(node.hasProperty(PROP_GEOMENCODER_CONFIG));
});
}
@Test
public void create_a_simple_pointlayer_using_named_and_configured_encoder() {
testCall(db, "CALL spatial.addLayerWithEncoder('geom','SimplePointEncoder','x:y:mbr')", (r) -> {
Node node = dump((Node) r.get("node"));
assertEquals("geom", node.getProperty(PROP_LAYER));
assertEquals("org.neo4j.gis.spatial.encoders.SimplePointEncoder",
node.getProperty(PROP_GEOMENCODER));
assertEquals("org.neo4j.gis.spatial.SimplePointLayer", node.getProperty(PROP_LAYER_CLASS));
assertEquals("x:y:mbr", node.getProperty(PROP_GEOMENCODER_CONFIG));
});
}
@Test
public void create_a_native_pointlayer_using_named_encoder() {
testCall(db, "CALL spatial.addLayerWithEncoder('geom','NativePointEncoder','')", (r) -> {
Node node = dump((Node) r.get("node"));
assertEquals("geom", node.getProperty(PROP_LAYER));
assertEquals("org.neo4j.gis.spatial.encoders.NativePointEncoder",
node.getProperty(PROP_GEOMENCODER));
assertEquals("org.neo4j.gis.spatial.SimplePointLayer", node.getProperty(PROP_LAYER_CLASS));
assertFalse(node.hasProperty(PROP_GEOMENCODER_CONFIG));
});
}
@Test
public void create_a_native_pointlayer_using_named_and_configured_encoder() {
testCall(db, "CALL spatial.addLayerWithEncoder('geom','NativePointEncoder','pos:mbr')", (r) -> {
Node node = dump((Node) r.get("node"));
assertEquals("geom", node.getProperty(PROP_LAYER));
assertEquals("org.neo4j.gis.spatial.encoders.NativePointEncoder",
node.getProperty(PROP_GEOMENCODER));
assertEquals("org.neo4j.gis.spatial.SimplePointLayer", node.getProperty(PROP_LAYER_CLASS));
assertEquals("pos:mbr", node.getProperty(PROP_GEOMENCODER_CONFIG));
});
}
@Test
public void create_a_native_pointlayer_using_named_and_configured_encoder_with_cartesian() {
testCall(db, "CALL spatial.addLayerWithEncoder('geom','NativePointEncoder','pos:mbr:Cartesian')", (r) -> {
Node node = dump((Node) r.get("node"));
assertEquals("geom", node.getProperty(PROP_LAYER));
assertEquals("org.neo4j.gis.spatial.encoders.NativePointEncoder",
node.getProperty(PROP_GEOMENCODER));
assertEquals("org.neo4j.gis.spatial.SimplePointLayer", node.getProperty(PROP_LAYER_CLASS));
assertEquals("pos:mbr:Cartesian", node.getProperty(PROP_GEOMENCODER_CONFIG));
});
}
@Test
public void create_a_native_pointlayer_using_named_and_configured_encoder_with_geographic() {
testCall(db, "CALL spatial.addLayerWithEncoder('geom','NativePointEncoder','pos:mbr:WGS-84')", (r) -> {
Node node = dump((Node) r.get("node"));
assertEquals("geom", node.getProperty(PROP_LAYER));
assertEquals("org.neo4j.gis.spatial.encoders.NativePointEncoder",
node.getProperty(PROP_GEOMENCODER));
assertEquals("org.neo4j.gis.spatial.SimplePointLayer", node.getProperty(PROP_LAYER_CLASS));
assertEquals("pos:mbr:WGS-84", node.getProperty(PROP_GEOMENCODER_CONFIG));
});
}
@Test
public void create_a_wkt_layer_using_know_format() {
testCall(db, "CALL spatial.addLayer('geom','WKT',null)",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
}
@Test
public void list_layer_names() {
String wkt = "LINESTRING (15.2 60.1, 15.3 60.1)";
execute("CALL spatial.addWKTLayer('geom','wkt')");
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", wkt));
testCall(db, "CALL spatial.layers()", (r) -> {
assertEquals("geom", r.get("name"));
assertEquals("EditableLayer(name='geom', encoder=WKTGeometryEncoder(geom='wkt', bbox='bbox'))",
r.get("signature"));
});
}
@Test
public void add_and_remove_layer() {
execute("CALL spatial.addWKTLayer('geom','wkt')");
testCallCount(db, "CALL spatial.layers()", null, 1);
execute("CALL spatial.removeLayer('geom')");
testCallCount(db, "CALL spatial.layers()", null, 0);
}
@Test
public void add_and_remove_multiple_layers() {
int NUM_LAYERS = 100;
String wkt = "LINESTRING (15.2 60.1, 15.3 60.1)";
for (int i = 0; i < NUM_LAYERS; i++) {
String name = "wktLayer_" + i;
testCallCount(db, "CALL spatial.layers()", null, i);
execute("CALL spatial.addWKTLayer($layerName,'wkt')", Map.of("layerName", name));
execute("CALL spatial.addWKT($layerName,$wkt)", Map.of("wkt", wkt, "layerName", name));
testCallCount(db, "CALL spatial.layers()", null, i + 1);
}
for (int i = 0; i < NUM_LAYERS; i++) {
String name = "wktLayer_" + i;
testCallCount(db, "CALL spatial.layers()", null, NUM_LAYERS - i);
execute("CALL spatial.removeLayer($layerName)", Map.of("layerName", name));
testCallCount(db, "CALL spatial.layers()", null, NUM_LAYERS - i - 1);
}
testCallCount(db, "CALL spatial.layers()", null, 0);
}
@Test
public void get_and_set_feature_attributes() {
execute("CALL spatial.addWKTLayer('geom','wkt')");
testCallCount(db, "CALL spatial.layers()", null, 1);
testCallCount(db, "CALL spatial.getFeatureAttributes('geom')", null, 0);
execute("CALL spatial.setFeatureAttributes('geom',['name','type','color'])");
testCallCount(db, "CALL spatial.getFeatureAttributes('geom')", null, 3);
}
@Test
public void list_spatial_procedures() {
testResult(db, "CALL spatial.procedures()", (res) -> {
Map<String, String> procs = new LinkedHashMap<>();
while (res.hasNext()) {
Map<String, Object> r = res.next();
procs.put(r.get("name").toString(), r.get("signature").toString());
}
for (String key : procs.keySet()) {
System.out.println(key + ": " + procs.get(key));
}
assertEquals("spatial.procedures() :: (name :: STRING, signature :: STRING)",
procs.get("spatial.procedures"));
assertEquals("spatial.layers() :: (name :: STRING, signature :: STRING)",
procs.get("spatial.layers"));
assertEquals("spatial.layer(name :: STRING) :: (node :: NODE)", procs.get("spatial.layer"));
assertEquals(
"spatial.addLayer(name :: STRING, type :: STRING, encoderConfig :: STRING) :: (node :: NODE)",
procs.get("spatial.addLayer"));
assertEquals("spatial.addNode(layerName :: STRING, node :: NODE) :: (node :: NODE)",
procs.get("spatial.addNode"));
assertEquals("spatial.addWKT(layerName :: STRING, geometry :: STRING) :: (node :: NODE)",
procs.get("spatial.addWKT"));
assertEquals("spatial.intersects(layerName :: STRING, geometry :: ANY) :: (node :: NODE)",
procs.get("spatial.intersects"));
});
}
@Test
public void list_layer_types() {
testResult(db, "CALL spatial.layerTypes()", (res) -> {
Map<String, String> procs = new LinkedHashMap<>();
while (res.hasNext()) {
Map<String, Object> r = res.next();
procs.put(r.get("name").toString(), r.get("signature").toString());
}
for (String key : procs.keySet()) {
System.out.println(key + ": " + procs.get(key));
}
assertEquals(
"RegisteredLayerType(name='SimplePoint', geometryEncoder=SimplePointEncoder, layerClass=SimplePointLayer, index=LayerRTreeIndex, crs='WGS84(DD)', defaultConfig='longitude:latitude')",
procs.get("simplepoint"));
assertEquals(
"RegisteredLayerType(name='NativePoint', geometryEncoder=NativePointEncoder, layerClass=SimplePointLayer, index=LayerRTreeIndex, crs='WGS84(DD)', defaultConfig='location')",
procs.get("nativepoint"));
assertEquals(
"RegisteredLayerType(name='WKT', geometryEncoder=WKTGeometryEncoder, layerClass=EditableLayerImpl, index=LayerRTreeIndex, crs='WGS84(DD)', defaultConfig='geometry')",
procs.get("wkt"));
assertEquals(
"RegisteredLayerType(name='WKB', geometryEncoder=WKBGeometryEncoder, layerClass=EditableLayerImpl, index=LayerRTreeIndex, crs='WGS84(DD)', defaultConfig='geometry')",
procs.get("wkb"));
assertEquals(
"RegisteredLayerType(name='Geohash', geometryEncoder=SimplePointEncoder, layerClass=SimplePointLayer, index=LayerGeohashPointIndex, crs='WGS84(DD)', defaultConfig='longitude:latitude')",
procs.get("geohash"));
assertEquals(
"RegisteredLayerType(name='ZOrder', geometryEncoder=SimplePointEncoder, layerClass=SimplePointLayer, index=LayerZOrderPointIndex, crs='WGS84(DD)', defaultConfig='longitude:latitude')",
procs.get("zorder"));
assertEquals(
"RegisteredLayerType(name='Hilbert', geometryEncoder=SimplePointEncoder, layerClass=SimplePointLayer, index=LayerHilbertPointIndex, crs='WGS84(DD)', defaultConfig='longitude:latitude')",
procs.get("hilbert"));
assertEquals(
"RegisteredLayerType(name='NativeGeohash', geometryEncoder=NativePointEncoder, layerClass=SimplePointLayer, index=LayerGeohashPointIndex, crs='WGS84(DD)', defaultConfig='location')",
procs.get("nativegeohash"));
assertEquals(
"RegisteredLayerType(name='NativeZOrder', geometryEncoder=NativePointEncoder, layerClass=SimplePointLayer, index=LayerZOrderPointIndex, crs='WGS84(DD)', defaultConfig='location')",
procs.get("nativezorder"));
assertEquals(
"RegisteredLayerType(name='NativeHilbert', geometryEncoder=NativePointEncoder, layerClass=SimplePointLayer, index=LayerHilbertPointIndex, crs='WGS84(DD)', defaultConfig='location')",
procs.get("nativehilbert"));
});
}
@Test
public void find_layer() {
String wkt = "LINESTRING (15.2 60.1, 15.3 60.1)";
execute("CALL spatial.addWKTLayer('geom','wkt')");
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", wkt));
testCall(db, "CALL spatial.layer('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
testCallFails(db, "CALL spatial.layer('badname')", null, "No such layer 'badname'");
}
@Test
public void add_a_node_to_the_spatial_rtree_index_for_simple_points() {
execute("CALL spatial.addPointLayer('geom')");
Node node = createNode("CREATE (n:Node {latitude:60.1,longitude:15.2}) RETURN n", "n");
testCall(db, "MATCH (n:Node) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_a_node_to_the_spatial_geohash_index_for_simple_points() {
execute("CALL spatial.addPointLayerGeohash('geom')");
Node node = createNode("CREATE (n:Node {latitude:60.1,longitude:15.2}) RETURN n", "n");
testCall(db, "MATCH (n:Node) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_a_node_to_the_spatial_zorder_index_for_simple_points() {
execute("CALL spatial.addPointLayerZOrder('geom')");
Node node = createNode("CREATE (n:Node {latitude:60.1,longitude:15.2}) RETURN n", "n");
testCall(db, "MATCH (n:Node) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_a_node_to_the_spatial_hilbert_index_for_simple_points() {
execute("CALL spatial.addPointLayerHilbert('geom')");
Node node = createNode("CREATE (n:Node {latitude:60.1,longitude:15.2}) RETURN n", "n");
testCall(db, "MATCH (n:Node) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_a_node_to_multiple_different_indexes_for_both_simple_and_native_points() {
String[] encoders = new String[]{"Simple", "Native"};
String[] indexes = new String[]{"Geohash", "ZOrder", "Hilbert", "RTree"};
for (String encoder : encoders) {
String procName = (encoder.equalsIgnoreCase("Native")) ? "addNativePointLayer" : "addPointLayer";
for (String indexType : indexes) {
String layerName = (encoder + indexType).toLowerCase();
String query =
"CALL spatial." + procName + (indexType.equals("RTree") ? "" : indexType) + "('" + layerName
+ "')";
execute(query);
}
}
testResult(db, "CALL spatial.layers()", (res) -> {
while (res.hasNext()) {
Map<String, Object> r = res.next();
String encoder =
r.get("name").toString().contains("native") ? "NativePointEncoder" : "SimplePointEncoder";
MatcherAssert.assertThat("Expect simple:native encoders to appear in simple:native layers",
r.get("signature").toString(), containsString(encoder));
}
});
testCallCount(db, "CALL spatial.layers()", null, indexes.length * encoders.length);
Node node = createNode("CREATE (n:Node {latitude:60.1,longitude:15.2}) SET n.location=point(n) RETURN n", "n");
for (String encoder : encoders) {
for (String indexType : indexes) {
String layerName = (encoder + indexType).toLowerCase();
testCall(db, "MATCH (node:Node) RETURN node", r -> assertEquals(node, r.get("node")));
testCall(db, "MATCH (n:Node) WITH n CALL spatial.addNode('" + layerName + "',n) YIELD node RETURN node",
r -> assertEquals(node, r.get("node")));
testCall(db, "CALL spatial.withinDistance('" + layerName + "',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
}
for (String encoder : encoders) {
for (String indexType : indexes) {
String layerName = (encoder + indexType).toLowerCase();
execute("CALL spatial.removeLayer('" + layerName + "')");
}
}
testCallCount(db, "CALL spatial.layers()", null, 0);
}
@Test
public void testDistanceNode() {
execute("CALL spatial.addPointLayer('geom')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void testDistanceNodeWithGeohashIndex() {
execute("CALL spatial.addPointLayer('geom','geohash')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void testDistanceNodeGeohash() {
execute("CALL spatial.addPointLayerGeohash('geom')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void testDistanceNodeZOrder() {
execute("CALL spatial.addPointLayerZOrder('geom')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void testDistanceNodeHilbert() {
execute("CALL spatial.addPointLayerHilbert('geom')");
Node node = createNode(
"CREATE (n:Node {latitude:60.1,longitude:15.2}) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
"node");
testCall(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_a_node_to_the_spatial_index_short() {
execute("CALL spatial.addPointLayerXY('geom','lon','lat')");
Node node = createNode("CREATE (n:Node {lat:60.1,lon:15.2}) RETURN n", "n");
testCall(db, "MATCH (n:Node) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_a_node_to_the_spatial_index_short_with_geohash() {
execute("CALL spatial.addPointLayerXY('geom','lon','lat','geohash')");
Node node = createNode("CREATE (n:Node {lat:60.1,lon:15.2}) RETURN n", "n");
testCall(db, "MATCH (n:Node) WITH n CALL spatial.addNode('geom',n) YIELD node RETURN node",
r -> assertEquals(node, r.get("node")));
}
@Test
public void add_two_nodes_to_the_spatial_layer() {
execute("CALL spatial.addPointLayerXY('geom','lon','lat')");
String node1;
String node2;
try (Transaction tx = db.beginTx()) {
Result result = tx.execute(
"CREATE (n1:Node {lat:60.1,lon:15.2}),(n2:Node {lat:60.1,lon:15.3}) WITH n1,n2 CALL spatial.addNodes('geom',[n1,n2]) YIELD count RETURN n1,n2,count");
Map<String, Object> row = result.next();
node1 = ((Node) row.get("n1")).getElementId();
node2 = ((Node) row.get("n2")).getElementId();
long count = (Long) row.get("count");
assertEquals(2L, count);
result.close();
tx.commit();
}
testResult(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)", res -> {
assertTrue(res.hasNext());
assertEquals(node1, ((Node) res.next().get("node")).getElementId());
assertTrue(res.hasNext());
assertEquals(node2, ((Node) res.next().get("node")).getElementId());
assertFalse(res.hasNext());
});
try (Transaction tx = db.beginTx()) {
Node node = (Node) tx.execute("MATCH (node) WHERE elementId(node) = $nodeId RETURN node",
Map.of("nodeId", node1)).next().get("node");
Result removeResult = tx.execute("CALL spatial.removeNode('geom',$node) YIELD nodeId RETURN nodeId",
Map.of("node", node));
assertEquals(node1, removeResult.next().get("nodeId"));
removeResult.close();
tx.commit();
}
testResult(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)", res -> {
assertTrue(res.hasNext());
assertEquals(node2, ((Node) res.next().get("node")).getElementId());
assertFalse(res.hasNext());
});
try (Transaction tx = db.beginTx()) {
Result removeResult = tx.execute("CALL spatial.removeNode.byId('geom',$nodeId) YIELD nodeId RETURN nodeId",
Map.of("nodeId", node2));
assertEquals(node2, removeResult.next().get("nodeId"));
removeResult.close();
tx.commit();
}
testResult(db, "CALL spatial.withinDistance('geom',{lon:15.0,lat:60.0},100)",
res -> assertFalse(res.hasNext()));
}
@Test
public void add_many_nodes_to_the_simple_point_layer_using_addNodes() {
// Playing with this number in both tests leads to rough benchmarking of the addNode/addNodes comparison
int count = 1000;
execute("CALL spatial.addLayer('simple_poi','SimplePoint','')");
String query = "UNWIND range(1,$count) as i\n" +
"CREATE (n:Point {id:i, latitude:(56.0+toFloat(i)/100.0),longitude:(12.0+toFloat(i)/100.0)})\n" +
"WITH collect(n) as points\n" +
"CALL spatial.addNodes('simple_poi',points) YIELD count\n" +
"RETURN count";
testCountQuery("addNodes", query, count, "count", Map.of("count", count));
testRemoveNodes("simple_poi", count);
}
@Test
public void add_many_nodes_to_the_simple_point_layer_using_addNode() {
// Playing with this number in both tests leads to rough benchmarking of the addNode/addNodes comparison
int count = 1000;
execute("CALL spatial.addLayer('simple_poi','SimplePoint','')");
String query = "UNWIND range(1,$count) as i\n" +
"CREATE (n:Point {id:i, latitude:(56.0+toFloat(i)/100.0),longitude:(12.0+toFloat(i)/100.0)})\n" +
"WITH n\n" +
"CALL spatial.addNode('simple_poi',n) YIELD node\n" +
"RETURN count(node)";
testCountQuery("addNode", query, count, "count(node)", Map.of("count", count));
testRemoveNode("simple_poi", count);
}
@Test
public void add_many_nodes_to_the_native_point_layer_using_addNodes() {
// Playing with this number in both tests leads to rough benchmarking of the addNode/addNodes comparison
int count = 1000;
execute("CALL spatial.addLayer('native_poi','NativePoint','')");
String query = "UNWIND range(1,$count) as i\n" +
"WITH i, Point({latitude:(56.0+toFloat(i)/100.0),longitude:(12.0+toFloat(i)/100.0)}) AS location\n" +
"CREATE (n:Point {id: i, location:location})\n" +
"WITH collect(n) as points\n" +
"CALL spatial.addNodes('native_poi',points) YIELD count\n" +
"RETURN count";
testCountQuery("addNodes", query, count, "count", Map.of("count", count));
testRemoveNodes("native_poi", count);
}
@Test
public void add_many_nodes_to_the_native_point_layer_using_addNode() {
// Playing with this number in both tests leads to rough benchmarking of the addNode/addNodes comparison
int count = 1000;
execute("CALL spatial.addLayer('native_poi','NativePoint','')");
String query = "UNWIND range(1,$count) as i\n" +
"WITH i, Point({latitude:(56.0+toFloat(i)/100.0),longitude:(12.0+toFloat(i)/100.0)}) AS location\n" +
"CREATE (n:Point {id: i, location:location})\n" +
"WITH n\n" +
"CALL spatial.addNode('native_poi',n) YIELD node\n" +
"RETURN count(node)";
testCountQuery("addNode", query, count, "count(node)", Map.of("count", count));
testRemoveNode("native_poi", count);
}
private void testRemoveNode(String layer, int count) {
// Check all nodes are there
testCountQuery("withinDistance",
"CALL spatial.withinDistance('" + layer + "',{lon:15.0,lat:60.0},1000) YIELD node RETURN count(node)",
count, "count(node)", null);
// Now remove half the points
String remove = "UNWIND range(1,$count) as i\n" +
"MATCH (n:Point {id:i})\n" +
"WITH n\n" +
"CALL spatial.removeNode('" + layer + "',n) YIELD nodeId\n" +
"RETURN count(nodeId)";
testCountQuery("removeNode", remove, count / 2, "count(nodeId)", Map.of("count", count / 2));
// Check that only half remain
testCountQuery("withinDistance",
"CALL spatial.withinDistance('" + layer + "',{lon:15.0,lat:60.0},1000) YIELD node RETURN count(node)",
count / 2, "count(node)", null);
}
private void testRemoveNodes(String layer, int count) {
// Check all nodes are there
testCountQuery("withinDistance",
"CALL spatial.withinDistance('" + layer + "',{lon:15.0,lat:60.0},1000) YIELD node RETURN count(node)",
count, "count(node)", null);
// Now remove half the points
String remove = "UNWIND range(1,$count) as i\n" +
"MATCH (n:Point {id:i})\n" +
"WITH collect(n) as points\n" +
"CALL spatial.removeNodes('" + layer + "',points) YIELD count\n" +
"RETURN count";
testCountQuery("removeNodes", remove, count / 2, "count", Map.of("count", count / 2));
// Check that only half remain
testCountQuery("withinDistance",
"CALL spatial.withinDistance('" + layer + "',{lon:15.0,lat:60.0},1000) YIELD node RETURN count(node)",
count / 2, "count(node)", null);
}
@Test
public void import_shapefile() {
testCountQuery("importShapefile", "CALL spatial.importShapefile('shp/highway.shp')", 143, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_shapefile_without_extension() {
testCountQuery("importShapefile", "CALL spatial.importShapefile('shp/highway')", 143, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_shapefile_to_layer() {
execute("CALL spatial.addWKTLayer('geom','wkt')");
testCountQuery("importShapefileToLayer", "CALL spatial.importShapefileToLayer('geom','shp/highway.shp')", 143,
"count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_osm() {
testCountQuery("importOSM", "CALL spatial.importOSM('map.osm')", 55, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_osm_twice_should_fail() {
testCountQuery("importOSM", "CALL spatial.importOSM('map.osm')", 55, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
testCallFails(db, "CALL spatial.importOSM('map.osm')", null, "Layer already exists: 'map.osm'");
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_osm_without_extension() {
testCountQuery("importOSM", "CALL spatial.importOSM('map')", 55, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_osm_to_layer() {
execute("CALL spatial.addLayer('geom','OSM','')");
testCountQuery("importOSMToLayer", "CALL spatial.importOSMToLayer('geom','map.osm')", 55, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_osm_twice_should_pass_with_different_layers() {
execute("CALL spatial.addLayer('geom1','OSM','')");
execute("CALL spatial.addLayer('geom2','OSM','')");
testCountQuery("importOSM", "CALL spatial.importOSMToLayer('geom1','map.osm')", 55, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 2);
testCallCount(db, "CALL spatial.withinDistance('geom1',{lon:6.3740429666,lat:50.93676351666},10000)", null,
217);
testCallCount(db, "CALL spatial.withinDistance('geom2',{lon:6.3740429666,lat:50.93676351666},10000)", null, 0);
testCountQuery("importOSM", "CALL spatial.importOSMToLayer('geom2','map.osm')", 55, "count", null);
testCallCount(db, "CALL spatial.layers()", null, 2);
testCallCount(db, "CALL spatial.withinDistance('geom1',{lon:6.3740429666,lat:50.93676351666},10000)", null,
217);
testCallCount(db, "CALL spatial.withinDistance('geom2',{lon:6.3740429666,lat:50.93676351666},10000)", null,
217);
}
@Disabled
@Test
public void import_cracow_to_layer() {
execute("CALL spatial.addLayer('geom','OSM','')");
testCountQuery("importCracowToLayer", "CALL spatial.importOSMToLayer('geom','issue-347/cra.osm')", 256253,
"count", null);
testCallCount(db, "CALL spatial.layers()", null, 1);
}
@Test
public void import_osm_to_layer_without_changesets() {
execute("CALL spatial.addLayer('osm_example','OSM','')");
testCountQuery("importOSMToLayerWithoutChangesets", "CALL spatial.importOSMToLayer('osm_example','sample.osm')",