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

Getting Down To Earth: Geospatial Analysis With Rails

Getting Down To Earth: Geospatial Analysis With Rails

Presentation by Daniel Azuma from RailsConf 2012. Covers example implementations of geospatial projects in Ruby on Rails. For more info, see http://daniel-azuma.com/railsconf2012

E06aa8f63d2a1753a2b352bc1cabbde2?s=128

Daniel Azuma

April 23, 2012
Tweet

Transcript

  1. Getting Down to Earth: Daniel Azuma RailsConf 2012 Geospatial Analysis

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

    with Rails
  3. None
  4. Daniel Azuma Rails Developer since 2006 Author of RGeo and

    related gems Chief Architect at Pirq
  5. We’re hiring!

  6. Beyond placing pushpins on a Google Map

  7. Let’s dive directly into code We can worry about the

    concepts another time
  8. Our agenda Setting up a geospatial Rails app Project 1:

    Visualizing geo-activity Project 2: Timezone lookup for a location Further material available online
  9. Setting up Rails

  10. Geospatial software stack libgeos — geometric algorithms libproj — coordinate

    projections PostGIS — spatial plugin for PostgreSQL RGeo — Ruby libraries
  11. …use Postgres — it’s awesome…

  12. None
  13. …check out the README for more info…

  14. None
  15. We installed some software We created a Rails app as

    usual But there were a few more configs …and that’s it!
  16. Project 1: Visualizing location activity

  17. …use the “point” data type…

  18. …configuring the migration…

  19. …SRID 3785 is the Google Maps Mercator projection…

  20. …make sure the index is marked “spatial”…

  21. …configuring the model…

  22. …assign an RGeo Factory to the model…

  23. Need Input!

  24. …some data to load…

  25. None
  26. …remember to transform coordinate systems…

  27. Goal: visualize data on a map

  28. …query for points in a rectangle…

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

  30. …search for overlap with the box…

  31. Rectangles are not rectangular in geographic coordinates

  32. None
  33. …use Squeel — it’s awesome…

  34. None
  35. OUCH!

  36. None
  37. …create a heatmap in javascript…

  38. None
  39. 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
  40. Project 2: Location time zone lookup

  41. World time zone regions

  42. …a timezone has_many polygon regions…

  43. None
  44. …configure the migration…

  45. …configure the model with an RGeo Factory…

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

  47. “I don’t even see the code anymore. All I see

    is point, polygon, circle…”
  48. …load a shapefile as a stream of records…

  49. …obtain the timezone object…

  50. …populate the polygon table…

  51. …query using the st_contains() SQL function…

  52. None
  53. None
  54. None
  55. …our original query…

  56. …also query for “nearby” polygons…

  57. …check the distance…

  58. …distances also have a coordinate system…

  59. …take the closest polygon…

  60. Ain’t my fault you asked for st_distance…

  61. None
  62. None
  63. …PostGIS can optimize intersections…

  64. A “tall” table with many rows

  65. A database index eliminates lots of rows quickly

  66. Spatial databases are sometimes “fat”.

  67. Reduce width by increasing the row count

  68. Reduce width by increasing the row count

  69. 4-to-1 subdivision

  70. Looking for the sweet spot

  71. None
  72. …let’s add polygon subdivision…

  73. None
  74. None
  75. …the core of the algorithm…

  76. …check the number of sides…

  77. …create a bounding box and subdivide…

  78. …use RGeo — it’s awesome…

  79. Whew! That was a lot of code!

  80. 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
  81. For further reading... http:/ /daniel-azuma.com/railsconf2012 …and look for the GeoRails

    Summit BOF