Building Spatial Search Algorithms for Neo4j Craig Taverner Neo4j Cypher and Spatial @craigtaverner
Agenda Two Minute History • Neo4j 3.4 WITH point({latitude: 55.612149, longitude: 12.995090}) AS poi • MATCH (l:Location)<-[:AT]-(b:Business)-[:OF]->(c:Category) WHERE c.name = "coffee" AND distance(l.location, poi) < 10000 Which Spatial Algorithms? RETURN distance(l.location, poi) as distance, • b.name as coffee_shop ORDER BY distance DESC Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 2
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 3
y a r r b i l g n i l l e d o m a t a d a e ! s a b a t 2010 - “Neo4j Spatial” a d l a t i a p s a t o n Core Libraries • • GeometryEncoder JTS • • SimplePointEncoder GeoTools • • WKTGeometryEncoder CoordinateReferenceSystems • • RTree thousands (geotools-crs) • • addNodes bulk inserver Dimensions • • OpenStreetMap 2D only! • • OSMImporter Integrations • • OSMGeometryEncoder uDIG • • OSMLayer GeoServer • • 4
OpenStreeMap 5
2011 - GeoProcessing 6
2016 - Procedures addLayer (OSM, Point, WKT) • Import (ESRI Shapefile, OSM) • addNode, addGeometry • addNodes (bulk import to RTree) • Find within bounding box • Find within polygon • etc. • 7
Agenda Two Minute History • Neo4j 3.4 • WITH point({latitude: 55.612149, longitude: 12.995090}) AS poi MATCH (l:Location)<-[:AT]-(b:Business)-[:OF]->(c:Category) WHERE c.name = "coffee" What Spatial Algorithms? • AND distance(l.location, poi) < 10000 RETURN distance(l.location, poi) as distance, b.name as coffee_shop Route Finding • ORDER BY distance DESC Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 8
Neo4j 3.4 Space Time Point Type Types (Date, DateTime, ● ● Distance LocalDateTime) ● Spatial index Duration ● ● 2D and 3D Temporal index ● ● Geographic and Cartesian Cypher functions ● ● 9
Spatial: Location Based Search Neo4j 3.4 focuses on very specific use case: • Find points within region • Special case of region => circle (distance from point)
Spatial: Location Based Search WITH point({latitude: 55.612149, longitude: 12.995090}) AS poi MATCH (l:Location)<-[:AT]-(b:Business)-[:OF]->(c:Category) WHERE c.name = "coffee" AND distance(l.location, poi) < 10000 RETURN distance(l.location, poi) as distance, b.name as coffee_shop ORDER BY distance DESC 9 index 5 hits https://neo4j.com/docs/cypher-manual/current/functions/spatial/
Coordinate Reference Systems • Geographic (2D and 3D) • Points on an ellipsoid model of the earth • Units in decimal degrees • Latitude, longitude (y, x) • Height above ellipse (z) - not altitude • Distance function returns value in meters • Cartesian (2D and 3D) • Points on cartesian axes in euclidean space • Units undefined (whatever the user means them to be) • Distance function returns same units using Pythagoras https://neo4j.com/docs/cypher-manual/current/syntax/spatial/
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 13
Internship on Spatial Algorithms 14
Complex Polygons 15
Cartesian versus Geographic 16
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 17
Routing With Cypher’s shortestPath Function MATCH (a:PointOfInterest) WHERE a.name = 'The View Restaurant & Lounge' MATCH (b:PointOfInterest) WHERE b.name = 'Gregory Coffee' MATCH p= shortestPath ((a)-[:ROUTE*..100]-(b)) RETURN p; https://neo4j.com/docs/developer-manual/3.4/cypher/execution-plans/shortestpath-planning/ 18
Routing With Cypher’s shortestPath Function MATCH (a:PointOfInterest) WHERE a.name = 'The View Restaurant & Lounge' MATCH (b:PointOfInterest) WHERE b.name = 'Gregory Coffee' MATCH p= shortestPath ((a)-[:ROUTE*..100]-(b)) RETURN p; 19
Weighted Paths Total weight: 1258.48m 20
Shortest Weighted Path Algorithms Dijkstra A* 21
GraphConnect 2018 NY 22
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 23
24
Find Points of Interest in Manhattan MATCH (m:OSMRelation) WHERE m.name = 'Manhattan' MATCH (p:PointOfInterest) WHERE distance(p.location, $mapCenter ) < $circleRadius AND amanzi.withinPolygon(m.polygon,p.location) RETURN p 25
Find Points of Interest in Manhattan MATCH (m:OSMRelation) WHERE m.name = 'Manhattan' WITH m.polygon as manhattan, amanzi.boundingBoxFor(m.polygon) as bbox MATCH (p:PointOfInterest) WHERE bbox.min < p.location < bbox.max AND amanzi.withinPolygon(manhattan,p.location) RETURN p 26
Point in Polygon X X X X X 27
Live Demo 28
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 29
Shoelace Formula 30
Girard’s Theorem https://vanderbei.princeton.edu/WebGL/GirardThmProof.html 31
Querying for Polygon Area MATCH (r:OSMRelation)-[:POLYGON_STRUCTURE*]->(p) USING INDEX r:OSMRelation(relation_osm_id) WHERE r.relation_osm_id IN $regionIds AND exists (p.polygon) RETURN r.relation_osm_id AS region_id, spatial.algo.area(p.polygon) AS area ; 32
Algorithms UDF Code 33
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 34
Intersection: monotone chain sweep line 35
Intersection in Geographic Coordinates 36
Intersection in Geographic Coordinates 37
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, oh my! • 38
Polygon Distance 39
Querying for Polygon Distances UNWIND $pairs AS pair MATCH (r1:OSMRelation)-[:POLYGON_STRUCTURE*]->(p1) USING INDEX r1:OSMRelation(relation_osm_id) WHERE r1.relation_osm_id=pair[0] AND exists (p1.polygon) OPTIONAL MATCH (r2:OSMRelation)-[:POLYGON_STRUCTURE*]->(p2) USING INDEX r2:OSMRelation(relation_osm_id) WHERE r2.relation_osm_id=pair[1] AND exists (p2.polygon) RETURN pair, spatial.algo.distance. ends (p1.polygon, p2.polygon) AS distance; 40
Agenda Two Minute History • Neo4j 3.4 • Which Spatial Algorithms? • Route Finding • Point in Polygon • Polygon Area • Polygon Intersection • Polygon Distance • Convex Hull, Oh My! • 41
Convex Hull, Oh My! 42
Convex Hull: Graham Scan 43
Convex Hull in Geographic Coordinates 44
Querying for Polygons and Convex Hull MATCH (r:OSMRelation)-[:POLYGON_STRUCTURE*]->(p) USING INDEX r:OSMRelation(relation_osm_id) WHERE r.relation_osm_id IN $regionIds AND exists (p.polygon) RETURN p.polygon as region; MATCH (r:OSMRelation)-[:POLYGON_STRUCTURE*]->(p) USING INDEX r:OSMRelation(relation_osm_id) WHERE r.relation_osm_id IN $regionIds AND exists (p.polygon) RETURN spatial.algo.convexHull( p.polygon ) as region; 45
The end... 46
Hunger Games Questions 1. Easy : What is the difference between Cartesian and Geographic Coords? a. Cartesian uses latitude and longitude while geographic uses x and y b. Cartesian is euclidean while geographic uses polar coordinates c. They are actually the same 2. Medium : Which library has the most spatial algorithms? a. Old “Neo4j Spatial” from 2010 b. OpenStreetMap importing library c. New “Spatial Algorithms” library from 2019 3. Hard : What is the name of the function to calculate the distance between polygons and return the result together with the two closest points? Answer here: r.neo4j.com/hunger-games 47
References Links to the demo application as well as library used in the data modeling and building of the app: • Demo “OSM Routing App”: • https://github.com/johnymontana/osm-routing-app • OSM importer and route graph generation procedures: • https://github.com/neo4j-contrib/osm • The 'Spatial Algorithms' library and functions: • https://github.com/neo4j-contrib/spatial-algorithms • Dijkstra, A-star and batching large data imports: • https://github.com/neo4j-contrib/neo4j-apoc-procedures 48
Reference Slides from Graph Connect Importing OSM & Building a Route Graph 49
Recommend
More recommend