Perform complex spatial operations in OpenLayers using JSTS

Image Credit:

OpenLayers is a great open source GIS library which is used to develop a rich featured GIS web applications. But sometime you need to implement advanced spatial/geometrical operations. For example, union of two geometry, creating buffers, creating hole in polygon, dividing a polygon in two parts by a line as shown in following images.

Image Credit: Google Images

But, these advance spatial operations are not supported by OpenLayers. We will use another small open source gis library which can perform these advance operations and it can be easily integrated with OpenLayers.

To support these advance spatial operations, we can use open source gis library known as JSTS(JavaScript Topology Suite) using which we can implement advance spatial operations and this library can be integrated easily with OpenLayers. This library enable implementation of advance spatial operations on front end.

JSTS provide many methods which can perform advance operations. JSTS library can parse OpenLayers geometry object. Let’s create buffer geometry using JSTS.

In following code snippet, we are creating OpenLayers parser at line number 1. At line number 2, we are reading OpenLayer geometry object(You need to pass actual openlayers object). This will convert the OpenLayers geometry object to JSTS object. At line number 3, we are using buffer method to create a buffer by 40 units. At line number 4, we converting JSTS object to OpenLayers type.

We just created buffer geometry of 40 units of OpenLayers geometry using JSTS.

Image Credit: Google Images

JSTS can also read WKT geometry and can perform same advance spatial operations. In following code snippet, we are creating WKT reader. This WKT reader can read, any WKT geometry. Here, we are reading two WKT point geometry and creating a buffer of 40 units. We just used buffer method and creating a circle of 40 units.

Image Credit: Google Images

JSTS is included with many advance spatial functions. But we just did is only the tip of iceberg. You can create much more advance spatial operations. In next post, we will show how to divide a polygon using line.

Leave a ReplyCancel reply