Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising InvalidGeometry for a valid one geometry #194

Closed
lucasgomide opened this issue Jul 29, 2018 · 5 comments
Closed

Raising InvalidGeometry for a valid one geometry #194

lucasgomide opened this issue Jul 29, 2018 · 5 comments

Comments

@lucasgomide
Copy link

I got this error RGeo::Error::InvalidGeometry: LinearRing failed ring test

after run it: linear_ring(points)

I didn't understand this error. I've already validated the GeoJSON data, and its valid.

Following data:

{
      "type" => "MultiPolygon",
      "coordinates" => [[[[-43.93918, -19.86877],
      [-43.93462, -19.86751],
      [-43.92989, -19.86586],
      [-43.93463, -19.85896],
      [-43.93544, -19.85711],
      [-43.93523, -19.85598],
      [-43.93613, -19.85486],
      [-43.93613, -19.85482],
      [-43.93615, -19.85502],
      [-43.93595, -19.85514],
      [-43.93598, -19.85562],
      [-43.93557, -19.85225],
      [-43.9357, -19.85345],
      [-43.93523, -19.85206],
      [-43.9337, -19.8502],
      [-43.93304, -19.84768],
      [-43.93346, -19.84467],
      [-43.9356, -19.84139],
      [-43.93748, -19.83908],
      [-43.938, -19.83863],
      [-43.93849, -19.83838],
      [-43.93915, -19.8377],
      [-43.94054, -19.83476],
      [-43.94083, -19.83269],
      [-43.94159, -19.8309],
      [-43.94379, -19.82638],
      [-43.94586, -19.82163],
      [-43.9498, -19.81362],
      [-43.95158, -19.80659],
      [-43.97759, -19.80085],
      [-44.00359, -19.80287],
      [-44.01354, -19.82161],
      [-44.01458, -19.85648],
      [-44.00569, -19.86225],
      [-43.99123, -19.87779],
      [-43.97657, -19.88014],
      [-43.96242, -19.87814],
      [-43.95292, -19.87739],
      [-43.95022, -19.87477],
      [-43.94938, -19.87218],
      [-43.93918, -19.86877]]]]
    }

Providing few points like:

{
      "type" => "MultiPolygon",
      "coordinates" => [[[[-43.93918, -19.86877],
      [-43.93462, -19.86751],
      [-43.92989, -19.86586],
      [-43.93463, -19.85896],
      [-43.93544, -19.85711],
      [-43.93523, -19.85598],
      [-43.93613, -19.85486],
      [-43.93613, -19.85482]]]]
    }

no error messages was raised.

Any ideas ?

@dramalho
Copy link

Yeah, I'm getting the same with my dataset (it's large, but it failed on the first example)

