rgeo/rgeo-geojson

Decode returns empty geometry collection for non-empty geojson

Jeehut opened this issue ยท 8 comments

When decoding the geojson from this OpenStreetMap region with the following code I receive an empty collection although there's a lot of data:

RGeo::GeoJSON.decode(geojson_string, json_parser: :json)
 => #<RGeo::Geos::CAPIGeometryCollectionImpl:0x3febdfd71198 "GEOMETRYCOLLECTION EMPTY">

Would be great to see this fixed! ๐Ÿ‘

pnull commented

Would be cool to have this fixed

Just fixed this on the usage side by defining a geo_factory like this:

geo_factory = RGeo::Cartesian.simple_factory(srid: 4326)
RGeo::GeoJSON.decode(geojson_string, geo_factory: geo_factory, json_parser: :json)
 => #<RGeo::Cartesian::GeometryCollectionImpl:0x3fc223fdb428 "GEOMETRYCOLLECTION (MULTIPOLYGON (((9.4834096 48.7080301, 9.4834569 48.7079371, ...

Well, this didn't really solve the problem. I still can't convert the cartesian factory version to a Geos factory which I need though in order to use methods like contains?. Here's a rspec test case where I can reproduce the problem:

it "reproduces the issue for fetching data via GeoJSON" do
  geojson_data = HTTP.get("http://polygons.openstreetmap.fr/get_geojson.py?id=51477&params=0.010000-0.005000-0.005000").to_s.strip
  geo_factory = ::RGeo::Cartesian.preferred_factory(srid: 4326)
  geom_collection = RGeo::GeoJSON.decode(geojson_data, geo_factory: geo_factory, json_parser: :json)
  germany = geom_collection.first

  %w(62518 191645).map do |osm_id| # Karlsruhe, Dresden
    geojson_data = HTTP.get("http://polygons.openstreetmap.fr/get_geojson.py?id=#{osm_id}&params=0").to_s.strip
    geom_collection = RGeo::GeoJSON.decode(geojson_data, geo_factory: geo_factory, json_parser: :json)
    expect(geom_collection.is_empty?).to be false
  end
end

Note that this test includes two regions, one of them (Karlsruhe) working just fine and the other (Dresden) failing. I don't know what the issue is, but it would be great if this could be fixed.

My guess is that too big GeoJSON data (like Dresden with >5k points) could to be the problem.

@Dschee I ran into this same problem using data from Airbnb, and solved it like so:

  geo_factory = RGeo::Cartesian.simple_factory(uses_lenient_assertions: true)
  feature_collection = RGeo::GeoJSON.decode(data, json_parser: :json, geo_factory: geo_factory)

I have some areas like Chelsea, NY which don't satisfy the validations that rgeo is performing in https://github.com/rgeo/rgeo/blob/master/lib/rgeo/impl_helper/basic_line_string_methods.rb#L135-L137

I'm not actually sure what the end impact of disabling assertions but seems rgeo is overly strict.

The RGeo::Cartesian::Factory hides all exceptions interestingly

@superslau Thank you for sharing your solution. I will consider adding it as a fallback option โ€“ broken data should be still better than no data at all. ;)

Edited

@Jeehut thanks for the clear and reproducible test ! However, it looks like I'm too late to the party, and now the polygons work.

And it seems like indeed @superslau's solution is the way to go, for now. I'm closing this PR in favor
of #33, because they look like the same issue ! If not, please open a new PR ๐Ÿ™‚

@BuonOmo Thanks for taking a look at this, but since this is 4 years old, I don't even remember which project I came across this problem. But good to hear that it's probably solved. :)