Skip to content

Completing deterministic labels and symbols effort - #1586

Merged
devemux86 merged 8 commits into
mapsforge:masterfrom
Sublimis:labels04
Dec 5, 2024
Merged

devemux86 merged 8 commits into
mapsforge:masterfrom
Sublimis:labels04

Conversation

@Sublimis

@Sublimis Sublimis commented Dec 3, 2024 •

Copy link
Copy Markdown
Contributor
  1. Important improvements to MapElementContainer.compareTo() and MapElementContainer.clashesWith()

  2. Important improvements to PointTextContainer.clashesWith()

  3. Improvements to LayerUtil.collisionFreeOrdered()

The above improvements together make non-deterministic labels and symbols a history!

Additional:

  • Prevent labels and symbols overlap when the overlapping items are both marked as "display always"

  • DirectRenderer is now obsolete. Now that the labels are deterministic, it doesn't bring anything new to the table that isn't already covered by the DatabaseRenderer. It is thus converted into a simple "empty shell" subclass of the DatabaseRenderer with the same interface as before, so it won't break any apps that use it. (Being able to render tile by tile #1085)

  • Slight performance improvement when drawing labels: Avoid sorting when not needed; Reverse order of a sorted list instead of sorting again; Use List-s instead of Set-s where possible.

* Slight performance improvement when drawing labels (faster sorting)
* Slight performance improvement when drawing labels: 1) Avoid sorting when not needed: 2) Reverse order of a sorted list instead of sorting again; 3) Use List-s instead of Set-s where possible.
* Slight performance improvement when drawing labels: 1) Avoid sorting when not needed: 2) Reverse order of a sorted list instead of sorting again; 3) Use List-s instead of Set-s where possible.
@Sublimis

Sublimis commented Dec 3, 2024

Copy link
Copy Markdown
Contributor Author

Sorry for the additional commits, it was just about including the PointTextContainer which was forgotten (but is largely related to this PR).

