2020import java .io .IOException ;
2121import java .nio .charset .StandardCharsets ;
2222import java .util .*;
23+ import java .util .stream .Collectors ;
2324import java .util .stream .Stream ;
2425import 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
0 commit comments