Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem writing shapefiles with writeOGR in R

Tags:

r

gdal

I've been getting an error in a script that iterates over some text files in a directory, does some data manipulation and writes to a ESRI shapefile using writeOGR from the rgdal package in R. I keep getting an error after a certain number of files are written (appears to be about 43-44 individual files, or ~14 complete shapefiles, as each shapefile creates a number of files).

The error is usually something like

GDAL Error 4: Unable to open ./test_8.shp or ./test_8.SHP

Additionally, after the script has crashed I get an error like

 cannot open file 'file_test.R': Too many open files

When I try to read or do anything with another file.

showConnections

does not show any open files at this point.

Here is some code that reproduces the problem:

library(gstat)
library(rgdal)
data(meuse)

coordinates(meuse) = ~x + y

for (i in 1:50){
    writeOGR(meuse,'.',paste('test',i,sep="_"),driver="ESRI Shapefile")
}

I'm using R 2.12.2 on OSX. Error happens on both 64 bit and 32 bit versions.

like image 354
Jesse Anderson Avatar asked Oct 10 '22 20:10

Jesse Anderson


1 Answers

Your code worked for me on Ubuntu 11.04 and R 2.13, might be worth also to try writeSpatialShape from the maptools package.

like image 157
johannes Avatar answered Oct 17 '22 15:10

johannes