Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate random points from a given point coordinates

    Hi,

    I have a database of coordinates (lat, lon) and I would like to randomly generate a set of new coordinates on a circle around the initial coordinates (and not within a circle). For each point (lat,lon) I want to generate 20 points (lat1;lon1), ... (lat20;lon20), etc. I have already succeeded in generating coordinates within a circle around the centroids, but those are at a distance that varies. I would like all the new coordinates to be at a given distance from the initial points that have the coordinates (lat,lon) in the following code:

    use coordinates.dta

    forvalues i = 1/20 {
    local phi = 2*runiform()*_pi
    local r = 0.1
    gen lon`i' = `r'* cos(`phi') + lon
    gen lat`i' = `r'* sin(`phi') + lat
    }

    Any help or tips would be much appreciated!
    Last edited by rouault eleonor; 02 Nov 2021, 04:00.

  • #2
    Your code does generate random points on a circle. To visualize:

    Code:
    clear
    set obs 200
    gen lon = .
    gen lat = .
    qui  {
    forvalues i = 1/200 {
    local phi = 2*runiform()*_pi
    local r = 0.1
    replace lon = `r'* cos(`phi')  in `i'
    replace lat = `r'* sin(`phi') in `i'
    }
    }
    scatter lat lon, aspect(1)

    Comment


    • #3
      Thank you very much !!

      Comment

      Working...
      X