Announcement

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

  • Random Coordinates in Polygon

    Hi,

    Using a shape file diving a city into 20 zones, I would like to randomly generate a GPS coordinate and test whether it is in a given zone, and keep on resampling until I have sampled 30 GPS coordinates in a given zone while ensuring that two GPS coordinates are no more than 50 meters away from each other. I think the steps to follow are:

    1) Random select a GPS coordinate (using the shape file to bound the latitudes and longitudes per zone, or else it could take a while to get a first GPS coordinate in a given zone).
    2) Randomly select a GPS coordinate within a 50 meter radius and check that this point is still in the same zone.

    I was thinking of using gpsbound but that would only help to check whether the gps coordinate is in the correct zone. I would first need to generate the random GPS coordinate falling within the boundaries of the zone (polygon).

    Any help/tips would be much appreciated!


  • #2
    Here is a start that makes use of Richard Picard's -geodist- and -geoinpoly- :

    Code:
    cd "C:\Users\Scott\Desktop\tmp"
    clear
    //shapefile
    input   _ID _X _Y
    1         .         .                                                       
    1        -110        37          
    1        -110        41         
    1        -106        41        
    1        -106        37          
    1        -110         37         
    2         .         .          
    2        -106       37         
    2        -106       41        
    2        -102       41        
    2        -102       37         
    2        -106       37          
    end
    
    sort _ID
    save coordinates,replace
    
    //random coordinates
    clear
    set obs 10
    set seed 2439
    gen double lat = 37 + (41 - 37) * uniform()
    gen double lon = -110 + (110 - 102) * uniform()
    sum lat lon 
    
    //determine in which polygon the random point are located
    geoinpoly lat lon using coordinates, noprojection
    
    //calculate distance from first point
    sort _ID lat
    by _ID: gen j = _n
    reshape wide lat lon, i(_ID) j(j)
    
    forv i = 2/5{
        geodist lat1 lon1 lat`i' lon`i', gen(distance`i')
    }
    
    reshape long lat lon distance, i(_ID) j(order)
    l

    Comment

    Working...
    X