Giter Site home page Giter Site logo

postgis-rails-3.2-experiments's Introduction

postgis-rails-3.2-experiments

This is a Rails 3.2.x proof-of-concept application.

Its purpose is to demonstrate how to use the PostGIS extensions for Postgresql through a simple example of business locations.

Most of this guide was sourced from the activerecord-postgis-adapter guide process

Setup

Using the PostGIS extension requires a little extra work. But it's not difficult.

Database & Rails Configuration

Add to the Gemfile

gem 'activerecord-postgis-adapter'

If you don't have a non-admin Postgres database user, now is the perfect time to create one

$ psql -c "CREATE ROLE rails_user WITH PASSWORD 'rails_user' LOGIN CREATEDB;"

Change the adapter setting on database.yml

development: &postgres
  adapter:   postgis
  database:  postgis-ft-dev
  host:      localhost
  user:      rails_user
  password:  rails_user
  port:      5432
  pool:      25
  timeout:   5000
  schema_search_path: public

Note: When I was working on my local OSX machine, I had to add the PostGIS extension to Postgres' template1. This will then include the PostGIS extension in every new database.

For me this was necessary to silence Postgres type errors when running migrations with the new GEOGRAPHY type. Postgres did not have knowledge of the GIS types.

The work around:

psql -c 'CREATE EXTENSION postgis;' -d template1

Create the current database and enable the PostGIS extension

$ rake db:create
$ psql -c 'CREATE EXTENSION IF NOT EXISTS postgis;' -d postgis-ft-dev
$ psql -c 'CREATE EXTENSION IF NOT EXISTS postgis;' -d postgis-ft-test

Generate Model for Businesses

Use the Rails generators to create the basics

$ rails g scaffold Business name:string latlong:point

Edit the new migration to add additional constraints and an index

class CreateBusinesses < ActiveRecord::Migration
  def change
    create_table :businesses do |t|
      t.string :name
      t.point :latlon, geographic: true

      t.timestamps
    end
    add_index :businesses, :latlon, using: :gist
  end
end

Migrate the database and (optionally) load seed data

$ rake db:migrate
$ rake db:seed

Finding Businesses that are within a given radius

Now this gets fun. Let's customize the Business class to filter by distance from a given point.

class Business < ActiveRecord::Base
  attr_accessible :latlong, :name

  set_rgeo_factory_for_column(:latlong,
    RGeo::Geographic.spherical_factory(:srid => 4326))

  delegate :latitude, :to => :latlong
  delegate :longitude, :to => :latlong

  scope :within_range_from_lat_long, ->(range_in_meters, lat, long) { 
    where("ST_Distance(latlong, 'POINT(#{long} #{lat})') < #{range_in_meters}") }
end

Now we can call Business.within_range_from_lat_long to easily find all businesses that are within a specific range from any given point!

Testing

RSpec tests exist to verify functionality and demonstrated use.

More reading

Daniel Azuma's 9-part tutorial on working with geospatial operations in Rails

postgis-rails-3.2-experiments's People

Contributors

crftr avatar

Watchers

 avatar

Forkers

sangamgupta85

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.