Skip to content

Map writer: optimize geometry processing and clipping - #1741

Closed
menion wants to merge 3 commits into
mapsforge:masterfrom
asamm:dev/build_speed_impr
Closed

menion wants to merge 3 commits into
mapsforge:masterfrom
asamm:dev/build_speed_impr

Conversation

@menion

@menion menion commented Sep 19, 2025

Copy link
Copy Markdown
Contributor

Based on my attempt to speed up map generation, here are two improvements. It has a significant effect when generating over "RAM" tile cache. When using HDD, this does not have significant effect, as cliping itself consume most of time.

Again, this is used in our production generator for a few months and no negative effect detected. In the case of RAM generation, we reduced time by approx 30%!

Major changes:

  • Dependencies update: updated Guava and JTS library
  • Pre-calculate tile geometry: The JTS Geometry representation of a tile is now calculated once and reused for all ways within that tile, speed up and reducing redundant calculations.
  • Optimized clipping:
    • The clipToTile method now accepts a pre-calculated tile Geometry instead of a TileCoordinate and enlargement factor, simplifying the call site.
    • An early exit is added to clipToTile if the geometry is already fully contained within the tile's envelope, avoiding unnecessary intersection calculations.
  • Use StringBuilder for preferred languages: Improved string concatenation performance when constructing the preferred languages string.

geometry = JTSUtils.repairInvalidPolygon(geometry);
if (!geometry.isValid()) {
LOGGER.warning("load(), invalid geometry even after attempt to fix " + way.getId());
return null;

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.

@menion If the repaired geometry is still not valid, why return null and not throw an exception like above?

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.

You are correct. I've modified the change so it mimics the geometry==null style. Thanks.

@devemux86 devemux86 changed the title Optimize geometry processing and clipping Map writer: optimize geometry processing and clipping Sep 24, 2025
@devemux86

Copy link
Copy Markdown
Collaborator

@menion Very nice performance improvement!

However, the pull request code creates maps with different map rendering?

For example, some buildings now do not have have roofs.
(bottom and upper-left at 2nd image)

The comparison is made with VTM, checking the 3D buildings and their colors:


Master
master


Pull request
pull_request

} else {
ret = tileAsGeometry.intersection(geometry);
}
ret = tileBBJTS.intersection(geometry);

@devemux86 devemux86 Sep 24, 2025 •

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.

@menion The rendering problem seems to be caused here:

  • Declare ret like before and the rendering seems to work correctly.

  • Also, why remove the repair section if geometry is not valid?

if (!geometry.isValid()) {
    // this should stop the problem of non-noded intersections that trigger an error when
    // clipping
    LOGGER.warning("invalid geometry prior to tile clipping, trying to repair " + way.getId());
    geometry = JTSUtils.repairInvalidPolygon(geometry);
    if (!geometry.isValid()) {
        LOGGER.warning("invalid geometry even after attempt to fix " + way.getId());
    }
}
ret = tileAsGeometry.intersection(geometry);

@devemux86

Copy link
Copy Markdown
Collaborator

@menion As a general note, please avoid changing the formatting of the code, new lines, etc.
so that it is easier to review and see only the actual code changes. 🙂

@menion

menion commented Sep 24, 2025

Copy link
Copy Markdown
Contributor Author

PR needs more testing, just as the second one.

I'm closing it for now and will reopen once all is properly tested and verified on my side. Thanks.

And "general note" > I'm trying, really 😊. I'm usually reverting many changes made automatically. Will be more careful next time. This old Java 😱 code simply needs some major automatic refactoring ...

@menion menion closed this Sep 24, 2025
@devemux86

Copy link
Copy Markdown
Collaborator

@menion Thanks!

This is a very nice performance improvement, so we should not forget it.

With the changes suggested above, current tests seem to work correctly.

@devemux86

Copy link
Copy Markdown
Collaborator

@menion

Probably the most needed improvement in map writer is the reduction of RAM usage.

So we can use in more maps the fast type=ram option instead of the slow type=hd.

@menion

menion commented Sep 25, 2025

Copy link
Copy Markdown
Contributor Author

Fully agree. I just miss a few things to look at it more ...

  • free time
  • deep understanding of the generator
  • and Kotlin of course 😊

@devemux86

Copy link
Copy Markdown
Collaborator

@menion

and Kotlin of course 😊

I prefer not to introduce a new language into the project. 🙂

It will make maintenance difficult and fewer developers will understand and contribute to the code.

@menion

menion commented Sep 25, 2025

Copy link
Copy Markdown
Contributor Author

I expected you will write it.

For me (and I believe that for many other Java/Kotlin devs), it works the opposite. Writing in Java is pure pain these days, and I try to avoid it at all costs.

This is anyway too offtopic.

I'll try to look at two problematic PR next week and give you more info.

@devemux86 devemux86 added this to the 0.27.0 milestone Nov 7, 2025
@devemux86

Copy link
Copy Markdown
Collaborator

@menion

I merged it via 4bb9457 with the above restore of previous code for same rendering.
So now we just have the important performance improvement with the tileAsGeometry.

Thanks again for the contribution! 👍

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.

2 participants