paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
886 stars 103 forks source link

PointAtBearingAndDistance return points with large vertical distance #136

Closed tommynanny closed 7 months ago

tommynanny commented 12 months ago

Hi, I want to get bounding box coordinates that are 25 miles from a center point with the following code. This is the result I get which I suppose should be a square-like shape, but what I get is a diamond with a very long vertical distance. I am not sure what is wrong here - any help would be great!

North: [38.260160983034716 -76.10090588831387]
South: [38.25476768014666 -76.82375214625628]
East: [39.80116910842734 -76.45759539852678]
West: [36.71390085530215 -76.45759539852678]

image

package main

import (
    "fmt"

    "github.com/paulmach/orb"
    "github.com/paulmach/orb/geo"
)

const MILES_TO_METERS = 1609.34

func GetBoundingBox(center orb.Point, distanceMeters float64) orb.Bound {
    north := geo.PointAtBearingAndDistance(center, 0, distanceMeters)   // 0° is north
    south := geo.PointAtBearingAndDistance(center, 180, distanceMeters) // 180° is south
    east := geo.PointAtBearingAndDistance(center, 90, distanceMeters)     // 90° is east
    west := geo.PointAtBearingAndDistance(center, 270, distanceMeters)    // 270° is west

    fmt.Println("North:", north)
    fmt.Println("South:", south)
    fmt.Println("East:", east)
    fmt.Println("West:", west)
    return orb.Bound{
        Min: orb.Point{south[0], west[1]},
        Max: orb.Point{north[0], east[1]},
    }
}

func main() {
    center := orb.Point{38.257534981864744, -76.46232903171732}
    distanceMeters := 25 * MILES_TO_METERS // 25 miles converted to meters
    bbox := GetBoundingBox(center, distanceMeters)
    fmt.Printf("Bounding Box: %+v\n", bbox)
}
paulmach commented 7 months ago

The library is [Lng, Lat], [x, y]. When I swap the coordinates it get the correct answer

Screenshot 2024-01-10 at 8 34 47 AM