adelaide ruby meetup postgis notes
DESCRIPTION
Notes from Introduction to PostGIS & Geospatial Datasets in Ruby & Rails talk at Adelaide Ruby meetup.TRANSCRIPT
Intro to PostGIS in Ruby
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
What do I need to know about geo-spatial ‘stuff’ to get going?
It depends entirely on what you’re doing :)
The Building Blocks:
Point
LineString
Polygon
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
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
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 :)
Spatial Indexes
Greatly improves the speed of querying complex spatial datasets
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
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.’
What’s missing?
A nice abstraction layer allowing scoped spatial queriesi.e.Hotel.near(location).with_wifiRegion.within(country) country.outside?(region)
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?
Common Spatial Commands
Plenty more, check out the API!
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!
So, what is this good for?
Working with existing spatial datasets
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
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
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.
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
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
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
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
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
Getting Started in Rails
And create spatial indexes like this:add_index :spatials, :area, spatial: true
Resources
http://gis.stackexchange.com/
http://postgis.net/documentation/
http://daniel-azuma.com/articles/georails
https://www.qgis.org/