I am trying to calculate the centroids of a set of polygons.
My dataset, geodata
, contains five columns including one geometry
column of class sfc_GEOMETRY
, with 45759 rows.
When I run sf::st_centroid(geodata)
, I get the following message
Error in CPL_geos_op("centroid", x, numeric(0), integer(0), numeric(0), : Evaluation error: IllegalArgumentException: Points of LinearRing do not form a closed linestring.
In addition: Warning messages:
1: In st_centroid.sf(geodata) : st_centroid assumes attributes are constant over geometries of x
2: In st_centroid.sfc(st_geometry(x), of_largest_polygon = of_largest_polygon) : st_centroid does not give correct centroids for longitude/latitude data
- Should I run a loop to detect which geometry is not closed?
- Is this a problem with the class of my geometry? Should it be
sfc_MULTIPOLYGON
?
Possible solution:
I was encountering this problem when reading in a list of files through a loop. The loop would read in the files and then rbind
them together into geodata
, and then calculate the centroid:
for(i in 1:length(list)){
file <- st_read(list[i])
geodata <- rbind(geodata, file) #geodata is here a void sf object
}
geocent <- st_centroid(geodata)
When I calculated the centroids within the loop (for each file in the list), the error disappeared.
for(i in 1:length(list)){
file <- st_read(list[i])
file <- st_centroid(file)
geocent <- rbind(geodata, file) #geodata is here a void sf object
}
Hence, I think the problem lay in the binding operation.
- Perhaps I had not defined my void
sf
object in the right manner. - Perhaps
rbind
was not the appropriate function, or I should have specified its parameters.