尝试将 Rgeo 与 rails 一起使用

Try to use Rgeo with rails

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

安装GeoJSON的正确方法是什么(and/or地方)。

宝石文件:

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

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

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

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

你正在使用工厂,而我正在使用运行时默认值,但要使用工厂,你只需要对它们调用相同的方法即可。