ol3 and jsts – feature overlap in layer

The following function will help you check if there is an overlap between a certain feature and a layer. In order to work, the example needs ol3 and the JSTS library loaded.

/*
 * The main object
 */
var factoryJsts = new jsts.geom.GeometryFactory();

var ol3JstsIntersect = {
	// Create the JSTS geometry
	createJstsFromFeature: function (feature) {
		var coord = feature.getGeometry().getLinearRing().getCoordinates(),
			out = [];

		for (a = 0; a < coord.length; a++) {
			out.push(new jsts.geom.Coordinate(coord[a][0], coord[a][1]));
		}

		return factoryJsts.createPolygon(factoryJsts.createLinearRing(out));
	},
	
	// Check the overlap
	check: function (feature, layerSource) {
		var features = layerSource.getFeatures(),
			status = true,
			poly1,
			poly2;

		// The checked polygon
		poly1 = ol3JstsIntersect.createJstsFromFeature(feature);
		
		// Iterate through the existing polygons
		for (var a in features) {
			if (features.hasOwnProperty(a)) {
				if (features[a].getId() !== feature.getId()) {
					// Check the intersection
					poly2 = ol3JstsIntersect.createJstsFromFeature(features[a]);

					if (poly1.intersects(poly2)) {
						status = false;
					}
				}
			}
		}
		
		// Return the overlap status
		return status;
	}
};

// Example call
ol3JstsIntersect.check(feature, theSourceOfTheLayerWithFeatures);
Code language: PHP (php)

The code above can be improved.