Skip to content

Converting GRID to SHAPE in R (was: Re: [Fwd: Re: rgdal: problem with grid data])

8 messages · Agustin Lobo, Javier Garcia-Pintado, Roger Bivand

#
Thanks for all the inputs I've got. I've
tried the following solution, but the steps
with as.SpatialPolygons.GridTopology() and
unionSpatialPolygons() take too long a time, had to abort the
second one after 1h, so
the procedure does not seem to be a really working
solution (note than I'm using the landcover map of a tiny country as example)

#Data from http://www.diva-gis.org/data/cov/ISR_cov.zip

isrlc <-
readGDAL("/media/Transcend/MASTER_ICTA2007_2008/GEODATA_miniprojs/Kefah/ISR_cov/isr_cov.gri")

 > class(isrlc)
[1] "SpatialGridDataFrame"
attr(,"package")
[1] "sp"

isrlcSP <- as.SpatialPolygons.GridTopology(getGridTopology(isrlc))

 > class(isrlcSP)
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"

isrlcSPU <- unionSpatialPolygons(isrlcSP, isrlc at data[,1])
Aborted after 1h

A second solution is saving as gtif, then gdal_polygonize:

gdal_translate isr_cov.gri isr_cov.tif
gdal_polygonize.py -f 'ESRI Shapefile' isr_cov.tif isr_cov

which takes longer for writing the commands than the actual execution time!
Then we still have to attach the legend, which can be easily done in R. So I 
think the best is writing a function using system() for the above 2 commands, then
reading the grid and the shape, attach the legend to the SPDF and save again as
shape.

So, problem solved. I'll post the function to the list as soon as I make it.

Agus
Michael Sumner wrote:
#
One further problem: it seems that readGDAL()
does not read the info from the grd file, just the gri.
Thus the legend is ignored.

Agus
Agustin Lobo wrote:
#
This is the function I've made. Instead of reading the shape files
(which takes a long time) I read the dbf file.
I also read the grd file,
writing back a modified dbf including the legend.
The only problem is that both read.dbf() and
write.dbf() read and write many more cases than those actually
present in the dbf file (hence the na.omit() after read.dbf() )
Roger, do you want me to send you the actual dbf file?

This is not perfect, but works and it's fast. Don't know
if this is a general solution dor grd files.

"migrid2shp" <- 
function(dirin="/media/Transcend/MASTER_ICTA2007_2008/GEODATA_miniprojs/Kefah/ISR_cov", 
infname="ISR_cov", 
dirout="/media/Transcend/MASTER_ICTA2007_2008/GEODATA_miniprojs/Kefah", 
ofname="ISR_cov")
{
require(rgdal)
require(foreign)
comm1 <- paste("gdal_translate ",paste(dirin,"/",infname,".gri",sep=""), " 
delme.tif")
system(comm1,inter=T)
comm2 <- paste("gdal_polygonize.py -f 'ESRI Shapefile' delme.tif ", 
paste(dirout,"/",ofname,sep=""), ofname)
system(comm2,inter=T)
system(paste("rm delme.tif"))

indbf <- read.dbf(paste(dirout,"/",ofname,"/",ofname, ".dbf",sep=""))
indbf <- na.omit(indbf)
a <- matrix(scan(paste(dirin,"/",infname,".grd",sep=""),what=" 
",skip=25,sep="=",fill=T),byrow=T,ncol=2)
b <- grep("Label", a[,1])
a <- a[b,]
n <- length(unlist(strsplit(a[,1],"Label")))
b <- as.numeric(unlist(strsplit(a[,1],"Label"))[seq(2,n,by=2)])
a[1:length(b),1] <-b
a <- data.frame(class=a[1:length(b),1], description=a[1:length(b),2])
row.names(a) <- a[,1]

b <- a[match(indbf$DN,a$class),]
indbf2 <- cbind(indbf,b)
write.dbf(indbf2,file=paste(dirout,"/",ofname,"/", ofname,".dbf", sep=""))
print(paste(dirout,"/",ofname,"/", ofname,".dbf", sep=""))
}
Agustin Lobo wrote:
3 days later
#
Hi Roger and the others,

I've noticed that readRAST6() uses closeAllConnection(), as it is trying
to close a raw "socket" type connection I had opened for parallel
processing (snow package).

Isn't this a side effect and perhaps closeAllConnection() should be
replaced here by any other alternative?

Thanks (also for the package) and best regards,
Javier

---
Javier Garc?a-Pintado
75 Trotsworth Court
Christchurh Road
Virginia Water
GU25 4AH
Surrey
United Kingdom
#
On Sun, 28 Mar 2010, jgarcia at ija.csic.es wrote:

            
Javier,

Could you try a change in the package source from sourceforge, check out 
the CVS source from:

cvs -z3 \
-d:pserver:anonymous at r-spatial.cvs.sourceforge.net:/cvsroot/r-spatial co \
-P spgrass6

I've added a flag to permit the call to closeAllConnections() to be 
avoided; you would use close_OK=FALSE. For most users it is still needed, 
as the number of connections opened in trying to guess what the GDAL GRASS 
plugin needs is not well-specified.

Hope this helps,

Roger

  
    
#
Sure! Thank you very much :-)
Javier

---
#
Hi Roger,
Just to confirm that everything goes OK.
Thanks again,
Javier
---
#
On Mon, 29 Mar 2010, jgarcia at ija.csic.es wrote:

            
Thanks, fresh spgrass6 submitted to CRAN.

Roger