3

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

  • Check if geom is actually a geometry – bugmenot123 Mar 29 '18 at 9:47
  • Yes the geom above is valid geometry. – Alex Mar 29 '18 at 9:48
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 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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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