[R-sig-Geo] RE : 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 22:13:43 CEST 2012


Thanks Roger for your response,

I'm happy that the problem is now known, however I'm afraid I won't be able to help much with it, I'm no programmer just a user.  Hopefully somebody out there will be able to put your explanations to good use and provide a long term solution.

Thanks again,

Bastien


-----Message d'origine-----
De : Roger Bivand [mailto:Roger.Bivand at nhh.no]
Envoyé : 14 mai 2012 14:06
À : Ferland-Raymond, Bastien (DIF)
Cc : r-sig-geo at r-project.org
Objet : Re: [R-sig-Geo] Problem with writeOGR and overwrite_layer=T

On Mon, 14 May 2012, Roger Bivand wrote:

> On Mon, 14 May 2012, Bastien.Ferland-Raymond at mrnf.gouv.qc.ca wrote:
>
>> 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.
>
> I can reproduce the problem on Windows 7 with GDAL 1.9.0, that is, that parts
> of shapefiles created by OGR (the *.dbf and *.shp files) cannot be deleted by
> poDS->DeleteLayer() - line 473 in rgdal/src/ogrsource.cpp. This also applies
> before trying to overwrite them - they cannot be deleted in Windows Explorer
> either until the R session exits. Running as administrator does not help
> either.
>
> This appears to be an interaction between Windows (7 in my case), GDAL >=
> 1.8.0, which introduced bans on overwriting for Shapefiles, and possibly some
> unknown factors.
>
> This is not a problem I can resolve, certainly not any time soon. It is not
> an R code problem as such (I believe), and the stackoverflow speculation is
> misleading, this has nothing to do with connections, only with Windows
> apparent file usage status.
>
> I would appeal to interested Windows users to contribute a solution - you
> will need to be able to cross-compile GDAL for Windows under MSYS, and to
> build R packages.

Actually, it isn't so hard to contribute, as WinBuilder should be able to
produce viable Windows binary packages. So contributors should check out
rgdal anonymously from R-Forge, then let me have patches to insert, submit
to WinBuilder, and return a download link (say 6 hours later) to see if
the patch helped. So maybe a local build of GDAL may not be needed, but
the time involved would be greater.

Roger

> If Windows users know how to unlink files with an incorrect
> usage status without exiting R, this would possibly help, but doesn't get us
> much further.
>
> Other GDAL/OGR uses such as ogr2ogr exit on error, but it might be helpful to
> know if ogr2ogr does the same - that is, is unable to overwrite a layer.
> MapInfo File, GML and other drivers do not seem to suffer from the same
> issue. Again, checking across a wider range of drivers would help.
>
> Roger
>
>>
>> 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
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>

--
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