@Override
public boolean clashesWith(MapElementContainer other, Rotation rotation) {
if (super.clashesWith(other, rotation)) {
return true;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't super.clashesWith needed anymore?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't super.clashesWith needed anymore?

No, super.clashesWith does not take rotation into account, so it is not relevant for labels. The important part of the super.clashesWith was put here, i.e. the Display.ALWAYS == this.display checks.

undrawableElements.add(current);
private void processLabels(RenderContext renderContext) {
final List<MapElementContainer> labelsToDraw;
{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid this { so commit history is more clear with the changes.

@Sublimis Sublimis Dec 3, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are scope-reducing braces, that make it clear that the purpose of the code inside is to set the variable. In our opinion it is better to have more error-resistant code than not to have it. No problem to remove those, are you sure?

}

private Set<MapElementContainer> processLabels(RenderContext renderContext) {
/** TODO (2024): If this renderer is ever going to be used, please see how labels are handled in the {@link DatabaseRenderer}, and then implement here. */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DirectRenderer is not used by Mapsforge.

It could be used by apps to get bitmap tiles from vector maps in their own custom map engines.
However, it has worse map rendering than Mapsforge map engine, especially with the label layer.

It would be better if all developers work and improve the Mapsforge map engine,
so that our resources are not separated and we all benefit from the improvements.

@Sublimis
Sublimis marked this pull request as draft December 4, 2024 01:24
@devemux86 devemux86 added this to the 0.23.0 milestone Dec 4, 2024
@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Is there still work to be done?

…clashesWith() improvements!

2) Important PointTextContainer.clashesWith() improvements!

3) Important LayerUtil.collisionFreeOrdered() improvements!

The above improvements together make the following :

* Non-deterministic labels AND SYMBOLS are now history!

* Prevents labels and symbols overlap when the overlapping items are both marked as "display always"!

* Extra: Slight performance improvement when drawing labels: Avoid sorting when not needed; Reverse order of a sorted list instead of sorting again; Use List-s instead of Set-s where possible.
@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

Mostly done, but these upcoming simple yet far-reaching changes (which we're quite proud of) might cause some head scratching, so now we're investing time into detailed explanations. 🙂 As soon as those are finished, we'll send a new commit.

For example: The library will now not allow two labels or symbols that are both marked as Display.ALWAYS to overlap. If you feel this is wrong, we can discuss it. However, it makes the map less cluttered (e.g. those prominent gas-station symbols that cover the map over urban areas). We'll send some screenshots to explain our point. Of course, when only one of those items is marked as Display.ALWAYS, it will properly be displayed without overlapping with items not marked as Display.ALWAYS.

Also: DirectRenderer is now obsolete. Now that the labels are deterministic, it doesn't bring anything new to the table that isn't already covered by the DatabaseRenderer. It is thus converted into a simple "empty shell" subclass of the DatabaseRenderer, so it won't break any apps that use it.

#1085
@monsieurtanuki

@Sublimis Sublimis changed the title Final details for deterministic labels Completing deterministic labels and symbols effort Dec 4, 2024
@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

If you think there are improvements to the Motorider map theme, please write here:

It is written by @MotoUKRider and we contribute it to the Mapsforge community.

@devemux86

devemux86 commented Dec 4, 2024 •

Copy link
Copy Markdown
Collaborator

@Sublimis

The library will now not allow two labels or symbols that are both marked as Display.ALWAYS to overlap.

Of course, when only one of those items is marked as Display.ALWAYS, it will properly be displayed without overlapping with items not marked as Display.ALWAYS.

We do not want these, as it changes how "always" was designed (see here).

Labels / symbols marked with Display.ALWAYS must be displayed always.
So they should overlap with anything, other elements with or not "always".

If someone wants less labels / icons on the map can remove the "always".

@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

Labels / symbols marked with Display.ALWAYS must be displayed always, so they should overlap with anything. Otherwise the "always" loses its meaning. If someone wants less labels / icons can remove the "always".

Can you please explain under what circumstances it is a good (or positive) thing to have two Display.ALWAYS items to overlap? Maybe we're missing something. Note, the items are still drawn in order of their priority; The Display.ALWAYS is not the only property taken into account when drawing items.

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

Can you please explain under what circumstances it is a good (or positive) thing to have two Display.ALWAYS items to overlap?

Its name indicates its meaning: "always: an element will always be displayed regardless of space".

@Sublimis

Sublimis commented Dec 4, 2024 •

Copy link
Copy Markdown
Contributor Author

Here are some screenshots, so it's more clear.

Before

2024-12-04-always_overlap_on

After

2024-12-04-always_overlap_off-1 2024-12-04-always_overlap_off-2
2024-12-04-always_overlap_off-3 2024-12-04-always_overlap_off-4

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Thanks for the screenshots.

They appear nice and clear, I don't have a strong opinion on this.

But I don't know how the users will see this change in their maps.
We can merge it and we can improve it later if there are reactions?

By the way the "always" is also used in oneway arrows.
Have you checked if fewer oneway arrows now appear?

@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

Its name indicates its meaning: "always: an element will always be displayed regardless of space".

In other words, ugly overlap of two "always" items should has less priority over the "always" property? The logic is there, but maybe someone should communicate this to the theme creators and take their opinion into account. We think the "display always" property as currently defined (and abused) is too powerful. After all, we're talking about labels and symbols here, not rocket science...

In any case, this is really just a minor change in the code, easily reverted (and un-reverted in the future if needed).

@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

By the way the "always" is also used in oneway arrows. Have you checked if fewer oneway arrows now appear?

Will do this now and get back to you!

@Sublimis

Sublimis commented Dec 4, 2024 •

Copy link
Copy Markdown
Contributor Author

By the way the "always" is also used in oneway arrows. Have you checked if fewer oneway arrows now appear?

Will do this now and get back to you!

Did you mean something like this? (The screnshot is with the change included)

2024-12-04-always_overlap_off-arrows

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

Did you mean something like this?

That's it, can you check the oneway arrows before and after the change?

@MotoUKRider

Copy link
Copy Markdown

The "Fuel" icons do not overlap the City/Town labels on the current theme, so that behaviour seems unique to whatever application is producing those maps. In fact there was some talk about this back in August and I fixed/tamed it.

@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

One more thing to point out. The Display.ALWAYS issue in question does not affect the determinism(*) of the symbols, it's just a matter of how the map looks 👍 So, it's really not that big of a deal anyway.

(*) Maybe you can notice how gas station symbols in the first (overlapping) screenshot above are nicely drawn from left to right 💯 🙂

@devemux86

devemux86 commented Dec 4, 2024 •

Copy link
Copy Markdown
Collaborator

@MotoUKRider

It's more about the same elements with "always" rule. Please see the screenshots with fuel stations above:

@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

The "Fuel" icons do not overlap the City/Town labels on the current theme, so that behaviour seems unique to whatever application is producing those maps. In fact there was some talk about this back in August and I fixed/tamed it.

They do overlap, but they're below the City label, not above, is that what you meant?
Here is the screenshot from v0.22.0:

image

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis @MotoUKRider

We can probably merge it and see it in action within the apps.

Note: Mapsforge samples don't have layers menu, they display everything in the map theme.

@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

Here is the commit.

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Is this ready to merge?

Maybe you can update the pull request description with the details of the new commits?

@Sublimis
Sublimis marked this pull request as draft December 4, 2024 16:56
…lementContainer.clashesWith()

2) Important improvements to PointTextContainer.clashesWith()

3) Improvements to LayerUtil.collisionFreeOrdered()

The above improvements together make non-deterministic labels and symbols a history!

* Prevent labels and symbols overlap when the overlapping items are both marked as "display always"

* Additional: Slight performance improvement when drawing labels: Avoid sorting when not needed; Reverse order of a sorted list instead of sorting again; Use List-s instead of Set-s where possible.
…lementContainer.clashesWith()

2) Important improvements to PointTextContainer.clashesWith()

3) Improvements to LayerUtil.collisionFreeOrdered()

