首页 > 解决方案 > R sf: Points of LinearRing do not form a closed linestring

问题描述

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

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.

标签: rsf

解决方案


无需运行循环来查找哪个几何图形不好。该st_is_valid()函数应该告诉您哪些行有问题。

看起来您的几何图形之一可能由不正确数量的点组成。

有关在 r-spatial 查找和解决问题的更多信息:https ://www.r-spatial.org/r/2017/03/19/invalid.html


推荐阅读