Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix json rendering of large osm ids #7096

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

imannamdari
Copy link

@imannamdari imannamdari commented Jan 12, 2025

Issue

Fix #7016

Tasklist

Requirements / Relations

Link any requirements here. Other pull requests this PR is based on?

Copy link
Contributor

@afarber afarber left a comment

Choose a reason for hiding this comment

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

@@ -54,7 +54,10 @@ template <typename Out> struct Renderer
// `fmt::memory_buffer` stores first 500 bytes in the object itself(i.e. on stack in this
// case) and then grows using heap if needed
fmt::memory_buffer buffer;
fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{:.10g}"), number.value);
if (static_cast<std::uint64_t>(number.value) == number.value)
fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{}"), static_cast<std::uint64_t>(number.value));
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the if/else block really needed? Can't a single line below handle the big integer number up to 18446744073709551615 (which is 2^64-1)?

fmt::format_to(std::back_inserter(buffer), FMT_COMPILE("{}"), static_cast<std::uint64_t>(number.value));

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, nevermind, I see that it would break the fractional numbers

Copy link
Author

Choose a reason for hiding this comment

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

Exactly. For floating-points there gonna be problems.
The previous approach implemented before this commit can be found here (and it works fine):

void operator()(const Number &number)

A more robust solution could be separating floats from integers. Instead of Number struct, we can have Float and Integer structs for example. Each with their own json rendering.
However, that's a significant change! :D
If you agree with this approach (separating floats from integers), I can work on it in another pull request.

Copy link
Contributor

@afarber afarber Jan 25, 2025

Choose a reason for hiding this comment

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

Dear @DennisOSRM and @SiarheiFedartsou now that CI build is working again, could you please consider merging this fix by @imannamdari ?

In the past week I have tested his fix with map matching and it really seems to fix the problem with large OSM node/way ids.

And please consider adding a new git tag v5.28.1 after the merge?

Because the current tag v5.27.1 is already three years old and it would be nice to set a newer stable source code "checkpoint" for all users.

Copy link
Collaborator

Choose a reason for hiding this comment

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

new release coming soon

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @DennisOSRM I already run a version with this fix applied and it does fix the large OSM ids.

However the confidence is sometimes still printed as a scientific notation.

I am not sure if this is a problem or not and wonder why is json_renderer not called for the confidence.

Here the URL showing this:

http://localhost:5000/match/v1/driving/13.27537,52.50746;13.28206,52.50748;13.28541,52.51221;13.28271,52.51932;13.28133,52.52263;13.28103,52.52516;13.28451,52.53054;13.30747,52.53519?overview=false&generate_hints=false&gaps=ignore&annotations=nodes

image

Copy link
Author

Choose a reason for hiding this comment

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

Hi @afarber,

I've tried the example you provided, and it seems to convert the confidence value into scientific notation. The confidence value appears to be this number:
0.000000000000547

This section in json_renderer seems to be responsible for converting it to scientific notation:
FMT_COMPILE("{:.10g}")

This behavior already exists, and this merge request doesn't introduce it (since we're only addressing integer number representation in this MR).

I'm not sure what the best approach here should be. We could manually set the precision to 10 digits (similar to the previous implementation) or consider another solution. My question is: is the scientific notation really a problem? Since it's a valid representation of a float/double number, most JSON parsing frameworks should handle it without issues. Are you encountering any specific problems when parsing it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you have different OSM data than me, I have used berlin-latest.osm.pbf

Or maybe I have misapplied your patch.

No, it parses well for me with C# System.Text.Json

Copy link
Author

Choose a reason for hiding this comment

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

No, I get the same result as you (5.470068842e-13). The actual double value in the code is 0.000000000000547, which is represented in JSON as 5.470068842e-13.

The issue with this number is that it is too small. The previous implementation would return 0.0000000000 in the JSON response, which was not an ideal solution for representing very small floating-point numbers. As a result, the current approach was implemented to handle the rendering of extremely small float numbers more accurately (see this PR).

Therefore, I believe it is best to leave the current JSON rendering for floating-point numbers as it is and focus on editing it for integer numbers, which is the intent of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Json_render is not accurate for osm node ID
3 participants