[R-sig-Geo] Summary: ArcView shape files and GeoBUGS

Tord Snäll tord.snall at ekol.slu.se
Thu Feb 5 14:56:35 CET 2009


Thanks to you all for prompt replies!

As evident from the replies pasted below there are different solutions 
to this problem.

cheers,
Tord

------------------------------------------------------------------------------
"There are some tools avaiable to transform maps to \pkg{GeoBUGS}
format: the function \code{sp2WB} of package \pkg{maptools} can
transform \code{SpatialPolygons} object to the S-Plus mapin \pkg{R}
\citep{R}, or the ArcGIS extension of W. Thogmartin and colleagues what
helps users to develop adjacency
matrice.\footnote{http://www.umesc.usgs.gov/management/dss/adjacency
\_tool.html}."

my tool (maps2winbugs) has other functions as well to prepare maps for
geobugs
unfortunately it works just on MS Windows

Also:
http://maps2winbugs.sourceforge.net/

all the best
Norbert

------------------------------------------------------------------------------ 


If you are using R, you can use the function sp2WB in maptools to
convert your data into a format that WB can import from a shapefile.
Look at the manual page of sp2WB.

Hope this helps,

Virgilio

P.S: THis is the example in that manual page:

 xx <- readShapePoly(system.file("shapes/sids.shp",
package="maptools")[1],
      IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
     plot(xx, border="blue", axes=TRUE, las=1)
     sp2WB(as(xx, "SpatialPolygons"), filename="NC.map")
     x <- readAsciiGrid(system.file("grids/test.ag",
package="maptools")[1])
     xp <- as(x, "SpatialPixelsDataFrame")
     pp <- as.SpatialPolygons.SpatialPixels(xp)
     sp2WB(pp, filename="test.map")

------------------------------------------------------------------------------ 


Tord. Have found the easiest way to address this issue is to read the shape
files into R, then convert them into splus format, which bugs will import.
Following is the code:

library(maptools) # loads sp library too
library(maps)

# read in shape files (single folder with all associated files, no
extension)
x<-readShapePoly("C:\\xxx\\xxx\\xx\\")
# display the shapefile
plot(x) 
# convert the shape file into splus format to import into GeoBugs
sp2WB(x, "C:\\xxx\\xxxx\\xxxx\\xxxx.txt") # note '.txt' extension

Cheers
Charles

------------------------------------------------------------------------------ 

Dear Tord,

You can use R to convert a shapefile into a format readable from 
GeoBUGS. There are several R packages to handle geographical 
information. An easy way to convert a .shp file into what GeoBUGS refers 
to as SPLUS format  could be:

1) Load library maptools in R
library(maptools)

2) Import the shapefile into R:
object.shp <- readShapePoly("C:/mydir/myshapefile.shp", IDvar="area_id")

3) Export it in Splus format
sp2WB(as(object.shp, "SpatialPolygons"), filename="myname.txt")

Hope this helps.
Juanjo
------------------------------------------------------------------------------ 

Hi, I had the same problem and one solution is download the program 
maps2WinBUGS is free or you can look the libraries maps and maptools in 
R  the maptools package allows the importation of ArcView shape files to 
R (readShapePoly function) and also the export of maps to WinBUGS format 
with the function sp2WB. Both spatstat and splancs have functionality 
for mapping point process data.
Chao.
------------------------------------------------------------------------------ 


Dear Tord,

Look at the example in the manual page of function sp2WB (package
maptools). I believe that it is what you need.

Hope this helps.
Virgilio

------------------------------------------------------------------------------ 


I think this is it:
http://www.biostat.umn.edu/~brad/yuecui/index.html
http://www.biostat.umn.edu/~brad/yuecui/convert.r


#/09/23/03 The only different btw this convert.r and convert.s is that
#in convert.s, we us AsciiToInt("character")-AsciiToInt("0") to change
#character into numeric, in convert.r, we use type.convert("character")
#to do this.

#/09/23/03
#This version of convert.s corrects the problems found in convert2&3.s

#Tried every combination of options for scan, the following doesn't work.
#checking by comparing the number of observations in fortest and in .cgm file
#fortest<-scan("test.cgm",what=list(name=""),widths=c(90),multi.line=F,sep="\n")
#source("convert.r")

