Convert lat long for geometry

352 views
Skip to first unread message

Cassia Freitas

unread,
Feb 26, 2014, 9:23:44 AM2/26/14
to rgeo-...@googlegroups.com
I'm using the geocoder gem to geocode addresses. 
It returns me the latitude and longitude to save the postgis I need to turn this into a geometric object using: 

  SET geom = ST_SetSRID (ST_MakePoint (longitude, latitude), 4326); 

My question is at what point do this?? 
I thought about making the save, I create a class with this method and call her now? Does anyone have an example.

My class Stop:

class Stop < ActiveRecord::Base

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

  geocoded_by :adress
  after_validation :geocode
end

My Active record:

class CreateStops < ActiveRecord::Migration
  def change
    create_table :stops do |t|
      t.string :adress
      t.string :shortName
      t.float :latitude
      t.float :longitude
      t.point :point , :geographic => true

      t.timestamps
    end
  end
end

James Dullaghan

unread,
Feb 26, 2014, 11:49:35 AM2/26/14
to rgeo-...@googlegroups.com
It's not exactly what you need, but this is the functionality I used on a side project.

Model:

  # Create RGEO factory using the mercator coordinate system
  GEOFACTORY = RGeo::Geographic.simple_mercator_factory
  set_rgeo_factory_for_column(:coords, GEOFACTORY.projection_factory)

  # geocode by address created below
  geocoded_by :address do |obj,results|
    if geo = results.first
      # Take lat and long from geocoder and use them for point coords
      # Allows you to use geocoders functionality with postgis
      obj.coords = "POINT(#{geo.longitude} #{geo.latitude})"
    end
  end

  before_save :geocode

Controller:

    # instantiates empty array
    arr = []
    # creates factory

    # iterates projects returned in @project variable
    @projects.each do | project |
      factory = RGeo::GeoJSON::EntityFactory.instance
      # sets feature variable to feature properties returned from factory
      feature = factory.feature(project.coords,
                                project.id, { title: project.title,
                                              desc: project.description,
                                              address: project.address})
      # adds returned contents to empty array instantiated line 7
      arr.push(feature)
    end
    # sets new feature collection with array containing feature properties
    feature_collection = RGeo::GeoJSON::FeatureCollection.new(arr)
    respond_to do |format|
      format.html { render action: 'index' }
      # render geojson
      format.geojson { render json: (RGeo::GeoJSON.encode(feature_collection)) }
    end
  end


--
You received this message because you are subscribed to the Google Groups "RGeo-Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rgeo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
James Dullaghan
Reply all
Reply to author
Forward
0 new messages