1

I am trying to get a pseudo barycenter for polygons in a spatial polygon dataframe. Today I stumbled upon the coordinates function that actually returns something for a SpatialPolygonsDataFrame.

Unfortunately I found nothing in the help of coordinates about the value for SpatialPolygonsDataFrame. Could somebody tell me what these coordinates are?

cmbarbu
  • 4,354
  • 25
  • 45

2 Answers2

4

It is the polygon centroid. The source code is found here, look for function FindCG. The equations computed are equivalent to those found on wikipedia, but in addition deal with the special case of polygons with (near) zero area, and normalize polygon coordinates by the first point (to increase numerical precision and/or avoid overflow).

Edzer Pebesma
  • 3,814
  • 16
  • 26
  • you probably mean findCG? Also how did you figure out that this specific piece of code is defining the labpt slot? – cmbarbu Apr 13 '15 at 12:49
  • Yes. The constructor functions at some stage call there C counterpart, e.g. the`Polygon` R function calls `.Call(Polygon_c, coords, n, ihole)` which calls the underlying C function `Polygon_c`; these call then other C functions in turn. I am maintainer of sp, and author of large parts of it, although not these parts. – Edzer Pebesma Apr 14 '15 at 13:45
  • Great, can you correct your answer adding that to it, specially the correction? Also I just posted this question I would greatly appreciate look at : http://stackoverflow.com/q/29629049/1174052 – cmbarbu Apr 14 '15 at 13:49
0

Reading the definition of coordinates for SpatialPolygonsDataFrame I can see that it is actually the same than getSpPPolygonsLabptSlots as it retrieves the labpt slot, that is to say a convenient point to put a label for the polygon.

> selectMethod("coordinates",signature="SpatialPolygonsDataFrame")
Method Definition:

function (obj, ...) 
{
    .local <- function (obj) 
    {
        ret = t(sapply(slot(obj, "polygons"), function(i) slot(i, 
            "labpt")))
        dimnames(ret) = list(sapply(slot(obj, "polygons"), function(i) slot(i, 
            "ID")), NULL)
        ret
    }
    .local(obj, ...)
}
cmbarbu
  • 4,354
  • 25
  • 45