An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/21d07290/attachment.pl>
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:
Dear list members, I am trying to save a spatial point object as a shapefile, but I got an error that I do not have the right object class, why?
> sitiosMikeCRTM05
SpatialPoints:
x y
[1,] 480244.6 1181636
[2,] 480651.1 1184371
[3,] 477555.2 1181189
[4,] 486482.8 1167946
[5,] 483764.4 1164624
[6,] 477301.6 1192979
[7,] 483737.4 1192048
[8,] 480760.2 1192652
[9,] 479985.8 1150756
[10,] 481748.5 1148902
[11,] 476126.6 1165617
[12,] 464735.7 1167079
[13,] 481182.4 1185941
[14,] 500632.8 1132727
Coordinate Reference System (CRS) arguments: +proj=tmerc +lon_0=-84
+lat_0=0 +x_0=500000 +k=0.9999 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> writeOGR(sitiosMikeCRTM05,
dsn="C:/ProyectosRespacial/CBSS/Shapefiles_proyectados",
layer="sitiosMikeCRTM05", driver="ESRI Shapefile")
Error en writeOGR(sitiosMikeCRTM05, dsn =
"C:/ProyectosRespacial/CBSS/Shapefiles_proyectados", :
obj of wrong class
Best,
Manuel
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Alexandre Villers, PhD. Postdoc researcher Spatial Ecology & Population Dynamics Section of Ecology, Department of Biology University of Turku FIN-20014 Turku Finland @mail: alexandre.villers at utu.fi phone: 00358 (0)2 333 5049 Skype You can skype me (but think of using Ekiga instead !) <skype:aquila06?call> *Use open source and free softwares* <http://cran.r-project.org/> <http://grass.itc.it/> <http://www.qgis.org/> <http://fr.openoffice.org/> <http://www.mozilla-europe.org/fr/> <http://www.mozilla-europe.org/fr/> -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: Skypelogo.jpg Type: image/jpeg Size: 5395 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment.jpg> -------------- next part -------------- A non-text attachment was scrubbed... Name: Rlogo.jpg Type: image/jpeg Size: 1753 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment-0001.jpg> -------------- next part -------------- A non-text attachment was scrubbed... Name: GRASSlogo.png Type: image/png Size: 3277 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment.png> -------------- next part -------------- A non-text attachment was scrubbed... Name: QGIS.png Type: image/png Size: 4144 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment-0001.png> -------------- next part -------------- A non-text attachment was scrubbed... Name: OOlogo.gif Type: image/gif Size: 4246 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment.gif> -------------- next part -------------- A non-text attachment was scrubbed... Name: TBlogo.jpg Type: image/jpeg Size: 1385 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment-0002.jpg> -------------- next part -------------- A non-text attachment was scrubbed... Name: FFlogo.jpg Type: image/jpeg Size: 1686 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20101104/59f5840c/attachment-0003.jpg>
On 11/04/2010 11:16 AM, Manuel Sp?nola wrote:
Dear list members, I am trying to save a spatial point object as a shapefile, but I got an error that I do not have the right object class, why?
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))
> sitiosMikeCRTM05
SpatialPoints:
x y
[1,] 480244.6 1181636
[2,] 480651.1 1184371
[3,] 477555.2 1181189
[4,] 486482.8 1167946
[5,] 483764.4 1164624
[6,] 477301.6 1192979
[7,] 483737.4 1192048
[8,] 480760.2 1192652
[9,] 479985.8 1150756
[10,] 481748.5 1148902
[11,] 476126.6 1165617
[12,] 464735.7 1167079
[13,] 481182.4 1185941
[14,] 500632.8 1132727
Coordinate Reference System (CRS) arguments: +proj=tmerc +lon_0=-84
+lat_0=0 +x_0=500000 +k=0.9999 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
> writeOGR(sitiosMikeCRTM05,
dsn="C:/ProyectosRespacial/CBSS/Shapefiles_proyectados", layer="sitiosMikeCRTM05", driver="ESRI Shapefile") Error en writeOGR(sitiosMikeCRTM05, dsn = "C:/ProyectosRespacial/CBSS/Shapefiles_proyectados", : obj of wrong class Best, Manuel
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Edzer Pebesma Institute for Geoinformatics (ifgi), University of M?nster Weseler Stra?e 253, 48151 M?nster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics e.pebesma at wwu.de
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
On 11/04/2010 11:34 AM, Barry Rowlingson wrote:
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* :)
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.
Barry
Edzer Pebesma Institute for Geoinformatics (ifgi), University of M?nster Weseler Stra?e 253, 48151 M?nster, Germany. Phone: +49 251 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de http://www.52north.org/geostatistics e.pebesma at wwu.de