bjornharrtell / jsts

JavaScript Topology Suite
http://bjornharrtell.github.io/jsts/
Other
1.49k stars 232 forks source link

Error: TypeError: Cannot read properties of undefined (reading 'length') #484

Closed motto76 closed 2 years ago

motto76 commented 2 years ago

Hello,

we have a problem with equalsNorm().

In the following test case:

it('check multipolygon and polygon', () => {
  const geomA = createGeometry('MULTIPOLYGON (((0 0, 10 0, 10 10, 0 0)), ((15 5, 15 10, 20 10, 20 5, 15 5)))')
  const geomB = erzeugeGeometrie('POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))')
  const gf = createGeometryFactory(2)
  expect(geomA.equalsNorm(geomB).toBe(false)
})

the following error occurs:

TypeError: Cannot read properties of undefined (reading 'length'). Stack: _at MultiPolygon.call (node_modules/jsts/org/locationtech/jts/geom/GeometryCollection.js:76:67) at MultiPolygon.equalsExact (node_modules/jsts/org/locationtech/jts/geom/MultiPolygon.js:27:32) at MultiPolygon.apply (node_modules/jsts/org/locationtech/jts/geom/Geometry.js:33:35) at MultiPolygon.apply (node_modules/jsts/org/locationtech/jts/geom/GeometryCollection.js:86:32) at MultiPolygon.equalsExact (node_modules/jsts/org/locationtech/jts/geom/MultiPolygon.js:29:32) at MultiPolygon.equalsNorm (nodemodules/jsts/org/locationtech/jts/geom/Geometry.js:46:24)

If you swap the geometries you get another error:

RangeError: Maximum call stack size exceeded at Function.[Symbol.hasInstance] () Stack: _at LinearRing.equalsExact (node_modules/jsts/org/locationtech/jts/geom/LineString.js:43:72) at LinearRing.apply (node_modules/jsts/org/locationtech/jts/geom/Geometry.js:33:35) at LinearRing.equalsExact (node_modules/jsts/org/locationtech/jts/geom/LineString.js:59:32) at LinearRing.apply (node_modules/jsts/org/locationtech/jts/geom/Geometry.js:33:35) at LinearRing.equalsExact (node_modules/jsts/org/locationtech/jts/geom/LineString.js:59:32) at LinearRing.apply (node_modules/jsts/org/locationtech/jts/geom/Geometry.js:33:35) at LinearRing.equalsExact (nodemodules/jsts/org/locationtech/jts/geom/LineString.js:59:32) ...

Cause for case 1: The check in MultiPolygon if (!this.isEquivalentClass(other)) returns true, because the call getClass() returns [class Geometry] for both geometries. (#getClass is implemented only in Geometry and therefore returns

possible Solution: Each geometry class implements getClass Alternative: Instead of getClass, getGeometryType is checked.

bjornharrtell commented 2 years ago

Hmm interesting that this is not caught by existing tests. Perhaps you are the first to use equalsNorm.. ;)

Reproduced with https://github.com/bjornharrtell/jsts/pull/485.

bjornharrtell commented 2 years ago

This regressed in version 2.2.2 due to commit https://github.com/bjornharrtell/jsts/commit/fb3c89b8bb2339ce6692c795e41a957fa17f19f9 back in 2017. I guess it's not that common that people try and compare different geometry types.

bjornharrtell commented 2 years ago

I believe this is fixed now and released as 2.9.3

motto76 commented 2 years ago

Thanks for the quick troubleshooting. We have tested it successfully.