0

I am having trouble generating random geo locations from a given locations at a certain radius. I am getting an incorrect coordinate with the implementation I have done. How can I achieve the needed functionality?

type Location struct{
   Latitude     float64
   Longitude    float64
}

const EarthRadius = 6371000 /* meters  */
const DegToRad =  math.Pi / 180.0
const ThreePi = math.Pi*3
const TwoPi float64 = math.Pi*2


func pointAtDistance(location Location, radius float64) Location{
// Convert Degrees to radians
    loc := toRadians(location)

    sinLat := math.Sin(loc.Latitude)
    cosLat := math.Cos(loc.Latitude)

    bearing := rand.Float64() * TwoPi
    theta := radius / EarthRadius
    sinBearing := math.Sin(bearing)
    cosBearing := math.Cos(bearing)
    sinTheta   := math.Sin(theta)
    cosTheta   := math.Cos(theta)

   latitude := math.Asin(sinLat * cosTheta + cosLat*sinTheta*cosBearing)
    longitude := location.Longitude +
    math.Atan2(sinBearing * sinTheta*cosLat, (cosTheta-sinLat) * math.Sin(latitude))


    /* normalize -PI -> +PI radians */
    longitude = math.Mod(longitude+ThreePi, TwoPi) - math.Pi

     locs := toDegrees(Location{
        Latitude: latitude,
        Longitude: longitude,
    })

    return Location{
         Latitude: locs.Latitude,
         Longitude: locs.Longitude,
   }
}

 func GenerateLocations(location Location, radius float64) Location{
    rnd := rand.Float64()
    ranDist := math.Sqrt(rnd) * radius
     return pointAtDistance(location, ranDist)
}

 func toRadians(location Location) Location{
     lat := location.Latitude * DegToRad
     lon := location.Longitude * DegToRad

    return Location{
        Latitude: lat,
        Longitude: lon,
   }
}

func toDegrees(location Location) Location{
    lat := location.Latitude / DegToRad
    lon := location.Longitude / DegToRad

    return Location{
       Latitude: lat,
       Longitude: lon,
    }
}

Edit:

Input

func main(){
    location:= GenerateLocations(Location{
        Latitude: 6.981106276681917,
        Longitude: 79.87749937315392,
     }, 100)
     fmt.Println(location)
}

Output

{6.981757300245991 -103.35666511967649}

James Z
  • 12,209
  • 10
  • 24
  • 44
  • I did not check the math, but is it a typo? - `cosLat := math.Sin(loc.Latitude)` (cos vs sin mixup) – Michael Entin Jun 18 '21 at 01:01
  • @MichaelEntin It was a typo. But still getting a point way out of my radius. – Anjula Paulus Jun 18 '21 at 05:25
  • Note: it will not be really "random" (as uniform random). You will have many more point on center (smaller radius have smaller area, but same probability). In any case, try with 0,0 and print intermediate step (and maybe reproduce in a paper the expected calculations. I'm not sure about the part of "radius/earth radius". – Giacomo Catenazzi Jun 18 '21 at 10:11
  • 1
    This seems similar https://stackoverflow.com/questions/31192451/generate-random-geo-coordinates-within-specific-radius-from-seed-point – marco.m Jun 18 '21 at 16:29

0 Answers0