[R-sig-Geo] extending sp classes
Roger Bivand
Roger.Bivand at nhh.no
Sat Oct 7 14:32:44 CEST 2006
On Fri, 6 Oct 2006, Roger Bivand wrote:
> On Fri, 6 Oct 2006, Katarzyna Adamczyk wrote:
>
> > Dear members of r-sig-geo,
> >
> > I discovered "sp" library and I found
> > it very usefull for switching
> > between different classes of R spatial
> > libraries.
> >
> > I defined my own class, extending
> > SpatialPolygonsDataFrame.
> > When I try to apply "spplot" function to
> > the extended class, it doesn't work :
> > I have a coercion problem error message,
> > but I don't know what it means.
> >
> > I proceeded as follows:
> >
> > I considered SpatialPolygonsDataFrame help example :
> >
> > grd <- GridTopology(c(1,1), c(1,1), c(10,10))
> > polys <- as.SpatialPolygons.GridTopology(grd)
> > centroids <- getSpPPolygonsLabptSlots(polys)
> > x <- centroids[,1]
> > y <- centroids[,2]
> > z <- 1.4 + 0.1*x + 0.2*y + 0.002*x*x
> > ex_1.7 <- SpatialPolygonsDataFrame(polys, data=data.frame(x=x, y=y, z=z, row.names=getSpPPolygonsIDSlots(polys)))
> > spplot(ex_1.7)
> >
> >
> > I defined my own class :
> >
> > > setClass("myclass",contains="SpatialPolygonsDataFrame")
> > [1] "myclass"
> > > ex<-ex_1.7
> > > class(ex) = "myclass"
> > > is(ex)
> > [1] "myclass" "SpatialPolygonsDataFrame"
> > [3] "SpatialPolygons" "Spatial"
> > > spplot(ex)
> > Erreur dans as.data.frame.default(obj) : cannot coerce class "myclass"
> > into a data.frame
> >
> > Did anyone of you encounter a similar problem and can help me?
>
> Well, writing new-style classes is quite advanced. If you are not on
> Windows, please download the source package, and try editing line 158 of
> R/spplot.R from:
>
> sdf = as.data.frame(obj)
>
> to
>
> sdf = as(obj, "data.frame")
>
> to see if that way of doing method dispatch works:
>
> > t1 <- as.data.frame(ex)
> Error in as.data.frame.default(ex) : cannot coerce class "myclass" into a data.frame
> > t2 <- as(ex, "data.frame")
> > class(t2)
> [1] "data.frame"
>
> suggests that it might. I guess that "myclass" actually is richer, please
> let us know how you get on.
>
> If you are on Windows, you will need to write a small wrapper function
> like:
>
> > as.data.frame.myclass <- function(obj) obj at data
> > t1 <- as.data.frame(ex)
> > class(t1)
> [1] "data.frame"
>
> This doesn't yet work, but you get the idea - provide an old-style method
> to match the one that cannot be found.
The missing bit, after providing an as.data.frame method for your class,
was to either specify the zcol= argument:
spplot(ex, zcol=c("x", "y", "z"))
or provide a names() method for your class:
names.myclass <- function(x) names(x at data)
spplot(ex)
The next > 0.9-2 release of sp will remove the as.data.frame() problem,
but a names() method will be needed if zcol= cannot be used.
Roger
>
> Roger
>
> >
> > Thank you in advance.
> >
> > Katarzyna
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at stat.math.ethz.ch
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list