The above improvements together make non-deterministic labels and symbols a history!

* Prevent labels and symbols overlap when the overlapping items are both marked as "display always"

* Additional: Slight performance improvement when drawing labels: Avoid sorting when not needed; Reverse order of a sorted list instead of sorting again; Use List-s instead of Set-s where possible.
@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

@Sublimis Is this ready to merge?

A few more details will be added, sorry.

  1. One more change regarding DatabaseRenderer/MapWorkerPool (without changing behavior) that was planned for later should probably be included here, because it's closely related to what was mentioned at Being able to render tile by tile #1085 and because we are changing the DirectRenderer in this PR:
* in this last case, potentially because of a bug - as elements are never removed from `tileDependencies` _except_ in `MapWorkerPool`, that we don't want to use because of the display refresh
  1. One more slight optimization (no behavior change) in LayerUtil.collisionFreeOrdered()

Maybe you can update the pull request description with the details of the new commits?

Will do.

By the way, it appears that symbol clash check does not take rotation into account, maybe something that should be considered in the future.

@Sublimis
Sublimis marked this pull request as ready for review December 4, 2024 17:45
@Sublimis

Sublimis commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

@devemux86
This should be all for now.

@devemux86
devemux86 merged commit 801fb89 into mapsforge:master Dec 5, 2024
@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis Thanks for these nice improvements! 👍

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

I'm thinking about whether we should make a new release, since so many improvements have been implemented.
Or do you have more work planned?

@Sublimis

Sublimis commented Dec 6, 2024

Copy link
Copy Markdown
Contributor Author

More work! The labels are still not 100% perfect 😅 Maybe a day or two, if everything goes well. Also, the hill shading improvements have been completed for some time now, and are basically waiting to be pushed (this is a big commit, but tested a lot by now, and works great). After that yes, all of this will probably justify a new release.

@devemux86

Copy link
Copy Markdown
Collaborator

@Sublimis

No problem! It is better small pull requests step by step to understand the changes in history.

Then hill shading can come modifying only its own files, so we have a clear implementation. 🙂

@Sublimis

Sublimis commented Dec 6, 2024

Copy link
Copy Markdown
Contributor Author

Those h.s. changes couldn't be made in small steps, as there are changes to interfaces etc., you'll see 🙂 But yes, it mostly touches its own files, except for some small changes to GraphicContext and GraphicFactory just to add the color feature.

@voldapet

voldapet commented Jul 7, 2025

Copy link
Copy Markdown

I'm sorry to reopen this old discussion, but I’m reaching out on behalf of the Locus Map app development team, specifically regarding the internal theme, which I manage.

Unfortunately, this change has broken our internal theme due to a different behavior of the Display.ALWAYS parameter. (I’d also like to point out that the current documentation for Display.ALWAYS is now inaccurate.)
We useDisplay.ALWAYS to combine two icons into a single visual symbol. For marked trails, we rely on the osmc:symbol tag , which defines the trail symbol. In the previous implementation, it was possible to "merge" a background and foreground symbol to create a unified symbol. There’s another problem when displaying a cross symbol along a trail (specifically for via ferrata routes) together with a one-way symbol, there’s a risk of collision.
image

I may not fully understand the rationale behind this change, but if there are limitations with Display.IFSPACE, perhaps we could consider introducing a new parameter, something like symbol/label-margin - to define the minimum distance allowed between symbols or labels. This could help avoid overlaps and preserve clarity.

@Sublimis

Sublimis commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

I may not fully understand the rationale behind this change,

IIRC, there were some abuses of the "always" property in default rendering themes that caused unwanted overlapping of displayed items. No one ever mentioned using the property to merge icons, so that was probably not the original intention.

but if there are limitations with Display.IFSPACE, perhaps we could consider introducing a new parameter, something like symbol/label-margin - to define the minimum distance allowed between symbols or labels. This could help avoid overlaps and preserve clarity.

IMHO something like this should be the way to go if this functionality is to be implemented in Mapsforge, and Display.ALWAYS should be left as is.

@devemux86

Copy link
Copy Markdown
Collaborator

@voldapet @Sublimis

it was possible to "merge" a background and foreground symbol to create a unified symbol.

No one ever mentioned using the property to merge icons, so that was probably not the original intention.

Indeed this seems like a new function.

Anyway, if you all read the discussion above, could we allow always to work as before,
but enabling it via a flag in the Parameters class for those who need it?

It is important that the existing Mapsforge / OpenAndroMaps / Freizeitkarte themes
continue to work as they do now without the need for changes.

@voldapet

voldapet commented Jul 8, 2025 •

Copy link
Copy Markdown

@Sublimis @devemux86
Thank you for your quick reply. I realize we're discussing it half a year after the implementation, and I apologize for that. Anyway, I’m still thinking that if there were issues with the IFSPACE parameter, it would have been better to fix or improve that behavior rather than change an existing parameter Display.ALWAYS …. Especially when it works as expected (based on its name and documentation).

I understand that the new behaviour is already in use in some themes, but is there any chance this change could be reverted? Alternatively, what about introducing a new parameter, such as Display.ALWAYS_FORCE, to ensure an element is truly displayed every time?

Anyway, if you all read the discussion #1586 (comment), could we allow always to work as before,
but enabling it via a flag in the Parameters class for those who need it?

That might help, but is it really the right solution? The current behaviour of Display.ALWAYS no longer guarantees "always," which could lead to confusion or further issues down the line.

By the way, what is the difference between ALWAYS and IFSPACE now?

@Sublimis

Sublimis commented Jul 8, 2025

Copy link
Copy Markdown
Contributor Author

