TransWikia.com

Using JSTS isValid() with ES6 modules

Geographic Information Systems Asked on May 4, 2021

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

3 Answers

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...

Answered by Alex on May 4, 2021

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);
    }
})

Answered by Razzi Abuissa on May 4, 2021

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'

Answered by Björn Harrtell on May 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP