Intro to Spatial Programming Peter Jackson Director, Client Solutions, Intridea Inc. 1 Friday, July 9, 2010 Agenda • What is Spatial Programming? • Important Spatial Terms • Tools in the Ruby / Rails spatial stack • How you get started 2 Friday, July 9, 2010 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. Friday, July 9, 2010 Describing Objects Spatially • Locations on the earth • Geometric equations • Shapes of buildings • Parts in an assembly (a blueprint) • Positions of your battleships Friday, July 9, 2010 Spatial Logic We can use spatial techniques to answer questions that are difficult to answer with typical object relational data. Friday, July 9, 2010 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. Friday, July 9, 2010 Important terms 6 Friday, July 9, 2010 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 6 Friday, July 9, 2010 GIS A Geographical Information System visually represents data about geography. Friday, July 9, 2010 Most widely used GIS Friday, July 9, 2010 Most widely used GIS Friday, July 9, 2010 Most widely used GIS Friday, July 9, 2010 Layers Just like in Photoshop, Spatial tools let you layer RASTER and VECTOR data on the screen to tell a story. Friday, July 9, 2010 Layers Friday, July 9, 2010 Layers Friday, July 9, 2010 Layers Friday, July 9, 2010 Layers Friday, July 9, 2010 Layers Friday, July 9, 2010 Layers Friday, July 9, 2010 WMS Choose the best map service for highlighting your data OpenStreetMap Friday, July 9, 2010 WMS Choose the best map service for highlighting your data NASA WorldWind / JPL Friday, July 9, 2010 WMS Choose the best map service for highlighting your data Bing Friday, July 9, 2010 WMS Choose the best map service for highlighting your data Private Commercial Sources (i-Cubed) Friday, July 9, 2010 Projection • Method for representing the spheroid earth on a flat surface, such as a screen or paper map. They can be: Friday, July 9, 2010 Projection Polar Friday, July 9, 2010 Projection Planar Friday, July 9, 2010 Projection Conic Friday, July 9, 2010 Projection Cylindrical Friday, July 9, 2010 Projections are important for Layering + Mercator Projection Satellite Imagery Friday, July 9, 2010 Braun Projection Boundary Lines Projections are important for Layering = FAIL Friday, July 9, 2010 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 much more accurate ones 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/ Friday, July 9, 2010 Geometry • The basic spatial data type • <-- Point • Line • Polygon • Curve • Multiline • Multipolygon • Geometry Collection Friday, July 9, 2010 Spatial Stack • Spatial DBMS • PostGIS • Oracle Spatial • MySQL Spatial • spatial_adapter • GeoRuby • OpenLayers Friday, July 9, 2010 Spatial DBMS • Stores GEOMETRY in your tables • Provides spatial functions like DISTANCE() • Implements spatial indexing, so you can forget your High School math. Friday, July 9, 2010 Spatial DBMS • PostGIS -> The best, IMHO. • Oracle Spatial -> Powerful, but moody • MySQL Spatial -> Good, but use PostGIS if you can. Friday, July 9, 2010 Friday, July 9, 2010 :id :abbrev :name 1 MD Maryland 2 PA Pennsylvania 3 VA Virginia 4 WV West Virginia :geom Friday, July 9, 2010 :id :route :locality 1 95 MD 2 295 DC 3 395 DC 4 50 DC :geom :id 1 2 3 Friday, July 9, 2010 :first_name :last_name Peter Joe John Jackson :location 38.91094, -77.0327 38.91094, Grossberg -77.0327 Doe 41.22, -79.01 GeoRuby • Exposes the Geometry data types in Ruby. • Converts ESRI Shapefile data • Handles other interchange formats • Does a lot of geometric magic Friday, July 9, 2010 spatial_adapter • Translates GEOMETRY columns in your DB into GeoRuby::Geometry types. • Supports :geometry columns in migrations Friday, July 9, 2010 spatial_adapter If you are using PostGIS or MySQL, you can use the stock spatial_adapter maintained by “fragility”. If you are using Oracle Spatial, you need my fork of spatial_adapter, which is in early beta form. github.com/peteonrails/spatial_adapter Friday, July 9, 2010 OpenLayers One of many javascript visualization libraries Supports display of: • Bing Maps • Google Maps • Yahoo Maps • NASA Worldwind • KML Overlays • Your own custom imagery Friday, July 9, 2010 OpenLayers Friday, July 9, 2010 How it all hangs together Friday, July 9, 2010 Visualization Map viewer (JS Library) Spatial Logic Components Spatial Data Sets KML Friday, July 9, 2010 + Awesome Imagery your awesome biz logic here ESRI Shapefiles + Your Data = WOW GeoRuby spatial_adapter PostGIS Oracle MySQL Spatial Spatial Visualization Map viewer (JS Library) Spatial Logic Components Spatial Data Sets KML Friday, July 9, 2010 + Awesome Imagery your awesome biz logic here ESRI Shapefiles + Your Data = WOW GeoRuby spatial_adapter PostGIS Oracle MySQL Spatial Spatial Visualization Map viewer (JS Library) Spatial Logic Components Spatial Data Sets KML Friday, July 9, 2010 + Awesome Imagery your awesome biz logic here ESRI Shapefiles + Your Data = WOW GeoRuby spatial_adapter PostGIS Oracle MySQL Spatial Spatial Visualization Map viewer (JS Library) Spatial Logic Components Spatial Data Sets KML Friday, July 9, 2010 + Awesome Imagery your awesome biz logic here ESRI Shapefiles + Your Data = WOW GeoRuby spatial_adapter PostGIS Oracle MySQL Spatial Spatial Visualization Map viewer (JS Library) Spatial Logic Components Spatial Data Sets KML Friday, July 9, 2010 + Awesome Imagery your awesome biz logic here ESRI Shapefiles + Your Data = WOW GeoRuby spatial_adapter PostGIS Oracle MySQL Spatial Spatial How The Tools Implement GEOMETRY Friday, July 9, 2010 Tool Geometry Implementation Ruby GeoRuby::Geometry Oracle MDSYS.SDO_GEOMETRY PostGIS Geometry and Geography types MySQL GEOMETRY, POINT, LINE, etc Getting Started Friday, July 9, 2010 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 Friday, July 9, 2010 Getting Started $ gem install GeoRuby spatial_adapter Successfully installed GeoRuby-1.3.4 Successfully installed spatial_adapter-1.1.2 Successfully installed activesupport-2.3.8 3 gems installed Friday, July 9, 2010 Getting Started Now fire up IRB any try this: Friday, July 9, 2010 >> point = Point.new(4326, false, false) => #<GeoRuby::SimpleFeatures::Point:0x1014ef078 @with_z=false, @x=0.0, @srid=4326, @m=0.0, @z=0.0, @with_m=false, @y=0.0> >> point.x = -33.56 >> point.y = 40.23 # => -33.56 # => 40.23 >> point2 = Point.new >> point2.set_x_y_z(-35.65, 41.11, 0) >> point.spherical_distance(point2) => 201598.077888077 >> point.eudclidian_distance(point2) => 2.26770809408971 Friday, July 9, 2010 GeoRuby does tons more than calculating distance: • Bounding Box • Convert to KML • GeoRSS • Plays nice with spatial_adapter Friday, July 9, 2010 What ELSE can we do ? A lot. Let’s look: Friday, July 9, 2010 Spatial Queries More than just “Dots on a map” We can answer really difficult, meaningful questions. 26 Friday, July 9, 2010 Spatial Queries 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’ 26 Friday, July 9, 2010 Using PostGIS, spatial_adapter, and GeoRuby, your Rails app could do this: Friday, July 9, 2010 class CreateWells< ActiveRecord::Migration def self.up create_table :wells do |t| t.references :customer t.geometry :location end end # <= spatial_adapter magic. def self.down drop_table :wells end end # More spatial adapter magic add_index :wells, :location, :spatial => true Friday, July 9, 2010 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 Friday, July 9, 2010 Adding the View <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> Friday, July 9, 2010 Getting Started Summary • Install Spatial DBMS • Install GeoRuby and spatial_adapter • Use OpenLayers or some other JS library • Start thinking spatially! Friday, July 9, 2010 Friday, July 9, 2010 Agenda • What is Spatial Programming? • Important Spatial Terms • Tools in the Ruby / Rails spatial stack • How you get started 33 Friday, July 9, 2010 License Creative Commons Attribution 3.0 Unported 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. THIS IS NOT A SHARE-ALIKE LICENSE. YOU MAY MAKE CHANGES TO THIS WORK AND KEEP THEM FOR YOUR OWN USE, COMMERCIAL OR PERSONAL. http://creativecommons.org/licenses/by/3.0/ Friday, July 9, 2010