Slide 1

Slide 1 text

RethinkDB+ArcGIS Creating amazing geospatial visualizations GeoDev Meetup Sacramento, California June 1, 2015

Slide 2

Slide 2 text

Jorge Silva @thejsj Developer Evangelist @ RethinkDB

Slide 3

Slide 3 text

Introduction What is RethinkDB?

Slide 4

Slide 4 text

What is RethinkDB? • Open source database for building realtime web applications • NoSQL database that stores schemaless JSON documents • Distributed database that is easy to scale

Slide 5

Slide 5 text

Built for Realtime Apps • Subscribe to change notifications from database queries • No more polling — the database pushes changes to your app • Reduce the amount of plumbing needed to stream live updates

Slide 6

Slide 6 text

RethinkDB Structure Database → Table → Document MySQL: Database → Table → Row MongoDB: Database → Collection → Document

Slide 7

Slide 7 text

Sample Document { "name": "Will Riker", "position": "Commander", "height": 193, "birthdate": Mon Aug 19 2335, "ships": [ { "name": "USS Pegasus" }, { "name": "USS Potemkin" }, { "name": "USS Enterprise" }, ], ... }

Slide 8

Slide 8 text

Introduction to ReQL RethinkDB Query Language

Slide 9

Slide 9 text

Introduction to ReQL • ReQL embeds natively into your programming language • Compose ReQL queries by chaining commands

Slide 10

Slide 10 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Number of unique last names

Slide 11

Slide 11 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Access a database table

Slide 12

Slide 12 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Isolate a document property

Slide 13

Slide 13 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Consolidate duplicate values

Slide 14

Slide 14 text

Anatomy of a ReQL Query r.table("users") .pluck("last_name") .distinct().count() Display the number of items

Slide 15

Slide 15 text

