[R-sig-Geo] Problem with writeOGR and overwrite_layer=T

Bastien.Ferland-Raymond at mrnf.gouv.qc.ca Bastien.Ferland-Raymond at mrnf.gouv.qc.ca
Mon May 14 16:51:03 CEST 2012


Dear list,

I have to produce shapefile for a coworker which uses ArcGIS and I've hit a problem with the writeOGR function from the rgdal package.

Here is an example:

###

> library(rgdal)
Loading required package: sp
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.9.0, released 2011/12/29
Path to GDAL shared files: \\sebpriv/usagers/Ferba1/R/R-2.15.0/library/rgdal/gdal
Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470]
Path to PROJ.4 shared files: \\sebpriv/usagers/Ferba1/R/R-2.15.0/library/rgdal/proj
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=French_Canada.1252  LC_CTYPE=French_Canada.1252    LC_MONETARY=French_Canada.1252 LC_NUMERIC=C                   LC_TIME=French_Canada.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rgdal_0.7-8 sp_0.9-98

loaded via a namespace (and not attached):
[1] grid_2.15.0    lattice_0.20-6
> setwd("C:\\Documents and Settings\\ferba1\\Bureau\\test rgdal")

# make a basic shape:

> coo <- coordinates(cbind(runif(100,220000,300000),runif(100,5160000,5220000)))
> point.shape <- SpatialPointsDataFrame(coo,data.frame(ID=paste("point",1:100,sep="_")),
+   proj4string = CRS(" +proj=tmerc +lat_0=0 +lon_0=-73.5 +k=0.9999 +x_0=304800 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"))

# now if I try to write itm it works fine for the first 2 shapes:

> writeOGR(point.shape,".","point_shape1", driver="ESRI Shapefile")
>
> writeOGR(point.shape,".","point_shape2", driver="ESRI Shapefile" )

# but then if I try to overwrite, it fails:

> writeOGR(point.shape,".","point_shape1", driver="ESRI Shapefile", overwrite_layer=T)
Error in writeOGR(point.shape, ".", "point_shape1", driver = "ESRI Shapefile",  :
  Creation of output file failed

# Interestingly, it erases 2 files from point_shape1 before failing (the .prj and the .shx).
# and from now on, it's impossible to create new shapefile:

> writeOGR(point.shape,".","point_shape3", driver="ESRI Shapefile" )
Error in writeOGR(point.shape, ".", "point_shape3", driver = "ESRI Shapefile") :
  Creation of output file failed
###

However, after restarting the whole session and erasing the files created, and try this new order:

###

>
> writeOGR(point.shape,".","point_shape1", driver="ESRI Shapefile")
>
> writeOGR(point.shape,".","point_shape1", driver="ESRI Shapefile", overwrite_layer=T)
>
> writeOGR(point.shape,".","point_shape2", driver="ESRI Shapefile" )
>
> writeOGR(point.shape,".","point_shape3", driver="ESRI Shapefile" )
>

###
Everything works fine.  So two questions arise:

1) why does overwrite_layer doesn't work when the order is changed, can it be fixed?
2) How to "reset" the writeOGR function after a fail as it seems once it fails, it get impossible to reuse it in the same session.

After reading this post: http://stackoverflow.com/questions/6219368/problem-writing-shapefiles-with-writeogr-in-r, I think it may be some connection issue.  Is there a easy way to close manually the connection after using writeOGR?  Something like close.OGR.connection()?

I know maptools::writeSpatialShape can also be used to write shapefile, however it doesn't write the projection information which I want. It seems to me that there is a problem with writeOGR, which is a good function so it could deserve to be fix.

Thanks for your help,

Bastien Ferland-Raymond, M.Sc. Stat., M.Sc. Biol.
Division des orientations et projets spéciaux
Direction des inventaires forestiers
Ministère des Ressources naturelles et de la Faune



More information about the R-sig-Geo mailing list