|
37 | 37 | import org.neo4j.gis.spatial.osm.OSMImporter;
|
38 | 38 | import org.neo4j.gis.spatial.pipes.GeoPipeFlow;
|
39 | 39 | import org.neo4j.gis.spatial.pipes.GeoPipeline;
|
| 40 | +import org.neo4j.gis.spatial.pipes.processing.OrthodromicDistance; |
40 | 41 | import org.neo4j.gis.spatial.rtree.ProgressLoggingListener;
|
41 | 42 | import org.neo4j.graphdb.GraphDatabaseService;
|
42 | 43 | import org.neo4j.graphdb.Node;
|
|
49 | 50 | import org.neo4j.procedure.*;
|
50 | 51 |
|
51 | 52 | import static org.neo4j.gis.spatial.SpatialDatabaseService.RTREE_INDEX_NAME;
|
| 53 | +import static org.neo4j.helpers.collection.MapUtil.map; |
52 | 54 | import static org.neo4j.procedure.Mode.*;
|
53 | 55 |
|
54 | 56 | import org.opengis.referencing.ReferenceIdentifier;
|
|
70 | 72 |
|
71 | 73 | public class SpatialProcedures {
|
72 | 74 |
|
73 |
| - public static final String DISTANCE = "OrthodromicDistance"; |
74 | 75 | @Context
|
75 | 76 | public GraphDatabaseService db;
|
76 | 77 |
|
@@ -125,7 +126,12 @@ public static class GeometryResult {
|
125 | 126 | public final Object geometry;
|
126 | 127 |
|
127 | 128 | public GeometryResult(org.neo4j.graphdb.spatial.Geometry geometry) {
|
128 |
| - this.geometry = geometry; |
| 129 | + // Unfortunately Neo4j 3.4 only copes with Points, other types need to be converted to a public type |
| 130 | + if(geometry instanceof org.neo4j.graphdb.spatial.Point) { |
| 131 | + this.geometry = geometry; |
| 132 | + }else{ |
| 133 | + this.geometry = toMap(geometry); |
| 134 | + } |
129 | 135 | }
|
130 | 136 | }
|
131 | 137 |
|
@@ -534,31 +540,48 @@ public Stream<NodeDistanceResult> findGeometriesWithinDistance(
|
534 | 540 | Layer layer = getLayerOrThrow(name);
|
535 | 541 | return GeoPipeline
|
536 | 542 | .startNearestNeighborLatLonSearch(layer, toCoordinate(coordinate), distanceInKm)
|
537 |
| - .sort(DISTANCE) |
| 543 | + .sort(OrthodromicDistance.DISTANCE) |
538 | 544 | .stream().map(r -> {
|
539 |
| - double distance = r.hasProperty(DISTANCE) ? ((Number) r.getProperty(DISTANCE)).doubleValue() : -1; |
| 545 | + double distance = r.hasProperty(OrthodromicDistance.DISTANCE) ? ((Number) r.getProperty(OrthodromicDistance.DISTANCE)).doubleValue() : -1; |
540 | 546 | return new NodeDistanceResult(r.getGeomNode(), distance);
|
541 | 547 | });
|
542 | 548 | }
|
543 | 549 |
|
544 |
| - @Procedure("spatial.decodeGeometry") |
| 550 | + @UserFunction("spatial.decodeGeometry") |
545 | 551 | @Description("Returns a geometry of a layer node as the Neo4j geometry type, to be passed to other procedures or returned to a client")
|
546 |
| - public Stream<GeometryResult> decodeGeometry( |
| 552 | + public Object decodeGeometry( |
547 | 553 | @Name("layerName") String name,
|
548 | 554 | @Name("node") Node node) {
|
549 | 555 |
|
550 | 556 | Layer layer = getLayerOrThrow(name);
|
551 |
| - return Stream.of(layer.getGeometryEncoder().decodeGeometry(node)).map(geom -> new GeometryResult(toNeo4jGeometry(layer, geom))); |
| 557 | + GeometryResult result = new GeometryResult(toNeo4jGeometry(layer, layer.getGeometryEncoder().decodeGeometry(node))); |
| 558 | + return result.geometry; |
| 559 | + } |
| 560 | + |
| 561 | + @UserFunction("spatial.asMap") |
| 562 | + @Description("Returns a Map object representing the Geometry, to be passed to other procedures or returned to a client") |
| 563 | + public Object asMap(@Name("object") Object geometry) { |
| 564 | + return toGeometryMap(geometry); |
| 565 | + } |
| 566 | + |
| 567 | + @UserFunction("spatial.asGeometry") |
| 568 | + @Description("Returns a geometry object as the Neo4j geometry type, to be passed to other functions or procedures or returned to a client") |
| 569 | + public Object asGeometry( |
| 570 | + @Name("geometry") Object geometry) { |
| 571 | + |
| 572 | + return toNeo4jGeometry(null, geometry); |
552 | 573 | }
|
553 | 574 |
|
| 575 | + @Deprecated |
554 | 576 | @Procedure("spatial.asGeometry")
|
555 | 577 | @Description("Returns a geometry object as the Neo4j geometry type, to be passed to other procedures or returned to a client")
|
556 |
| - public Stream<GeometryResult> asGeometry( |
| 578 | + public Stream<GeometryResult> asGeometryProc( |
557 | 579 | @Name("geometry") Object geometry) {
|
558 | 580 |
|
559 | 581 | return Stream.of(geometry).map(geom -> new GeometryResult(toNeo4jGeometry(null, geom)));
|
560 | 582 | }
|
561 | 583 |
|
| 584 | + @Deprecated |
562 | 585 | @Procedure(value = "spatial.asExternalGeometry", deprecatedBy = "spatial.asGeometry")
|
563 | 586 | @Description("Returns a geometry object as an external geometry type to be returned to a client")
|
564 | 587 | // This only existed temporarily because the other method, asGeometry, returned the wrong type due to a bug in Neo4j 3.0
|
@@ -647,23 +670,23 @@ public Neo4jPoint(double x, double y, CRS crs) {
|
647 | 670 | }
|
648 | 671 | }
|
649 | 672 |
|
650 |
| - private org.neo4j.graphdb.spatial.Coordinate toNeo4jCoordinate(Coordinate coordinate) { |
| 673 | + private static org.neo4j.graphdb.spatial.Coordinate toNeo4jCoordinate(Coordinate coordinate) { |
651 | 674 | if (coordinate.z == Coordinate.NULL_ORDINATE) {
|
652 | 675 | return new org.neo4j.graphdb.spatial.Coordinate(coordinate.x, coordinate.y);
|
653 | 676 | } else {
|
654 | 677 | return new org.neo4j.graphdb.spatial.Coordinate(coordinate.x, coordinate.y, coordinate.z);
|
655 | 678 | }
|
656 | 679 | }
|
657 | 680 |
|
658 |
| - private List<org.neo4j.graphdb.spatial.Coordinate> toNeo4jCoordinates(Coordinate[] coordinates) { |
| 681 | + private static List<org.neo4j.graphdb.spatial.Coordinate> toNeo4jCoordinates(Coordinate[] coordinates) { |
659 | 682 | ArrayList<org.neo4j.graphdb.spatial.Coordinate> converted = new ArrayList<>();
|
660 | 683 | for (Coordinate coordinate : coordinates) {
|
661 | 684 | converted.add(toNeo4jCoordinate(coordinate));
|
662 | 685 | }
|
663 | 686 | return converted;
|
664 | 687 | }
|
665 | 688 |
|
666 |
| - private org.neo4j.graphdb.spatial.Geometry toNeo4jGeometry(Layer layer, Object value) { |
| 689 | + private static org.neo4j.graphdb.spatial.Geometry toNeo4jGeometry(Layer layer, Object value) { |
667 | 690 | if (value instanceof org.neo4j.graphdb.spatial.Geometry) {
|
668 | 691 | return (org.neo4j.graphdb.spatial.Geometry) value;
|
669 | 692 | }
|
@@ -703,9 +726,71 @@ private org.neo4j.graphdb.spatial.Geometry toNeo4jGeometry(Layer layer, Object v
|
703 | 726 | throw new RuntimeException("Can't convert " + value + " to a geometry");
|
704 | 727 | }
|
705 | 728 |
|
| 729 | + private static Object toPublic(Object obj) { |
| 730 | + if (obj instanceof Map) { |
| 731 | + return toPublic((Map) obj); |
| 732 | + } else if (obj instanceof PropertyContainer) { |
| 733 | + return toPublic(((PropertyContainer) obj).getProperties()); |
| 734 | + } else if (obj instanceof Geometry) { |
| 735 | + return toMap((Geometry) obj); |
| 736 | + } else { |
| 737 | + return obj; |
| 738 | + } |
| 739 | + } |
| 740 | + |
| 741 | + private static Map<String, Object> toGeometryMap(Object geometry) { |
| 742 | + return toMap(toNeo4jGeometry(null, geometry)); |
| 743 | + } |
| 744 | + |
| 745 | + private static Map<String, Object> toMap(Geometry geometry) { |
| 746 | + return toMap(toNeo4jGeometry(null, geometry)); |
| 747 | + } |
| 748 | + |
| 749 | + private static double[] toCoordinateArrayFromDoubles(List<Double> coords) { |
| 750 | + double[] coordinates = new double[coords.size()]; |
| 751 | + for (int i = 0; i < coordinates.length; i++) { |
| 752 | + coordinates[i] = coords.get(i); |
| 753 | + } |
| 754 | + return coordinates; |
| 755 | + } |
| 756 | + |
| 757 | + private static double[][] toCoordinateArrayFromCoordinates(List<org.neo4j.graphdb.spatial.Coordinate> coords) { |
| 758 | + List<double[]> coordinates = new ArrayList<>(coords.size()); |
| 759 | + for (org.neo4j.graphdb.spatial.Coordinate coord : coords) { |
| 760 | + coordinates.add(toCoordinateArrayFromDoubles(coord.getCoordinate())); |
| 761 | + } |
| 762 | + return toCoordinateArray(coordinates); |
| 763 | + } |
| 764 | + |
| 765 | + private static double[][] toCoordinateArray(List<double[]> coords) { |
| 766 | + double[][] coordinates = new double[coords.size()][]; |
| 767 | + for (int i = 0; i < coordinates.length; i++) { |
| 768 | + coordinates[i] = coords.get(i); |
| 769 | + } |
| 770 | + return coordinates; |
| 771 | + } |
| 772 | + |
| 773 | + private static Map<String, Object> toMap(org.neo4j.graphdb.spatial.Geometry geometry) { |
| 774 | + if (geometry instanceof org.neo4j.graphdb.spatial.Point) { |
| 775 | + org.neo4j.graphdb.spatial.Point point = (org.neo4j.graphdb.spatial.Point) geometry; |
| 776 | + return map("type", geometry.getGeometryType(), "coordinate", toCoordinateArrayFromDoubles(point.getCoordinate().getCoordinate())); |
| 777 | + } else { |
| 778 | + return map("type", geometry.getGeometryType(), "coordinates", toCoordinateArrayFromCoordinates(geometry.getCoordinates())); |
| 779 | + } |
| 780 | + } |
| 781 | + |
| 782 | + private static Map<String, Object> toPublic(Map incoming) { |
| 783 | + Map<String, Object> map = new HashMap<>(incoming.size()); |
| 784 | + for (Object key : incoming.keySet()) { |
| 785 | + map.put(key.toString(), toPublic(incoming.get(key))); |
| 786 | + } |
| 787 | + return map; |
| 788 | + } |
| 789 | + |
706 | 790 | private static CRS findCRS(String crs) {
|
707 | 791 | switch (crs) {
|
708 |
| - case "WGS-84": |
| 792 | + case "WGS-84": // name in Neo4j CRS table |
| 793 | + case "WGS84(DD)": // name in geotools crs library |
709 | 794 | return makeCRS(4326, "WGS-84", "http://spatialreference.org/ref/epsg/4326/");
|
710 | 795 | case "Cartesian":
|
711 | 796 | return makeCRS(7203, "cartesian", "http://spatialreference.org/ref/sr-org/7203/");
|
@@ -749,19 +834,19 @@ private Coordinate toCoordinate(Object value) {
|
749 | 834 | throw new RuntimeException("Can't convert " + value + " to a coordinate");
|
750 | 835 | }
|
751 | 836 |
|
752 |
| - private Coordinate toCoordinate(org.neo4j.graphdb.spatial.Coordinate point) { |
| 837 | + private static Coordinate toCoordinate(org.neo4j.graphdb.spatial.Coordinate point) { |
753 | 838 | List<Double> coordinate = point.getCoordinate();
|
754 | 839 | return new Coordinate(coordinate.get(0), coordinate.get(1));
|
755 | 840 | }
|
756 | 841 |
|
757 |
| - private Coordinate toCoordinate(Map map) { |
| 842 | + private static Coordinate toCoordinate(Map map) { |
758 | 843 | if (map == null) return null;
|
759 | 844 | Coordinate coord = toCoordinate(map, "longitude", "latitude");
|
760 | 845 | if (coord == null) return toCoordinate(map, "lon", "lat");
|
761 | 846 | return coord;
|
762 | 847 | }
|
763 | 848 |
|
764 |
| - private Coordinate toCoordinate(Map map, String xName, String yName) { |
| 849 | + private static Coordinate toCoordinate(Map map, String xName, String yName) { |
765 | 850 | if (map.containsKey(xName) && map.containsKey(yName))
|
766 | 851 | return new Coordinate(((Number) map.get(xName)).doubleValue(), ((Number) map.get(yName)).doubleValue());
|
767 | 852 | return null;
|
|
0 commit comments