Skip to content

Commit

Permalink
Updated for Neo4j 5.7
Browse files Browse the repository at this point in the history
Updated for Neo4j 5.7
Changed Multilinestring from function to procedure
  • Loading branch information
davidfauth committed May 2, 2023
1 parent d0ca2a0 commit 7b42195
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 55 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG - Neo4jH3

## 5.7 2023-05-01

* [Added] Tested through Neo4j versions 5.7
* [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.
* [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.


## 5.5 2023-02-24

* [Added] Tested through Neo4j versions 5.5
Expand Down
10 changes: 5 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.3.1</version>
<version>5.7.0</version>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>5.5.0</version>
<version>5.7.0</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -119,7 +119,7 @@
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<version>5.5.0</version>
<version>5.7.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -179,7 +179,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand All @@ -190,7 +190,7 @@
</dependency>
</dependencies>
<properties>
<neo4j.version>5.5.0</neo4j.version>
<neo4j.version>5.7.0</neo4j.version>
<commons.version>4.1</commons.version>
<csv.version>1.2</csv.version>
<lang3.version>3.7</lang3.version>
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

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

<properties>
<neo4j.version>5.5.0</neo4j.version>
<neo4j.version>5.7.0</neo4j.version>
<csv.version>1.2</csv.version>
<lang3.version>3.7</lang3.version>
<uber.version>4.0.2</uber.version>
Expand Down Expand Up @@ -82,7 +82,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

Expand Down
174 changes: 133 additions & 41 deletions src/main/java/com/neo4jh3/uber/Uberh3.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -738,45 +739,27 @@ public String pointash3String(
return h3Address;
}

// Geography Functions
@UserFunction(name = "com.neo4jh3.multilineash3")
// 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 Long multilineash3(
public Stream<H3LongAddress> multilineash3(
@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;
if (h3 == null) {
throw new InterruptedException("h3 failed to initialize");
}

final int h3Resolution = h3Res == null ? DEFAULT_H3_RESOLUTION : h3Res.intValue();
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 = "";

try {
if (h3Resolution > 0 && h3Resolution <= 15) {
Geometry geometry = GeometryReader.readGeometry(wktString);
//System.out.println(geometry.getGeometryType());
if (geometry.getGeometryType().toString().equalsIgnoreCase("MultiLineString")){
h3Address=h3.latLngToCell(geometry.getEnvelope().getMinX(), geometry.getEnvelope().getMinY(), h3Resolution);
}
} else {
h3Address = -2L;
}
} catch (Exception e) {
//System.out.println(e);
h3Address = -1L;
// TODO Auto-generated catch block
//e.printStackTrace();
}
return h3Address;
}
@UserFunction(name = "com.neo4jh3.multilineash3String")
@Description("com.neo4jh3.multilineash3String(wktString, resolution) - Provides the distance in grid cells between the two indexes.")
public String multilineash3String(
@Name("wktString") String wktString,
@Name("h3Res") Long h3Res) throws InterruptedException
{
String h3Address = "";
if (h3 == null) {
throw new InterruptedException("h3 failed to initialize");
}
Expand All @@ -785,22 +768,132 @@ public String multilineash3String(

try {
if (h3Resolution > 0 && h3Resolution <= 15) {
Geometry geometry = GeometryReader.readGeometry(wktString);
if (geometry.getGeometryType().toString().equalsIgnoreCase("MultiLineString")){
h3Address=h3.latLngToCellAddress(geometry.getEnvelope().getMinX(), geometry.getEnvelope().getMinY(), h3Resolution);
}
mls = wktString.replace("MULTILINESTRING((", "");
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 {
h3Address = "-2";
listh3Address = Collections.singletonList(-2L);

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

// New Geo Functions
@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(
@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("MULTILINESTRING((", "");
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")
@Description("com.neo4jh3.centeraswkb(hexAddress) - Provides the distance in grid cells between the two indexes.")
public String centeraswkb(
Expand Down Expand Up @@ -1657,7 +1750,6 @@ public Stream<H3StringAddress> gridpathcellString(@Name("fromAddress") String fr
return ringList.stream().map(H3StringAddress::of);
}
}


/*
* Return list of hex addresses for a line
Expand Down
21 changes: 15 additions & 6 deletions src/test/java/com/neo4jh3/Neo4jH3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,25 @@ public void should_return_hex_address() throws InterruptedException {
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");
assertEquals("\"-2\"",result.single().get(0).toString());

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");
assertEquals(631243922688264703L, result.single().get(0).asLong(),0);

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");
assertEquals("\"8c2a100d27549ff\"",result.single().get(0).toString());

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");
assertEquals("\"-1\"",result.single().get(0).toString());

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");
assertEquals("\"-2\"",result.single().get(0).toString());

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);

/* 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);

result = session.run("RETURN com.neo4jh3.multilineash3('MULTILINESTRING((37.2713558667319 -121.91508032705622), (37.353926450852256 -121.86222328902491))',13) AS value");
assertEquals(635714810904422079L, result.single().get("value").asLong(),0);

result = session.run("RETURN com.neo4jh3.multilineash3('MULTILINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018))',13) AS value");
assertEquals(635747522315622719L, result.single().get("value").asLong(),0);

result = session.run("RETURN com.neo4jh3.centeraswkb(599686042433355775) AS value");
assertEquals("\"00000000014042AC42F51330C7C05E7E7CF1A5AD49\"", result.single().get("value").toString());

Expand Down

0 comments on commit 7b42195

Please sign in to comment.