读取Shapefile
利用R读取Shapefile,主要用到readShapeSpatial方法,该方法将shapefile数据读取为一个Spatial*DataFrame对象,其使用方法如下:
readShapeSpatial(fn, proj4string=CRS(as.character(NA)),verbose=FALSE, repair=FALSE, IDvar=NULL, force_ring=FALSE,delete_null_obj=FALSE, retrieve_ABS_null=FALSE)
其中,fn为shapefile的存储路径以及文件名,IDvar为shapefile属性中ID属性名称,proj4string用于指定参考系统,示例代码如下:
>F.shapefile<-readShapeSpatial(file.path("C:/Users/GAOXIANG/Documents/ArcGIS/LINE"),IDvar="FID")
> plot(F.shapefile)
结果如下图所示:
存储新的shapefile
上述例子中的矢量属性为;
> summary(F.shapefile)
Object of class SpatialLinesDataFrame
Coordinates:
min max
x 820806.8 860451.8
y 3394161.4 3419346.4
Is projected: NA
proj4string : [NA]
Data attributes:
from_node to_node Grid
Min. :0 Min. :0 Min. :1.000
1st Qu.:0 1st Qu.:0 1st Qu.:3.000
Median :0 Median :0 Median :4.000
Mean :0 Mean :0 Mean :3.868
3rd Qu.:0 3rd Qu.:0 3rd Qu.:5.000
Max. :0 Max. :0 Max. :8.000
现在想要将Grid属性小于3的要素保存为新的Shapefile,代码如下:
> F.shapefile_new<-F.shapefile[F.shapefile$Grid < 3,]
查看新要素类属性:
> summary(F.shapefile_new)
Object of class SpatialLinesDataFrame
Coordinates:
min max
x 820926.8 860451.8
y 3394161.4 3419346.4
Is projected: NA
proj4string : [NA]
Data attributes:
from_node to_node Grid
Min. :0 Min. :0 Min. :1.000
1st Qu.:0 1st Qu.:0 1st Qu.:2.000
Median :0 Median :0 Median :2.000
Mean :0 Mean :0 Mean :1.771
3rd Qu.:0 3rd Qu.:0 3rd Qu.:2.000
Max. :0 Max. :0 Max. :2.000
对其进行绘制,如下图所示:
保存新要素,代码如下:
> file.path_old<-file.path("C:\\Users\\GAOXIANG\\Documents\\ArcGIS")
> file.path_new<-paste(file.path_old,"LINE_NEW",sep="\\")
> writeSpatialShape(F.shapefile_new, file.path_new)
检查新要素属性:
> getinfo.shape(paste(file.path_new,".shp",sep=""))
Shapefile type: PolyLine, (3), # of Shapes: 109
最终结果如下图所示: