Introduction to open source front-end GIS spatial analysis library (2) Combination of jsts and ol

foreword

openlayers is a heavy-duty front-end GIS visualization library, but it basically does not have the ability to perform spatial analysis operations. For example, such as:
1. Judging whether a line intersects with a line
2. Whether a line passes through a polygon
These typical spatial judgments are actually not available in ol. So this is sometimes embarrassing.

jsts is a front-end GIS spatial analysis library, which can make up for the shortcomings of openlayers in terms of capabilities. The following introduces the usage of openlayers in jsts.

1 html used in

If it is used in html, just add the references of these two libraries in the script tag.

<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jsts/2.6.1/jsts.min.js"></script>

2 Geometric object conversion

The parser for the geometry type in ol is provided in jsts. The usage is as follows:

var parser = new jsts.io.OL3Parser();
parser.inject(
  ol.geom.Point,
  ol.geom.LineString,
  ol.geom.LinearRing,
  ol.geom.Polygon,
  ol.geom.MultiPoint,
  ol.geom.MultiLineString,
  ol.geom.MultiPolygon
);

That is, inject the geometry class in ol into jsts.
In this way, the geometry object of ol and the geometry object of jsts can be converted to each other. Take a look at two simple conversion examples.

ol to jsts

var olPoint = new ol.geom.Point([1,1])
var jstsPoint = parser.read(olPoint)
console.log(jstsPoint.getCoordinate())

jsts to ol

var wktReader = new jsts.io.WKTReader()
var jstsLine = wktReader.read('LINESTRING (1 1, 1 2, 1 3, 1 4)')
var olLine = parser.write(jstsLine)
console.log(olLine.getCoordinates()) 

3 References in front-end modular projects

The relevant classes in ol are imported normally. In the jsts library, it seems that all the core class modules are in the 'jsts/org/locationtech/jts' directory, so import the required classes from this path. For
example

import OL3Parser from 'jsts/org/locationtech/jts/io/OL3Parser'
import OverlayOp from 'jsts/org/locationtech/jts/operation/overlay/OverlayOp'
import DistanceOp from 'jsts/org/locationtech/jts/operation/distance/DistanceOp'

If you want to convert the geometric object of ol to jsts, remember to inject first.

Then, by converting the ol geometric object into the geometric object in jsts, the spatial analysis ability of jsts can be used to make up for the lack of spatial analysis ability of ol.

References in this article:

1. Openlayers official website jsts example

Guess you like

Origin blog.csdn.net/u012413551/article/details/117234002