Slide 1

Slide 1 text

Getting Down to Earth: Daniel Azuma RailsConf 2012 Geospatial Analysis with Rails

Slide 2

Slide 2 text

Getting Down to Earth: Daniel Azuma RailsConf 2012 Geospatial Analysis with Rails

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Daniel Azuma Rails Developer since 2006 Author of RGeo and related gems Chief Architect at Pirq

Slide 5

Slide 5 text

We’re hiring!

Slide 6

Slide 6 text

Beyond placing pushpins on a Google Map

Slide 7

Slide 7 text

Let’s dive directly into code We can worry about the concepts another time

Slide 8

Slide 8 text

Our agenda Setting up a geospatial Rails app Project 1: Visualizing geo-activity Project 2: Timezone lookup for a location Further material available online

Slide 9

Slide 9 text

Setting up Rails

Slide 10

Slide 10 text

Geospatial software stack libgeos — geometric algorithms libproj — coordinate projections PostGIS — spatial plugin for PostgreSQL RGeo — Ruby libraries

Slide 11

Slide 11 text

…use Postgres — it’s awesome…

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

…check out the README for more info…

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

We installed some software We created a Rails app as usual But there were a few more configs …and that’s it!

Slide 16

Slide 16 text

Project 1: Visualizing location activity

Slide 17

Slide 17 text

…use the “point” data type…

Slide 18

Slide 18 text

…configuring the migration…

Slide 19

Slide 19 text

…SRID 3785 is the Google Maps Mercator projection…

Slide 20

Slide 20 text

…make sure the index is marked “spatial”…

Slide 21

Slide 21 text

…configuring the model…

Slide 22

Slide 22 text

…assign an RGeo Factory to the model…

Slide 23

Slide 23 text

Need Input!

Slide 24

Slide 24 text

…some data to load…

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

…remember to transform coordinate systems…

Slide 27

Slide 27 text

Goal: visualize data on a map

Slide 28

Slide 28 text

…query for points in a rectangle…

Slide 29

Slide 29 text

…queries need to use the data’s coordinate system…

Slide 30

Slide 30 text

…search for overlap with the box…

Slide 31

Slide 31 text

Rectangles are not rectangular in geographic coordinates

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

…use Squeel — it’s awesome…

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

OUCH!

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

…create a heatmap in javascript…

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Project 1 summary We created a model to hold location data We loaded a bunch of data into it We queried data within a map rectangle We visualized the data using a heatmap

Slide 40

Slide 40 text

Project 2: Location time zone lookup

Slide 41

Slide 41 text

World time zone regions

Slide 42

Slide 42 text

…a timezone has_many polygon regions…

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

…configure the migration…

Slide 45

Slide 45 text

…configure the model with an RGeo Factory…

Slide 46

Slide 46 text

http:/ /efele.net/maps/tz/world/

Slide 47

Slide 47 text

“I don’t even see the code anymore. All I see is point, polygon, circle…”

Slide 48

Slide 48 text

…load a shapefile as a stream of records…

Slide 49

Slide 49 text

…obtain the timezone object…

Slide 50

Slide 50 text

…populate the polygon table…

Slide 51

Slide 51 text

…query using the st_contains() SQL function…

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

…our original query…

Slide 56

Slide 56 text

…also query for “nearby” polygons…

Slide 57

Slide 57 text

…check the distance…

Slide 58

Slide 58 text

…distances also have a coordinate system…

Slide 59

Slide 59 text

…take the closest polygon…

Slide 60

Slide 60 text

Ain’t my fault you asked for st_distance…

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

…PostGIS can optimize intersections…

Slide 64

Slide 64 text

A “tall” table with many rows

Slide 65

Slide 65 text

A database index eliminates lots of rows quickly

Slide 66

Slide 66 text

Spatial databases are sometimes “fat”.

Slide 67

Slide 67 text

Reduce width by increasing the row count

Slide 68

Slide 68 text

Reduce width by increasing the row count

Slide 69

Slide 69 text

4-to-1 subdivision

Slide 70

Slide 70 text

Looking for the sweet spot

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

…let’s add polygon subdivision…

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

…the core of the algorithm…

Slide 76

Slide 76 text

…check the number of sides…

Slide 77

Slide 77 text

…create a bounding box and subdivide…

Slide 78

Slide 78 text

…use RGeo — it’s awesome…

Slide 79

Slide 79 text

Whew! That was a lot of code!

Slide 80

Slide 80 text

Project 2 summary We created timezone and polygon models We loaded data from a shapefile We wrote queries for polygons containing a point, and intersecting a buffer. We further optimized queries by subdividing polygons

Slide 81

Slide 81 text

For further reading... http:/ /daniel-azuma.com/railsconf2012 …and look for the GeoRails Summit BOF