代码之家  ›  专栏  ›  技术社区  ›  Ben

尝试将Rgeo与rails一起使用

  •  1
  • Ben  · 技术社区  · 6 年前

    我是rails的新手,尝试使用Rgeo和GeoJSON。 我想从我的数据中提取GeoJSON,以便在传单中使用。

    正确的安装方式是什么 GeoJSON ( 和/或地点 )。

    Gemfile:

    gem 'activerecord-postgis-adapter'
    gem 'rgeo-geojson'
    gem 'rgeo'
    gem 'rgeo-activerecord'
    

    是否需要任何其他配置,正确的安装方式是什么( GeoJSON )

    require 'rgeo/geo_json'
    RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
    # By default, use the GEOS implementation for spatial columns.
    config.default = RGeo::Geos.factory_generator
    
    # But use a geographic implementation for point columns.
    config.register(RGeo::Geographic.spherical_factory(srid: 4326), 
    geo_type: "point")
    end
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   boulder_ruby    6 年前

    要读取geojson数据,只需捕获变量中的纯文本并对其进行解析。

    str = File.open("sample.geojson").read
    shp = RGeo::GeoJSON.decode(str)
    

    我认为4326是默认的SRID,因此从技术上讲,没有任何东西可以为这个实例进行配置,因为您还没有重新投影它。

    您使用的是工厂,我使用的是运行时默认值,但要使用工厂,您只需对其调用相同的方法即可。