从shapefile(R)只读数据槽?

时间:2012-11-14 16:08:35

标签: r geospatial shapefile rgdal

我有一些非常大的shapefile。我可以使用SpatialPolygonsDataFrame函数rgdal将它们读入readOGR,但每个文件需要很长时间。我实际上只对data.frame广告位中显示的@data感兴趣。有没有办法只读取数据,跳过资源密集型多边形?

示例代码:

## State of Alabama census blocks (152 MB compressed, 266 MB uncompressed)
shpurl <- "http://www2.census.gov/geo/tiger/TIGER2011/TABBLOCK/tl_2011_01_tabblock.zip"
tmp    <- tempfile(fileext=".zip")
download.file(shpurl, destfile=tmp)
unzip(tmp, exdir=getwd())

## Read shapefile
nm  <- strsplit(basename(shpurl), "\\.")[[1]][1]
lyr <- readOGR(dsn=getwd(), layer=nm)

## Data I want
head(lyr@data)

2 个答案:

答案 0 :(得分:7)

Shapefile是复合文件,用于将其属性数据存储在扩展名为*.dbf的文件中。 (请参阅Wikipedia shapefile article以获取参考。)dbf后缀是指dBase file format,可由read.dbf()包中的函数foreign读取。

所以,试试这个:

library(foreign)
df <- read.dbf("tl_2011_01_tabblock.dbf")
## And, more generally, read.dbf("path/to/shapefile/shapefile-name.dbf")

答案 1 :(得分:0)

    FileInputStream fis = new FileInputStream( "/folder/file.dbf" ); 
DbaseFileReader dbfReader =  new DbaseFileReader(fis.getChannel(),false, Charset.forName("ISO-8859-1"));

        while ( dbfReader.hasNext() )   {
          final Object[] fields = dbfReader.readEntry();

          Long field1 = (Long) fields[0];
          Long field2 = (Long) fields[1];
          System.out.println("DBF field "+i+" value is: " + fields[0]);
          System.out.println("DBF field 2 value is: " + field2);
        }
 dbfReader.close();
 fis.close();