mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Prevent small GeoJSON line features from disappearing at low zoom levels

Open krisdages opened this issue 7 years ago • 13 comments

Motivation

For features in a line layer, when zoomed out past a certain point (guessing when lines reach ~1 px or less in length), the lines disappear entirely, even if they have a width and rounded endcaps.

For many use cases, such as showing a highlight for particular features, we would still like to be able to see that these features are present, as it's misleading otherwise.

Design Alternatives

Rendering the endcaps of the line would solve the visibility problem, showing a circle or a square instead of nothing. For compatibility with the existing behavior (which is desired behavior for other use cases), an opt-in layout property could be used to enable this.

Not sure about naming or implementation as I'm not an expert on the codebase, although I could certainly look into it :)

krisdages avatar Aug 14 '19 20:08 krisdages

@krisdages thanks for the note. Can you make a test case showing what you're describing? a 1px line should still be visible.

peterqliu avatar Aug 14 '19 23:08 peterqliu

Thanks for the quick feedback!

https://jsfiddle.net/cxeb0f2a/ The red line (more like a dot unless you zoom in further) disappears after zooming out a little.

https://jsfiddle.net/ug8hearz/ This one has a "line" that is really just a point. Two identical sets of coords. It doesn't show at all, which makes sense, but it would be nice if there was a way to make it render as a minimal-length line with the round endcaps. After poking around in the code, I'm guessing that would be a way to set a 1-unit floor for the size of the feature within the tile.

The real-world data source would be an arbitrary set of location pairs where the origin and destination could be the same exact spot, and they need to be visible at any zoom level.

I could currently do some simple processing to convert the true 0-length lines to Point features and add a circle layer with the same styling, but that wouldn't work for the first case...
https://jsfiddle.net/q471gcwm/ - the longer line also disappears once zooming out far enough

BTW, I tried adding a circle layer with the same source data, and the circles at each vertex also disappeared when the line did, since the feature itself no longer matched any tiles.

krisdages avatar Aug 15 '19 08:08 krisdages

The red line (more like a dot unless you zoom in further) disappears after zooming out a little.

Turns out this happens not in the rendering step, but earlier during converting geojson to vector tiles. geojson-vt looks at the extent of each feature, and drops them at tile zooms where they'd usually (but apparently not always!) be too small to need rendering. As you noticed, if you make the line really long, it will remain visible at much lower zooms.

This one has a "line" that is really just a point. Two identical sets of coords. It doesn't show at all

Same reason as above-- totally a tiling rather than line-rendering thing.

I could currently do some simple processing to convert the true 0-length lines to Point features and add a circle layer with the same styling, but that wouldn't work for the first case...

In this situation I'd generate a second source with all line endpoints as a bag of loose points, and visualize that in circles. At high zooms the lines and circles would overlap perfectly, and at low zooms the lines would disappear, but circles can step in and show the tiny nubs of lines.

peterqliu avatar Aug 15 '19 22:08 peterqliu

That was what I figured after stepping through and trying to find where it was actually decided whether a feature was present in a tile.

It looks like there's a place in geojson-vt where it rounds the line segment lengths to integer values, and if it rounds down to 0, it's gone. Feels like an option to collapse a line/polygon to a point rather than disappear would be helpful, if it was not too complicated. Although maybe it's an uncommon use case, if I'm the first one to be looking for something like this.

(Random aside: while testing this I realized that if I could render a 0-length line with squared endcaps it could simulate a square/rectangular marker that could scale and style like a circle layer, something that our users have been requesting)

Re: the suggestion about a second source, is there a way to tell whether the line is culled in order to show the circles selectively via an expression? With partially transparent lines, it would be best to show a circle only when needed to avoid the noticeable overlap.

Thanks!

Kris

krisdages avatar Aug 16 '19 07:08 krisdages

I guess the distillation of this particular use case from the usability perspective is the opposite of the "render tons of data efficiently via simplification" (which works great I must say 😃)

A few of the layers in our map have a different purpose: just a handful of features at any given time for the purpose of highlighting them to the user, so it's important that they be visible even when zoomed out.

krisdages avatar Aug 16 '19 07:08 krisdages

Feels like an option to collapse a line/polygon to a point rather than disappear would be helpful, if it was not too complicated.

Not too complicated, but you can imagine it can sow confusion between real point data and lines simplified to points.

Re: the suggestion about a second source, is there a way to tell whether the line is culled in order to show the circles selectively via an expression?

hahaa, was hoping your lines were opaque. Depending how fancy you want to get, you can either set a minzoom for the line layer at the maxzoom for the circle layer, to do the hand-off for all lines at the same zoom juncture. Or if your lines vary a lot in length, you can compute the projected length of each line to predict the zoom when it disappears, store those values as geojson feature properties, and write expressions using that property to set the circle visibility

peterqliu avatar Aug 16 '19 19:08 peterqliu

Turns out the geojson source options already contain a property that fixes this issue :) https://jsfiddle.net/oa07vLh5/

https://docs.mapbox.com/mapbox-gl-js/style-spec/#sources-geojson-tolerance

Setting tolerance to a very low value prevents the lines and points from being simplified to nothingness, and they stay visible even when zoomed out.

I'll go ahead and close this one out. Thanks for the suggestions and for helping me figure this one out!

krisdages avatar Aug 18 '19 18:08 krisdages

Sorry, tolerance did work for the longer line in the fiddle, but not for the smaller one. Didn't notice that at first.

Not sure if you think this would be an issue worth addressing, but if it is, I think the solution for not sowing confusion would be keeping the default behavior as-is but allowing a source property similar to tolerance that would be passed as an option to geojson-vt that would allow lines/shapes to collapse to points rather than disappear.

Something like minFeatureSize: 0 | 1 or lineCollapse: 'hide' | 'point' maybe?

krisdages avatar Aug 18 '19 18:08 krisdages

I did a little more digging. Tolerance did get past the issue with geojson-vt, but then the layer rendering itself was dropping the feature if the vertices of the line were the same.

I played around with it and tried adding a layout property for line-collapse where it would let the line render even if both vertices were the same. This fixed the issue I was having with single line segments and round endcaps.

Issues:

  • Didn't try much to make things work for polygons since I'm not too familiar with them and this is just a proof of concept
  • Collapsed lines with multiple segments can get chopped in half when they cross tile borders
  • Square endcaps on diagonal lines lose their orientation when the vertices are equal, as I just defaulted to using a horizontal normal vector instead of trying to find a way to compute the actual angle.

https://github.com/mapbox/mapbox-gl-js/compare/master...krisdages:feature/line-collapse

https://jsfiddle.net/oa07vLh5/4/

Thanks again for bearing with me, just wanted to let you know what I found. If it's not worth the trouble to build a robust solution to this (and it very well might not be), I can definitely just patch in a workaround that works just for my use case.

krisdages avatar Aug 19 '19 02:08 krisdages

@krisdages Thanks for sharing the branch with your proposed changes. Would you be willing to put up a PR with your changes to re-surface the discussion?

asheemmamoowala avatar Sep 30 '19 22:09 asheemmamoowala

Sure, sorry for the delay. I need to update it for the latest release, the last patch I have is from 1.2.1.

krisdages avatar Oct 10 '19 23:10 krisdages

Any update on this? I am in a similar situation and the proposed line-collapse property would solve my issue perfectly.

pinkMoneyKing avatar Jul 07 '21 19:07 pinkMoneyKing

Could you add this fix to v1 too? BTW: v1 allows tolerance: 0, but it is not real 0

Lewik avatar Oct 17 '21 20:10 Lewik