Announcement

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

  • Find centroids after mergepoly

    Dear Statalisters

    I'm using Robert Picard's mergepoly command to combine small areas on a map into larger areas. Is there a way to get the centroids of the new polygons? I tried D.H. Judson's centroid command, but that runs into an error, unfortunately, and only seems to output the distance between points, but not the coordinates of the centroids.

    Any help appreciated!

    Carolin




  • #2
    There's no centroid options in mergepoly.

    You can use the same code that shp2dta uses to generate centroids. Just load your coordinates in memory and run this code:

    Code:
    bys _ID: gen float TEMPa=(_X*_Y[_n+1])-(_X[_n+1]*_Y) if _n>1 & _n<_N
    bys _ID: gen float _AREA=sum(TEMPa)
    bys _ID: replace _AREA=_AREA[_N]/2
    bys _ID: gen float TEMPx=(_X+_X[_n+1])*(_X*_Y[_n+1]-_X[_n+1]*_Y) if _n>1 & _n<_N
    bys _ID: gen float _CX=sum(TEMPx)
    bys _ID: replace _CX=_CX[_N]/(6*_AREA)
    bys _ID: gen float TEMPy=(_Y+_Y[_n+1])*(_X*_Y[_n+1]-_X[_n+1]*_Y) if _n>1 & _n<_N
    bys _ID: gen float _CY=sum(TEMPy)
    bys _ID: replace _CY=_CY[_N]/(6*_AREA)
    collapse _CX _CY, by(_ID)
    You can then merge these centroids to your shapefile database dataset.

    Comment


    • #3
      Thanks a lot, works a charm!

      Comment

      Working...
      X