首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails / PostGIS / RGeo错误: NoMethodError: RGeo::GeoJSON::NoMethodError的未定义方法“工厂”

Rails / PostGIS / RGeo错误: NoMethodError: RGeo::GeoJSON::NoMethodError的未定义方法“工厂”
EN

Stack Overflow用户
提问于 2018-05-28 01:04:02
回答 2查看 1.1K关注 0票数 1

我使用Rails和PostGIS将GeoJSON地图数据存储在一个名为" map“的模型中。当我成功地保存一个带有简单GeoJSON特性的对象时,我会得到一个错误:"NoMethodError:未定义的方法‘工厂’for #“。

请参阅下面的示例、安装程序、我尝试过的内容以及指向资源的链接。

示例1(使用Rails控制台)

代码语言:javascript
复制
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

设置

  • Rails 5.1.4
  • PostGIS 9.6
  • Docker DB src: kartoza/postgis:9.6-2.4
  • Docker web src: Ruby2.3.5

文件

配置/初始化器/rGeo.rb

代码语言:javascript
复制
RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config| 
  config.default = RGeo::Geographic.spherical_factory(srid: 4326) 
end

Gemfile

代码语言:javascript
复制
# 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

代码语言:javascript
复制
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,但这并没有什么不同。

集装箱内:

代码语言:javascript
复制
apt install libgeos-dev
gem uninstall rgeo && gem install rgeo

在Rails控制台中:(重新启动后)

代码语言:javascript
复制
irb(main):001:0> RGeo::Geos.supported?
=> true

尝试和结果与上述相同(示例1)

EN

回答 2

Stack Overflow用户

发布于 2018-11-07 23:28:54

您需要使用特性的几何结构(调用Feature#geometry):

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2020-09-27 18:21:02

谢谢,发球。

我以你的例子为基础,向我展示了什么对我有用。唯一的区别在于最后一行,rgeo需要您通知功能的索引。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50557917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档