2

I am trying to configure JSTS to load using ES6 modules so that I don't have to include the entire library.

There are minimal examples of how to do this online, including using an extend function to pull in functionality, however all attempts at getting the isValid method to work have failed:

import extend from 'jsts/extend';
import Geometry from 'jsts/org/locationtech/jts/geom/Geometry';
import Valid from 'jsts/org/locationtech/jts/operation/valid';
import WKTReader from 'jsts/org/locationtech/jts/io/WKTReader';    

    extend(Geometry.prototype, {
        buffer: function () {
            return Valid.isValid(this, ...arguments);
        }
    })


    let reader = new WKTReader();
    var geom = reader.read('POLYGON ((80 300, 280 300, 280 80, 80 80, 80 300), (260 280, 180 200, 100 280, 100 100, 260 100, 260 280))');        
    console.log(geom.isValid());

Each time all I am getting back is: Uncaught TypeError: geom.isValid is not a function

Example JSTS ES6 implementation: https://github.com/DenisCarriere/jsts-es6-example

2
  • Check if geom is actually a geometry Commented Mar 29, 2018 at 9:47
  • Yes the geom above is valid geometry.
    – Alex
    Commented Mar 29, 2018 at 9:48

3 Answers 3

2

When you run

extend(Geometry.prototype, {
    buffer: function () {
        return Valid.isValid(this, ...arguments);
    }
})

You're adding the isValid method to Geometry, but calling it buffer.

Change it to be

extend(Geometry.prototype, {
    isValid: function () {
        return Valid.isValid(this, ...arguments);
    }
})
1

I'd like to point out that checking validity with JSTS is about geometry fx. to detect self-intersections in polygons. It has nothing to do with WKT.

The WKTReader in JSTS assumes valid WKT and will throw on invalid WKT.

Also, the import in the above example is wrong. It should be:

import IsValidOp from 'jsts/org/locationtech/jts/operation/valid/IsValidOp'
0

I have gotten it to work to some extent using the following:

    import WKTReader from 'jsts/org/locationtech/jts/io/WKTReader';
    import Valid from 'jsts/org/locationtech/jts/operation/valid/IsValidOp';

    const valid = new Valid();

    let reader = new WKTReader();
    var geom = reader.read('POLYGON ((80 300, 280 300, 280 80, 80 80, 80 300), (260 280, 180 200, 100 280, 100 100, 260 100, 260 280))');      

    console.log('Valid: ', valid.isValid(geom));

This now returns 'true' for the above Polygon. However if there an error in the Polygon then JSTS just throws an uncaught error, so catching errors from the library is my next issue...

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.