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 bbox_to_xyz #1070

Merged
merged 20 commits into from
Dec 18, 2023
Merged

Fix bbox_to_xyz #1070

merged 20 commits into from
Dec 18, 2023

Conversation

sharkAndshark
Copy link
Collaborator

@sharkAndshark sharkAndshark commented Dec 15, 2023

Figure out and fix the bug with tile-grid crate or remove the crate and implement it ourself.

pub fn wgs84_to_webmercator(lon: f64, lat: f64) -> (f64, f64) {
let x = EARTH_CIRCUMFERENCE / 360.0 * lon;
let y = ((90.0 + lat) * PI / 360.0).tan().ln() / (PI / 180.0);
let y = EARTH_CIRCUMFERENCE / 360.0 * y;
Copy link
Member

@nyurik nyurik Dec 16, 2023

Choose a reason for hiding this comment

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

i merged the two suggestions here, but as the result, it no longer matches the expected result due to rounding (?). I suspect that you would need to call .min((1<<zoom) -1) somewhere

Copy link
Member

Choose a reason for hiding this comment

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

oh, and another suspicious thing - are you sure y should cover 360? maybe it does, i don't know. Make sure you read https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This code is from our private server. TBH I don't remember the source....

I've adopted some code from esri. Sadly i'm bad at math😔,any help?

@nyurik
Copy link
Member

nyurik commented Dec 16, 2023

I made a few minor cleanups and rebased this on main. You may have to reset your local repo though, sorry about that. At least now i see the "more correct" values (I do not know if they are perfectly ok, or just "somewhat" ok).

If tile_grid has issues like this, we probably should setup a new project for this to have it cleaned up

fn test_tile_index() {
assert_eq!((0, 0), tile_index(-180.0, 85.0511, 0));
fn test_tile_colrow() {
assert_eq!((0, 0), tile_colrow(-180.0, 85.0511, 0));
}

#[test]
fn test_xyz_to_bbox() {
Copy link
Collaborator Author

@sharkAndshark sharkAndshark Dec 17, 2023

Choose a reason for hiding this comment

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

Due to the inherent precision limitations of floating point numbers, performing back-and-forth transformations between XYZ and bounding box coordinates within a single unit test seems not a good idea?
I removed the transform back test code. And actually if you add them back, you can see some failed. Any idea? I can't see logic error in these methods. Is it our const not accurate. My mind get lost now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Or maybe we should reserve tile-grid in this?

@@ -248,13 +251,31 @@ pub fn webmercator_to_wgs84(x: f64, y: f64) -> (f64, f64) {
(lng, lat)
}

/// transform WGS84 to WebMercator
// from https://github.com/Esri/arcgis-osm-editor/blob/e4b9905c264aa22f8eeb657efd52b12cdebea69a/src/OSMWeb10_1/Utils/WebMercator.cs
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's from esri.

Copy link
Member

Choose a reason for hiding this comment

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

I think this code is actually from the OSM wiki that has all this math

Copy link
Member

@nyurik nyurik left a comment

Choose a reason for hiding this comment

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

The extra tests are great! I think we keep using old names (index/x/y) as they are more expected. I think this is almost ready to go. It is totally ok to not have a "perfect roundtrip" - as long as they are close enough, esp if we reduce the lat/lng by some tiny percentage, we are good to go

martin-tile-utils/src/lib.rs Outdated Show resolved Hide resolved
martin-tile-utils/src/lib.rs Outdated Show resolved Hide resolved
martin-tile-utils/src/lib.rs Outdated Show resolved Hide resolved
@sharkAndshark sharkAndshark marked this pull request as ready for review December 18, 2023 02:26
Copy link
Member

@nyurik nyurik left a comment

Choose a reason for hiding this comment

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

looks great, thanks!

@@ -248,13 +251,31 @@ pub fn webmercator_to_wgs84(x: f64, y: f64) -> (f64, f64) {
(lng, lat)
}

/// transform WGS84 to WebMercator
// from https://github.com/Esri/arcgis-osm-editor/blob/e4b9905c264aa22f8eeb657efd52b12cdebea69a/src/OSMWeb10_1/Utils/WebMercator.cs
Copy link
Member

Choose a reason for hiding this comment

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

I think this code is actually from the OSM wiki that has all this math

@nyurik nyurik merged commit f21119d into maplibre:main Dec 18, 2023
18 checks passed
@sharkAndshark sharkAndshark deleted the tilecolrow branch December 19, 2023 03:27
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