[R-sig-Geo] get the centroid of multiple grid centroids

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon May 16 09:55:11 CEST 2011


On Mon, May 16, 2011 at 8:41 AM, Mathieu Rajerison
<mathieu.rajerison at gmail.com> wrote:
> Hi,
>
> To get the centroids, you just have to type this:
>
> ctds<-coordinates(polygons)
>
> polygons being a SpatialPolygons(DataFrame) object
> and
> ctds being a SpatialPoints(DataFrame) object

 I'm not sure thats exactly what she needs here...

>> I have multiple bird distribution maps in the form of ArcGIS shape files.
>> The distribution maps are composed of grid polygons. (1 grid= 100 km*
>> 100km)

 Which makes me think you have files like 'bird001.shp', and
'bird002.shp' and so on. One for each bird. The shapefile then has a
square polygon for each grid square the bird has visited. Is that
right?

>> Now I want to obtain the centroids of each bird distribution maps.
>> In the first step, I use "Feature to point"  to retrieve centroid of each
>> grid within the polygons.
>> Then, please kindly advise if any method in R is available to get the
>> centroid of the grid centroid of each polygons.

 The centroid of a set of points of equal weight is simply the
arithmetic mean of the x and y coordinates. (Note this isn't true for
a polygon defined by a set of points). So just import your point
features into R (save as a point shapefile, read in as a
SpatialPointDataFrame, get the coordinates with coordinates(X) as a
two-column matrix, then take the mean of each column.

 If you read in your original grid squares into R as
SpatialPolygonDataFrame, "G" say, then you can do the equivalent to
'Feature to Point' by doing pts=coordinates(G). That will give you the
coordinates of the centre of each grid square.

 The other way of doing this would be to merge all the grid squares in
R to a single feature (a SpatialPolygonDataFrame with one row) and
using coordinates(XX) on that to get a single coordinate. But I think
you'd get the same answer.

 If you want to weight some grid squares more than others, maybe by
the number of visits by the bird, then duplicate the coordinate that
many times in the coordinates matrix and take the means again.

Barry



More information about the R-sig-Geo mailing list