building spatial search algorithms for neo4j
play

Building Spatial Search Algorithms for Neo4j Craig Taverner Neo4j - PowerPoint PPT Presentation

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


  1. Building Spatial Search Algorithms for Neo4j Craig Taverner Neo4j Cypher and Spatial @craigtaverner

  2. 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

  3. 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

  4. 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

  5. OpenStreeMap 5

  6. 2011 - GeoProcessing 6

  7. 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

  8. 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

  9. 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

  10. 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)

  11. 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/

  12. 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/

  13. 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

  14. Internship on Spatial Algorithms 14

  15. Complex Polygons 15

  16. Cartesian versus Geographic 16

  17. 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

  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; https://neo4j.com/docs/developer-manual/3.4/cypher/execution-plans/shortestpath-planning/ 18

  19. 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

  20. Weighted Paths Total weight: 1258.48m 20

  21. Shortest Weighted Path Algorithms Dijkstra A* 21

  22. GraphConnect 2018 NY 22

  23. 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. 24

  25. 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

  26. 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

  27. Point in Polygon X X X X X 27

  28. Live Demo 28

  29. 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

  30. Shoelace Formula 30

  31. Girard’s Theorem https://vanderbei.princeton.edu/WebGL/GirardThmProof.html 31

  32. 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

  33. Algorithms UDF Code 33

  34. 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

  35. Intersection: monotone chain sweep line 35

  36. Intersection in Geographic Coordinates 36

  37. Intersection in Geographic Coordinates 37

  38. 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

  39. Polygon Distance 39

  40. 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

  41. 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

  42. Convex Hull, Oh My! 42

  43. Convex Hull: Graham Scan 43

  44. Convex Hull in Geographic Coordinates 44

  45. 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

  46. The end... 46

  47. 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

  48. 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

  49. Reference Slides from Graph Connect Importing OSM & Building a Route Graph 49

Recommend


More recommend