golang / geo

S2 geometry library in Go
Apache License 2.0
1.69k stars 182 forks source link

Inverted params #89

Closed mblasi closed 2 years ago

mblasi commented 2 years ago

Hi, I'm pretty sure about inverted params here:

https://github.com/golang/geo/blob/master/s2/latlng.go#L36

// LatLngFromDegrees returns a LatLng for the coordinates given in degrees.
func LatLngFromDegrees(lat, lng float64) LatLng {
    return LatLng{s1.Angle(lat) * s1.Degree, s1.Angle(lng) * s1.Degree}
}

Not sure about the formule, but I tested it with a known polygon and a couple of known points, and it works as expected inverting the params, teste follows:

package main

import (
    "fmt"
    "github.com/golang/geo/s2"
    "github.com/twpayne/go-polyline"
    "go.opencensus.io/plugin/ochttp"
)

func main() {

    //Ituzaingo
    polyline := "|qkrEfpmeJkAsDwDoEcAq@uDWgDCoAw@cEwJiBe@aI_@_@KiAk@wAc@mGAyDg@wDvAcWkBsAa@{@_@gAk@yFkE}FsEaPsEkGgBiFyAaBW_@G}BmCqD{A_EsBqBeBwAaCoFsJiAeB}MsSu@k@mCsByQw^sE{FgAoDkBkQ{CuOeL}QfCiDhLoOjCqDtD_FnAcB`CcDxBsC`RyVhBrB`BpBdBrBdBhB`D_ExBsCjCgDjKiNjGeIbC_DfCaDdG_IlCkD`G_IfB_CdHkJvHyJjDmExCyD~@mAnBiChDoExEkGV[dG_IdC_DbCaD`DgEnBcCjBgCZa@lCoDxAeBRWtAkBhAuAv@_@\\KRETCfBIjAEhAGj@Cl@?j@Bd@Db@H`ElArDbAxCr@vDz@tDx@|FhA|FbAtDl@zDh@|Dh@~Ch@`@FtDl@zDn@pDj@vDp@`En@hFz@hC^|@N~Ch@vCd@vEt@tEr@tCd@fARr@@xCl@H@bDh@n@HRDH@H@b@H`AP|Cd@bBXtB\\jC`@hARvAT^F`BVb@F~Ch@\\D`C`@p@JnGbAnCb@h@HtDr@pEr@j@JJ@zB\\nB\\nANdEr@bFr@fDj@pDp@nARXDl@JrIrAd@HfFv@xDn@`BTxDj@`BTpAT|ATfBX|Cd@nB\\z@RdBRdBVdCb@ZDwCrDmB`CaBpBgB~BcBxB}AnBiAxAeBxBiAzAMP_CtCmB`CuAfBwC~D{BjCoBpC_BhBeArAo@x@aBvBY\\yAjB[b@gA~AeApAW\\aBpBqBhCaBrB_BtBsBnC{AjBq@x@q@x@}ApBa@f@mA~AcC~CgCbDsCpD}BvCUXsCpDsAdBm@v@kBbCm@t@c@j@EDEFUZwD~E_BjB_@j@cC`DKNuBhCs@|@[^uC~DqDlEeAtA{BxCcC`D_CzCaC~CkCjDoCpDwCtDqCjDY`@a@j@wBnCc@h@}CzDkEpF}AlBOPqA`B{@dAsC~D]`@mD|E_DtDwBnC}BrCmChDkC`DuAfBsX`^m`@lg@??"

    points := decode(polyline)

    regionPoints := []s2.Point{}

    for _, bound := range points {
        lat := bound[0]
        lng := bound[1]
        fmt.Printf("[%v, %v],\n", lng, lat)
        regionPoints = append(regionPoints, s2.PointFromLatLng(s2.LatLngFromDegrees(lng, lat).Normalized()))
    }
    lat := points[0][0]
    lng := points[0][1]
    fmt.Printf("[%v, %v]\n", lng, lat)
    regionPoints = append(regionPoints, s2.PointFromLatLng(s2.LatLngFromDegrees(lng, lat).Normalized()))

    loop := s2.LoopFromPoints(regionPoints)
    loop.Normalize()

    fmt.Printf("Polyline: %s\n", polyline)

    point1 := s2.PointFromLatLng(s2.LatLngFromDegrees(-58.435703, -34.606762).Normalized())
    point2 := s2.PointFromLatLng(s2.LatLngFromDegrees(-58.68175, -34.64108).Normalized())

    fmt.Printf("point 1 contained in loop1: %v (should be false)\n", loop.ContainsPoint(point1))
    fmt.Printf("point 2 contained in loop1: %v (should be true)\n", loop.ContainsPoint(point2))
}

func decode(ncodedP string) [][]float64 {
    points, _, err := polyline.DecodeCoords([]byte(ncodedP))
    if err != nil {
        fmt.Printf("Error decoding: %s", err.Error())
    }

    return points
}
mblasi commented 2 years ago

I worked on that again, and it looks that the problem was the polygon definition. My fault.