Skip to content

Filter expressions: Is it possible to filter polygons which contain a particular point? #9891

Closed

Description

@atyshka

mapbox-gl-js version: 1.10

Question

I have a layer containing geojson polygons, and I would like to filter it to polygons that contain a certain lat long point. I would like to devise a filter expression that accomplishes this. Looking through the reference of expressions, I see a "within" statement that returns true when a particular point is within a polygon. That is almost what I want, but its first argument is the point, and the second arg is the polygon to search. It is my understanding that when applying a filter to a layer, the item from the layer is going to be inserted at the beginning of the filter expression. This would not work for me then, as it would be trying to determine whether the polygon is within the point, which makes no sense. Is there any way I can manipulate the ordering of the filter expression, perhaps using variables, such that I can perform a query as to whether a particular point lies within a polygon for each polygon in a layer?

Links to related documentation

https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/#within

Activity

  1. ryanhamley commented on Jul 27, 2020

    @ryanhamley
    Contributor

    There isn't a contains expression that's the opposite of within. A possible workaround might be to use queryRenderedFeatures (or querySouceFeatures) to find the features that intersect with a particular point and then use setFeatureState to update the fill-opacity of your polygons, similar to the hover effect example. If you need more guidance, I'd recommend contacting Mapbox Support.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions