Skip to content

Commit

Permalink
Updated for Neo4j 5.8 / Uber 4.1.1
Browse files Browse the repository at this point in the history
Updated for Neo4j 5.8
Updated Uber H3 library to  4.1.1
Added LINESTRING as a valid geo type
  • Loading branch information
davidfauth committed May 19, 2023
1 parent 7af478e commit 42f0f1d
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 154 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ src/.DS_Store
src/.DS_Store
src/.DS_Store
*.class
.vscode/settings.json
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG - Neo4jH3

## 5.8 2023-0519

* [Added] Tested through Neo4j 5.7
* [Added] Added support for converting a LINESTRING geospatial object to H3 number - com.neo4jh3.lineash3
* [Added] Added support for converting a LINESTRING geospatial object to H3 string - com.neo4jh3.lineash3String
* [Updated] Bumped H3 library to 4.1.1


## 5.7 2023-05-01

* [Added] Tested through Neo4j versions 5.7
Expand Down
60 changes: 58 additions & 2 deletions Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,63 @@ A value of the type of STRING representing, as a hexadecimal string, the H3 cell

RETURN com.neo4jh3.latlongash3String( 97.8199, -222.4783, 13) AS value
-4

## com.neo4jh3.lineash3( geographyExpr, resolutionExpr )
Returns the H3 cell ID (as a LONG) corresponding to the provided LINESTRING at the specified resolution.

### Syntax
RETURN com.neo4jh3.lineash3( geographyExpr, resolutionExpr )

### Arguments
* geographyExpr: A STRING expression representing a LINESTRING geography in WKT format
* resolutionExpr: An INT expression, whose value is expected to be between 0 and 15 inclusive, specifying the resolution of the child H3 cell ID.

### Returns
Returns the H3 cell ID (as a LONG) corresponding to the provided point at the specified resolution.

### Error conditions
If geographyExpr is of type STRING and the value is either an invalid WKT or does not represent a point, the function returns -1

If resolutionExpr is smaller than 0 or larger than 15, the function returns -2

### Example
RETURN com.neo4jh3.lineash3('LINESTRING((37.2713558667319 -121.91508032705622), (37.353926450852256 -121.86222328902491))',13) AS value
635714810904422079

RETURN com.neo4jh3.lineash3('zzz(37.8199 -122.4783)',13) AS value
-1

RETURN com.neo4jh3.lineash3('LINESTRING((37.2713558667319 -121.91508032705622), (37.353926450852256 -121.86222328902491))',16) AS value
-2

## com.neo4jh3.lineash3String( geographyExpr, resolutionExpr )
Returns the H3 cell ID (as a STRING) corresponding to the provided LINESTRING at the specified resolution.

### Syntax
RETURN com.neo4jh3.lineash3String( geographyExpr, resolutionExpr )

### Arguments
* geographyExpr: A STRING expression representing a LINESTRING geography in WKT format
* resolutionExpr: An INT expression, whose value is expected to be between 0 and 15 inclusive, specifying the resolution of the child H3 cell ID.

### Returns
Returns the H3 cell ID (as a LONG) corresponding to the provided LINESTRING at the specified resolution.

### Error conditions
If geographyExpr is of type STRING and the value is either an invalid WKT or does not represent a point, the function returns -1

If resolutionExpr is smaller than 0 or larger than 15, the function returns -2

### Example
RETURN com.neo4jh3.lineash3String('LINESTRING((37.2713558667319 -121.91508032705622), (37.353926450852256 -121.86222328902491))',13) AS value
8d283409a69a6bf

RETURN com.neo4jh3.lineash3String('zzz(37.8199 -122.4783)',13) AS value
-1

RETURN com.neo4jh3.lineash3String('LINESTRING((37.2713558667319 -121.91508032705622), (37.353926450852256 -121.86222328902491))',16) AS value
-2

## com.neo4jh3.maxChild( h3CellIdExpr, resolutionExpr )
Returns the child of minimum value of the input H3 cell at the specified resolution.

Expand Down Expand Up @@ -792,7 +848,7 @@ A value of the same type as the type of the h3CellIdExpr expression, correspondi

### Error conditions
If h3CellIdExpr is an invalid h3 address, the function returns -1.
If resolutionExpr is an invalid resolution or smaller than h3_resolution(h3CellIdExpr), the function returns -2.
If resolutionExpr is an i nvalid resolution or smaller than h3_resolution(h3CellIdExpr), the function returns -2.

