Ekkehardt.Altpeter@bag.admin.ch
2003-Feb-13 13:02 UTC
[R] ESRI shape file import and time-space models
Dear R user, I am running R under Windows 2000. I am looking for a routine for importing - shape files (ESRI) into R - dbase files (FOXPRO) into R and I am looking for time-space models for description and prediction of Bernoulli-, Binomial- and Poissonvaraibles. Thank's a lot for a reply. Sincerely yours, Ekkehardt Altpeter Swiss Federal Office of Public Health. -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20030213/a6ea5247/attachment.html
See the Rmap project for importing shapefiles: http://www.maths.lancs.ac.uk/Software/Rmap>Dear R user, > >I am running R under Windows 2000. > > > >I am looking for a routine for importing > > > >- shape files (ESRI) into R > >- dbase files (FOXPRO) into R > > > >and I am looking for time-space models for description and >prediction of Bernoulli-, Binomial- and Poissonvaraibles. > > > >Thank''s a lot for a reply. > > > >Sincerely yours, > >Ekkehardt Altpeter > >Swiss Federal Office of Public Health.-- Stéphane DRAY --------------------------------------------------------------- Biométrie et Biologie évolutive - Equipe "Écologie Statistique" Universite Lyon 1 - Bat 711 - 69622 Villeurbanne CEDEX - France Tel : 04 72 43 27 56 Fax : 04 78 89 27 19 04 72 43 27 57 E-mail : dray@biomserv.univ-lyon1.fr --------------------------------------------------------------- ADE-4 http://pbil.univ-lyon1.fr/ADE-4/ADE-4F.html --------------------------------------------------------------- [[alternate HTML version deleted]]
david.whiting@ncl.ac.uk
2003-Feb-14 05:20 UTC
[R] ESRI shape file import and time-space models
On Thu, Feb 13, 2003 at 12:36:51PM +0100, Ekkehardt.Altpeter at bag.admin.ch wrote:> Dear R user, > > I am running R under Windows 2000. > > I am looking for a routine for importing > > - shape files (ESRI) into R > > - dbase files (FOXPRO) into RI assume that as you say dbase (Foxpro) you mean Foxpro version < 3.0, e.g. 2.x. I don't know of a way of opening dbase/foxpro files from within R (I think I saw once that RODBC can connect with dbase files, and if it does I'm sure you'll hear from someone else who knows more about it than me). If you have Foxpro and don't need an elegant solution, but just need to get the job done then you can export files from foxpro as tab-delimited files (COPY TO foxdata.txt DELIM WITH TAB) which you can then read into R with: dta <- read.delim("foxdata.txt", header=FALSE, sep="\t", strip.white TRUE) This gets the data in, but does not give you the field names. I have written a little foxpro program that creates a tab-delimited file with the headers (field names) that seems to work. Again, I read the data into R with read.delim(). Let me know if you want me to send you the Foxpro program. HTH, Dave -- Dave Whiting Dar es Salaam, Tanzania
Benjamin.STABLER@odot.state.or.us
2003-Feb-14 17:30 UTC
[R] ESRI shape file import and time-space models
Attached are some functions that I wrote to read and write shapefiles and dbfs easily from within R. You do not need any additional libraries or C code. I am still working out a few bugs but I have tested it with quite a few different files and it seems to be working pretty well. There is little documentation at this point though. I'd appreciate any comments about the code. Thanks. Benjamin Stabler Transportation Planning Analysis Unit Oregon Department of Transportation 555 13th Street NE, Suite 2 Salem, OR 97301 Ph: 503-986-4104>Dear R user, >I am running R under Windows 2000.>I am looking for a routine for importing> shape files (ESRI) into R> dbase files (FOXPRO) into R>and I am looking for time-space models for description and prediction of >Bernoulli-, Binomial- and Poissonvaraibles.>Thank's a lot for a reply.>Sincerely yours,>Ekkehardt Altpeter>Swiss Federal Office of Public Health.-------------- next part -------------- A non-text attachment was scrubbed... Name: shapefiles.R Type: application/octet-stream Size: 26290 bytes Desc: not available Url : https://stat.ethz.ch/pipermail/r-help/attachments/20030214/fad15efe/shapefiles.obj
Thanks for providing your functions, especially those for reading and writing dBase files (read.dbf and write.dbf), which presumably are of general interest because there is no other implementation for reading and writing these formats (apart from ODBC), as far as I know. However, I suggest changing one byte character readBin to readChar as the latter does not expect zero-terminated strings which were not present in my dBase-III-files' headers. One such header entry for example was (hex): 4B 4C 49 4e 00 00 00 00 00 00 00 43 2B 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 (field "KLIN", type "C" [note "43" followed by "2B", not "00"], width/length 2, padded to size 32) ## --------------------------------------------------- ## From read.dbf: ## Field Descriptions (32 bytes each) for (i in 1:num.fields) { field.name.test <- readBin (infile, character(), 1, size=10, endian="little") field.name <- c (field.name, field.name.test) if (nchar (field.name.test)!=10) { file.temp <- readBin (infile,integer(), 10 - (nchar (field.name.test)), 1, endian="little") } ## RH 2003-02-16: replaced readBin by readChar in next line field.type <- c (field.type, readChar (infile, 1)) ## RH 2003-02-16: incremented by 1 to 4 items in next line ## to compensate for above change file.temp <- readBin (infile, integer(), 4, 1, endian="little") field.length <- c (field.length, readBin (infile, integer(), 1, 1, endian="little")) file.temp <- readBin (infile, integer(), 15, 1, endian="little") } ## --------------------------------------------------- An enhancement might be to also set the appropriate type for date fields, maybe like this (although I don't know internals of dBase date and time storage variants): ## --------------------------------------------------- ## From read.dbf: ## Set the numeric fields to numeric for (i in 1:ncol(dbf)) { ## RH 2003-02-16: added next line for date type setting if(fields$TYPE[i]=="D") {dbf[,i] <- strptime (as.character (dbf[,i]), format="%Y%m%d")} if(fields$TYPE[i]=="C") {dbf[,i] <- as.character (dbf[,i])} if(fields$TYPE[i]=="N") {dbf[,i] <- as.numeric (as.character (dbf[,i]))} if(fields$TYPE[i]=="F") {dbf[,i] <- as.numeric (as.character (dbf[,i])) warning("Possible trouble converting numeric field in the DBF\n") } } ## --------------------------------------------------- Thanks and greetings - Ralf Herold -- Dr. med. Ralf Herold | Koordinationszentrale Kompetenznetz | P?diatrische Onkologie und H?matologie | http://www.kinderkrebsinfo.de/ | Charit? Campus Virchow-Klinikum | Medizinische Fakult?t Humboldt-Universit?t | D-13353 Berlin, Augustenburger Platz 1 | Raum 4.3425 4. Etage Mittelallee 8 | Tel. +49 (30) 450-566834 Fax -566906 | mailto:ralf.herold at charite.de> ----- Original Message ----- > From: Benjamin.STABLER at odot.state.or.us > To: Ekkehardt.Altpeter at bag.admin.ch > Cc: r-help at stat.math.ethz.ch > Subject: Re: [R] ESRI shape file import and time-space models > Date: Fri, 14 Feb 2003 08:29:12 -0800[...]> Attached are some functions that I wrote to read and write > shapefiles and > dbfs easily from within R. You do not need any additional > libraries or C > code. I am still working out a few bugs but I have tested it[...]> Benjamin Stabler > Transportation Planning Analysis Unit > Oregon Department of Transportation > 555 13th Street NE, Suite 2 > Salem, OR 97301 Ph: 503-986-4104[...]
Benjamin.STABLER@odot.state.or.us
2003-Feb-18 23:13 UTC
[R] ESRI shape file import and time-space models
Thanks to R. Herold for the suggested change from readBin to readChar for the field type in the field header descriptions. The code below is the revised read.dbf function. Fan's odbc.dbase function is much faster than my read.dbf() function, and it is defintely better for large files. Thanks also to Fan for the comments. Finally, I am working on putting together a package to read and write shapefiles. Regards, Benjamin Stabler Transportation Planning Analysis Unit Oregon Department of Transportation 555 13th Street NE, Suite 2 Salem, OR 97301 Ph: 503-986-4104 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ #Read DBF format #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~ read.dbf <- function(dbf.name) { infile<-file(dbf.name,"rb") #Header file.version <- readBin(infile,integer(), 1, size=1, endian="little") file.year <- readBin(infile,integer(), 1, size=1, endian="little") file.month <- readBin(infile,integer(), 1, size=1, endian="little") file.day <- readBin(infile,integer(), 1, size=1, endian="little") num.records <- readBin(infile,integer(), 1, size=4, endian="little") header.length <- readBin(infile,integer(), 1, size=2, endian="little") record.length <- readBin(infile,integer(), 1, size=2, endian="little") file.temp <- readBin(infile,integer(), 20, size=1, endian="little") header <- list(file.version,file.year, file.month, file.day, num.records, header.length, record.length) names(header) <- c("file.version","file.year","file.month","file.day","num.records","header.l ength","record.length") rm(file.version,file.year, file.month, file.day, num.records, header.length, record.length) #Calculate the number of fields num.fields <- (header$header.length-32-1)/32 field.name <- NULL field.type <- NULL field.length <- NULL #Field Descriptions (32 bytes each) for (i in 1:num.fields) { field.name.test <- readBin(infile,character(), 1, size=10, endian="little") field.name <- c(field.name,field.name.test) if (nchar(field.name.test)!=10) { file.temp <- readBin(infile,integer(), 10-(nchar(field.name.test)), 1, endian="little") } field.type <- c(field.type,readChar(infile, 1)) file.temp <- readBin(infile,integer(), 4, 1, endian="little") field.length <- c(field.length,readBin(infile,integer(), 1, 1, endian="little")) file.temp <- readBin(infile,integer(), 15, 1, endian="little") } #Create a table of the field info fields <- data.frame(NAME=field.name,TYPE=field.type,LENGTH=field.length) #Set all fields with length<0 equal to correct number of characters fields$LENGTH[fields$LENGTH<0]<-(256+fields$LENGTH[fields$LENGTH<0]) #Read in end of attribute descriptions terminator - should be integer value 13 file.temp <- readBin(infile,integer(), 1, 1, endian="little") #Increase the length of field 1 by one to account for the space at the beginning of each record fields$LENGTH[1]<-fields$LENGTH[1]+1 #Add fields to the header list header <- c(header,fields=NULL) header$fields <- fields #Read in all the records data and the end of file value - should be value 26 all.records <- readBin(infile, integer(), header$num.records*header$record.length, size=1, endian="little") file.temp <- readBin(infile,integer(), 1, 1, endian="little") close(infile) #Compress the binary values using run length encoding all.records <- rle(all.records) #Swap ASCII decimal codes for ASCII character codes ascii <- c(32,46,48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,73,74,75,76,77 ,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106 ,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,33,35,36,37 ,38,39,40,41,42,43,44,45,47,58,59,60,61,62,63,64,91,92,93,94,95,123,124,125, 126) ascii.values <- c(" ",".","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H ","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a ","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t ","u","v","w","x","y","z","!","#","$","%","&","'","(",")","*","+","\,","-"," /",":",";","<","=",">","?","@","["," ","]","^","_","{","|","}","~") all.records$values <- ascii.values[match(as.character(all.records$values),as.character(ascii), nomatch=1)] all.records <- inverse.rle(all.records) #Create a matrix of the ASCII data by record base.data <- t(matrix(all.records,header$record.length,header$num.records)) rm(all.records) #Function to collapse the ASCII codes, string split them and replace " " with "" format.record <- function(record) { record <- paste(record,collapse="") record <- substring(record, c(1,cumsum(fields$LENGTH)[1:length(cumsum(fields$LENGTH))-1]+1),cumsum(field s$LENGTH)) record <- gsub(" + ","", record) record } #Format the base.data ASCII record stream dbf <- as.data.frame(t(apply(base.data,1,format.record))) #Set the numeric fields to numeric for (i in 1:ncol(dbf)) { if(fields$TYPE[i]=="C") { dbf[[i]] <- as.character(dbf[[i]]) } if(fields$TYPE[i]=="N") { dbf[[i]] <- as.numeric(as.character(dbf[[i]])) } if(fields$TYPE[i]=="F") { dbf[[i]] <- as.numeric(as.character(dbf[[i]])) warning("Possible trouble converting numeric field in the DBF\n") } } colnames(dbf) <- as.character(fields$NAME) list(dbf=dbf, header=header) }>-----Original Message----- >From: R. Herold [mailto:ralf.herold at charite.de] >Sent: Sunday, February 16, 2003 8:49 AM >To: r-help at stat.math.ethz.ch >Cc: STABLER Benjamin >Subject: Re: Re: [R] ESRI shape file import and time-space models > > >Thanks for providing your functions, especially those for >reading and writing dBase files (read.dbf and write.dbf), >which presumably are of general interest because there is >no other implementation for reading and writing these >formats (apart from ODBC), as far as I know. > >However, I suggest changing one byte character readBin to >readChar as the latter does not expect zero-terminated >strings which were not present in my dBase-III-files' headers. >One such header entry for example was (hex): > >4B 4C 49 4e 00 00 00 00 00 00 00 43 2B 00 00 00 >02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 > >(field "KLIN", type "C" [note "43" followed by "2B", > not "00"], width/length 2, padded to size 32) > >## --------------------------------------------------- >## From read.dbf: >## Field Descriptions (32 bytes each) >for (i in 1:num.fields) { > field.name.test <- readBin (infile, character(), 1, size=10, >endian="little") > field.name <- c (field.name, field.name.test) > if (nchar (field.name.test)!=10) { > file.temp <- readBin (infile,integer(), 10 - (nchar >(field.name.test)), 1, endian="little") > } > ## RH 2003-02-16: replaced readBin by readChar in next line > field.type <- c (field.type, readChar (infile, 1)) > ## RH 2003-02-16: incremented by 1 to 4 items in next line > ## to compensate for above change > file.temp <- readBin (infile, integer(), 4, 1, endian="little") > field.length <- c (field.length, readBin (infile, integer(), 1, 1, >endian="little")) > file.temp <- readBin (infile, integer(), 15, 1, endian="little") >} >## --------------------------------------------------- > >An enhancement might be to also set the appropriate type for >date fields, maybe like this (although I don't know internals >of dBase date and time storage variants): > >## --------------------------------------------------- >## From read.dbf: >## Set the numeric fields to numeric >for (i in 1:ncol(dbf)) { > ## RH 2003-02-16: added next line for date type setting > if(fields$TYPE[i]=="D") {dbf[,i] <- strptime (as.character (dbf[,i]), >format="%Y%m%d")} > if(fields$TYPE[i]=="C") {dbf[,i] <- as.character (dbf[,i])} > if(fields$TYPE[i]=="N") {dbf[,i] <- as.numeric (as.character >(dbf[,i]))} > if(fields$TYPE[i]=="F") {dbf[,i] <- as.numeric (as.character >(dbf[,i])) > warning("Possible trouble converting numeric >field in the DBF\n") > } >} >## --------------------------------------------------- > >Thanks and greetings - Ralf Herold > >-- Dr. med. Ralf Herold >| Koordinationszentrale Kompetenznetz >| P?diatrische Onkologie und H?matologie >| http://www.kinderkrebsinfo.de/ >| Charit? Campus Virchow-Klinikum >| Medizinische Fakult?t Humboldt-Universit?t >| D-13353 Berlin, Augustenburger Platz 1 >| Raum 4.3425 4. Etage Mittelallee 8 >| Tel. +49 (30) 450-566834 Fax -566906 >| mailto:ralf.herold at charite.de > >> ----- Original Message ----- >> From: Benjamin.STABLER at odot.state.or.us >> To: Ekkehardt.Altpeter at bag.admin.ch >> Cc: r-help at stat.math.ethz.ch >> Subject: Re: [R] ESRI shape file import and time-space models >> Date: Fri, 14 Feb 2003 08:29:12 -0800 >[...] >> Attached are some functions that I wrote to read and write >> shapefiles and >> dbfs easily from within R. You do not need any additional >> libraries or C >> code. I am still working out a few bugs but I have tested it >[...] >> Benjamin Stabler >> Transportation Planning Analysis Unit >> Oregon Department of Transportation >> 555 13th Street NE, Suite 2 >> Salem, OR 97301 Ph: 503-986-4104 >[...] >