Skip to content

Commit 7b42195

Browse files
committed
Updated for Neo4j 5.7
Updated for Neo4j 5.7 Changed Multilinestring from function to procedure
1 parent d0ca2a0 commit 7b42195

File tree

5 files changed

+163
-55
lines changed

5 files changed

+163
-55
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG - Neo4jH3
22

3+
## 5.7 2023-05-01
4+
5+
* [Added] Tested through Neo4j versions 5.7
6+
* [Modified] Converted com.neo4jh3.multilineash3 from a function to a procedure. This procedure returns a list of H3 numbers that are along the line using the H3 gridpathcells feature.
7+
* [Modified] Converted com.neo4jh3.multilineash3String from a function to a procedure. This procedure returns a list of H3 numbers that are along the line using the H3 gridpathcells feature.
8+
9+
310
## 5.5 2023-02-24
411

512
* [Added] Tested through Neo4j versions 5.5

dependency-reduced-pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.neo4jh3</groupId>
55
<artifactId>neo4jh3</artifactId>
6-
<version>5.3.1</version>
6+
<version>5.7.0</version>
77
<build>
88
<plugins>
99
<plugin>
@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>org.neo4j</groupId>
4545
<artifactId>neo4j</artifactId>
46-
<version>5.5.0</version>
46+
<version>5.7.0</version>
4747
<scope>provided</scope>
4848
<exclusions>
4949
<exclusion>
@@ -119,7 +119,7 @@
119119
<dependency>
120120
<groupId>org.neo4j.test</groupId>
121121
<artifactId>neo4j-harness</artifactId>
122-
<version>5.5.0</version>
122+
<version>5.7.0</version>
123123
<scope>test</scope>
124124
<exclusions>
125125
<exclusion>
@@ -179,7 +179,7 @@
179179
<dependency>
180180
<groupId>junit</groupId>
181181
<artifactId>junit</artifactId>
182-
<version>4.12</version>
182+
<version>4.13.2</version>
183183
<scope>test</scope>
184184
<exclusions>
185185
<exclusion>
@@ -190,7 +190,7 @@
190190
</dependency>
191191
</dependencies>
192192
<properties>
193-
<neo4j.version>5.5.0</neo4j.version>
193+
<neo4j.version>5.7.0</neo4j.version>
194194
<commons.version>4.1</commons.version>
195195
<csv.version>1.2</csv.version>
196196
<lang3.version>3.7</lang3.version>

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
<groupId>com.neo4jh3</groupId>
88
<artifactId>neo4jh3</artifactId>
9-
<version>5.3.1</version>
9+
<version>5.7.0</version>
1010

1111
<properties>
12-
<neo4j.version>5.5.0</neo4j.version>
12+
<neo4j.version>5.7.0</neo4j.version>
1313
<csv.version>1.2</csv.version>
1414
<lang3.version>3.7</lang3.version>
1515
<uber.version>4.0.2</uber.version>
@@ -82,7 +82,7 @@
8282
<dependency>
8383
<groupId>junit</groupId>
8484
<artifactId>junit</artifactId>
85-
<version>4.12</version>
85+
<version>4.13.2</version>
8686
<scope>test</scope>
8787
</dependency>
8888

src/main/java/com/neo4jh3/uber/Uberh3.java

Lines changed: 133 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.*;
23+
import java.util.stream.Collectors;
2324
import java.util.stream.Stream;
2425
import java.util.stream.StreamSupport;
2526

@@ -738,45 +739,27 @@ public String pointash3String(
738739
return h3Address;
739740
}
740741

