Skip to content

Commit

Permalink
Added in MultiPolygonSearch for WKT MultiPolygon
Browse files Browse the repository at this point in the history
Added in MultiPolygonSearch for WKT MultiPolygon which returns a list of cells that matches the resolution
  • Loading branch information
davidfauth committed Jun 7, 2024
1 parent 784b0f5 commit 6b6f7f6
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 7 deletions.
60 changes: 58 additions & 2 deletions Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1189,11 +1189,67 @@ If resolutionExpr is smaller than 0 or larger than 15, the function returns -2
call com.neo4jh3.multilineash3String('MULTILINESTRING((40.736691045913472 73.99311953429248), (40.73733046783797 -73.99265431029018) , (40.93733046783797 -74.00265431029018))',17, 'latlon') yield value return value
'-2'

## com.neo4jh3.multipolygonash3( geographyExpr, resolutionExpr )
Returns a list of H3 cell IDs (as a LONG) corresponding to the provided POLYGON at the specified resolution.

### Syntax
call com.neo4jh3.multipolygonash3( geographyExpr, resolutionExpr ) yield value

### Arguments
* geographyExpr: A STRING expression representing a POLYGON geography in WKT format (Longitude and then Latitude)
* 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 a list of H3 cell IDs (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 POLYGON, the function returns -1

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

### Example
call com.neo4jh3.multipolygonash3('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',8) yield value return value
613229524731035647, 613229523017662463, 613229524217233407, 613229524714258431, 613229524733132799

call com.neo4jh3.multipolygonash3('ZZZ(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',8) yield value return value
-1

call com.neo4jh3.multipolygonash3('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',19) yield value return value
-2

## com.neo4jh3.multipolygonash3String( geographyExpr, resolutionExpr )
Returns a list of H3 cell IDs (as a STRING) corresponding to the provided POLYGON at the specified resolution.

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

### Arguments
* geographyExpr: A STRING expression representing a POLYGON geography in WKT format (Longitude and then Latitude)
* 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 a list of H3 cell IDs (as a STRING) corresponding to the provided POLYGON 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 polygon, the function returns -1

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

### Example
call com.neo4jh3.multipolygonash3String('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',8) yield value return value
'882a100f35fffff','882a1008d3fffff','882a100d4bfffff', '882a100f25fffff', '882a100f37fffff'
call com.neo4jh3.multipolygonash3String('zzz(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',8) yield value return value
-1

call com.neo4jh3.multipolygonash3String('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',18) yield value return value
-2

## com.neo4jh3.polygonash3( geographyExpr, resolutionExpr )
Returns a list of H3 cell IDs (as a LONG) corresponding to the provided POLYGON at the specified resolution.

### Syntax
call com.neo4jh3. polygonash3( geographyExpr, resolutionExpr ) yield value
call com.neo4jh3.polygonash3( geographyExpr, resolutionExpr ) yield value

### Arguments
* geographyExpr: A STRING expression representing a POLYGON geography in WKT format (Longitude and then Latitude)
Expand Down Expand Up @@ -1221,7 +1277,7 @@ If resolutionExpr is smaller than 0 or larger than 15, the function returns -2
Returns a list of H3 cell IDs (as a STRING) corresponding to the provided POLYGON at the specified resolution.

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

### Arguments
* geographyExpr: A STRING expression representing a POLYGON geography in WKT format (Longitude and then Latitude)
Expand Down
97 changes: 92 additions & 5 deletions src/main/java/com/neo4jh3/uber/Uberh3.java
Original file line number Diff line number Diff line change
Expand Up @@ -1283,9 +1283,9 @@ public Stream<H3StringAddress> polygonash3String(
return listh3Address.stream().map(H3StringAddress::of);
}

// Stub procedure
// Added June 2024
@Procedure(name = "com.neo4jh3.multipolygonash3", mode = Mode.READ)
@Description("com.neo4jh3.multipolygonash3(wktString, resolution) - Returns the hex addresses as a long from a WKT Polygon string.")
@Description("com.neo4jh3.multipolygonash3(wktString, resolution) - Returns the hex addresses as a long from a WKT MultiPolygon string.")
public Stream<H3LongAddress> multipolygonash3(
@Name("wktString") String wktString,
@Name("h3Res") Long h3Res) throws InterruptedException
Expand All @@ -1295,17 +1295,104 @@ public Stream<H3LongAddress> multipolygonash3(
List<LatLng> hexHoles = new ArrayList<>();
List<List<LatLng>> holesList = new ArrayList<>();
String mls = "";

String mps = "";

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("MULTIPOLYGON", "");
String[] polygonStrings = mls.split("\\)\\),\\(\\(");
// Loop through each polygon and create Hex Addresses
for (int i=0; i<polygonStrings.length; i++){
mps = polygonStrings[i];
mps = mps.replace("(","");
mps = mps.replace(")","");
String[] lonlatPairs = mps.split(",");
for (int ii = 0; ii < lonlatPairs.length; ii++) {
LatLng tmpGeoCoord = null;
tmpGeoCoord = returnLngLat(lonlatPairs[ii]);
hexPoints.add(tmpGeoCoord);
}

if (!hexHoles.isEmpty()) {
holesList.add(hexHoles);
listh3Address = h3.polygonToCells(hexPoints, holesList, h3Resolution);
} else {
listh3Address = h3.polygonToCells(hexPoints, null, h3Resolution);
}
}
} else {
listh3Address = Collections.singletonList(-2L);

}
} catch (Exception e) {
System.out.println(e);
listh3Address = Collections.singletonList(-1L);
//e.printStackTrace();
}

