Adelaide Ruby Meetup PostGIS Notes

25
Intro to PostGIS in Ruby

description

Notes from Introduction to PostGIS & Geospatial Datasets in Ruby & Rails talk at Adelaide Ruby meetup.

Transcript of Adelaide Ruby Meetup PostGIS Notes

Page 1: Adelaide Ruby Meetup PostGIS Notes

Intro to PostGIS in Ruby

Page 2: Adelaide Ruby Meetup PostGIS Notes

What is PostGIS

A set of geo-spatial extensions for the PostgreSQL database

And for those who don’t know, PostgreSQL is an SQL database similar to MySQL & MS SQL Server

Page 3: Adelaide Ruby Meetup PostGIS Notes

What do I need to know about geo-spatial ‘stuff’ to get going?

It depends entirely on what you’re doing :)

Page 4: Adelaide Ruby Meetup PostGIS Notes

The Building Blocks:

Point

LineString

Polygon

Page 5: Adelaide Ruby Meetup PostGIS Notes

With these Blocks, we can ask:

Does this LineString intersect any Polygons?

Which Polygons does a Point exist within?

How much do these Polygons overlap?

What are the closest alternative Points to this Point?

much, much more

Page 6: Adelaide Ruby Meetup PostGIS Notes

SRID?

Spatial Reference ID

Need to understand that when displaying a 3D world on a 2D screen, decisions on how to need to be made

Page 7: Adelaide Ruby Meetup PostGIS Notes

SRID?

Much like a graph needs labeled axis to mean anything, SRIDs provide context to correctly interpret spatial coordinates

There’s a collation of industry standard SRID’s that get setup on your db - look at your shiny new spatial_ref_sys table

Rule of thumb - unless you know better, just use 4326 :)

Page 8: Adelaide Ruby Meetup PostGIS Notes

Spatial Indexes

Greatly improves the speed of querying complex spatial datasets

Page 9: Adelaide Ruby Meetup PostGIS Notes

Pulling this all into Ruby Land!

RGeo provides the spatial ‘building blocks’ for Ruby

ActiveRecord PostGIS Adapter provides the migration support & RGeo type casting layer to ActiveRecord/Rails

Page 10: Adelaide Ruby Meetup PostGIS Notes

RGeo

https://github.com/rgeo/rgeo

‘An implementation of the industry standard OGC Simple Features Specification, which provides data representations of geometric objects such as points, lines and polygons, along with a set of geometric analysis operations.’

Page 11: Adelaide Ruby Meetup PostGIS Notes

What’s missing?

A nice abstraction layer allowing scoped spatial queriesi.e.Hotel.near(location).with_wifiRegion.within(country) country.outside?(region)

Page 12: Adelaide Ruby Meetup PostGIS Notes

Common Spatial Commands

ST_IntersectsDoes a line or polygon intersect with another line or polygon? Useful for ‘areas travelled through’ or ‘area union’ kind of questions

ST_DWithinLets us ask whether a geographic object is within a certain distance of another geographic object

ST_ContainsDo all given points lie within a given polygon?

ST_OverlapsDo 2 different geometries share space?

Page 13: Adelaide Ruby Meetup PostGIS Notes

Common Spatial Commands

Plenty more, check out the API!

Page 14: Adelaide Ruby Meetup PostGIS Notes

So, what is this good for?

Criminally underused in the web dev world

Incredibly powerful for providing context to applications that utilise location related data(in the Rails world, think mobile APIs…)

Great for data analysis & strategically targeting markets - think growth hacking!

Page 15: Adelaide Ruby Meetup PostGIS Notes

So, what is this good for?

Working with existing spatial datasets

Page 16: Adelaide Ruby Meetup PostGIS Notes

ESRI Shapefile

A popular format for storing geometric location information & associated metadata

Used heavily by the Australian Government- e.g. municipal boundaries, ABS data & census data

Most of our friendly geo-spatial software will happily find a way to work with this data format

Page 17: Adelaide Ruby Meetup PostGIS Notes

Some Gotchas

Make sure your schema includes the postgis extension

Don’t delete the spatial_ref_sys table!

Pay close attention to your SRIDs, and unless you’re sure, stick to using 4326 on your db

When importing data, convert it to 4326 if a data source is in a different SRID

Page 18: Adelaide Ruby Meetup PostGIS Notes

Some Gotchas

Rails likes to use SELECT table.* - beware that this can be painful to performance if your table contains lots of intricate spatial data. Consider using a join to store complex data in a separate table & only call when necessary.

Even with spatial indexing, large datasets can become a burden on the database. Consider using traditional id based relationships as a cache for complex geospatial result sets.

Page 19: Adelaide Ruby Meetup PostGIS Notes

Alternatives

MySQL Spatial Extensions

Not as mature as PostGIS

Only MyISAM supports spatial indexes - ok if you’re not into data retention :)

SQL Server

More mature than MySQL & not a bad choice if you’re a MS Shop

Page 20: Adelaide Ruby Meetup PostGIS Notes

Alternatives

Oracle Spatial

I’ve heard it’s pretty good if you have the wallet for it!

I’ve also been informed that there’s a free version that may cut it for many people :)

MongoDB

Great for basic geospatial functionality

Not as fully featured as PostGIS, may leave you wanting in certain areas

Page 21: Adelaide Ruby Meetup PostGIS Notes

Getting Started in Rails

First step: Get a copy of PostgreSQL & PostGIS

Easiest way on OS/X is to grab Postgres.app from http://postgresapp.com/

Grab a copy of QGIS while you’re at it - http://www.kyngchaos.com/software/qgis for Mac users

Page 22: Adelaide Ruby Meetup PostGIS Notes

Getting Started in Rails

rails new spatial_project -d postgresql!

gem ‘activerecord-postgis-adapter’ Gemfile!

adapter: postgis config/database.yml!

rake db:create!

rake db:gis:setup

Page 23: Adelaide Ruby Meetup PostGIS Notes

Getting Started in Rails

You can now run migrations like these:create_table :spatials, do |t| t.string :name, t.multi_polygon, :area, srid: 4326, t.point, :position, srid: 4326, t.line_string, :path, srid: 4326 end

Page 24: Adelaide Ruby Meetup PostGIS Notes

Getting Started in Rails

And create spatial indexes like this:add_index :spatials, :area, spatial: true

Page 25: Adelaide Ruby Meetup PostGIS Notes

Resources

http://gis.stackexchange.com/

http://postgis.net/documentation/

http://daniel-azuma.com/articles/georails

https://www.qgis.org/