Skip to content
Prev 327431 / 398502 Next

SpatialPolygonsDataFrame and unique()

Nicola Rossi <nicola.rossi20 <at> gmail.com> writes:
Consider posting to the R-sig-geo list; this is not a general R question.
Have you actually thought through what you are doing in the light of the
fact that Polygon objects are defined as having coords slots "Object of
class "matrix"; coordinates of the polygon; first point should equal the
last point" in ?"Polygon-class"? You are choosing the 2:n-1 rows of this
matrix, so removing the end points which must by definition be identical.

So for an arbitrary SpatialPolygons object, you see:
Error in validObject(slot(slot(spatial, "polygons")[[1]], "Polygons")[[1]]) : 
  invalid class ?Polygon? object: ring not closed

There are lots of other infelicities in your script, which uses the internal
@ operator - never do this, always use access functions, lapply(), and
class-based constructors to ensure that the related internal objects get
updated. With your coding choices, none of the automatic mechanisms checking
validity get invoked.

You would need to reconstruct the coords matrix by retaining the first and
last rows, and using unique only on rows 2:n-1, using rbind(), then pass
this through Polygon() and Polygons() to recreate the internal representations.

Hope this clarifies,

Roger
coords[,1])-1
coords<-unique(spatial <at> polygons
Please never post HTML!