return listh3Address.stream().map(H3LongAddress::of);
}


// Added June 2024
@Procedure(name = "com.neo4jh3.multipolygonash3String", mode = Mode.READ)
@Description("com.neo4jh3.multipolygonash3String(wktString, resolution) - Returns the hex addresses as a String from a WKT MultiPolygon string.")
public Stream<H3StringAddress> multipolygonash3String(
@Name("wktString") String wktString,
@Name("h3Res") Long h3Res) throws InterruptedException
{
List<String> listh3Address = new ArrayList<String>();
List<LatLng> hexPoints = new ArrayList<>();
List<LatLng> hexHoles = new ArrayList<>();
List<List<LatLng>> holesList = new ArrayList<>();
String mls = "";
String mps = "";

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("MULTIPOLYGON", "");
String[] polygonStrings = mls.split("\\)\\),\\(\\(");
// Loop through each polygon and create Hex Addresses
for (int i=0; i<polygonStrings.length; i++){
mps = polygonStrings[i];
mps = mps.replace("(","");
mps = mps.replace(")","");
String[] lonlatPairs = mps.split(",");
for (int ii = 0; ii < lonlatPairs.length; ii++) {
LatLng tmpGeoCoord = null;
tmpGeoCoord = returnLngLat(lonlatPairs[ii]);
hexPoints.add(tmpGeoCoord);
}

if (!hexHoles.isEmpty()) {
holesList.add(hexHoles);
listh3Address = h3.polygonToCellAddresses(hexPoints, holesList, h3Resolution);
} else {
listh3Address = h3.polygonToCellAddresses(hexPoints, null, h3Resolution);
}
}
} else {
listh3Address = Collections.singletonList("-2");

}
} catch (Exception e) {
System.out.println(e);
listh3Address = Collections.singletonList("-1");
//e.printStackTrace();
}

return listh3Address.stream().map(H3StringAddress::of);
}

// Geography Functions

Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/neo4jh3/Neo4jH3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,24 @@ public void should_return_hex_address() throws InterruptedException {

result = session.run("call com.neo4jh3.polygonash3String('POLYGON((-77.436031 38.471420, -77.395123 38.536569, -77.294124 38.511703, -77.310611 38.395709, -77.436031 38.471420))',13) yield value return value limit 1");
assertEquals("\"8d2aabc333558bf\"",result.single().get(0).toString());

result = session.run("call com.neo4jh3.multipolygonash3('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-77.436031 38.471420, -77.395123 38.536569, -77.294124 38.511703, -77.310611 38.395709, -77.436031 38.471420)))',19) yield value return value limit 1");
assertEquals(-2L,result.single().get(0).asLong());

result = session.run("call com.neo4jh3.multipolygonash3('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-77.436031 38.471420, -77.395123 38.536569, -77.294124 38.511703, -77.310611 38.395709, -77.436031 38.471420)))',8) yield value return value limit 1");
assertEquals(613240230178193407L,result.single().get(0).asLong());

result = session.run("call com.neo4jh3.multipolygonash3('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-77.436031 38.471420, -77.395123 38.536569, -77.294124 38.511703, -77.310611 38.395709, -77.436031 38.471420)))',8) yield value return count(value)");
assertEquals(162L,result.single().get(0).asLong());

result = session.run("call com.neo4jh3.multipolygonash3String('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-77.436031 38.471420, -77.395123 38.536569, -77.294124 38.511703, -77.310611 38.395709, -77.436031 38.471420)))',19) yield value return value limit 1");
assertEquals("\"-2\"", result.single().get("value").toString());

result = session.run("call com.neo4jh3.multipolygonash3String('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',8) yield value return value limit 1");
assertEquals("\"882a100f35fffff\"", result.single().get("value").toString());

result = session.run("call com.neo4jh3.multipolygonash3String('MULTIPOLYGON(((-73.927881 40.769855, -73.915189 40.763915,-73.923600 40.753839, -73.944151 40.759309, -73.927881 40.769855)),((-73.945007 40.796650, -73.944668 40.791605, -73.946504 40.789007, -73.949935 40.790397)))',8) yield value return count(value)");
assertEquals(5L,result.single().get(0).asLong());

}
driver.close();
Expand Down

0 comments on commit 6b6f7f6

Please sign in to comment.