0

I need to check if a Polygon is inside another Polygon. In order to do so I iterate over each point of the feature and test if it is inside the main feature. If this is correct for all the points. Then the feature must be inside the main feature.

The problem that I am facing is that sometimes features might be just on the edge or just over the edge of the main feature. Is there a way to add a bit of leeway and not be so strict on which features are inside or not.

I thought about making the main feature just a bit larger. But I don't know how to do this or if this is even possible. I have found an example on how to increase the features: example.

But I don't see any code on how to achieve this. The description describes the use of the geometry.resize method. But in the Openlayers docs there is no resize method in the Geometry class.

  const testIfFeatureInside = (mainFeature, feature) => {
    const mainFeaturePolygone = mainFeature.getGeometry();
    const featureCoords = feature.getGeometry().getCoordinates();

    for (let i = 0; i < featureCoords.length; i++) {
      const coords = featureCoords[i];
      for (let j = 0; j < coords.length; j++) {
        const coord = coords[j];
        if (!mainFeaturePolygone.intersectsCoordinate(coord)) {
          return false;
        }
      }
    }

    return true;
  };
7
  • The assumption is erroneous. Think of a star-shaped polygon and a 2nd square polygon whose vertices are located inside the star "arms". While the 4 vertices are contained inside the main polygon, the square polygon edges would be outside of the main one.
    – JGH
    Jun 14 at 19:20
  • Should you keep this approach, you can compute the distance between the vertex and the main polygon, and give some tolerance there (if distance is less than X meters, consider it is within)
    – JGH
    Jun 14 at 19:21
  • @JGH I know it is not the most correct way to check if a Feature is inside. But it doesn’t need te be 100% correct.
    – Stephen
    Jun 14 at 19:22
  • I would use turf.buffer and turf.booleanContains functions from turf.js library for that.
    – TomazicM
    Jun 14 at 19:27
  • 1
    And by the way, method for resizing polygon is called .scale now.
    – TomazicM
    Jun 14 at 19:31
0

I have fixed the problem by making a clone of the main feature and then scaling it up by a certain size. Then I use this feature to test if other features are inside.

  const testIfFeatureInsidePlot = (mainFeature, feature) => {
    const scaledFeaturePolygone = mainFeature.getGeometry().clone();
    scaledFeaturePolygone.scale(1.005, 1.005);

    const featureCoords = feature.getGeometry().getCoordinates();

    for (let i = 0; i < featureCoords.length; i++) {
      const coords = featureCoords[i];
      for (let j = 0; j < coords.length; j++) {
        const coord = coords[j];
        if (!scaledFeaturePolygone.intersectsCoordinate(coord)) {
          return false;
        }
      }
    }

    return true;
  };
2
  • It would be more efficient to clone the geometry instead of the whole feature.
    – Mike
    Jun 15 at 8:54
  • @Mike Thanks for the tip! I have updated the code.
    – Stephen
    Jun 15 at 8:57

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.