geospatial graphs made easy with orientdb - codemotion warsaw 2016
TRANSCRIPT
![Page 1: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/1.jpg)
Geospatial Graphs made easy with
![Page 2: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/2.jpg)
#OrientDB - @ldellaquila
Luigi Dell’AquilaCore Developer and Director of ConsultingOrientDB LTDTwitter: @ldellaquilahttp://www.orientdb.com
![Page 3: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/3.jpg)
#OrientDB - @ldellaquila
Summary• Getting started with OrientDB
• OrientDB GeoSpatial API
• Importing Geo data (Node.js)
• Querying Geo data (OrientDB Studio)
• Displaying Geo data (Angular2, Google Maps)
• Adding Relationships - graph data
• Graph + Spatial queries
![Page 4: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/4.jpg)
#OrientDB - @ldellaquila
What is OrientDB• Multi-Model Database (Document, Graph and more)
• Tables Classes
• Extended SQL
• JOIN Physical Pointers
• Schema, No-Schema, Hybrid
• HTTP + Binary protocols
• Stand-alone or Embedded
• Distributed Multi-Master
• Apache 2 license
![Page 5: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/5.jpg)
#OrientDB - @ldellaquila
Install• http://www.orientdb.com/download/
• http://central.maven.org/maven2/com/orientechnologies/orientdb-spatial/VERSION/orientdb-spatial-VERSION-dist.jar
> cd orientdb-community/bin/ > ./server.sh
![Page 6: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/6.jpg)
#OrientDB - @ldellaquila
OrientDB GeoSpatial Classes• OPoint
• OLine
• OPolygon
• OMultiPoint
• OMultiline
• OMultiPlygon
![Page 7: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/7.jpg)
#OrientDB - @ldellaquila
OrientDB GeoSpatial Functions• ST_GeomFromText(text)
• ST_Equals(geom, geom)
• ST_Contains(geom, geom)
• ST_Disjoint(geom, geom)
• ST_Intersects(geom, geom)
• ST_Distance_Sphere(geom, geom)
• and more…
![Page 8: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/8.jpg)
#OrientDB - @ldellaquila
Our goal
![Page 9: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/9.jpg)
#OrientDB - @ldellaquila
Our Data Model CREATE CLASS POI EXTENDS V CREATE PROPERTY POI.location EMBEDDED OPoint CREATE INDEX POI.location on POI(location) SPATIAL ENGINE LUCENE CREATE CLASS Natural EXTENDS V CREATE PROPERTY Natural.location EMBEDDED OPolygon CREATE INDEX Natural.location on Natural(location) SPATIAL ENGINE LUCENE
CREATE CLASS Person EXTENDS V CREATE PROPERTY Person.location EMBEDDED OPoint CREATE CLASS FriendOf EXTENDS E
![Page 10: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/10.jpg)
#OrientDB - @ldellaquila
Now let’s import data!
> npm init > npm install orientjs > npm install fast-csv > touch index.js
import files http://www.mapcruzin.com/free-poland-maps.htm (convert to WKT using QGis)
![Page 11: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/11.jpg)
#OrientDB - @ldellaquila
Dependencies
var fs = require("fs") var ODatabase = require("orientjs").ODatabasevar csv = require("fast-csv")
![Page 12: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/12.jpg)
#OrientDB - @ldellaquila
Read from CSVvar stream = fs.createReadStream("data/poland-poi.csv");var csvStream = csv.parse({headers: true}) .on("data", function(data){ console.log(data) }) .on("end", function(){ console.log("Done!") });stream.pipe(csvStream);
![Page 13: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/13.jpg)
#OrientDB - @ldellaquila
DB connectionvar db = new ODatabase({ host: "localhost", port: 2424, username: "admin", password: "admin", name: "geo"})
db.open().then(function(){ }
![Page 14: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/14.jpg)
#OrientDB - @ldellaquila
Insert
var insertElement = function(data){ db.insert().into(className).set( { name: data.name, type: data.type, location: db.rawExpression("ST_GeomFromText('"+data.WKT+"')") } ).one().then(function(){})}
![Page 15: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/15.jpg)
#OrientDB - @ldellaquila
Is it done?Not yet :-(
Node.js… asynchronous… promises…
Final version:https://github.com/luigidellaquila/wkt-to-orient
![Page 16: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/16.jpg)
#OrientDB - @ldellaquila
Querying geo data
We are here:
(52.231807953759706, 21.013154983520508)
(lat, lon)
![Page 17: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/17.jpg)
#OrientDB - @ldellaquila
Front-End!
![Page 18: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/18.jpg)
#OrientDB - @ldellaquila
Clone the scaffolding> git clone https://github.com/luigidellaquila/geospatial-demo > git checkout warsaw_demo_step0 > cd geospatial-demo > npm install
(it’s a clone of https://github.com/angular/quickstart)
> npm start
![Page 19: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/19.jpg)
#OrientDB - @ldellaquila
Clone the scaffolding> git clone https://github.com/luigidellaquila/geospatial-demo > git checkout warsaw_demo_step0 > cd geospatial-demo > npm install
(it’s a clone of https://github.com/angular/quickstart)
> npm start
> cd <orientdb-home>/www > ln -s <quickstart-path> > tsc -w
![Page 20: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/20.jpg)
#OrientDB - @ldellaquila
We need Google Maps
<script src=“https://maps.googleapis.com/maps/api/js?key=API_KEY" async defer></script>
![Page 21: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/21.jpg)
#OrientDB - @ldellaquila
Let’s display a map (app.html)<div class=“container"> <div class="row"> <div class="col-md-12" id="map" style=“height:600px"></div> </div> </div>
![Page 22: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/22.jpg)
#OrientDB - @ldellaquila
Draw the map
drawMap(){ var controller = this; let mapProp = { center: new google.maps.LatLng(52.231807953759706, 21.013154983520508), zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP }; controller.map = new google.maps.Map(document.getElementById("map"), mapProp); controller.map.addListener("click", function(point: any){ controller.zone.run(()=> { controller.lat = point.latLng.lat(); controller.lon = point.latLng.lng(); }); }); }
![Page 23: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/23.jpg)
#OrientDB - @ldellaquila
Create a Person
createPerson(): void{ var location = { // the location object } var queryString = ””; // OrientDB statement this.orient.command( queryString, (result) => { /* Success callback */ }, (error) => { /* Error callback */ } );}
![Page 24: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/24.jpg)
#OrientDB - @ldellaquila
Create a Person
createPerson(): void{ var location = { "@class": "OPoint", coordinates: [this.lon, this.lat] } var queryString = ””; // OrientDB statement this.orient.command( queryString, (result) => { /* Success callback */ }, (error) => { /* Error callback */ } );}
![Page 25: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/25.jpg)
#OrientDB - @ldellaquila
Create a Person
createPerson(): void{ var location = { "@class": "OPoint", coordinates: [this.lon, this.lat] } var queryString = `insert into Person set name = '${this.personName}', location = ${JSON.stringify(location)}`; this.orient.command( queryString, (result) => { /* Success callback */ }, (error) => { /* Error callback */ } );}
![Page 26: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/26.jpg)
#OrientDB - @ldellaquila
Create a Person
createPerson(): void{ var location = { "@class": "OPoint", coordinates: [this.lon, this.lat] } var queryString = `insert into Person set name = '${this.personName}', location = ${JSON.stringify(location)}`; this.orient.command( queryString, (res) => { let body = res.json(); let person = body.result[0]; this.addPersonToMap(person) }, (e) => { console.log(e) });}
![Page 27: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/27.jpg)
#OrientDB - @ldellaquila
Add Person Vertex to Orient via REST API
command(statement: string, success: (data: any) => void, error: (err: any) => void): void{ var url = this.url + "sql/-/-1" var headers = new Headers(); headers.append("Authorization", "Basic " + btoa(this.username+":"+this.password)); this.http.post( // HTTP POST url, // the URL JSON.stringify({ "command": statement // the SQL command }), {headers: headers} // the authentication data ).toPromise() .then(success) .catch(error);}
![Page 28: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/28.jpg)
#OrientDB - @ldellaquila
Add Person to the Map
addPersonToMap(personData:any){ let location = personData.location; let coordinates = location.coordinates; let controller = this; let marker = new google.maps.Marker({ position: {lat:coordinates[1], lng:coordinates[0]}, map: this.map, title: personData.name, rid: personData["@rid"] }); google.maps.event.addListener(marker, 'click', function() { controller.onMarkerClick(marker); });}
![Page 29: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/29.jpg)
#OrientDB - @ldellaquila
Add an edge between people (FriendOf)
createEdge(from:any, to:any): void{ this.orient.command( `create edge FriendOf from ${from.rid} to ${to.rid}`, (x)=>{console.log(x)}, (x)=>{console.log(x)} ) this.addEdgeBetweenMarkersToMap(from, to);}
![Page 30: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/30.jpg)
#OrientDB - @ldellaquila
Query the Geospatial Graph
(DEMO)
![Page 31: Geospatial Graphs made easy with OrientDB - Codemotion Warsaw 2016](https://reader031.vdocuments.us/reader031/viewer/2022022413/58ef678a1a28ab6a518b46c1/html5/thumbnails/31.jpg)
#OrientDB - @ldellaquila
Thank you!