library(sp)
<- Line(matrix(runif(6), ncol=2))
ln1 str(ln1)
Formal class 'Line' [package "sp"] with 1 slot
..@ coords: num [1:3, 1:2] 0.9818 0.1443 0.5396 0.0636 0.4016 ...
sp
包第一个在R中引入空间数据类型的包为sp
。 空间数据初始结构是空间类别。存在两个”slots” (R中的新格式S4对象有预先定义的成分称为slots)
创建一个几何对象的步骤:首先对于所创建几何对象,其类别可划分为: - 点(Point):是最基础的空间对象,是通过单个坐标所生成; - 线(Line):是由Line
所生成的对象,实际上,线是由一系列的点所组成的元素; - 多边形(polygons):是由Polygons
所生成的对象,是起始坐标与终点坐标相同的多条线构成的元素。
之后对一个空间对象来考虑:也就是在空间中生成基本的几何对象。实际上在这一步需要通过添加边界框(自动)和坐标参考系或CRS的插槽(需要手动填充值)。 SpatialPoints
可直接通过坐标点来实现,SpatialLines
和SpatialPolygons
可分别通过Line和Polygons生成。
最后将空间上的数据点添加属性,此时的空间坐标数据会由列表转换为空间数据框。从数据框的属性来看,SpatialPoints
是作为SpatialDataFrame
的属性名(行名)与其相联系一起。
比如我们想要生成一个多条高速公路的数据,首先创建Line
数据对象来包装高速公路数据。其中的x
和y
用来表示Points
来构建坐标点。(在这里用随机数生成xy)
library(sp)
<- Line(matrix(runif(6), ncol=2))
ln1 str(ln1)
Formal class 'Line' [package "sp"] with 1 slot
..@ coords: num [1:3, 1:2] 0.9818 0.1443 0.5396 0.0636 0.4016 ...
<- Line(matrix(runif(6), ncol=2))
ln2
<- Lines(list(ln1), ID = c("hwy1"))
lns1 <- Lines(list(ln2), ID = c("hwy2"))
lns2 str(lns1)
Formal class 'Lines' [package "sp"] with 2 slots
..@ Lines:List of 1
.. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. ..@ coords: num [1:3, 1:2] 0.9818 0.1443 0.5396 0.0636 0.4016 ...
..@ ID : chr "hwy1"
<- SpatialLines(list(lns1, lns2))
sp_lns str(sp_lns)
Formal class 'SpatialLines' [package "sp"] with 3 slots
..@ lines :List of 2
.. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
.. .. .. ..@ Lines:List of 1
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 0.9818 0.1443 0.5396 0.0636 0.4016 ...
.. .. .. ..@ ID : chr "hwy1"
.. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
.. .. .. ..@ Lines:List of 1
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 0.99 0.873 0.396 0.697 0.55 ...
.. .. .. ..@ ID : chr "hwy2"
..@ bbox : num [1:2, 1:2] 0.1443 0.0636 0.9905 0.6968
.. ..- 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
<- data.frame(id = c("hwy1", "hwy2"), # note how we use the same IDs from above!
dfr cars_per_hour = c(78, 22))
<- SpatialLinesDataFrame(sp_lns, dfr, match.ID = "id")
sp_lns_dfr str(sp_lns_dfr)
Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
..@ data :'data.frame': 2 obs. of 2 variables:
.. ..$ id : chr [1:2] "hwy1" "hwy2"
.. ..$ cars_per_hour: num [1:2] 78 22
..@ lines :List of 2
.. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
.. .. .. ..@ Lines:List of 1
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 0.9818 0.1443 0.5396 0.0636 0.4016 ...
.. .. .. ..@ ID : chr "hwy1"
.. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
.. .. .. ..@ Lines:List of 1
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
.. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 0.99 0.873 0.396 0.697 0.55 ...
.. .. .. ..@ ID : chr "hwy2"
..@ bbox : num [1:2, 1:2] 0.1443 0.0636 0.9905 0.6968
.. ..- 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
sf
包sf空间对象被存储在简单数据框,该数据框的特殊列包含了几何坐标信息。 同时列的行数和数据框的行维度相同。
library(sf)
Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
<- st_linestring(matrix(runif(6), ncol=2))
lnstr_sfg1 <- st_linestring(matrix(runif(6), ncol=2))
lnstr_sfg2 class(lnstr_sfg1)
[1] "XY" "LINESTRING" "sfg"
之后再组合成简单特征集合:
<- st_sfc(lnstr_sfg1, lnstr_sfg2)) # just one feature here (lnstr_sfc
Geometry set for 2 features
Geometry type: LINESTRING
Dimension: XY
Bounding box: xmin: 0.04717682 ymin: 0.1237561 xmax: 0.9555352 ymax: 0.3792805
CRS: NA
LINESTRING (0.04717682 0.1293945, 0.5407734 0.3...
LINESTRING (0.8111804 0.3792805, 0.4844811 0.24...
最后再用上述的特征集合生成sf
对象:
<- st_sf(dfr , lnstr_sfc)) (lnstr_sf
Simple feature collection with 2 features and 2 fields
Geometry type: LINESTRING
Dimension: XY
Bounding box: xmin: 0.04717682 ymin: 0.1237561 xmax: 0.9555352 ymax: 0.3792805
CRS: NA
id cars_per_hour lnstr_sfc
1 hwy1 78 LINESTRING (0.04717682 0.12...
2 hwy2 22 LINESTRING (0.8111804 0.379...