Skip to content

Instantly share code, notes, and snippets.

@sabman
Created November 1, 2009 10:23
Show Gist options
  • Save sabman/223461 to your computer and use it in GitHub Desktop.
Save sabman/223461 to your computer and use it in GitHub Desktop.
set up an AR schema for PostGIS outside of Rails
system('sudo gem install GeoRuby')
system('git clone git://github.com/fragility/spatial_adapter.git')
$:.unshift(File.join(File.dirname(__FILE__), 'spatial_adapter', 'lib'))
require "rubygems"
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "localhost",
:database => "veitnam_development",
:username => "sabman",
:password => ""
)
# These should be added AFTER the AR connection is established otherwise you will get
# "uninitialized constant ActiveRecord::..." error
require "post_gis_adapter"
require "geo_ruby"
class Poi < ActiveRecord::Base
end
# create a postgis database
system('createdb -T template_postgis vietnam_development')
# convert shapefile to sql
system('shp2pgsql -s 4326 -g geom pois.shp pois > pois.sql')
# change encoding to correct format - get script from https://gist.github.com/e658eb1df6f085d68352
system('convert_encoding.rb -f latin1 pois.sql')
# load the data in postgresql
system('psql -f pois.sql -d vietnam_development')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment