Announcement

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

  • Generate random GPS points within polygons

    Hello,

    I am trying to generate 50 random GPS points in each polygon of a shapefile that I have. I've looked into -geodist- and -geoinpoly- but haven't really found a way to do this. I'm looking for a solution that does not require inputting manually a min/max lat/long, as I have a lot of polygons (+80), and their shapes are often not rectangles.

    Any tips?

  • #2
    Are they all convex?

    The idea is to exploit the property explained in this video.

    Your case is simpler since you need to generate P, having A,B,C and two random w1, and w2.

    If the polygon is convex, you can randomly select any 3 vertices for a triangle of course.
    But if not, then you need to triangulate it first.

    Best, Sergiy
    Last edited by Sergiy Radyakin; 09 Sep 2020, 00:24. Reason: Added links

    Comment


    • #3
      Here is one way: using Maurizio Pisati's -spgrid- (ssc desc spgrid) to overlay the polygons with hexagonal grid and then randomly sample center points of the grid within each polygon.

      Code:
      spgrid using "Italy-RegionsCoordinates.dta",   ///
          resolution(w5000) unit(miles)             ///
          cells("Italy-GridCells.dta")                 ///
          points("Italy-GridPoints.dta")               ///
          replace compress dots
      
      use "Italy-GridPoints.dta", clear
      spmap using "Italy-GridCells.dta", id(spgrid_id)  ocolor(gs12)  ///
          polygon(data("Italy-RegionsCoordinates.dta")     ///
          ocolor(red) osize(medium)) name(map_cells,replace)
      
      use Italy-GridPoints.dta,clear
      
      bysort spgrid_mapid: gen random_order = runiform()
      sort spgrid_mapid random_order
      by spgrid_mapid: gen tag = 1 if _n<=10
      save,replace
      list spgrid_mapid spgrid_id spgrid_xcoord spgrid_ycoord in 1/10
      
      use "Italy-RegionsData.dta", clear
      spmap using "Italy-RegionsCoordinates.dta", id(id)  /// 
          point(data(Italy-GridPoints.dta) select(keep if tag ==1 ) /// 
          x(spgrid_xcoord ) y(spgrid_ycoord) size(*.5) /// 
          fcolor(blue%50) ocolor(blue%10))
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	156.2 KB
ID:	1571990

      Comment


      • #4
        Thanks both for the super useful advice. Scott, your code works perfectly for what I was trying to do. Thanks!

        Comment

        Working...
        X