mapbox / mapbox-gl-js

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
https://docs.mapbox.com/mapbox-gl-js/
Other
10.89k stars 2.19k forks source link

Distance expression operator #6062

Open 1ec5 opened 6 years ago

1ec5 commented 6 years ago

It would be nice to have an expression operator that evaluates to the physical distance between two geographic coordinates. Coupled with an operator that evaluates to the centroid of the current feature and even the ability to apply a gradient to a shape (#4095), it would enable interesting visualizations that draw the viewer’s attention to a particular feature. A more mundane use case would be displaying the distance of each city on the map as you hover over a particular city.

This request is inspired by the distanceToLocation:fromLocation:(there, here) function built into NSExpression on iOS and macOS, which returns the distance in meters from the coordinate there to the coordinate here.

/cc @anandthakker

anandthakker commented 6 years ago

Something like ["geographic_distance", lng1, lat1, lng2, lat2] would definitely be doable. I'm wary, though, of adding any expressions that refer to a feature's geometry, as that could open up a pretty big can of performance worms

gorbypark commented 6 years ago

This should be possible using Turfjs/turf-distance and Turfjs/turf-centroid.

1ec5 commented 6 years ago

I'm wary, though, of adding any expressions that refer to a feature's geometry, as that could open up a pretty big can of performance worms

Something I didn’t consider is that features from vector tiles are… tiled. So a feature centroid would only be meaningful for points plus polylines/polygons that are small enough to fit in a single tile at the current zoom level.

1ec5 commented 4 years ago

The upcoming within expression operator (#9352) would enable a workaround for the lack of a distance operator: create a circle around the other coordinate, perhaps using turf-circle, then pass it into a within expression. Depending on the number of steps (number of sides), this workaround could be much less performant than directly testing the distance between two points.

/cc @zmiao

1ec5 commented 4 years ago

A distance expression operator was implemented in mapbox/mapbox-gl-native#16397, mapbox/mapbox-gl-native-android#339, and mapbox/mapbox-gl-native-ios#295 in every map SDK except GL JS.

The expression operator has not yet been added to the style specification, so a JSON stylesheet that includes it might fail validation (not sure).

/cc @chloekraw

avi-c commented 3 years ago

Any chance this is being worked on now? There are several instances and customer use cases where it would be very helpful.

enersis-pst commented 2 years ago

Can please someone tell if this feature will come this year for mapbox-gl? It looks like feature is full implemented since months.

1ec5 commented 2 years ago

There’s a draft implementation in #10616.