{
    "type": "MultiPolygon",
    "coordinates": [
      [
        [
          [
            42.322,
            -0.659
          ],
          [
            42.32,
            -0.641
          ],
          [
            42.347,
            -0.623
          ],
          [
            42.334,
            -0.658
          ],
          [
            42.322,
            -0.659
          ]
        ]
      ],
      [
        [
          [
            42.25,
            -0.751
          ],
          [
            42.25,
            -0.744
          ],
          [
            42.256,
            -0.741
          ],
          [
            42.257,
            -0.748
          ],
          [
            42.25,
            -0.751
          ]
        ]
      ],
      [
        [
          [
            42.262,
            -0.746
          ],
          [
            42.273,
            -0.727
          ],
          [
            42.298,
            -0.71
          ],
          [
            42.262,
            -0.746
          ]
        ]
      ],
      [
        [
          [
            42.114,
            -0.916
          ],
          [
            42.11,
            -0.91
          ],
          [
            42.117,
            -0.905
          ],
          [
            42.118,
            -0.907
          ],
          [
            42.114,
            -0.916
          ]
        ]
      ],
      [
        [
          [
            42.129,
            -0.902
          ],
          [
            42.171,
            -0.848
          ],
          [
            42.228,
            -0.799
          ],
          [
            42.144,
            -0.895
          ],
          [
            42.129,
            -0.902
          ]
        ]
      ],
      [
        [
          [
            42.229,
            -0.79
          ],
          [
            42.224,
            -0.788
          ],
          [
            42.238,
            -0.776
          ],
          [
            42.229,
            -0.79
          ]
        ]
      ],
      [
        [
          [
            41.923,
            -1.163
          ],
          [
            41.917,
            -1.156
          ],
          [
            41.945,
            -1.131
          ],
          [
            41.941,
            -1.145
          ],
          [
            41.923,
            -1.163
          ]
        ]
      ],
      [
        [
          [
            41.974,
            -1.086
          ],
          [
            41.983,
            -1.071
          ],
          [
            41.991,
            -1.07
          ],
          [
            41.992,
            -1.077
          ],
          [
            41.974,
            -1.086
          ]
        ]
      ],
      [
        [
          [
            42.024,
            -1.022
          ],
          [
            42.016,
            -1.011
          ],
          [
            42.053,
            -0.981
          ],
          [
            42.035,
            -1.016
          ],
          [
            42.024,
            -1.022
          ]
        ]
      ],
      [
        [
          [
            42.1,
            -0.948
          ],
          [
            42.097,
            -0.94
          ],
          [
            42.106,
            -0.928
          ],
          [
            42.106,
            -0.936
          ],
          [
            42.1,
            -0.948
          ]
        ]
      ],
      [
        [
          [
            42.543,
            -0.394
          ],
          [
            42.52,
            -0.377
          ],
          [
            42.178,
            -0.81
          ],
          [
            42.066,
            -0.842
          ],
          [
            42.102,
            -0.862
          ],
          [
            42.023,
            -0.99
          ],
          [
            41.964,
            -1.008
          ],
          [
            41.893,
            -1.21
          ],
          [
            41.783,
            -1.153
          ],
          [
            41.837,
            -1.25
          ],
          [
            41.559,
            -1.662
          ],
          [
            41.559,
            -1.598
          ],
          [
            40.994,
            -0.84
          ],
          [
            40.994,
            2.823
          ],
          [
            41.331,
            3.158
          ],
          [
            41.95,
            4.063
          ],
          [
            42.079,
            4.179
          ],
          [
            42.541,
            4.211
          ],
          [
            42.844,
            4.284
          ],
          [
            43.126,
            4.635
          ],
          [
            43.531,
            4.817
          ],
          [
            43.999,
            4.957
          ],
          [
            44.97,
            4.913
          ],
          [
            48.001,
            8.001
          ],
          [
            47.001,
            8.001
          ],
          [
            43.995,
            9.003
          ],
          [
            43.643,
            9.357
          ],
          [
            43.462,
            9.416
          ],
          [
            43.396,
            9.554
          ],
          [
            43.299,
            9.606
          ],
          [
            43.255,
            9.847
          ],
          [
            43.085,
            9.905
          ],
          [
            43.009,
            10.086
          ],
          [
            42.854,
            10.219
          ],
          [
            42.799,
            10.452
          ],
          [
            42.671,
            10.617
          ],
          [
            42.91,
            10.879
          ],
          [
            43.259,
            11.461
          ],
          [
            43.305,
            11.473
          ],
          [
            43.332,
            11.424
          ],
          [
            43.455,
            11.348
          ],
          [
            43.505,
            11.372
          ],
          [
            43.515,
            11.195
          ],
          [
            43.916,
            10.713
          ],
          [
            44.306,
            10.423
          ],
          [
            44.591,
            10.378
          ],
          [
            44.959,
            10.414
          ],
          [
            45.233,
            10.56
          ],
          [
            45.32,
            10.661
          ],
          [
            45.462,
            10.665
          ],
          [
            45.805,
            10.869
          ],
          [
            45.979,
            10.778
          ],
          [
            46.237,
            10.783
          ],
          [
            46.341,
            10.694
          ],
          [
            46.433,
            10.682
          ],
          [
            46.666,
            10.747
          ],
          [
            47.1,
            10.988
          ],
          [
            47.149,
            11.063
          ],
          [
            47.414,
            11.178
          ],
          [
            47.536,
            11.182
          ],
          [
            47.698,
            11.091
          ],
          [
            48.149,
            11.129
          ],
          [
            48.354,
            11.27
          ],
          [
            48.521,
            11.31
          ],
          [
            48.658,
            11.326
          ],
          [
            48.957,
            11.241
          ],
          [
            49.425,
            11.337
          ],
          [
            49.571,
            11.454
          ],
          [
            49.946,
            11.482
          ],
          [
            49.937,
            11.513
          ],
          [
            50.075,
            11.508
          ],
          [
            50.269,
            11.587
          ],
          [
            50.476,
            11.714
          ],
          [
            50.579,
            11.911
          ],
          [
            50.806,
            11.979
          ],
          [
            50.775,
            11.988
          ],
          [
            50.797,
            11.987
          ],
          [
            50.861,
            11.946
          ],
          [
            50.975,
            11.92
          ],
          [
            51.06,
            11.871
          ],
          [
            51.28,
            11.84
          ],
          [
            51.257,
            11.679
          ],
          [
            51.125,
            11.5
          ],
          [
            51.081,
            11.315
          ],
          [
            51.094,
            11.181
          ],
          [
            51.183,
            11.143
          ],
          [
            51.119,
            10.958
          ],
          [
            51.172,
            10.576
          ],
          [
            51.029,
            10.448
          ],
          [
            51.047,
            10.411
          ],
          [
            51.235,
            10.433
          ],
          [
            51.187,
            10.544
          ],
          [
            51.392,
            10.478
          ],
          [
            51.405,
            10.416
          ],
          [
            51.375,
            10.364
          ],
          [
            51.272,
            10.376
          ],
          [
            51.249,
            10.423
          ],
          [
            51.107,
            10.41
          ],
          [
            50.899,
            10.312
          ],
          [
            50.9,
            9.994
          ],
          [
            50.802,
            9.585
          ],
          [
            50.829,
            9.419
          ],
          [
            50.657,
            9.207
          ],
          [
            50.631,
            9.064
          ],
          [
            50.442,
            8.899
          ],
          [
            50.328,
            8.542
          ],
          [
            50.129,
            8.197
          ],
          [
            49.82,
            7.923
          ],
          [
            49.823,
            7.737
          ],
          [
            49.211,
            6.745
          ],
          [
            49.065,
            6.245
          ],
          [
            48.916,
            5.928
          ],
          [
            48.205,
            4.903
          ],
          [
            47.96,
            4.474
          ],
          [
            46.686,
            3.083
          ],
          [
            46.023,
            2.439
          ],
          [
            44.764,
            1.711
          ],
          [
            44.32,
            1.375
          ],
          [
            43.448,
            0.607
          ],
          [
            42.613,
            -0.269
          ],
          [
            42.543,
            -0.394
          ]
        ],
        [
          [
            50.765,
            11.97
          ],
          [
            50.764,
            11.97
          ],
          [
            50.765,
            11.97
          ],
          [
            50.765,
            11.97
          ]
        ],
        [
          [
            50.764,
            11.97
          ],
          [
            50.763,
            11.97
          ],
          [
            50.764,
            11.968
          ],
          [
            50.764,
            11.97
          ],
          [
            50.764,
            11.97
          ]
        ],
        [
          [
            50.77,
            11.974
          ],
          [
            50.768,
            11.972
          ],
          [
            50.77,
            11.974
          ],
          [
            50.77,
            11.974
          ]
        ]
      ],
      [
        [
          [
            45.006,
            10.448
          ],
          [
            45.003,
            10.452
          ],
          [
            45.006,
            10.448
          ],
          [
            45.006,
            10.448
          ]
        ]
      ],
      [
        [
          [
            43.477,
            11.363
          ],
          [
            43.463,
            11.36
          ],
          [
            43.468,
            11.362
          ],
          [
            43.477,
            11.363
          ]
        ]
      ],
      [
        [
          [
            43.477,
            11.363
          ],
          [
            43.478,
            11.363
          ],
          [
            43.478,
            11.363
          ],
          [
            43.477,
            11.363
          ]
        ]
      ],
      [
        [
          [
            43.496,
            11.374
          ],
          [
            43.487,
            11.369
          ],
          [
            43.495,
            11.375
          ],
          [
            43.496,
            11.374
          ]
        ]
      ],
      [
        [
          [
            43.496,
            11.374
          ],
          [
            43.497,
            11.374
          ],
          [
            43.497,
            11.374
          ],
          [
            43.496,
            11.374
          ]
        ]
      ],
      [
        [
          [
            43.455,
            11.421
          ],
          [
            43.459,
            11.446
          ],
          [
            43.473,
            11.446
          ],
          [
            43.472,
            11.433
          ],
          [
            43.455,
            11.421
          ]
        ]
      ],
      [
        [
          [
            43.335,
            11.448
          ],
          [
            43.356,
            11.467
          ],
          [
            43.364,
            11.495
          ],
          [
            43.36,
            11.464
          ],
          [
            43.335,
            11.448
          ]
        ]
      ],
      [
        [
          [
            50.759,
            11.969
          ],
          [
            50.758,
            11.969
          ],
          [
            50.761,
            11.971
          ],
          [
            50.759,
            11.969
          ]
        ]
      ],
      [
        [
          [
            43.451,
            11.491
          ],
          [
            43.452,
            11.5
          ],
          [
            43.465,
            11.511
          ],
          [
            43.466,
            11.509
          ],
          [
            43.451,
            11.491
          ]
        ]
      ]
    ]
}

GeoJSON is valid and I know which point throws it off - decoded is a list of RGeo::Geos::CAPIPolygonImpl I got by individually decoding each polygon

this is fine

a = RGeo::Cartesian.preferred_factory.multi_polygon(decoded.first(13))
=> #<RGeo::Geos::CAPIMultiPolygonImpl:0x3ff4d9224580 "MULTIPOLYGON

This is the end of the earth as we know it

a = RGeo::Cartesian.preferred_factory.multi_polygon(decoded.first(14))
Traceback (most recent call last):
        1: from (irb):84
RGeo::Error::InvalidGeometry (Parse error)

The last point that works and the one that breaks it look like

ap decoded[12..13]
[
    [0] #<RGeo::Geos::CAPIPolygonImpl:0x3ff4d91c6b74 "POLYGON ((43.477 11.363, 43.463 11.36, 43.468 11.362, 43.477 11.363))">,
    [1] #<RGeo::Geos::CAPIPolygonImpl:0x3ff4d91c65fc "POLYGON ((43.477 11.363, 43.478 11.363, 43.478 11.363, 43.477 11.363))">
]

@dramalho
Copy link

I have a hard time getting to the point where I understand why the geometry is incorrect or how to debug this.

I'm using the CAPIFactory because 🤷‍♂ , it's the default one.