### Example
RETURN com.neo4jh3.minChild(599686042433355775, 10) AS value
Expand Down
14 changes: 9 additions & 5 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.neo4jh3</groupId>
<artifactId>neo4jh3</artifactId>
<version>5.7.0</version>
<version>5.8.0</version>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>5.7.0</version>
<version>5.8.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand All @@ -58,6 +58,10 @@
<artifactId>neo4j-fabric</artifactId>
<groupId>org.neo4j</groupId>
</exclusion>
<exclusion>
<artifactId>neo4j-query-router</artifactId>
<groupId>org.neo4j</groupId>
</exclusion>
<exclusion>
<artifactId>neo4j-procedure</artifactId>
<groupId>org.neo4j</groupId>
Expand Down Expand Up @@ -119,7 +123,7 @@
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<version>5.7.0</version>
<version>5.8.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -190,11 +194,11 @@
</dependency>
</dependencies>
<properties>
<neo4j.version>5.7.0</neo4j.version>
<neo4j.version>5.8.0</neo4j.version>
<commons.version>4.1</commons.version>
<csv.version>1.2</csv.version>
<lang3.version>3.7</lang3.version>
<uber.version>4.0.2</uber.version>
<uber.version>4.1.1</uber.version>
</properties>
</project>

6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>com.neo4jh3</groupId>
<artifactId>neo4jh3</artifactId>
<version>5.7.0</version>
<version>5.8.0</version>

<properties>
<neo4j.version>5.7.0</neo4j.version>
<neo4j.version>5.8.0</neo4j.version>
<csv.version>1.2</csv.version>
<lang3.version>3.7</lang3.version>
<uber.version>4.0.2</uber.version>
<uber.version>4.1.1</uber.version>
<commons.version>4.1</commons.version>
</properties>

Expand Down
156 changes: 154 additions & 2 deletions src/main/java/com/neo4jh3/uber/Uberh3.java
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ public String pointash3String(
return h3Address;
}

