将GeoJSON字符串保存到Rails / PostGIS对象中

时间:2018-11-12 21:36:55

标签: ruby-on-rails postgis geojson rgeo

我正在尝试保存一些地理空间数据,但是遇到了麻烦。

如何从前端获取此POST参数(多边形数组):

"bounds"=>[{"type"=>"Feature", "properties"=>{}, "geometry"=>{"type"=>"Polygon", "coordinates"=>[[[-108.379462, 47.037318], [-103.104889, 42.439323], [-109.655197, 41.886168], [-108.379462, 47.037318]]]}}, {"type"=>"Feature", "properties"=>{}, "geometry"=>{"type"=>"Polygon", "coordinates"=>[[[-106.955884, 41.525499], [-101.228794, 41.275293], [-101.225638, 38.73149], [-106.955884, 41.525499]]]}}]

保存到这样的模型中:

# migration
class CreateMarkets < ActiveRecord::Migration[5.0]
  def change
    create_table :markets do |t|
      t.string :name
      t.attachment :image
      t.text :text
      t.multi_polygon :bounds, geographic: true

      t.timestamps
    end
  end
end

在RGeo和RGeo-GeoJSON中使用Rails / PostGIS吗?

目前我正在尝试:

# controller
def create

    @market = Market.new(market_params)
    geo = RGeo::GeoJSON::FeatureCollection.new(params[:market][:bounds])
    @market.bounds = geo

    if @market.save
      render json: @market, notice: 'Market was successfully created.'
    else
      render json: @market.errors, status: :bad_request
    end
  end

并得到错误:

NoMethodError (undefined method `factory' for #<RGeo::GeoJSON::FeatureCollection:0x3fc422bd22b8>)

尽管具有此初始值设定项:

# initializers/rgeo.rb
RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = RGeo::Geos.factory_generator
end

0 个答案:

没有答案