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(geometries): Fix missing precision in WKT coordinates #73

Merged
merged 1 commit into from
Jan 2, 2025

Conversation

saibotk
Copy link
Member

@saibotk saibotk commented Feb 10, 2024

We missed the "." here and thus PHP formatted numbers with the default precision of 6 and prefixed it with whitespaces.
See PHP docs: https://www.php.net/manual/en/function.sprintf.php

This has been addressed by using the json_encode function to print the
float. This function uses PHP's serialize_precision instead, which
by default uses a similar algorithm to the one PostGIS uses for
printing out the float in a minimal representation and also
allows to print out at the maximum precision, as long as the
user did not change the serialization precision config.

Thanks @ronnypolloqueri for reporting this. (ref #72)

@saibotk saibotk requested a review from ahawlitschek February 10, 2024 12:00
@saibotk saibotk self-assigned this Feb 10, 2024
Copy link
Member

@ahawlitschek ahawlitschek left a comment

Choose a reason for hiding this comment

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

LGTM

@saibotk
Copy link
Member Author

saibotk commented Feb 10, 2024

@ahawlitschek I'm unsure whether we should fix it like this and get cases where the numbers are "more" precise, but for some is more imprecise, see the tests for example. Or we may fix this in a different way, maybe check out how postgis / geos does it under the hood.

Also i just saw that maybe such imprecision could cause invalid geometries, at least according to ST_AsText, when setting precision there a warning exists about that: https://postgis.net/docs/ST_AsText.html

And PostGis seems to handle the value 50.12345 from the test cases perfectly.
So i'd love to mirror this behaviour.

@saibotk
Copy link
Member Author

saibotk commented Feb 10, 2024

Okay update:

Seems like PostGIS handles them the same way internally:
CleanShot 2024-02-10 at 22 25 46
Or TablePlus is rounding those idk

But it's still weird and we should adjust our WKT generator to try to output the same strings as PostGIS does.
This part might be useful form PostGIS codebase, which takes care of printing the decimal: https://github.com/postgis/postgis/blob/5560fb5cd14162a2d170a464f9e2b13e8998b1f7/deps/ryu/d2s.c#L537

@saibotk saibotk force-pushed the fix-precision-formatting branch 2 times, most recently from 35201c2 to b1dc9eb Compare January 2, 2025 20:46
We missed the "." here and thus PHP formatted numbers with the default precision of 6 and prefixed it with whitespaces.
See PHP docs: https://www.php.net/manual/en/function.sprintf.php

This has been addressed by using the `json_encode` function to print the
float. This function uses PHP's `serialize_precision` instead, which
by default uses a similar algorithm to the one PostGIS uses for
printing out the float in a minimal representation and also
allows to print out at the maximum precision, as long as the
user did not change the serialization precision config.

Thanks @ronnypolloqueri for reporting this.
@saibotk saibotk force-pushed the fix-precision-formatting branch from b1dc9eb to 3c364e3 Compare January 2, 2025 20:46
@saibotk saibotk changed the title fix(geometries): Fix stringifyFloat to correctly format number fix(geometries): Fix missing precision in WKT coordinates Jan 2, 2025
@saibotk saibotk merged commit 06bcd20 into main Jan 2, 2025
13 checks passed
@saibotk saibotk deleted the fix-precision-formatting branch January 2, 2025 20:49
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.

2 participants