gogeometrygeogeofencinggeofence

Check if lat/long point within an area


I am trying to find a way of checking if a pair of lat/lng coordinates are within an area (generated by other lat/lng coordinates).

For example, if my area is a rectangle generated with these coordinates:

43.672162 , -79.43585
43.629845 , -79.314585

And I wanted to check if these coordinates are within that area: 43.651989 , -79.371993

I have tried using this package but I can't make it work: github.com/kellydunn/golang-geo

p1 := geo.NewPoint(coords[0].LatX, coords[0].LonX)
p2 := geo.NewPoint(coords[0].LatY, coords[0].LonY)
geo.NewPolygon(p1, p2)

I was wondering if anyone has an implementation of this they can share, or any resources that can point me in the right direction? I am open to using google maps API as well.


Solution

  • In your example, which is a rectangle, you can calculate it like this:

    1. find MinPoint of area. MinimumPoint.X = Min(p1.X, p2.X) and MinimumPoint.Y = Min(p1.Y, p2.Y)
    2. find MaxPoint of area. MaximumPoint.X = Max(p1.X, p2.X) and MaximumPoint.Y = Max(p1.Y, p2.Y)
    3. check the point is between them: CheckPoint.X >= MinimumPoint.X and CheckPoint.X <= MaximumPoint.X and CheckPoint.Y >= MinimumPoint.Y and CheckPoint.Y <= MaximumPoint.Y

    Or you can use contains function from this: https://pkg.go.dev/github.com/paulmach/orb

    Add to project: go get github.com/paulmach/orb

    This is the sample code I wrote for your question:

    package main
    
    import (
        "fmt"
    
        "github.com/paulmach/orb"
    )
    
    func main() {
        p1 := orb.Point{43.672162, -79.43585}
        p2 := orb.Point{43.629845, -79.314585}
    
        bound := orb.MultiPoint{p1, p2}.Bound()
    
        fmt.Printf("bound: %+v\n", bound)
    
        checkList := []orb.Point{orb.Point{43.651989, -79.371993}, p1, p2, orb.Point{43, -79}}
    
        fmt.Printf("\ncontains?\n")
        for _, checkPoint := range checkList {
            fmt.Printf("    %+v:%t\n", checkPoint, bound.Contains(checkPoint))
        }
    }
    

    result:

    bound: {Min:[43.629845 -79.43585] Max:[43.672162 -79.314585]}
    
    contains?
        [43.651989 -79.371993]:true
        [43.672162 -79.43585]:true
        [43.629845 -79.314585]:true
        [43 -79]:false