741-
// Geography Functions
742-
@UserFunction(name = "com.neo4jh3.multilineash3")
742+
// New Geo Functions
743+
@Procedure(name = "com.neo4jh3.multilineash3", mode = Mode.READ)
743744
@Description("com.neo4jh3.multilineash3(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
744-
public Long multilineash3(
745+
public Stream<H3LongAddress> multilineash3(
745746
@Name("wktString") String wktString,
746747
@Name("h3Res") Long h3Res) throws InterruptedException
747748
{
749+
List<Long> listh3Address = new ArrayList<Long>();
750+
List<Long> gpCells = new ArrayList<Long>();
748751
Long h3Address = 0L;
749-
if (h3 == null) {
750-
throw new InterruptedException("h3 failed to initialize");
751-
}
752-
753-
final int h3Resolution = h3Res == null ? DEFAULT_H3_RESOLUTION : h3Res.intValue();
752+
Long h3StartAddress = 0L;
753+
Long h3MidAddress = 0L;
754+
Long h3EndAddress = 0L;
755+
Double fromLat = 0.0;
756+
Double fromLon = 0.0;
757+
Double toLat = 0.0;
758+
Double toLon = 0.0;
759+
Double midLat = 0.0;
760+
Double midLon = 0.0;
761+
String mls = "";
754762

755-
try {
756-
if (h3Resolution > 0 && h3Resolution <= 15) {
757-
Geometry geometry = GeometryReader.readGeometry(wktString);
758-
//System.out.println(geometry.getGeometryType());
759-
if (geometry.getGeometryType().toString().equalsIgnoreCase("MultiLineString")){
760-
h3Address=h3.latLngToCell(geometry.getEnvelope().getMinX(), geometry.getEnvelope().getMinY(), h3Resolution);
761-
}
762-
} else {
763-
h3Address = -2L;
764-
}
765-
} catch (Exception e) {
766-
//System.out.println(e);
767-
h3Address = -1L;
768-
// TODO Auto-generated catch block
769-
//e.printStackTrace();
770-
}
771-
return h3Address;
772-
}
773-
@UserFunction(name = "com.neo4jh3.multilineash3String")
774-
@Description("com.neo4jh3.multilineash3String(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
775-
public String multilineash3String(
776-
@Name("wktString") String wktString,
777-
@Name("h3Res") Long h3Res) throws InterruptedException
778-
{
779-
String h3Address = "";
780763
if (h3 == null) {
781764
throw new InterruptedException("h3 failed to initialize");
782765
}
@@ -785,22 +768,132 @@ public String multilineash3String(
785768

786769
try {
787770
if (h3Resolution > 0 && h3Resolution <= 15) {
788-
Geometry geometry = GeometryReader.readGeometry(wktString);
789-
if (geometry.getGeometryType().toString().equalsIgnoreCase("MultiLineString")){
790-
h3Address=h3.latLngToCellAddress(geometry.getEnvelope().getMinX(), geometry.getEnvelope().getMinY(), h3Resolution);
791-
}
771+
mls = wktString.replace("MULTILINESTRING((", "");
772+
mls = mls.replace(" (","");
773+
mls = mls.replace(")","");
774+
mls = mls.replace("(","");
775+
String[] latlonPairs = mls.split(",");
776+
for (int i = 0; i < latlonPairs.length; i++) {
777+
if (i > 0){
778+
fromLat = Double.valueOf(latlonPairs[i-1].split(" ")[0]);
779+
fromLon = Double.valueOf(latlonPairs[i-1].split(" ")[1]);
780+
toLat = Double.valueOf(latlonPairs[i].split(" ")[0]);
781+
toLon = Double.valueOf(latlonPairs[i].split(" ")[1]);
782+
midLat = (fromLat + toLat) / 2;
783+
midLon = (fromLon + toLon) / 2;
784+
h3StartAddress = h3.latLngToCell(fromLat, fromLon, h3Resolution);
785+
h3MidAddress = h3.latLngToCell(midLat, midLon, h3Resolution);
786+
h3EndAddress = h3.latLngToCell(toLat, toLon, h3Resolution);
787+
try {
788+
gpCells = h3.gridPathCells(h3StartAddress, h3MidAddress);
789+
for (int j = 0; j < gpCells.size(); j++) {
790+
listh3Address.add(gpCells.get(j));
791+
}
792+
gpCells.clear();
793+
} catch (Exception e1){
794+
}
795+
try {
796+
gpCells = h3.gridPathCells((h3MidAddress), h3EndAddress);
797+
for (int j = 0; j < gpCells.size(); j++) {
798+
listh3Address.add(gpCells.get(j));
799+
}
800+
gpCells.clear();
801+
} catch (Exception e1){
802+
803+
}
804+
805+
}
806+
}
792807
} else {
793-
h3Address = "-2";
808+
listh3Address = Collections.singletonList(-2L);
809+
794810
}
795811
} catch (Exception e) {
796812
//System.out.println(e);
797-
h3Address = "-1";
813+
listh3Address = Collections.singletonList(-1L);
798814
// TODO Auto-generated catch block
799815
//e.printStackTrace();
800816
}
801-
return h3Address;
817+
return listh3Address.stream().map(H3LongAddress::of);
802818
}
803819

820+
// New Geo Functions
821+
@Procedure(name = "com.neo4jh3.multilineash3String", mode = Mode.READ)
822+
@Description("com.neo4jh3.multilineash3String(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
823+
public Stream<H3StringAddress> multilineash3String(
824+
@Name("wktString") String wktString,
825+
@Name("h3Res") Long h3Res) throws InterruptedException
826+
{
827+
List<String> listh3Address = new ArrayList<String>();
828+
List<String> gpCells = new ArrayList<String>();
829+
String h3StartAddress = "";
830+
String h3MidAddress = "";
831+
String h3EndAddress = "";
832+
Double fromLat = 0.0;
833+
Double fromLon = 0.0;
834+
Double toLat = 0.0;
835+
Double toLon = 0.0;
836+
Double midLat = 0.0;
837+
Double midLon = 0.0;
838+
String mls = "";
839+
840+
if (h3 == null) {
841+
throw new InterruptedException("h3 failed to initialize");
842+
}
843+
844+
final int h3Resolution = h3Res == null ? DEFAULT_H3_RESOLUTION : h3Res.intValue();
845+
846+
try {
847+
if (h3Resolution > 0 && h3Resolution <= 15) {
848+
mls = wktString.replace("MULTILINESTRING((", "");
849+
mls = mls.replace(" (","");
850+
mls = mls.replace(")","");
851+
mls = mls.replace("(","");
852+
String[] latlonPairs = mls.split(",");
853+
for (int i = 0; i < latlonPairs.length; i++) {
854+
if (i > 0){
855+
fromLat = Double.valueOf(latlonPairs[i-1].split(" ")[0]);
856+
fromLon = Double.valueOf(latlonPairs[i-1].split(" ")[1]);
857+
toLat = Double.valueOf(latlonPairs[i].split(" ")[0]);
858+
toLon = Double.valueOf(latlonPairs[i].split(" ")[1]);
859+
midLat = (fromLat + toLat) / 2;
860+
midLon = (fromLon + toLon) / 2;
861+
h3StartAddress = h3.latLngToCellAddress(fromLat, fromLon, h3Resolution);
862+
h3MidAddress = h3.latLngToCellAddress(midLat, midLon, h3Resolution);
863+
h3EndAddress = h3.latLngToCellAddress(toLat, toLon, h3Resolution);
864+
try {
865+
gpCells = h3.gridPathCells(h3StartAddress, h3MidAddress);
866+
for (int j = 0; j < gpCells.size(); j++) {
867+
listh3Address.add(gpCells.get(j));
868+
}
869+
gpCells.clear();
870+
} catch (Exception e1){
871+
//listh3Address = Collections.singletonList("-1");
872+
}
873+
try {
874+
gpCells = h3.gridPathCells((h3MidAddress), h3EndAddress);
875+
for (int j = 0; j < gpCells.size(); j++) {
876+
listh3Address.add(gpCells.get(j));
877+
}
878+
gpCells.clear();
879+
} catch (Exception e1){
880+
//listh3Address = Collections.singletonList("-1");
881+
}
882+
}
883+
}
884+
} else {
885+
listh3Address = Collections.singletonList("-2");
886+
}
887+
} catch (Exception e) {
888+
//System.out.println(e);
889+
listh3Address = Collections.singletonList("-1");
890+
// TODO Auto-generated catch block
891+
//e.printStackTrace();
892+
}
893+
return listh3Address.stream().map(H3StringAddress::of);
894+
}
895+
// Geography Functions
896+
804897
@UserFunction(name = "com.neo4jh3.centeraswkb")
805898
@Description("com.neo4jh3.centeraswkb(hexAddress) - Provides the distance in grid cells between the two indexes.")
806899
public String centeraswkb(
@@ -1657,7 +1750,6 @@ public Stream<H3StringAddress> gridpathcellString(@Name("fromAddress") String fr
16571750
return ringList.stream().map(H3StringAddress::of);
16581751
}
16591752
}
1660-
16611753

16621754
/*
16631755
* Return list of hex addresses for a line

src/test/java/com/neo4jh3/Neo4jH3Test.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,25 @@ public void should_return_hex_address() throws InterruptedException {
296296
result=session.run("call com.neo4jh3.polygonToCellsString(['37.7866,-122.3805','37.7198,-122.3544','37.7076,-122.5123','37.7835,-122.5247','37.8151,-122.4798'],[],20,'latlon') yield value return value limit 1");
297297
assertEquals("\"-2\"",result.single().get(0).toString());
298298

299+
result = session.run("call com.neo4jh3.multilineash3('MULTILINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018) , (40.93733046783797 -74.00265431029018))',12) yield value return value limit 1");
300+
assertEquals(631243922688264703L, result.single().get(0).asLong(),0);
301+
302+
result = session.run("call com.neo4jh3.multilineash3String('MULTILINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018) , (40.93733046783797 -74.00265431029018))',12) yield value return value limit 1");
303+
assertEquals("\"8c2a100d27549ff\"",result.single().get(0).toString());
304+
305+
result = session.run("call com.neo4jh3.multilineash3String('ZZZ((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018) , (40.93733046783797 -74.00265431029018))',12) yield value return value limit 1");
306+
assertEquals("\"-1\"",result.single().get(0).toString());
307+
308+
result = session.run("call com.neo4jh3.multilineash3String('MULTILINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018) , (40.93733046783797 -74.00265431029018))',17) yield value return value limit 1");
309+
assertEquals("\"-2\"",result.single().get(0).toString());
310+
311+
result = session.run("call com.neo4jh3.multilineash3('MULTILINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018) , (40.93733046783797 -74.00265431029018))',17) yield value return value limit 1");
312+
assertEquals(-2L,result.single().get(0).asLong(),0);
313+
299314
/* Geography tests */
300315
result = session.run("RETURN com.neo4jh3.pointash3('POINT(37.8199 -122.4783)',13) AS value");
301316
assertEquals(635714569676958015L, result.single().get("value").asLong(),0);
302317

303-
result = session.run("RETURN com.neo4jh3.multilineash3('MULTILINESTRING((37.2713558667319 -121.91508032705622), (37.353926450852256 -121.86222328902491))',13) AS value");
304-
assertEquals(635714810904422079L, result.single().get("value").asLong(),0);
305-
306-
result = session.run("RETURN com.neo4jh3.multilineash3('MULTILINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018))',13) AS value");
307-
assertEquals(635747522315622719L, result.single().get("value").asLong(),0);
308-
309318
result = session.run("RETURN com.neo4jh3.centeraswkb(599686042433355775) AS value");
310319
assertEquals("\"00000000014042AC42F51330C7C05E7E7CF1A5AD49\"", result.single().get("value").toString());
311320

0 commit comments

Comments
 (0)