[R-sig-Geo] readOGR or writeOGR not closing connections?

MacQueen, Don macqueen1 at llnl.gov
Tue Dec 4 02:03:39 CET 2012


I am using R to help me interactively create and manage GIS layers, stored
as shapefiles, and I'm encountering a problem. From time to time I get an
error:

Error in writeOGR(tmpp, "shps", fn, "a.name", driver = "ESRI Shapefile", :
Layer creation failed

I can work around it by quitting and restarting R, but it would be nice if
I didn't have to.


Below is a little script that reproduces the behavior (on my machine).
('shps' is a sub-directory)



The implication, as I see it, is that one or both of readOGR or writeOGR
is not closing some connections. If I'm right about this, it would be nice
to have a way to force-close them whenever necessary, but I haven't found
a way to do that. Information for how, or any other suggestions, would be
appreciated.



Note that in each iteration there are 4 files written and 4 files read.
  2*4*14=112
which is getting close to the number available according to ?open:

-- quote --
    A maximum of 128 connections can be allocated (not necessarily
     open) at any one time.  Three of these are pre-allocated (see
     'stdout').  The OS will impose limits on the numbers of
     connections of various types, but these are usually larger than
     125.
-- end quote --





#### begin script
require(sp)
require(rgdal)

print(sessionInfo())

data(meuse.grid)
coordinates(meuse.grid) = ~x+y
tmpp <- meuse.grid[1:10,]

for(i in 1:100) {
  fn <- paste('tmpp',i,sep='')
  writeOGR(tmpp, 'shps',fn,'a.name',driver='ESRI Shapefile',
overwrite_layer=TRUE)
  foo <- readOGR('shps',fn)
}

print(sessionInfo())
#### end script


And here is the output when I source the script in a fresh R session

-----------------------------
>source('try-problem.r')
Loading required package: sp
Loading required package: rgdal
rgdal: (SVN revision 360)
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:
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/rgdal/gdal
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
Path to PROJ.4 shared files:
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/rgdal/proj

R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C

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

other attached packages:
[1] rgdal_0.7-24 sp_1.0-2

loaded via a namespace (and not attached):
[1] grid_2.15.2     lattice_0.20-10

OGR data source with driver: ESRI Shapefile
Source: "shps", layer: "tmpp1"
with 10 features and 5 fields
Feature type: wkbPoint with 2 dimensions
OGR data source with driver: ESRI Shapefile
Source: "shps", layer: "tmpp2"
with 10 features and 5 fields
Feature type: wkbPoint with 2 dimensions

--- output from iterations 2 through 12 omitted ---

OGR data source with driver: ESRI Shapefile
Source: "shps", layer: "tmpp13"
with 10 features and 5 fields
Feature type: wkbPoint with 2 dimensions
OGR data source with driver: ESRI Shapefile
Source: "shps", layer: "tmpp14"
with 10 features and 5 fields
Feature type: wkbPoint with 2 dimensions
Error in writeOGR(tmpp, "shps", fn, "a.name", driver = "ESRI Shapefile",  :
  Layer creation failed
>

## then manually:

>print(sessionInfo())
Error in gzfile(file, "rb") : cannot open the connection
In addition: Warning message:
In gzfile(file, "rb") :
  cannot open compressed file
'/Library/Frameworks/R.framework/Versions/2.15/Resources/library/rgdal/Meta
/package.rds', probable reason 'Too many open files'



I'm beginning to suspect this has to do with "gzcon" or "gzfile"
connections (not sure of correct terminology).
?open also says,

-- quote --
     'close' closes and destroys a connection.  This will happen
     automatically in due course (with a warning) if there is no longer
     an R object referring to the connection.
-- end quote --

But ?showConnections says

-- quote --
... However, if there is no R level object referring to
     the connection it will be closed automatically at the next garbage
     collection (except for 'gzcon' connections).
-- end quote --


So if read/write OGR involve zipping and unzipping, I guess the
connections don't get closed. (?)

Anyway, it would be very nice if I didn't have to quit and restart my R
sessions every so often when I'm working with my shapefiles.


Thanks
-Don



More information about the R-sig-Geo mailing list