Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Location Matters -- RubyNation

Location Matters -- RubyNation

My slides from "Location Matters: Geospace your Rails Apps" at RubyNation. Lots of reference material in there. Thanks for attending my talk. Feel free to reuse and remix the content.

I only skipped a few of the slides. For real. It was a fast paced talk. :)

Thank you to the RubyNation team for hosting me, and to Intridea for sponsoring and paying my way to the conference.

peteonrails

March 23, 2012
Tweet

Other Decks in Technology

Transcript

  1. My company is hiring. Ruby 1.9? Rails 3.2? Onsite in

    DC or NYC for 6 months? Find me after the talk or tweet @peteonrails. 6 Friday, March 23, 12
  2. Agenda • Why Does Location Matter? • What is Spatial

    Programming? • Important Spatial Terms • The Spatial Stack • How you get started! 7 7 Friday, March 23, 12
  3. Why Location Matters The future belongs to those who know

    how to use location data, tools, and services. -- Jackie Hadley, Where 2.0, 2010 8 Friday, March 23, 12
  4. What is Spatial Programming? • Exposing physical space as a

    first-order programming concept. • Rich, built-in support for shapes, space, and the relationship of physical objects to one another. 12 Friday, March 23, 12
  5. Describing Objects Spatially • Locations on the earth • Geometric

    equations • Shapes of buildings • Parts in an assembly (a blueprint) • Positions of your battleships 13 Friday, March 23, 12
  6. Spatial Logic We can use spatial techniques to answer questions

    that are difficult to answer with typical object relational data. 14 Friday, March 23, 12
  7. Spatial Logic • Find components of a vehicle close to

    the fuel line that emit heat. • Determine which customers are in certain census tracts • Find competitors located within 10 miles of “Route 95” where there is an available billboard within 20 miles. 14 Friday, March 23, 12
  8. Spatial Logic • Components of a vehicle close to the

    fuel line that emit heat. RGeo::Shapefile::Reader.open('car_blueprint.shp') do |file| file.each do |record| components << record.geometry end end fuel_line = components.select { |c| c.name => “fuel line”} danger_zone = fuel_line.buffer(15) # 15 units away. oh_noes = components.select { |c| c.intersects?(buffer) && c.hot? } 15 Friday, March 23, 12
  9. Spatial Logic SELECT Round(100.0 * Sum(t.edu_graduate_dipl) / Sum(t.edu_total), 1) AS

    graduate_pct, n.name, n.boroname FROM nyc_neighborhoods n JOIN nyc_census_tracts t ON ST_Contains(n.the_geom, ST_Centroid(t.the_geom)) # MAGIC! WHERE t.edu_total > 0 GROUP BY n.name, n.boroname ORDER BY graduate_pct DESC LIMIT 10; • New York Neighborhoods / Graduate Degrees. 17 Friday, March 23, 12
  10. • New York Neighborhoods / Graduate Degrees. Spatial Logic graduate_pct

    | name | boroname --------------+-------------------+----------- 40.4 | Carnegie Hill | Manhattan 40.2 | Flatbush | Brooklyn 34.8 | Battery Park | Manhattan 33.9 | North Sutton Area | Manhattan 33.4 | Upper West Side | Manhattan 33.3 | Upper East Side | Manhattan 32.0 | Tribeca | Manhattan 31.8 | Greenwich Village | Manhattan 29.8 | West Village | Manhattan 29.7 | Central Park | Manhattan 18 Friday, March 23, 12
  11. Important terms • GIS: Geographical Information System • Layers: just

    like the layers in Photoshop, but they're georeferenced. • WMS: Web Mapping Service • Projection: algorithm for flatting the globe • Geometry: the core data type in a GIS 19 19 Friday, March 23, 12
  12. Layers Just like in Photoshop, Spatial tools let you layer

    RASTER and VECTOR data on the screen to tell a story. 22 Friday, March 23, 12
  13. WMS Choose the best map service for highlighting your data

    OpenStreetMap 23 Friday, March 23, 12
  14. WMS Choose the best map service for highlighting your data

    NASA WorldWind / JPL 23 Friday, March 23, 12
  15. WMS Choose the best map service for highlighting your data

    Private Commercial Sources (i-Cubed) 23 Friday, March 23, 12
  16. WMS • Being replaced with services tilestreams, MapBox, and GeoCommons

    • WMS will remain relevant, though. 24 Friday, March 23, 12
  17. Projection • Method for representing the spheroid earth on a

    flat surface, such as a screen or paper map. They can be: 25 Friday, March 23, 12
  18. Projections are important for Layering Mercator Projection Satellite Imagery +

    Braun Projection Boundary Lines 26 Friday, March 23, 12
  19. Projections are important to your users nationalgeographic.com wikipedia.com Pew Templeton

    - Robinson NatGeo - Winkel Tripel 29 Friday, March 23, 12
  20. Projection Hints • Roughly equivalent to SRID (System Reference ID)

    in the various spatial databases • When in doubt, try EPSG:4326 or EPSG:900913 (Google). These are the most widely used, but there are many more available. • EPSG:4326 and EPSG:4236 are similar, but have different origin points. Dyslexia FTW. If you really want to know more: http://spatialreference.org/ 33 Friday, March 23, 12
  21. Geometry • The basic spatial data type • <-- Point

    • Line • Polygon • Curve • Multiline • Multipolygon • Geometry Collection 34 Friday, March 23, 12
  22. Geography Just like the Geometry type, but uses a curved

    coordinate set. 35 Friday, March 23, 12
  23. Spatial DBMS • Stores GEOMETRY in your tables • Provides

    spatial functions like DISTANCE() • Implements spatial indexing, so you can forget your High School math. 37 Friday, March 23, 12
  24. Spatial DBMS • PostGIS -> The best, IMHO. • SqlServer

    2008+ • Oracle Spatial -> Powerful, but moody • MySQL Spatial -> Good, but use PostGIS if you can. 37 Friday, March 23, 12
  25. :id :abbrev :name :geom 1 MD Maryland 2 PA Pennsylvania

    3 VA Virginia 4 WV West Virginia 38 Friday, March 23, 12
  26. :id :route :locality :geom 1 95 MD 2 295 DC

    3 395 DC 4 50 DC 38 Friday, March 23, 12
  27. :id :first_name :last_name :location 1 Peter Jackson 38.91094, -77.0327 2

    Joe Grossberg 38.91094, -77.0327 3 John Doe 41.22, -79.01 38 Friday, March 23, 12
  28. Spatial Adapter Translates GEOMETRY columns in your DB into the

    Geometry types of your Framework 39 Friday, March 23, 12
  29. Visualization: OL OpenLayers is one way. Supports display of: •

    Bing Maps • Google Maps • Yahoo Maps • NASA Worldwind • KML Overlays • Your own custom imagery 41 Friday, March 23, 12
  30. You can also use • Wax (Mapbox) • Polymaps •

    OpenStreetMap • Google Maps, Bing Maps 44 Friday, March 23, 12
  31. Spatial Data Sets PostGIS KML ESRI Shapefiles Oracle Spatial MySQL

    Spatial Spatial Logic Components spatial_adapter GeoRuby your awesome biz logic here Visualization Map viewer (JS Library) + Awesome Imagery + Your Data = WOW RGeo 45 Friday, March 23, 12
  32. Spatial Data Sets PostGIS KML ESRI Shapefiles Oracle Spatial MySQL

    Spatial Spatial Logic Components spatial_adapter GeoRuby your awesome biz logic here Visualization Map viewer (JS Library) + Awesome Imagery + Your Data = WOW RGeo 45 Friday, March 23, 12
  33. Spatial Data Sets PostGIS KML ESRI Shapefiles Oracle Spatial MySQL

    Spatial Spatial Logic Components spatial_adapter GeoRuby your awesome biz logic here Visualization Map viewer (JS Library) + Awesome Imagery + Your Data = WOW RGeo 45 Friday, March 23, 12
  34. Spatial Data Sets PostGIS KML ESRI Shapefiles Oracle Spatial MySQL

    Spatial Spatial Logic Components spatial_adapter GeoRuby your awesome biz logic here Visualization Map viewer (JS Library) + Awesome Imagery + Your Data = WOW RGeo 45 Friday, March 23, 12
  35. Spatial Data Sets PostGIS KML ESRI Shapefiles Oracle Spatial MySQL

    Spatial Spatial Logic Components spatial_adapter GeoRuby your awesome biz logic here Visualization Map viewer (JS Library) + Awesome Imagery + Your Data = WOW RGeo 45 Friday, March 23, 12
  36. Getting Started • Decide how much Stack you want to

    host • Develop the story you’re trying to tell • Find Inspiration and Code! 46 Friday, March 23, 12
  37. Getting Started tar xvfz postgis-1.5.1.tar.gz cd postgis-1.5.1 ./configure make make

    install createdb yourdatabase createlang plpgsql yourdatabase psql -d yourdatabase -f postgis.sql psql -d yourdatabase -f postgis_comments.sql psql -d yourdatabase -f spatial_ref_sys.sql 47 Friday, March 23, 12
  38. # Verify that your install works and can do point

    geometry factory = ::RGeo::Cartesian.preferred_factory => #<RGeo::Cartesian::Factory:0x101cd0eb8 @has_z=false, @proj4=nil, @srid=0, @coord_sys=nil, @has_m=false> ?> p00 = factory.point(0, 0) => #<RGeo::Cartesian::PointImpl:0x80e662a4 "Point(0.0 0.0)"> 48 Friday, March 23, 12
  39. ?> zigzag_line = factory.line_string([p00, p10, p01, p11]) => #<RGeo::Cartesian::LineStringImpl:0x80e5ba5c "LineString(0.0

    0.0,1.0 0.0,0.0 1.0,1.0 1.0)"> >> zigzag_line.is_simple? => true >> p00.distance(p11) => 1.4142135623731 49 Friday, March 23, 12
  40. # Other useful stuff you can do # Calculations p1.distance(p2)

    # Distance polygon.envelope # Find a bounding box surface1.area # Area # Aggregation multi = p1 + p2 # Check for useful stuff line1.crosses?(line2) # intersection line1.touches?(line2) # adjacency multi.contains?(p1) # containment # Create a buffer around something line_string.buffer(256) 50 Friday, March 23, 12
  41. # Import a shapefile # You can get the timezone

    shapefiles from: # http://blog.peteonrails.com/ RGeo::Shapefile::Reader.open('timezone.shp') do |file| puts "File contains #{file.num_records} records." file.each do |record| puts "Record number #{record.index}:" puts " Geometry: #{record.geometry.as_text}" puts " Attributes: #{record.attributes.inspect}" end end 51 Friday, March 23, 12
  42. RGeo does tons more than calculating distance: •Find Bounding Boxes

    and Envelopes •Convert to GeoRSS / GeoJSON •Serialization of Geometry / Geography •Transformation between coordinate systems 52 Friday, March 23, 12
  43. Spatial Queries More than just “Dots on a map” We

    can answer really difficult, meaningful questions. 53 53 Friday, March 23, 12
  44. Spatial Queries 53 Q: How many freshwater wells are located

    within 5 miles of a chemical plant? A: (in Oracle Spatial) SELECT c.chemical_plant_name FROM well_table a, chemical_plants b WHERE sdo_within_distance (b.geom, a.geom, 'distance=5 unit=mile') = ‘TRUE’ 53 Friday, March 23, 12
  45. class CreateWells< ActiveRecord::Migration def self.up create_table :wells do |t| t.references

    :customer t.geometry :location # <= spatial_adapter magic. end end def self.down drop_table :wells end end # More spatial adapter magic add_index :wells, :location, :spatial => true 55 Friday, March 23, 12
  46. class Well < ActiveRecord::Base def self.find_by_proximity(km) # This example uses

    Oracle syntax find(:all, :joins => :chemical_plant, :conditions => [“sdo_within_distance (chemical_plants.geom, wells.geom, 'distance=? unit=km') = ‘TRUE’”, km]) end end class WellController < ApplicationController def index @wells = Well.find_by_proximity(params[:buffer]) end end 56 Friday, March 23, 12
  47. <script src="../OpenLayers.js"></script> <script type="text/javascript"> var map, layer; function init(){ map

    = new OpenLayers.Map('map', {maxResolution:'auto'}); map.addControl(new OpenLayers.Control.LayerSwitcher()); layer = new OpenLayers.Layer.WMS( "OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} ); map.addLayer(layer); map.setCenter(new OpenLayers.LonLat(0, 0), 0); var newl = new OpenLayers.Layer.Markers( 'POIs'); map.addLayer(newl); } </script> Adding the View # Please Note: This Sucks 57 Friday, March 23, 12
  48. var  template  =  'http://c.tiles.mapbox.com/v3/mapbox.mapbox-­‐streets/{Z}/{X}/{Y}.png'; var  provider  =  new  MM.TemplatedLayer(template); var

     map  =  new  MM.Map('map',  provider); map.setCenter({  lat:  37,  lon:  139  }).setZoom(8); 58 Friday, March 23, 12
  49. Inspiration ✦ Find some interesting data ✦ Use it to

    tell a compelling story ✦ Visualize it creatively 59 Friday, March 23, 12
  50. Inspiration What if all you have is “Dots on a

    Map”? 1. Go Temporal 2. Use Scale 3. Integrate Real-Time data 4. Let people touch the map 63 Friday, March 23, 12
  51. Inspiration How do we finish the IRAQ Casualty Map story?

    Overlay an Infrastructure Layer Add news stories Aggregate the data into Hot Spots Show Enemy Strongholds Troop Positions And Operations Correlate with Political Events 65 Friday, March 23, 12
  52. Integrating Real Time Data Make your map more valuable by

    being timely. 66 Friday, March 23, 12
  53. Inspiration • Start Thinking Like A Geographer • Aggregate •

    Tell a compelling story about People • Let people touch and change your map 67 Friday, March 23, 12
  54. Getting Started • Decide how much Stack you want to

    host • Develop the story you’re trying to tell • Find Inspiration And Code! 72 Friday, March 23, 12
  55. Agenda • What is Spatial Programming? • Important Spatial Terms

    • The Spatial Stack • How you get started! 73 73 Friday, March 23, 12
  56. http://creativecommons.org/licenses/by/3.0/ You are free: 1. to Share — to copy,

    distribute and transmit the work 2. to Remix — to adapt the work Attribution — You must attribute this work to Peter Jackson (with link). If you want to remix or redistribute the work, including for your own personal or commercial use with no redistribution, please be sure that the prior sentence remains intact. License Creative Commons Attribution 3.0 Unported THIS IS NOT A SHARE-ALIKE LICENSE. YOU MAY MAKE CHANGES TO THIS WORK AND KEEP THEM FOR YOUR OWN USE, COMMERCIAL OR PERSONAL. 74 Friday, March 23, 12