rgeo/rgeo-geojson

decode returns nil

sashazykov opened this issue · 11 comments

RGeo::GeoJSON.decode returns nil for this geojson object:

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          -0.1990879058837436,
          51.477187144041416
        ],
        [
          -0.2087009429931186,
          51.51160110043464
        ],
        [
          -0.1915348052978061,
          51.54342691492687
        ],
        [
          -0.15651588439941405,
          51.56071897849311
        ],
        [
          -0.05651588439941406,
          51.56071897849311
        ],
        [
          -0.019093704223678287,
          51.54769710912033
        ],
        [
          -0.0022708892822720372,
          51.50946436000716
        ],
        [
          -0.017720413208053287,
          51.478470108414456
        ],
        [
          -0.05651588439941406,
          51.46071897849311
        ],
        [
          -0.15651588439941405,
          51.46071897849311
        ],
        [
          -0.1990879058837436,
          51.477187144041416
        ]
      ],
      [
        [
          -0.07509474754328949,
          51.506753202923626
        ],
        [
          -0.07595305442805511,
          51.505273053858204
        ],
        [
          -0.06530280113224762,
          51.50174688870582
        ],
        [
          -0.05860800743107575,
          51.501608592703285
        ],
        [
          -0.047239065170288086,
          51.506897754163354
        ],
        [
          -0.04824938774106613,
          51.521613351060154
        ],
        [
          -0.06485271453857422,
          51.51832211713747
        ],
        [
          -0.07064137458792175,
          51.51599207347446
        ],
        [
          -0.07466559410090667,
          51.51464339775207
        ],
        [
          -0.07342104911799652,
          51.51155315619659
        ],
        [
          -0.07509474754328949,
          51.506753202923626
        ]
      ],
      [
        [
          -0.11228219406325,
          51.500309005889754
        ],
        [
          -0.12699723243713382,
          51.50865565022965
        ],
        [
          -0.12831687927246097,
          51.50922324175514
        ],
        [
          -0.12943267822265628,
          51.508421934016845
        ],
        [
          -0.12951850891113284,
          51.507620612185384
        ],
        [
          -0.12814521789550784,
          51.50708638980146
        ],
        [
          -0.11293737150024191,
          51.500256669057535
        ],
        [
          -0.1151615142822493,
          51.500079000259696
        ],
        [
          -0.12205123901367188,
          51.49633869308233
        ],
        [
          -0.12347106933589203,
          51.48981913832698
        ],
        [
          -0.11729125976557953,
          51.483370456473764
        ],
        [
          -0.10660171508789062,
          51.47968223294051
        ],
        [
          -0.09934539794926422,
          51.485615316067104
        ],
        [
          -0.09608383178715485,
          51.494664629895134
        ],
        [
          -0.10312194824223297,
          51.50104074392209
        ],
        [
          -0.11228219406325,
          51.500309005889754
        ]
      ],
      [
        [
          -0.1337242126464844,
          51.50893655316364
        ],
        [
          -0.12651443481445315,
          51.500174768121695
        ],
        [
          -0.11243820190429688,
          51.495686372106675
        ],
        [
          -0.10179519653320314,
          51.503594201752165
        ],
        [
          -0.12067794799804689,
          51.513637304278454
        ],
        [
          -0.1337242126464844,
          51.50893655316364
        ]
      ]
    ],
    [
      [
        [
          -0.1112794876098633,
          51.50120953016752
        ],
        [
          -0.11227726936340333,
          51.501015848029354
        ],
        [
          -0.1115691661834717,
          51.50054165725099
        ],
        [
          -0.1112794876098633,
          51.50120953016752
        ]
      ]
    ]
  ]
}

It was generated by leaflet and can be parsed by http://geojson.io/ and other tools.

A few extra data points:

  1. I reproduced the problem:

     geom=RGeo::GeoJSON.decode(g, json_parser: :json, geo_factory:  RGeo::Geos.factory(srid: 4326))
    

returns nil with the source geojson

  1. I was able to process this if I remove the last triangle in the MultiPolygon, like this.
  2. geojsonhint doesn't raise any problems. If I'm reading the geojson spec correctly, this MultiPolygon has two Polygons: One with a bunch of holes in it. A second (the triangle).

for me it started working when I added json_parser: :json option to decode

I got the same "error". Any ideas ?

@sheerun still working for you ?

running into this as well

Interestingly if you create a polygon from a LineString, encode it as GeoJSON, rgeo is able to then decode the encoded GeoJSON. If you copy and past that encoded GeoJSON and try to use the string literal, it'll fail.

It's really strange. I have no ideia the reason ):
This issue should be moved to GeoJSON repo as well

@dorthwein It means that probably the problem is rgeo-geojson. Do you mind if you paste here the code that u have made ?

A few extra data points:

  1. I reproduced the problem:
     geom=RGeo::GeoJSON.decode(g, json_parser: :json, geo_factory:  RGeo::Geos.factory(srid: 4326))
    

returns nil with the source geojson

  1. I was able to process this if I remove the last triangle in the MultiPolygon, like this.
  2. geojsonhint doesn't raise any problems. If I'm reading the geojson spec correctly, this MultiPolygon has two Polygons: One with a bunch of holes in it. A second (the triangle).

Thanks for the reproduction.

@keithdoggett this looks like a bug in rgeo. I'm marking this as a bug and make it high priority. I guess the root of that bug is in the create_geometry_collection method.

The given MultiPolygon is not OGC compliant since the first polygon has 2 intersected exclusion rings, because of this libgeos behaviour is undefined.
If you only need to decode the geojson, you can set uses_lenient_assertions to true:

 RGeo::GeoJSON.decode(g, geo_factory:  RGeo::Geos.factory(srid: 4326, uses_lenient_assertions: true))

Thanks @Quiwin, I think we can close that issue!

@keithdoggett having assertions delayed to methods that really need it will fix all those issues altogether.