By the way, what is the difference between ALWAYS and IFSPACE now?

In the current behavior ALWAYS behaves like a "king". If other kings are too close, they must make a deal because it is assumed there can only be one king. IFSPACE are "pawns" who make deals only with each other and give way to kings.

Anyway, I’m still thinking that if there were issues with the IFSPACE parameter, it would have been better to fix or improve that behavior rather than change an existing parameter Display.ALWAYS …. Especially when it works as expected (based on its name and documentation).

There were no issues with IFSPACE as such, there were issues with everything, including compareTo() and equals() inconsistency, and what not...

I understand that the new behaviour is already in use in some themes, but is there any chance this change could be reverted? Alternatively, what about introducing a new parameter, such as Display.ALWAYS_FORCE, to ensure an element is truly displayed every time?

  1. IF we go the "revert route", we must make sure this doesn't break the mentioned compareTo() and equals() consistency, and label determinism.

  2. IF we go the "Display.ALWAYS_FORCE", it could be easier to implement with 100% guarantee of not breaking anything.

  3. IF we go "Parameters class route" this also sounds reasonable and not complicated @devemux86.

That might help, but is it really the right solution? The current behaviour of Display.ALWAYS no longer guarantees "always," which could lead to confusion or further issues down the line.

Reading again into the description of Display.ALWAYS, it says: "ALWAYS means that an element will always be displayed (so it will be overlapped by others and will not be part of the element placing algorithm)". Of course we saw that description last year when implementing this PR. Why is this not 100% followed, you ask? It is -- unless two "kings" happen to step on each other's toes. Remember that the code was inconsistent, and this gave us some liberty in interpreting the above sentence.

Maybe someone could contact @ludwigb (Ludwig M Brinckmann) who wrote the original code in 2014. and ask whether they had creating icons in mind when conceiving Display.ALWAYS?

If they didn't, then using this parameter to make multilayer icons feels like a hack IMHO, and we should go for either 2) or 3), or something else entirely.

@devemux86

devemux86 commented Jul 12, 2025 •

Copy link
Copy Markdown
Collaborator

@voldapet @Sublimis

If someone can provide a pull request with a new Display.FORCE theme option or something similar,
I can review it.

@Sublimis

Copy link
Copy Markdown
Contributor Author

If someone can provide a pull request with a new Display.FORCE theme option or something similar, I can review it.

Can do. Please allow a few days.

A question to @voldapet regarding multilayer icons: Is there some deeper reason why you are delegating this to Mapsforge instead of implementing it in your application? Even with the FORCED option, this still somehow feels like a hack (in the sense that it seems like it could break again in the future for other currently unknown and unforeseen reasons).

@voldapet

voldapet commented Jul 23, 2025 •

Copy link
Copy Markdown

@Sublimis @devemux86
I think we (@menion ) can also prepare the pull request for Display.FORCE. Just may take some time due to vacations and other commitments.

Is there some deeper reason why you are delegating this to Mapsforge instead of implementing it in your application?

I'm not sure I can understand. A few years ago, we maintained our own fork of Mapforge within the app. However, this approach created complications for the community - particularly around theme compatibility and other inconsistencies—so we ultimately chose to align with the official Mapsforge library

As for why we "merge" icons in Mapsforge, there are two main reasons:

  • to simplify the theme. osmc:symbol can have thousands of combinations for foreground and background, which would otherwise require thousands of individual <rule> elements.
  • to ensure that icons (merged) are displayed just near to another one (see the image in my previous post where icons "touch" each other
  • we also considered using “shields” (icons with embedded text) to represent road numbers.

@Sublimis

Copy link
Copy Markdown
Contributor Author

Ok, thanks for the info. We've started working on this, PR will probably be ready by tomorrow at the latest.

@Sublimis

Copy link
Copy Markdown
Contributor Author

#1717
That should be it. Please check if it meets all the requirements and works correctly.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants