yjh0502 / rust-s2

S2 geometry library in Rust
Apache License 2.0
77 stars 20 forks source link

CellID invalid from LatLng #9

Closed Afourcat closed 3 years ago

Afourcat commented 4 years ago

Hey, Thank you for your work. We are iterating through France's Nodes and we think the following call CellID::from(LatLng...)) failed!

CellID::from(LatLng::new(Angle::from(Deg(lat)), Angle::from(Deg(lon)))).pos()

with

lat = 44.910730699999995
lon = 4.8773971

Gave us the position 573461372891856251 instead of 5185147391319244000.

Have you any idea? Maybe we are using it the wrong way. Thank you again.

yjh0502 commented 4 years ago

Could you share us where did you get 5185147391319244000? I tested with golang implementation and it give a same result with rust one. @Afourcat

Golang code

package main

import (
        "fmt"
        "github.com/golang/geo/s2"
)

func main() {
        var lat = 44.910730699999995
        var lng = 4.8773971

        var ll = s2.LatLngFromDegrees(lat, lng)
        var cell = s2.CellFromLatLng(ll)
        var cellid = cell.ID()
        fmt.Printf("ll=%v, cellid=%v, pos=%v", ll, cellid, cellid.Pos())
}

output

ll=[44.9107307, 4.8773971], cellid=2/033322223000221013310003322331, pos=573461372891856251

Rust version

use s2::cellid::CellID;
use s2::latlng::LatLng;
use s2::s1::angle::*;

fn main() {
    let lat = 44.910730699999995;
    let lon = 4.8773971;
    let cellid = CellID::from(LatLng::new(Angle::from(Deg(lat)), Angle::from(Deg(lon))));
    let ll = LatLng::from(&cellid);

    println!("ll={:?}, cellid={:?}, pos={:?}", ll, cellid, cellid.pos());
}

output

ll=[44.9107307, 4.8773971], cellid=2/033322223000221013310003322331, pos=573461372891856251
Afourcat commented 4 years ago

Actually, we are using this tool. Maybe it's broken. We will investigate :policeman:.