1

我正在使用 Ruby on Rails 和 PostGIS 将 GeoJSON 地图数据存储在一个名为“Map”的模型中。当我保存一个使用简单 GeoJSON 功能成功加载的对象时,我收到错误消息“NoMethodError: undefined method `factory' 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

设置

  • 导轨 5.1.4
  • 地理信息系统 9.6
  • Docker 数据库源:kartoza/postgis:9.6-2.4
  • Docker web src:红宝石 2.3.5

文件

配置/初始化程序/rgeo.rb

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config| 
  config.default = RGeo::Geographic.spherical_factory(srid: 4326) 
end

宝石文件

# 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

来源

除其他外,我没有任何运气就通过了以下来源:

Daniel Azuma 关于 2011 年设置的原始帖子

StackOverflow 帖子讨论了为什么其中一部分已过时 2015-06-26

  • 请注意已接受答案下方的注释,该注释阐明了如何为所有内容设置默认球形工厂-这对我和我上面所做的都很好。

鉴于这已经是一个单一功能,Github 问题响应似乎并不相关

更新和活跃的 Github activerecord-postgis-adaptor 响应,触及以上所有点

建议尽管不使用 Geos 安装需要与 2015 年搏斗

  • 尽管它看起来不相关,但我仍然在搞乱这个选项
  • 试过但没有效果(见下文)

尝试

我也尝试安装 Geos,但这并没有什么不同。

在容器中:

apt install libgeos-dev
gem uninstall rgeo && gem install rgeo

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

irb(main):001:0> RGeo::Geos.supported?
=> true

尝试和结果同上(例1)

4

2 回答 2

1

您需要使用特征的几何形状(调用 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)
于 2018-11-07T23:28:54.617 回答
0

谢谢,三通。

我以您的示例为基础来展示对我有用的方法。唯一的区别在于最后一行,其中 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)
于 2020-09-27T18:21:02.477 回答