1

我有一个 ARC/INFO 生成文件,其内容如下所示:

3594 -124.049541 44.429077
 -123.381222 44.530192
 -123.479913 44.625517
 -123.578917 44.720704
 -123.678234 44.815755
 -123.777866 44.910669
 -123.946044 44.885032
 -124.114074 44.858987
 -124.281949 44.832529
 -124.449663 44.805654
 -124.516511 44.684660
 -124.583091 44.563597
 -124.649404 44.442465
 -124.715451 44.321261
 -124.615376 44.227772
 -124.515601 44.134147
 -124.416125 44.040385
 -124.316948 43.946486
 -124.151513 43.973082
 -123.985926 43.999247
 -123.820193 44.024987
 -123.654322 44.050307
 -123.586447 44.170362
 -123.518307 44.290360
 -123.449899 44.410303
 -123.381222 44.530192
END
3595 -123.103772 45.009223
 -122.427717 45.101578
 -122.525757 45.198252
 -122.624122 45.294789
 -122.722814 45.391191
 -122.821833 45.487459
 -122.992014 45.464007
 -123.162072 45.440175
 -123.332002 45.415959
 -123.501798 45.391355
 -123.571234 45.271264
 -123.640389 45.151121
 -123.709266 45.030923
 -123.777866 44.910669
 -123.678234 44.815755
 -123.578917 44.720704
 -123.479913 44.625517
 -123.381222 44.530192
 -123.213811 44.554460
 -123.046278 44.578334
 -122.878629 44.601816
 -122.710869 44.624913
 -122.640504 44.744148
 -122.569859 44.863337
 -122.498931 44.982480
 -122.427717 45.101578
END
3676 -122.989567 44.147495
 -122.323040 44.238368
 -122.419523 44.335217
 -122.516322 44.431923
 -122.613437 44.528488
 -122.710869 44.624913
 -122.878629 44.601816
 -123.046278 44.578334
 -123.213811 44.554460
 -123.381222 44.530192
 -123.449899 44.410303
 -123.518307 44.290360
 -123.586447 44.170362
 -123.654322 44.050307
 -123.556277 43.955264
 -123.458534 43.860080
 -123.361093 43.764751
 -123.263953 43.669279
 -123.098838 43.693189
 -122.933613 43.716694
 -122.768285 43.739802
 -122.602857 43.762515
 -122.533309 43.881546
 -122.463492 44.000532
 -122.393403 44.119472
 -122.323040 44.238368
END
END

我的策略是读取文件,生成经纬度点列表,并在每次遇到 END 时开始一个新的唯一组 ID。然后我将使用ggplot" andgeom_polygon" 进行绘图。

唉,我不确定如何有效地完成文件的读取。

有什么想法吗?

4

1 回答 1

2

读取 CRAN 上的空间任务视图,然后使用 rgdal 包中的 readOGR 读入 sp 类对象。您需要安装带有 ARCGEN 格式支持的 GDAL/OGR,尽管它被列为“默认编译” http://www.gdal.org/ogr/ogr_formats.html我的系统上没有。

否则,将文件作为连接打开,读取每一行,构建一个多边形,然后是多边形和空间多边形。

这是一个相当次优但有效的功能:

readUng <- function(f){
  require(sp)
  stream = file(f,"r")
  first = readLines(stream,1)
  bits = strsplit(first," ")[[1]]
  polys = list();ids=NULL
  while(TRUE){
    id=bits[1] # label pt = bits[2],bits[3]
    ids=c(ids,id)
    coords=NULL
    while(TRUE){
      xy=readLines(stream,1)
      if(xy=="END"){
        break
      }
      coords=rbind(coords,strsplit(xy," ")[[1]])      
    }

    polys[[length(polys)+1]] = Polygons(list(Polygon(matrix(as.numeric(coords[,2:3]),ncol=2))),ID=id)
    lines = readLines(stream,1)
    if(lines == "END"){
      break
    }
    bits = strsplit(lines," ")[[1]]
  }
  return(SpatialPolygons(polys))
}

现在它是一个合适的空间数据对象,你也可以给它一个坐标系(在我看来像 lat-long,所以 epsg:4326,但只有你知道)。现在您可以修改所有这些以生成 ggplot 想要的任何内容,但如果它的空间数据,那么您应该将其保留为空间数据类,并且 ggplot 应该能够处理此类。

于 2012-07-16T19:59:13.050 回答