Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...