// New Geo Functions
@Procedure(name = "com.neo4jh3.multilineash3", mode = Mode.READ)
@Description("com.neo4jh3.multilineash3(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
public Stream<H3LongAddress> multilineash3(
Expand Down Expand Up @@ -817,7 +816,7 @@ public Stream<H3LongAddress> multilineash3(
return listh3Address.stream().map(H3LongAddress::of);
}

// New Geo Functions
// New Geo Procedures
@Procedure(name = "com.neo4jh3.multilineash3String", mode = Mode.READ)
@Description("com.neo4jh3.multilineash3String(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
public Stream<H3StringAddress> multilineash3String(
Expand Down Expand Up @@ -892,6 +891,159 @@ public Stream<H3StringAddress> multilineash3String(
}
return listh3Address.stream().map(H3StringAddress::of);
}

@Procedure(name = "com.neo4jh3.lineash3", mode = Mode.READ)
@Description("com.neo4jh3.lineash3(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
public Stream<H3LongAddress> lineash3(
@Name("wktString") String wktString,
@Name("h3Res") Long h3Res) throws InterruptedException
{
List<Long> listh3Address = new ArrayList<Long>();
List<Long> gpCells = new ArrayList<Long>();
Long h3Address = 0L;
Long h3StartAddress = 0L;
Long h3MidAddress = 0L;
Long h3EndAddress = 0L;
Double fromLat = 0.0;
Double fromLon = 0.0;
Double toLat = 0.0;
Double toLon = 0.0;
Double midLat = 0.0;
Double midLon = 0.0;
String mls = "";

if (h3 == null) {
throw new InterruptedException("h3 failed to initialize");
}

final int h3Resolution = h3Res == null ? DEFAULT_H3_RESOLUTION : h3Res.intValue();

try {
if (h3Resolution > 0 && h3Resolution <= 15) {
mls = wktString.replace("LINESTRING((", "");
mls = mls.replace(" (","");
mls = mls.replace(")","");
mls = mls.replace("(","");
String[] latlonPairs = mls.split(",");
for (int i = 0; i < latlonPairs.length; i++) {
if (i > 0){
fromLat = Double.valueOf(latlonPairs[i-1].split(" ")[0]);
fromLon = Double.valueOf(latlonPairs[i-1].split(" ")[1]);
toLat = Double.valueOf(latlonPairs[i].split(" ")[0]);
toLon = Double.valueOf(latlonPairs[i].split(" ")[1]);
midLat = (fromLat + toLat) / 2;
midLon = (fromLon + toLon) / 2;
h3StartAddress = h3.latLngToCell(fromLat, fromLon, h3Resolution);
h3MidAddress = h3.latLngToCell(midLat, midLon, h3Resolution);
h3EndAddress = h3.latLngToCell(toLat, toLon, h3Resolution);
try {
gpCells = h3.gridPathCells(h3StartAddress, h3MidAddress);
for (int j = 0; j < gpCells.size(); j++) {
listh3Address.add(gpCells.get(j));
}
gpCells.clear();
} catch (Exception e1){
}
try {
gpCells = h3.gridPathCells((h3MidAddress), h3EndAddress);
for (int j = 0; j < gpCells.size(); j++) {
listh3Address.add(gpCells.get(j));
}
gpCells.clear();
} catch (Exception e1){

}

}
}
} else {
listh3Address = Collections.singletonList(-2L);

}
} catch (Exception e) {
//System.out.println(e);
listh3Address = Collections.singletonList(-1L);
// TODO Auto-generated catch block
//e.printStackTrace();
}
return listh3Address.stream().map(H3LongAddress::of);
}

// New Geo Procedures
@Procedure(name = "com.neo4jh3.lineash3String", mode = Mode.READ)
@Description("com.neo4jh3.lineash3String(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
public Stream<H3StringAddress> lineash3String(
@Name("wktString") String wktString,
@Name("h3Res") Long h3Res) throws InterruptedException
{
List<String> listh3Address = new ArrayList<String>();
List<String> gpCells = new ArrayList<String>();
String h3StartAddress = "";
String h3MidAddress = "";
String h3EndAddress = "";
Double fromLat = 0.0;
Double fromLon = 0.0;
Double toLat = 0.0;
Double toLon = 0.0;
Double midLat = 0.0;
Double midLon = 0.0;
String mls = "";

if (h3 == null) {
throw new InterruptedException("h3 failed to initialize");
}

final int h3Resolution = h3Res == null ? DEFAULT_H3_RESOLUTION : h3Res.intValue();

try {
if (h3Resolution > 0 && h3Resolution <= 15) {
mls = wktString.replace("LINESTRING((", "");
mls = mls.replace(" (","");
mls = mls.replace(")","");
mls = mls.replace("(","");
String[] latlonPairs = mls.split(",");
for (int i = 0; i < latlonPairs.length; i++) {
if (i > 0){
fromLat = Double.valueOf(latlonPairs[i-1].split(" ")[0]);
fromLon = Double.valueOf(latlonPairs[i-1].split(" ")[1]);
toLat = Double.valueOf(latlonPairs[i].split(" ")[0]);
toLon = Double.valueOf(latlonPairs[i].split(" ")[1]);
midLat = (fromLat + toLat) / 2;
midLon = (fromLon + toLon) / 2;
h3StartAddress = h3.latLngToCellAddress(fromLat, fromLon, h3Resolution);
h3MidAddress = h3.latLngToCellAddress(midLat, midLon, h3Resolution);
h3EndAddress = h3.latLngToCellAddress(toLat, toLon, h3Resolution);
try {
gpCells = h3.gridPathCells(h3StartAddress, h3MidAddress);
for (int j = 0; j < gpCells.size(); j++) {
listh3Address.add(gpCells.get(j));
}
gpCells.clear();
} catch (Exception e1){
//listh3Address = Collections.singletonList("-1");
}
try {
gpCells = h3.gridPathCells((h3MidAddress), h3EndAddress);
for (int j = 0; j < gpCells.size(); j++) {
listh3Address.add(gpCells.get(j));
}
gpCells.clear();
} catch (Exception e1){
//listh3Address = Collections.singletonList("-1");
}
}
}
} else {
listh3Address = Collections.singletonList("-2");
}
} catch (Exception e) {
//System.out.println(e);
listh3Address = Collections.singletonList("-1");
// TODO Auto-generated catch block
//e.printStackTrace();
}
return listh3Address.stream().map(H3StringAddress::of);
}
// Geography Functions

@UserFunction(name = "com.neo4jh3.centeraswkb")
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/neo4jh3/Neo4jH3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ public void should_return_hex_address() throws InterruptedException {
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");
assertEquals(-2L,result.single().get(0).asLong(),0);

result = session.run("call com.neo4jh3.lineash3('LINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018) , (40.93733046783797 -74.00265431029018))',12) yield value return value limit 1");
assertEquals(631243922688264703L, result.single().get(0).asLong(),0);

/* Geography tests */
result = session.run("RETURN com.neo4jh3.pointash3('POINT(37.8199 -122.4783)',13) AS value");
assertEquals(635714569676958015L, result.single().get("value").asLong(),0);
Expand Down
Binary file removed target/original-neo4jh3-5.3.1.jar
Binary file not shown.
Loading

0 comments on commit 42f0f1d

Please sign in to comment.