Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Within Expression for Polygon & Polygon Centroid #9795

Open
strech345 opened this issue Jun 16, 2020 · 11 comments
Open

Feature: Within Expression for Polygon & Polygon Centroid #9795

strech345 opened this issue Jun 16, 2020 · 11 comments

Comments

@strech345
Copy link

Motivation

Would be nice to style or filter all polygons which are inside a specific polygon.
Within means the polygon is full inside of the other. But for a polygon selection it looks like more correct when only the centroid of the polygon is within the other.

So it would be nice not only to use within with a polygon but also with it centroid.

Otherwise if the calculation of centroid is to heavy, it would be nice to precalculate the centroid, load it into the mbtile as property and then use it for the within. This means using the within with properties.

@ryanhamley
Copy link
Contributor

This is a request to extend #9352 to work with polygons within polygons in addition to points and lines within polygons. cc @zmiao

@markusand
Copy link

Sorry I opened a similar request in #10479. I reference it here to track them together.

This feature is very useful, and I'm curious why Polygon check was left out. It should not be too hard to implement as long as Polygon perimeter can be considered a LineString.

I'm also wondering if a less strict interpretation of what within means (I'm thinking in partially contained) would open more use cases.

@zmiao
Copy link
Contributor

zmiao commented Apr 8, 2021

I am now looking into this issue. I will update once I have a solution for it.

@markusand
Copy link

'distance' expression in this pull request

#10616

would be a solution to this issue. It's not a strict 'within' expression, but distance is 0 if two polygons intersect or one is within another.

@enersis-pst
Copy link
Contributor

@zmiao
Thanks for that work.
Will it also work with Holes in Polygons?

image

{
  "type": "MultiPolygon",
  "coordinates": [
    [
      [
        [
          -47.900390625,
          -14.944784875088372
        ],
        [
          -51.591796875,
          -19.91138351415555
        ],
        [
          -41.11083984375,
          -21.309846141087192
        ],
        [
          -43.39599609375,
          -15.390135715305204
        ],
        [
          -47.900390625,
          -14.944784875088372
        ]
      ],
      [
        [
          -46.6259765625,
          -17.14079039331664
        ],
        [
          -47.548828125,
          -16.804541076383455
        ],
        [
          -46.23046874999999,
          -16.699340234594537
        ],
        [
          -45.3515625,
          -19.31114335506464
        ],
        [
          -46.6259765625,
          -17.14079039331664
        ]
      ],
      [
        [
          -44.40673828125,
          -18.375379094031825
        ],
        [
          -44.4287109375,
          -20.097206227083888
        ],
        [
          -42.9345703125,
          -18.979025953255267
        ],
        [
          -43.52783203125,
          -17.602139123350838
        ],
        [
          -44.40673828125,
          -18.375379094031825
        ]
      ]
    ]
  ]
}

@zmiao
Copy link
Contributor

zmiao commented Aug 18, 2021

@enersis-pst are you referring to the distance expression? If so, in principle it would work as @markusand mentioned above. I just tried a very simple test case with the polygon you provided:

                    {
                        id: "polygons",
                        type: "fill",
                        source: "rectangles",
                        paint: {
                            "fill-color": [
                                "case",
                                [">", ["distance", polygonWithHole], 0],
                                "red",
                                "blue",
                            ],
                            "fill-opacity": 0.2,
                        },
                    },

and it looks like:
Screen Shot 2021-08-18 at 9 23 05 PM

@enersis-pst
Copy link
Contributor

enersis-pst commented Aug 19, 2021

@zmiao yes. looks good. Thanks for that example. Nice and very flexible feature. Hope it will be merged soon.

@mkandarEsource
Copy link

Is there any status update on this feature. Just ran into a need for it today.

@zmiao
Copy link
Contributor

zmiao commented Dec 18, 2024

@mkandarEsource we've introduced the distance expression: https://docs.mapbox.com/style-spec/reference/expressions/#distance, would this fit for your need as a workaround?

@mkandarEsource
Copy link

mkandarEsource commented Dec 19, 2024

@zmiao Possibly, let me explain the use case. We have a snipping tool on the map we're building. The user draws a polygon and then the features within that polygon are filtered in. Issues we've run into so far:

  1. Within expression cannot seemingly be combined with other filters in an 'any' or 'all'. We receive an error that it is not one of the valid logical expressions.
  2. Within doesn't work with polygon features

If a distance of 0 entails that the feature is within (or likely intersected by) the polygon drawn by our snipping tool, we can work with that. There isn't a hard requirement to do "within" so an "intersects" would work. I'll test it out and let you know.

@mkandarEsource
Copy link

@zmiao That does work as a geo intersects, which is sufficient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants