rgeo/activerecord-postgis-adapter

my point factory is CAPIFactory, not SphericalPointImpl

wildrhombus opened this issue · 2 comments

I'm trying to create a basic app following the instructions in the readme, and when I create a point on my model it's type is CAPIFactory, not SphericalPointImpl.

record.lonlat = 'POINT(-122 47)'
record.lonlat

returns #<RGeo::Geos::CAPIPointImpl:0x3ff57d7645c8 "POINT (-122.0 47.0)">

I can't figure out what I'm doing wrong.
I'm using Rails 5, and ruby 2.3.1 and postgres 9.6

My model - my_spatial_table.rb

class CreateMySpatialTables < ActiveRecord::Migration[5.0]
  def change
    create_table :my_spatial_tables do |t|
      t.column :shape1, :geometry
      t.geometry :shape2
      t.line_string :path, srid: 3785
      t.st_point :lonlat, geographic: true
      t.st_point :lonlatheight, geographic: true, has_z: true

      t.timestamps
    end

My config -activerecord_rgeo.rb

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geos.factory_generator
  config.register(RGeo::Geographic.spherical_factory(srid: 4326), geo_type: "point")
end

And my database config

default: &default
  adapter: postgis
  encoding: unicode
  schema_search_path: public, postgis
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

Not sure if this helps. I didn't create an initialisation file and it worked when setting

myrecord.lonlat ='POINT(-122 47)'

But failed when trying to use the factory

p = factory.point(100,45)

Because, fairly obviously 'factory' hasn't been defined anywhere.

It's traditional in open source documentation to leave out critical information. Makes the users work harder!

So now I have to read the source code to work out how to create an instance of the factory

You need to specify has_z: true when you register the factory in order for it to match your data type.

config.register(RGeo::Geographic.spherical_factory(srid: 4326), geo_type: "point", has_z: true)

Please ask questions like this on Stack Overflow or other Q/A forums.