0

I'm trying to find the centroid (center of mass) for several IUCN species range maps. Using the code below I've run into an issue in R.

library(rgdal)
library(rgeos)
shp <- readOGR("/Documents/data_1/data_0.shp")
class(shp)
plot(shp)
writeOGR(shp, ".", "shp", driver="ESRI Shapefile")
cents <- coordinates(shp)
cents <- SpatialPointsDataFrame(coords=cents, data=shp@data, 
                                proj4string=CRS("+proj=longlat +datum=WGS84"))
points(cents, col = "Blue")
writeOGR(cents, ".", "cents", driver="ESRI Shapefile")
trueCentroids = gCentroid(shp,byid=TRUE)
points(coordinates(shp),pch=1)
points(trueCentroids,pch=19,col = "Red", cex = 1.8)

This is one of the resulting maps

Map of Duttaphrynus melanostictus habitat range

The problem is that for some of the polygon maps I'm finding multiple centroids [as shown above in red] instead of just one (same result in QGIS).

Is there a way to change my coding and find a single center of mass for the polygon?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338

0 Answers0