[R] SpatialPolygonsDataFrame and unique()
Roger Bivand
Roger.Bivand at nhh.no
Thu Jul 25 17:35:37 CEST 2013
Nicola Rossi <nicola.rossi20 <at> gmail.com> writes:
>
> Hello everyone!
>
> I'm a newbie in using the RGDAL and sp packages in R and as written in the
> object I have a problem with a SPDF and unique():
Consider posting to the R-sig-geo list; this is not a general R question.
>
> I would have liked to write a simple script to delete in a couple of clicks
> the duplicated nodes that sometimes pop-up during the digitizing process in
> QGIS (I know that this can be done by hand, but when you have lots of
> features using a script in R would save some time).
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:
> validObject(slot(slot(spatial, "polygons")[[1]], "Polygons")[[1]])
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
>
> I tried to use unique() in the "coords" slot, but it simply doesn't work
> and I can't figure out why. here's a copy of the script that I wrote:
>
> > library(rgdal)
> > p4s<-"+proj=tmerc +lat_0=0 +lon_0=21 +k=1 +x_0=1500000 +y_0=0 +ellps=intl
> +units=m +no_defs"
> > layer<-c("Kataja")
> > dsn<-c("C:/Users/vagabond/Desktop/copy_for_r")
> > spatial<-readOGR(dsn=dsn,layer=layer,p4s=p4s)
> > len<-length(spatial)
> > for (i in 1:len){
> + temp<-length(spatial <at> polygons[[i]] <at> Polygons[[1]] <at>
coords[,1])-1
> + spatial <at> polygons[[i]] <at> Polygons[[1]] <at>
coords<-unique(spatial <at> polygons
> [[i]] <at> Polygons[[1]] <at> coords[2:temp,])
> + }
>
> Thank you very much for the help!
>
> Nicola
>
Please never post HTML!
> [[alternative HTML version deleted]]
>
>
More information about the R-help
mailing list