Sample ReQL Queries r.table("user") .filter(r.row(“age").gt(30)) r.table(“post") .eqJoin(“uId”, r.table(“user”)) .zip() r.table("fellowship") .filter({ species: "hobbit" }) .update({ species: "halfling" })

Slide 16

Slide 16 text

Running Queries https://github.com/thejsj/arcgis-rethinkdb

Slide 17

Slide 17 text

Additional ReQL Features • Date and time functions for time data • Support for storing binary objects • r.http allows you to execute http requests from the database

Slide 18

Slide 18 text

Realtime Updates Working with Changefeeds

Slide 19

Slide 19 text

Subscribe to change notifications on database queries Changefeeds

Slide 20

Slide 20 text

r.table("users").changes() Track changes on the users table Changefeeds

Slide 21

Slide 21 text

Changefeeds r.table("users").changes() r.table("users") .insert({name: "Bob"}) Changefeed output: { new_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob', ... }, old_val: null }

Slide 22

Slide 22 text

Changefeeds r.table("users").changes() r.table("users") .filter({name: "Bob"}).delete() Changefeed output: { new_val: null, old_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob', ... } }

Slide 23

Slide 23 text

Changefeeds r.table("users").changes() r.table("users") .get("362ae837-2e29-4695-adef-4fa415138f90") .update({name: "Bobbby"}) Changefeed output: { new_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bobby' }, old_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob' } }

Slide 24

Slide 24 text

Changefeeds r.table("players") .orderBy({index: r.desc("score")}) .limit(3).changes() Track top three players by score Chain the changes command to an actual ReQL query:

Slide 25

Slide 25 text

Using Changefeeds

Slide 26

Slide 26 text

Advanced ReQL Geospatial Features

Slide 27

Slide 27 text

Introduction to Geospatial • Geospatial queries are integrated into ReQL • With changefeeds, you can listen to changes in your geospatial data

Slide 28

Slide 28 text

Geospatial capabilities • Geometry types: point, line, polygon • Geometry Manipulation: fill, polygonSub • Non-Indexed Operations: distance, intersects, includes • Indexed operations: getNearest, getIntersecting

Slide 29

Slide 29 text

Geospatial ReQL Query r.table("starbucks_locations") .getIntersecting(cityOfSacramento, { index: 'location'}) .orderBy(function (row) { return r.point(-122.482, 38.578) .distance(row('location')); }) Closest Starbucks to Hacker Lab in SAC

Slide 30

Slide 30 text

Geospatial ReQL Query r.table("starbucks_locations") .getIntersecting(cityOfSacramento, { index: 'location'}) .orderBy(function (row) { return r.point(-122.482, 38.578) .distance(row('location')); }) Access the database table

Slide 31

Slide 31 text

Geospatial ReQL Query r.table("starbucks_locations") .getIntersecting(cityOfSacramento, { index: 'location'}) .orderBy(function (row) { return r.point(-122.482, 38.578) .distance(row('location')); }) Get all points within polygon

Slide 32

Slide 32 text

Geospatial ReQL Query r.table("starbucks_locations") .getIntersecting(cityOfSacramento, { index: 'location'}) .orderBy(function (row) { return r.point(-122.482, 38.578) .distance(row('location')); }) Order by distance to point

Slide 33

Slide 33 text

Geospatial ReQL Query r.table("starbucks_locations") .getIntersecting(cityOfSacramento, { index: 'location'}) .orderBy(function (row) { return r.point(-122.482, 38.578) .distance(row('location')); }) Return the distance to a point

Slide 34

Slide 34 text

Running Geospatial Queries https://github.com/thejsj/arcgis-rethinkdb

Slide 35

Slide 35 text

Demo Application Using RethinkDB with ArcGIS

Slide 36

Slide 36 text

Question Which counties in the USA have the highest density of restaurants?

Slide 37

Slide 37 text

Demo

Slide 38

Slide 38 text

County + Restaurant data

Slide 39

Slide 39 text

Restaurants in county query return r.table("counties") // 1. Get number of restaurants .merge(...) // 2. Calculate restaurant density .merge(...) Get restaurant density

Slide 40

Slide 40 text

Restaurants in county query Get all counties return r.table("counties") // 1. Get number of restaurants .merge(...) // 2. Calculate restaurant density .merge(...)

Slide 41

Slide 41 text

Restaurants in county query return r.table("counties") // 1. Get number of restaurants .merge({ restaurants: // .. }) // 2. Calculate restaurant density .merge(...) Merge a property into county

Slide 42

Slide 42 text

Restaurants in county query return r.table("counties") // 1. Get number of restaurants .merge({ restaurants: r.table('restaurants') .getIntersecting(row('geometry'), { index: 'geometry' }) .count() }) // 2. Calculate restaurant density .merge(...) Get number of restaurants in county

Slide 43

Slide 43 text

Restaurants in county query Get restaurants table return r.table("counties") // 1. Get number of restaurants .merge({ restaurants: r.table('restaurants') .getIntersecting(row('geometry'), { index: 'geometry' }) .count() }) // 2. Calculate restaurant density .merge(...)

Slide 44

Slide 44 text

Restaurants in county query Get intersecting geometries return r.table("counties") // 1. Get number of restaurants .merge({ restaurants: r.table('restaurants') .getIntersecting(row('geometry'), { index: 'geometry' }) .count() }) // 2. Calculate restaurant density .merge(...)

Slide 45

Slide 45 text

Restaurants in county query Count intersecting geometries return r.table("counties") // 1. Get number of restaurants .merge({ restaurants: r.table('restaurants') .getIntersecting(row('geometry'), { index: 'geometry' }) .count() }) // 2. Calculate restaurant density .merge(...)

Slide 46

Slide 46 text

Restaurants in county query return r.table("counties") // 1. Get number of restaurants .merge(...) // 2. Calculate restaurant density .merge({ stat: row('restaurants').div(row('area')) }) Add new `stat` property

Slide 47

Slide 47 text

Restaurants in county query return r.table("counties") // 1. Get number of restaurants .merge(...) // 2. Calculate restaurant density .merge({ stat: row('restaurants').div(row('area')) }) Divide `restaurants` by `area`

Slide 48

Slide 48 text

Processed data

Slide 49

Slide 49 text

Client/ArcGIS • Data is queried and sent through a WebSocket connection • Polygon geometry is converted into ArcGIS Graphic and added to map • Color (Symbol) is applied dynamically based on max

Slide 50

Slide 50 text

Summary • ReQL allows for complex geospatial queries • RethinkDB makes building realtime applications a lot easier

Slide 51

Slide 51 text

Additional Resources • RethinkDB website:
 http://rethinkdb.com • RethinkDB cookbook:
 http://rethinkdb.com/docs/cookbook • RethinkDB installation:
 http://rethinkdb.com/docs/install/

Slide 52

Slide 52 text

Questions?