我使用Rails和PostGIS将GeoJSON地图数据存储在一个名为" map“的模型中。当我成功地保存一个带有简单GeoJSON特性的对象时,我会得到一个错误:"NoMethodError:未定义的方法‘工厂’for #“。
请参阅下面的示例、安装程序、我尝试过的内容以及指向资源的链接。
示例1(使用Rails控制台)
irb(main):007:0> feature = '{ "type": "Feature", "properties": { "name": "Coors Field", "amenity": "Baseball Stadium", "popupContent": "This is where the Rockies play!" }, "geometry": { "type": "Point", "coordinates": [-104.99404, 39.75621] }}'
=> "{ \"type\": \"Feature\", \"properties\": { \"name\": \"Coors Field\", \"amenity\": \"Baseball Stadium\", \"popupContent\": \"This is where the Rockies play!\" }, \"geometry\": { \"type\": \"Point\", \"coordinates\": [-104.99404, 39.75621] }}"
irb(main):008:0> geom_feature = RGeo::GeoJSON.decode(feature)
=> #<RGeo::GeoJSON::Feature:0x304e48c id=nil geom="POINT (-104.99404 39.75621)">
irb(main):009:0> Map.create(name: 'demo', geometry: geom_feature)
(0.9ms) BEGIN
(0.6ms) ROLLBACK
NoMethodError: undefined method `factory' for #<RGeo::GeoJSON::Feature:0x000000000609c918>
from (irb):9设置
文件
配置/初始化器/rGeo.rb
RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
config.default = RGeo::Geographic.spherical_factory(srid: 4326)
endGemfile
# Mapping
gem 'activerecord-postgis-adapter'
gem 'rgeo'
gem 'rgeo-geojson'
# need this? found on random post - willing to try anything
gem 'rgeo-activerecord'db/migrate/20182039293939_create_maps.rb
create_table :maps do |t|
t.string :name
t.st_polygon :polygon
t.geometry :geometry
t.multi_polygon :multi_polygon
t.references :mappable, polymorphic: true, index: true
t.timestamps
end资料来源
除其他外,我还通过了以下消息来源,但没有任何运气:
丹尼尔·祖马关于2011年设置的原始帖子
关于2015年-06-26的部分原因的StackOverflow帖子
Github发布的响应似乎不相关,因为这已经是一个单一的特性
最近的和活跃的Github activerecord-postgis-适配器响应,击中以上所有点。
建议尽管不使用地球观测系统,但安装工作需要与2015年进行斗争
尝试
我也试过安装Geos,但这并没有什么不同。
集装箱内:
apt install libgeos-dev
gem uninstall rgeo && gem install rgeo在Rails控制台中:(重新启动后)
irb(main):001:0> RGeo::Geos.supported?
=> true尝试和结果与上述相同(示例1)
发布于 2018-11-07 23:28:54
您需要使用特性的几何结构(调用Feature#geometry):
json = '{ "type": "Feature", "properties": { "name": "Coors Field", "amenity": "Baseball Stadium", "popupContent": "This is where the Rockies play!" }, "geometry": { "type": "Point", "coordinates": [-104.99404, 39.75621] }}'
feature = RGeo::GeoJSON.decode(json)
Map.create(name: 'demo', geometry: feature.geometry)发布于 2020-09-27 18:21:02
谢谢,发球。
我以你的例子为基础,向我展示了什么对我有用。唯一的区别在于最后一行,rgeo需要您通知功能的索引。
json = '{ "type": "Feature", "properties": { "name": "Coors Field", "amenity": "Baseball Stadium", "popupContent": "This is where the Rockies play!" }, "geometry": { "type": "Point", "coordinates": [-104.99404, 39.75621] }}'
feature = RGeo::GeoJSON.decode(json)
Map.create(name: 'demo', geometry: feature[0].geometry)https://stackoverflow.com/questions/50557917
复制相似问题