1

I am trying to draw a line on polygon which I want to split, Example: I want to split 616 polygon as show in image

enter image description here

with below code when I tied to get intersected polygon I am getting 616,617,615 and Roads adjacent to 616.But I want only 616 which cut by line

draw.on('drawend', function (evt) {
                    var plcd = [], featureGeom;
                    var justNowFeature = evt.feature;
                    var line = evt.feature.getGeometry();
                     featureGeom = justNowFeature.getGeometry().getCoordinates();

                    console.log("FEATURESGEOJSON  " + featureGeom);
                    var features = plotboundary_layer.getSource().getFeatures();
                    for (var i = 0; i < features.length; i++) {
                        if (line.intersectsExtent(features[i].getGeometry().getExtent())) {
                            plcd.push(features[i].values_.plcd);
                        }
                    }
                });
  • Please ask only one question per question. – TomazicM Jun 24 at 8:42
  • now i changed my question – veena hosur Jun 26 at 12:24
1

There are two problems with your approach. First problem is that you are checking line intersection with extent of the feature, which is smalest rectangle encompassing the whole feature. That's why you are getting roads also.

The second problem is that line crosses also neigboring features, that's why you are getting polygons 615 and 617. This would be solvable if intersectsExtent method would give number of points where line intersects polygon, but it return only true/false.

Library Turf.js can help here. Method turf.lineIntersect gives you geometry which is intersection of line and geometry. There is no extent involved here and from resulting geometry you can count interesecing points. If count is dividable by 2, line croses polygon.

Code would then look something like this:

var geoJSON = new ol.format.GeoJSON();
var plcd = [];

draw.on('drawend', function (evt) {
  var intersectPoints, nPoints, featureGeoJSON, featureGeometry, lineGeoJSON, lineGeometry;
  lineGeoJSON = geoJSON.writeFeatureObject(evt.feature);
  lineGeometry = turf.getGeom(lineGeoJSON);
  var features = plotboundary_layer.getSource().getFeatures();
  for (var i = 0; i < features.length; i++) {
    featureGeoJSON = geoJSON.writeFeatureObject(features[i]);
    featureGeometry = turf.getGeom(featureGeoJSON);
    intersectPoints = turf.lineIntersect(featureGeometry, lineGeometry);
    nPoints = intersectPoints.features.length;
    if ((nPoints > 0) && ((nPoints % 2) == 0)) {
      plcd.push(features[i].values_.plcd);
    }
  }
});
  • I am getting Unknown Geometry Type error – veena hosur Jun 28 at 6:12
  • My mistake, forgot that OpenLayers geometry is not the same as GeoJSON geometry (which is needed by Turf.js). Answer corrected. – TomazicM Jun 28 at 16:22
  • great work.thank you – veena hosur Jun 29 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.