[R] ESRI shape file import and time-space models

R. Herold ralf.herold at charite.de
Sun Feb 16 17:51:03 CET 2003


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
[...]




More information about the R-help mailing list