首先读入shp文件:
LB<-readShapePoly("LondonBorough.shp",verbose = T)
可以看到LB的类型为:
> class(LB)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
我们把LB显示出来:(太长了,这里把中间截掉了一部分)
> str(LB)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..@ data :'data.frame': 35 obs. of 15 variables:
.. ..$ NAME : Factor w/ 35 levels "Barking and Dagenham",..: 29 22 9 5 19 10 17 18 16 4 ...
.. ..$ AREA_CODE : Factor w/ 3 levels "DIS","LBO","UTA": 1 2 2 2 2 2 2 2 2 2 ...
.. ..$ DESCRIPTIO: Factor w/ 3 levels "District","London Borough",..: 1 2 2 2 2 2 2 2 2 2 ...
.. ..$ FILE_NAME : Factor w/ 3 levels "BUCKINGHAMSHIRE_COUNTY",..: 1 2 2 2 2 2 2 2 2 2 ...
.. ..$ NUMBER : int [1:35] 7 3 6 9 12 14 18 20 24 26 ...
.. ..$ NUMBER0 : int [1:35] 412 749 776 800 823 848 869 893 918 941 ...
.. ..$ POLYGON_ID: int [1:35] 51574 50448 51330 50904 51138 50523 50851 51011 50515 50485 ...
.. ..$ UNIT_ID : int [1:35] 11693 11412 10896 10772 11489 11399 10807 11539 11391 11447 ...
.. ..$ CODE : Factor w/ 35 levels "00AA","00AB",..: 35 21 8 6 18 9 16 17 15 5 ...
.. ..$ HECTARES : num [1:35] 14127 3726 8650 15013 5659 ...
.. ..$ AREA : num [1:35] 0 0 0 0 60.8 ...
.. ..$ TYPE_CODE : Factor w/ 1 level "AA": 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ DESCRIPT0 : Factor w/ 1 level "CIVIL ADMINISTRATION AREA": 1 1 1 1 1 1 1 1 1 1 ...
.. ..$ TYPE_COD0 : Factor w/ 0 levels: NA NA NA NA NA NA NA NA NA NA ...
.. ..$ B_Name : Factor w/ 35 levels "Barking and Dagenham",..: 29 22 9 5 19 10 17 18 16 4 ...
.. ..- attr(*, "data_types")= chr [1:15] "C" "C" "C" "C" ...
..@ polygons :List of 35 #说明一共有35个polygons 在这里只粘贴保留了3个
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..@ Polygons :List of 1
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 505184 179204
.. .. .. .. .. .. ..@ area : num 0.19
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:4, 1:2] 505183 505184 505186 505183 179201 ...
.. .. .. ..@ plotOrder: int 1
.. .. .. ..@ labpt : num [1:2] 505184 179204
.. .. .. ..@ ID : chr "0"
.. .. .. ..@ area : num 0.19
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..@ Polygons :List of 1
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 519298 166820
.. .. .. .. .. .. ..@ area : num 37261175
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:1633, 1:2] 521672 521672 521662 521639 521630 ...
.. .. .. ..@ plotOrder: int 1
.. .. .. ..@ labpt : num [1:2] 519298 166820
.. .. .. ..@ ID : chr "1"
.. .. .. ..@ area : num 37261175
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..@ Polygons :List of 1
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..@ labpt : num [1:2] 533290 163541
.. .. .. .. .. .. ..@ area : num 86503635
.. .. .. .. .. .. ..@ hole : logi FALSE
.. .. .. .. .. .. ..@ ringDir: int 1
.. .. .. .. .. .. ..@ coords : num [1:2635, 1:2] 533702 533709 533704 533694 533680 ...
.. .. .. ..@ plotOrder: int 1
.. .. .. ..@ labpt : num [1:2] 533290 163541
.. .. .. ..@ ID : chr "2"
.. .. .. ..@ area : num 86503635
..@ plotOrder : int [1:35] 4 8 7 11 3 17 16 21 5 19 ...
..@ bbox : num [1:2, 1:2] 503568 155851 561958 200934
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr NA
..$ comment: chr "FALSE"
①第一行Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
中的5个“插槽”就是下面位于同一级的data、polygons、plotOrder、bbox、proj4string;
②..@ polygons :List of 35
说明一共有35个polygons (在这里只粘贴保留了3个),也就是说,length(LB@polygons)=35
> length(LB@polygons)
[1] 35
关于@符号
其实在刚刚我们已经用了@符号来进行拆解,实际上就是在进行像俄罗斯套娃一样的操作,用一层@不够就再多加一层
现在我们来看一下刚刚使用@之后的结果(同样是只粘贴保留了3个)
> str(LB@polygons)
List of 35
$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. ..@ Polygons :List of 1
.. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. ..@ labpt : num [1:2] 505184 179204
.. .. .. .. ..@ area : num 0.19
.. .. .. .. ..@ hole : logi FALSE
.. .. .. .. ..@ ringDir: int 1
.. .. .. .. ..@ coords : num [1:4, 1:2] 505183 505184 505186 505183 179201 ...
.. ..@ plotOrder: int 1
.. ..@ labpt : num [1:2] 505184 179204
.. ..@ ID : chr "0"
.. ..@ area : num 0.19
$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. ..@ Polygons :List of 1
.. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. ..@ labpt : num [1:2] 519298 166820
.. .. .. .. ..@ area : num 37261175
.. .. .. .. ..@ hole : logi FALSE
.. .. .. .. ..@ ringDir: int 1
.. .. .. .. ..@ coords : num [1:1633, 1:2] 521672 521672 521662 521639 521630 ...
.. ..@ plotOrder: int 1
.. ..@ labpt : num [1:2] 519298 166820
.. ..@ ID : chr "1"
.. ..@ area : num 37261175
$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. ..@ Polygons :List of 1
.. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. ..@ labpt : num [1:2] 533290 163541
.. .. .. .. ..@ area : num 86503635
.. .. .. .. ..@ hole : logi FALSE
.. .. .. .. ..@ ringDir: int 1
.. .. .. .. ..@ coords : num [1:2635, 1:2] 533702 533709 533704 533694 533680 ...
.. ..@ plotOrder: int 1
.. ..@ labpt : num [1:2] 533290 163541
.. ..@ ID : chr "2"
.. ..@ area : num 86503635
可以看到,这时候我们已经把最外面的一层拿掉了
再来看一下加两层@的结果
> str(LB@polygons[[1]]@Polygons[[1]])
Formal class 'Polygon' [package "sp"] with 5 slots
..@ labpt : num [1:2] 505184 179204
..@ area : num 0.19
..@ hole : logi FALSE
..@ ringDir: int 1
..@ coords : num [1:4, 1:2] 505183 505184 505186 505183 179201 ...
注意这里,由于LB@polygons一共有35个,所以需要在后面用[[]]来注明要拆解的是哪一个;
LB@polygons[[1]]@Polygons[[1]]最后也要注明[[1]]是因为LB@polygons[[1]]@Polygons同样可能有多个,比如刚刚没有粘贴上的最后一个
> str(LB@polygons[[35]])
Formal class 'Polygons' [package "sp"] with 5 slots
..@ Polygons :List of 2 #注意这里!
.. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. ..@ labpt : num [1:2] 555427 182164
.. .. .. ..@ area : num 2.56
.. .. .. ..@ hole : logi FALSE
.. .. .. ..@ ringDir: int 1
.. .. .. ..@ coords : num [1:4, 1:2] 555415 555478 555387 555415 182188 ...
.. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. ..@ labpt : num [1:2] 557201 182784
.. .. .. ..@ area : num 2.56
.. .. .. ..@ hole : logi FALSE
.. .. .. ..@ ringDir: int 1
.. .. .. ..@ coords : num [1:6, 1:2] 557188 557184 557232 557197 557188 ...
..@ plotOrder: int [1:2] 2 1
..@ labpt : num [1:2] 557201 182784
..@ ID : chr "34"
..@ area : num 5.13
再加一层
> str(LB@polygons[[35]]@Polygons[[1]]@coords)
num [1:4, 1:2] 555415 555478 555387 555415 182188 ...
可以看到,结果和我们在上面看到的一样,说明拆解的结果正确
这样用法就非常清楚了
slot的用法
最后补充一下slot()的用法
> slot(LB@polygons[[1]],"Polygons")
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] 505184.2 179204.2
Slot "area":
[1] 0.1900001
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] 505182.8 179201.1
[2,] 505184.1 179204.0
[3,] 505185.8 179207.5
[4,] 505182.8 179201.1
其实slot()作用和@符号相似,但是用这个函数提取出来的变量类型都为list,后续在使用的时候必须加上序号,即使提取出来的值只有一个
但是在需要多层@拆解时,可以先把一部分的拆解结果赋给某一个变量,在后面可以简化代码,比如:
> a<-slot(LB@polygons[[1]],"Polygons")
> class(a)
[1] "list"
> a[[1]]@coords #在这里就不再需要写前面长长的东西了,而直接写a[[1]](注意后面的序号)
[,1] [,2]
[1,] 505182.8 179201.1
[2,] 505184.1 179204.0
[3,] 505185.8 179207.5
[4,] 505182.8 179201.1