Skip to content

Saving a spatial point object into a shapefile

5 messages · Manuel Spínola, Alexandre Villers, Barry Rowlingson +1 more

#
Hey,

The writeOGR function specifies (?writeOGR)
|obj| a SpatialPointsDataFrame, SpatialLinesDataFrame, or a 
SpatialPolygonsDataFrame object.

You need to convert first your SpatialPoints into spgdf

Best regards


Alex
On 04/11/2010 12:16, Manuel Sp?nola wrote:

  
    
#
On 11/04/2010 11:16 AM, Manuel Sp?nola 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.
...

you can create a SpatialPointsDataFrame with dummy attributes from your
object by:

x = SpatialPointsDataFrame(sitiosMikeCRTM05, data.frame(dummy = rep(1,14))

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

            
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
#
On 11/04/2010 11:34 AM, Barry Rowlingson wrote:
Although you're the first one to bring up this feature (bug?) of
writeOGR, I fully agree. Using methods would also allow e.g. package
raster to define a writeGDAL method for Raster, stacks, bricks, and the
like. In that case we might have to define the method first in sp (rgdal
depends on sp), so that other package don't require rgdal - CRAN still
does not provide OS-X and solaris ports.