Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

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

  • 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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's no need to run a loop to find which geometry is bad. The st_is_valid() function should tell you which row(s) have the problem.

It looks like one of your geometries might be made up of an incorrect number of points.

More info about finding and fixing the problem at r-spatial: https://www.r-spatial.org/r/2017/03/19/invalid.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...