Given this issue has been open for almost a year I am going to assume there's no active maintenance here, but if there is I can cook up a better example - data came from Geonames in GeoJSON format, this is just the end of the long series of attempts to debug this, but the result is pretty much the same as doing

irb(main):090:0> data
=> {"type"=>"MultiPolygon", "coordinates"=>[[[[42.322, -0.659], [42.32, -0.641], [42.347, -0.623], [42.334, -0.658], [42.322, -0.659]]], [[[42.25, -0.751], [42.25, -0.744], [42.256, -0.741], [42.257, -0.748], [42.25, -0.751]]], [[[42.262, -0.746], [42.273, -0.727], [42.298, -0.71], [42.262, -0.746]]], [[[42.114, -0.916], [42.11, -0.91], [42.117, -0.905], [42.118, -0.907], [42.114, -0.916]]], [[[42.129, -0.902], [42.171, -0.848], [42.228, -0.799], [42.144, -0.895], [42.129, -0.902]]], [[[42.229, -0.79], [42.224, -0.788], [42.238, -0.776], [42.229, -0.79]]], [[[41.923, -1.163], [41.917, -1.156], [41.945, -1.131], [41.941, -1.145], [41.923, -1.163]]], [[[41.974, -1.086], [41.983, -1.071], [41.991, -1.07], [41.992, -1.077], [41.974, -1.086]]], [[[42.024, -1.022], [42.016, -1.011], [42.053, -0.981], [42.035, -1.016], [42.024, -1.022]]], [[[42.1, -0.948], [42.097, -0.94], [42.106, -0.928], [42.106, -0.936], [42.1, -0.948]]], [[[42.543, -0.394], [42.52, -0.377], [42.178, -0.81], [42.066, -0.842], [42.102, -0.862], [42.023, -0.99], [41.964, -1.008], [41.893, -1.21], [41.783, -1.153], [41.837, -1.25], [41.559, -1.662], [41.559, -1.598], [40.994, -0.84], [40.994, 2.823], [41.331, 3.158], [41.95, 4.063], [42.079, 4.179], [42.541, 4.211], [42.844, 4.284], [43.126, 4.635], [43.531, 4.817], [43.999, 4.957], [44.97, 4.913], [48.001, 8.001], [47.001, 8.001], [43.995, 9.003], [43.643, 9.357], [43.462, 9.416], [43.396, 9.554], [43.299, 9.606], [43.255, 9.847], [43.085, 9.905], [43.009, 10.086], [42.854, 10.219], [42.799, 10.452], [42.671, 10.617], [42.91, 10.879], [43.259, 11.461], [43.305, 11.473], [43.332, 11.424], [43.455, 11.348], [43.505, 11.372], [43.515, 11.195], [43.916, 10.713], [44.306, 10.423], [44.591, 10.378], [44.959, 10.414], [45.233, 10.56], [45.32, 10.661], [45.462, 10.665], [45.805, 10.869], [45.979, 10.778], [46.237, 10.783], [46.341, 10.694], [46.433, 10.682], [46.666, 10.747], [47.1, 10.988], [47.149, 11.063], [47.414, 11.178], [47.536, 11.182], [47.698, 11.091], [48.149, 11.129], [48.354, 11.27], [48.521, 11.31], [48.658, 11.326], [48.957, 11.241], [49.425, 11.337], [49.571, 11.454], [49.946, 11.482], [49.937, 11.513], [50.075, 11.508], [50.269, 11.587], [50.476, 11.714], [50.579, 11.911], [50.806, 11.979], [50.775, 11.988], [50.797, 11.987], [50.861, 11.946], [50.975, 11.92], [51.06, 11.871], [51.28, 11.84], [51.257, 11.679], [51.125, 11.5], [51.081, 11.315], [51.094, 11.181], [51.183, 11.143], [51.119, 10.958], [51.172, 10.576], [51.029, 10.448], [51.047, 10.411], [51.235, 10.433], [51.187, 10.544], [51.392, 10.478], [51.405, 10.416], [51.375, 10.364], [51.272, 10.376], [51.249, 10.423], [51.107, 10.41], [50.899, 10.312], [50.9, 9.994], [50.802, 9.585], [50.829, 9.419], [50.657, 9.207], [50.631, 9.064], [50.442, 8.899], [50.328, 8.542], [50.129, 8.197], [49.82, 7.923], [49.823, 7.737], [49.211, 6.745], [49.065, 6.245], [48.916, 5.928], [48.205, 4.903], [47.96, 4.474], [46.686, 3.083], [46.023, 2.439], [44.764, 1.711], [44.32, 1.375], [43.448, 0.607], [42.613, -0.269], [42.543, -0.394]], [[50.765, 11.97], [50.764, 11.97], [50.765, 11.97], [50.765, 11.97]], [[50.764, 11.97], [50.763, 11.97], [50.764, 11.968], [50.764, 11.97], [50.764, 11.97]], [[50.77, 11.974], [50.768, 11.972], [50.77, 11.974], [50.77, 11.974]]], [[[45.006, 10.448], [45.003, 10.452], [45.006, 10.448], [45.006, 10.448]]], [[[43.477, 11.363], [43.463, 11.36], [43.468, 11.362], [43.477, 11.363]]], [[[43.477, 11.363], [43.478, 11.363], [43.478, 11.363], [43.477, 11.363]]], [[[43.496, 11.374], [43.487, 11.369], [43.495, 11.375], [43.496, 11.374]]], [[[43.496, 11.374], [43.497, 11.374], [43.497, 11.374], [43.496, 11.374]]], [[[43.455, 11.421], [43.459, 11.446], [43.473, 11.446], [43.472, 11.433], [43.455, 11.421]]], [[[43.335, 11.448], [43.356, 11.467], [43.364, 11.495], [43.36, 11.464], [43.335, 11.448]]], [[[50.759, 11.969], [50.758, 11.969], [50.761, 11.971], [50.759, 11.969]]], [[[43.451, 11.491], [43.452, 11.5], [43.465, 11.511], [43.466, 11.509], [43.451, 11.491]]]]}
irb(main):091:0> geom = RGeo::GeoJSON.decode(data)
Traceback (most recent call last):
        1: from (irb):91
