=Mapfish server plugin for Ruby on Rails

This HowTo describes step by step how to use MapFish[http://www.mapfish.org/] Server Framework to set up a MapFish project. A MapFish project defines Web Services on which MapFish Client components can rely. See MapFishProtocol for a description of the interfaces provided by MapFish Web Services.

The Mapfish server for Ruby is implemented as a plugin for the Ruby on Rails[http://www.rubyonrails.org/] framework.

==Create a MapFish project

Create a new Rails project:
  rails --database=postgresql MyMapFishProject
  cd MyMapFishProject

Install the latest version of the Mapfish plugin:
  ./script/plugin install svn://www.mapfish.org/svn/mapfish/trunk/MapFish/server/ruby/mapfish

Install the latest version of the Mapfish client libraries:
  svn export http://www.mapfish.org/svn/mapfish/trunk/MapFish/client/mfbase public/mfbase

Build the deployment libs
  [TODO]

Install the required plugins and gems:
  sudo gem install georuby
  ./script/plugin install svn://rubyforge.org/var/svn/georuby/SpatialAdapter/trunk/spatial_adapter


==Set up the PostGIS database

If you don't have PostGIS database template yet, create one:
  sudo su - postgres
  createdb -E UTF8 template_postgis # Create the template spatial database.
  createlang -d template_postgis plpgsql # Adding PLPGSQL language support.
  psql -d template_postgis -f /usr/share/postgresql-8.3-postgis/lwpostgis.sql
  psql -d template_postgis -f /usr/share/postgresql-8.3-postgis/spatial_ref_sys.sql
  cat <<EOS | psql -d template_postgis
  UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis';
  REVOKE ALL ON SCHEMA public FROM public;
  GRANT USAGE ON SCHEMA public TO public;
  GRANT ALL ON SCHEMA public TO postgres;
  GRANT SELECT, UPDATE, INSERT, DELETE
     ON TABLE public.geometry_columns TO PUBLIC;
  GRANT SELECT, UPDATE, INSERT, DELETE
     ON TABLE public.spatial_ref_sys TO PUBLIC;
  VACUUM FULL FREEZE;
  EOS

Change the connection properties in <tt>config/database.yml</tt>.
Add a line <tt>template: template_postgis</tt> for each environment.

Create the development database, if it does not exist:
  rake db:create

==Set up layers

You now need to create layers. In effect, a layer corresponds to a PostGIS table.

Create a model:
  ./script/generate model --skip-timestamps --skip-fixture WeatherStation name:string geom:point
  rake db:migrate

Import some data:
  [TODO] ./script/runner "Geonames::Weather.weather(:north => 44.1, :south => -9.9, :east => -22.4, :west => 55.2).each { |st| WeatherStation.create(:name => st.stationName, :geom => Point.from_x_y(st.lng, st.lat)) }"

Add a new controller with a the default actions for mapfish:

  class WeatherStationsController < ApplicationController

    [TODO] geojson_layer :weather_stations, :geometry => :geom

  end

Create a model for an existing table:
  ./script/generate model --skip-migration --skip-fixture Country

Insert table name and custom id in <tt>app/models/country.rb</tt>:
  set_table_name "world_factbk_simplified"
  set_primary_key "gid"

Add the controller:
  class CountriesController < ApplicationController

    [TODO] geojson_layer :readonly => true, :only => [:gid, :country, :birth_rt, :death_rt, :fertility, :simplify]

  end


Add the layers to <tt>routes.rb</tt>:
  map.resources :weather_stations
  map.resources :countries


==Starting the web server

You should be all set now. Try starting the web server:
  ./script/server

and checkout [http://localhost:3000/countries?maxfeatures=10]

Your browser should be displaying a nice GeoJSON object!

You can now go back to your webpage and configure MapFish widgets to access your layer through the URL {{{http://localhost:3000/countries}}}.


==License
The Mapfish server plugin for Rails is released under the LGPL license.

<em>Copyright (c) 2008 Pirmin Kalberer, Sourcepole AG</em>
