[R-sig-Geo] Saving a spatial point object into a shapefile

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Nov 4 11:34:12 CET 2010


On Thu, Nov 4, 2010 at 10:25 AM, Edzer Pebesma
<edzer.pebesma at uni-muenster.de> wrote:

> Because the documentation sais so. ?writeOGR tells me:
>
> Usage:
>
>     writeOGR(obj, dsn, layer, driver, dataset_options = NULL,
> layer_options=NULL, verbose = FALSE)
>
> Arguments:
>
>     obj: a SpatialPointsDataFrame, SpatialLinesDataFrame, or a
>          SpatialPolygonsDataFrame object.

Although that's not what the code says:

 if (!"data" %in% names(getSlots(class(obj))))
        stop("obj of wrong class")

 - it just looks to see if there's a 'data' slot in the argument, and
then carries on, stumbling blindly into all sorts of problems if one
is dumb enough to create a class with a data slot:

 > setClass("foo",representation(data="numeric"))
 > z=new("foo")
 > writeOGR(z,dsn="/tmp",layer="pts",driver="ESRI Shapefile")
Error in writeOGR(z, dsn = "/tmp", layer = "pts", driver = "ESRI Shapefile") :
  NULL NULL unknown data type

Surely there should be _methods_ for writeOGR for classes that it
supports, such as SpatialPointsDataFrame. Explicitly testing for the
presence of a slot/member is worse than explicitly testing for the
name of a class when you should really use 'inherits' which is worse
than the correct approach which is writing methods...

 *end mini-rant* :)

Barry



More information about the R-sig-Geo mailing list