rgeo/rgeo-geojson

Error NoMethodError (undefined method `factory' for #<RGeo::GeoJSON::FeatureCollection:0x3fc87ef243d4>)

hguzman opened this issue · 2 comments

I want to save a polygon.

This is Geojson http://geojson.io/#map=15/11.0132/-74.8069

str1 = "geojson....."
geom = RGeo::GeoJSON.decode(str1, json_parser: :json)
l = Limit.new
l.multipoligono = geom
l.save

NoMethodError: undefined method factory' for #RGeo::GeoJSON::FeatureCollection:0x3fc7febb7424`

I've worked around it in my current project using:

geometry = RGeo::GeoJSON.decode(geojson_string)
return geometry unless geometry.is_a? RGeo::GeoJSON::FeatureCollection
factory = RGeo::Geographic.spherical_factory(srid: 4326, buffer_resolution: 8)
factory.collection(geometry.instance_variable_get('@features').map(&:geometry))

I cannot access you geojson file, the link is invalid (and geojson.io works with local storage, hence you'd rather share a gist!). Maybe this snippet that checks if a geojson contains a point may help you:

geojson = RGeo::GeoJSON.decode(any_valid_geojson)
geometries = case geojson
             when  RGeo::GeoJSON::Feature
               [geojson.geometry]
             when  RGeo::GeoJSON::FeatureCollection
               geojson.map(&:geometry)
             when nil # case happening when given an incomplete geojson
               []
             else
               [geojson]
             end
geometries.any? { |geometry| geometry.contains?(factory.point(1, 1)) }

Be careful: this will work if you have geos installed, but may not work otherwise since contains? implementation is not widely supported in the ruby implementation.

Since the issue is more than 2 years old and I think the above snippet should answer it, I'm closing it now, if that is not enough, please open another one.