convert<-function(cgmfile)
  {
#outfile <- "test.txt"
 outfile<-paste(cgmfile,".txt",sep="")

#This one works
#fortest<-scan("test.cgm",what=list(name=""),sep="\n")
 fortest<-scan(paste(cgmfile,".cgm",sep=""),what=list(name=""),sep="\n")

fortest<-fortest$name
fortest<-fortest[grep("VIS",fortest)]
fortest<-as.matrix(fortest)

totpolyn<-length(fortest[grep("POLYGON_SET",fortest)])

polyn<-0
count<-0;
indicator<-0;
#First tried rep(0,1000), but there is a polygon with 167 rows, so we get
#167*3*2=1002 coordinates in one polygon. Error occurs with NA output
coord<-rep(0,5000)

write(paste("map:",totpolyn,"\n"),outfile)
for (i in 1:totpolyn) {
    write(paste( i, paste("grid",i,sep="")),outfile,append=T)
  }


for (i in 1:length(fortest)){
  for(j in (1:nchar(fortest[i]))){
      letter<-substring(fortest[i],j,j)
      #At first try to use AsciiToInt,stupid
      #if (AsciiToInt(letter)<=AsciiToInt("9")&
      #    AsciiToInt(letter)>=AsciiToInt("0"))
      if(letter<="9"&letter>="0"){
                         if (indicator==0) {count<-count+1;
                                            indicator<- 1
                                         }
                            coord[count]<-coord[count]*10+
                                 type.convert(letter)
                                 }
      #add the following else if statement because if and only when
      #a POLYGON_SET is encounted, polyn is increased by 1
      else if (letter=="P"){ polyn<-polyn+1;indicator<-0}

      #a CLOSEVIS is encounted, output the coordinates set, this is
      #done independently with increase of polyn since a POLYGON_SET may
      #consist of several small polygons
      else if(letter=="C"){
        coordmat<-cbind(rep(paste("grid",polyn,sep=""),count/2),
                        coord[2*(1:(count/2))-1],
                        coord[2*(1:(count/2))])

        if (polyn == 1) { write("",outfile,append=T)}
        else            { write(c(NA,NA,NA),outfile,append=T)}
        write(t(coordmat),outfile,append=T,ncol=3)
        #polyn<-polyn+1
        #the above statement is not right because 2 CLOSEVIS may lie in one
        #same polygon, but we output a polygon coordinates set whenever a
        #CLOSEVIS is encountered, although it may have the same label,
        #denoted by "grid&polyn" with previous or next coordinate sets.

        count<-0
        indicator<-0
        coord<-rep(0,5000)
        #if(grep("\r",substring(fortest[i],j+1,nchar(fortest[i]))))
        #break
       }

      else        indicator<-0
   }
}
write("END",outfile,append=T)
}
------------------------------------------------------------------------------
Hi Tord,

I think you will find the extract you require in this paper
https://beardocs.baylor.edu/bitstream/2104/3955/1/john_mcbride_phd.pdf

Search the document for Yue Cui and it will bring you to the page.

I have used the code myself and it does work.

Hope it helps.  

Karen
------------------------------------------------------------------------------
I believe that you are would like to convert a shapefile
into a BUGS map, correct?  If so, download maps2WinBugs and follow the tutorial
on how to do so. Just Google the program and it will pop right up.

Erinn

------------------------------------------------------------------------------



Tord Snäll wrote:
> Dear all,
> I would like to read in ArcView (or ArcGIS) shape files from which I 
> will pick information if fitting a geostatistical model using WinBUGS. 
> In the GeoBUGS Used Manual it says
> "GeoBUGS does not have an option for loading ArcView shape files 
> directly. However, Ms Yue Cui at the University of Minnesota has 
> written programs in Splus and R for converting shape files into the 
> GeoBUGS Splus format so that they can be loaded in GeoBUGS 
> (http://www.biostat.umn.edu/~yuecui/). "
>
> Unfortunately this site is unavailable, and the Biostatstics homepage 
> at University of Minnesota does not say anything about neither BUGS or 
> Ms Yue Cui.
>
> Does anyone have this R code, or can point me to Ms Yue Cui?
>
> Thanks,
> Tord
>

-- 

Tord Snäll
Department of Ecology / Swedish Species Information Centre
Swedish University of Agricultural Sciences (SLU)
P.O. 7044, SE-750 07 Uppsala, Sweden
Office/Mobile/Fax
+46-18-672612/+46-76-7662612/+46-18-673537
E-mail: tord.snall at ekol.slu.se 
www.ekol.slu.se/staff_tordsnall
www.artdata.slu.se/personal/fototsn.asp




More information about the R-sig-Geo mailing list