[R-sig-Geo] Error in function (classes, fdef, mtable)

Roger Bivand Roger.Bivand at nhh.no
Fri Apr 12 12:07:18 CEST 2013


On Fri, 12 Apr 2013, Ross Ahmed wrote:

> Hi all
>
> I?m trying to overlay a set of polygons over another set of polygons and
> calculate area of overlap. However, I?m getting the error:
>
> "Error in function (classes, fdef, mtable)  :  unable to find an inherited
> method for function ?is.projected? for signature ?"NULL???
>
> What am I doing wrong here? I realise this is likely to be a question
> regarding the fundamentals of coordinate reference systems, but after hours
> of googling, I cannot find a fix. My code looks like this:
>
>
> library(raster); library(sp); library(maptools); library(plyr);
> library(rgeos); library(rgdal)
>
> # make raster and convert to polygons
> sectors <- raster(ext=extent(8, 9, 12, 13), ncol=5, nrow=5, crs=NA)
> sectorsPoly <- rasterToPolygons(sectors)
> sectorsPoly at data$value <- 1:25
>
> # import shapefiles
> setwd(system.file("shapes", package="maptools"))
> columbus <- readShapeSpatial("columbus.shp")
>
> # assign coordinate system
> proj4string(sectorsPoly) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84
> +towgs84=0,0,0")
> proj4string(columbus) <- CRS("+proj=longlat +datum=WGS84 +ellps=WGS84
> +towgs84=0,0,0")
>
> # project
> sectorsPoly <- spTransform(sectorsPoly, CRS("+init=epsg:26392
> +datum=WGS84"))
> columbus <- spTransform(columbus, CRS("+init=epsg:26392 +datum=WGS84"))
>
> # calculate proportion of sectorsPoly that is overlapped by columbus
> llply(1:25, function(x) gArea(gIntersection(sectorsPoly, columbus[x,],
> byid=TRUE), byid=TRUE)/gArea(sectorsPoly[x,]))

You are indexing the wrong object - not columbus but sectorsPoly should 
be indexed 1:25:

llply(1:25, function(x) gArea(gIntersection(sectorsPoly[x,],
   columbus, byid=TRUE), byid=TRUE)/gArea(sectorsPoly[x,]))

works. Thanks for a reproducible example! Note that we do not know the CRS 
of columbus, I think the units are some multiple of miles or km, and that 
it is projected in an unknown projection.

Hope this helps,

Roger

>
>
> Thanks
> Ross
>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
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