RGeo::Error::InvalidGeometry (Parse error)

@dramalho
Copy link

🤔

ok, I was looking at the CAPI factory and there's something that seemed to work - namely the uses_lenient_assertions flag

This seemed to work

RGeo::Geos::CAPIFactory.create(uses_lenient_assertions: true).multi_polygon(decoded)
=> #<RGeo::Geos::CAPIMultiPolygonImpl:0x3ff4d91962d0 "MULTIPOLYGON (((42.322 -0.659, 42.32 -0.641, 42.347 -

I'll validate the data I want (envelope for instance) but I guess that flag is helpful in some scenarios? 🤷‍♂

@pedros007
Copy link

@dramalho I copied your data into http://geojsonlint.com/ and got

Line 1: Polygons and MultiPolygons should follow the right-hand rule

GeoJSON RFC 7946 states

A linear ring MUST follow the right-hand rule with respect to the area it bounds

Despite this, most software I've used seems to render Polygon/MultiPolygons with left-hand linear rings. I'm guessing uses_lenient_assertions skips the left vs right winding check.

If you're ever bored, I've found More than you ever wanted to know about GeoJSON to be a helpful resource which summarizes lots of details on the GeoJSON spec.

@keithdoggett
Copy link
Member

It looks like both of the geometries posted have at least one self-intersection in them, which is causing the factories to raise InvalidGeometry errors. While the GeoJSON data itself is valid and properly formatted, the multipolygons are not.

As @dramalho pointed out, you can skip this check by passing the uses_lenient_assertions flag to the factory.

I'm going to close this since this is the expected behavior of the factories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants