繁体   English   中英

将JSTS isValid()与ES6模块一起使用

[英]Using JSTS isValid() with ES6 modules

我正在尝试配置JSTS以使用ES6模块加载,这样我就不必包括整个库。

关于如何在线执行此操作的示例很少,包括使用扩展功能引入功能,但是,使isValid方法正常工作的所有尝试均失败了:

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

每次我回来的都是: 未捕获的TypeError:geom.isValid不是一个函数

JSTS ES6实现示例: https : //github.com/DenisCarriere/jsts-es6-example

我使用以下方法使它在某种程度上起作用:

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

现在,上述多边形返回“ true”。 但是,如果Polygon中存在错误,则JSTS只会引发未捕获的错误,因此从库中捕获错误是我的下一个问题...

您可以这样实现:

此示例与GeoJsonReader一起使用,但与WKTReader相似

    import * as jsts from 'jsts/dist/jsts';

    const reader = new jsts.io.GeoJSONReader();
    const g = reader.read({
      'type': 'Feature',
      'geometry': {
        'type': 'Polygon',
        'coordinates': [
            [-4.724834116462489, 48.54429850016129],
            [-4.806692014663952, 48.35426654640426],
            [-4.54747533707436, 48.36333181438215],
            [-4.533832354037941, 48.24535748577782],
            [-4.724834116462489, 48.54429850016129]
        ]
      }
    });
    if (! g.geometry.isValid()) {
        // Do what you want when error occurs
    }

亚历克斯回答的补充-可能会帮助某人...

输入:

declare namespace jsts {
  namespace operation {
    namespace valid {
      export class IsValidOp {
        constructor(geom: geom.Geometry);

        static isValid(coord?: geom.Coordinate): boolean;
      }
    }
  }
}

declare module 'jsts' {
  export = jsts;
}

用法:

import { IsValidOp as JstsIsValidOp } from 'jsts/org/locationtech/jts/operation/valid';

if (new JstsIsValidOp(jstsHole).isValid()) {
  ...
}

在正确地小部分而不是全局地正确导入JSTS时,请查看operation-namespace / -folder。 特别相关,覆盖和有效目录可能会有所帮助。

导入完整的ES5 / 6版本还将在全球范围内包含monkey.js,它将为Geometry添加便利功能,但会引发循环依赖警告并增加版本大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM