[Comp with R] 01:向量資料包介紹
sf包:為地理向量資料提供類系統,可以取代sp、rgeos(空間操作)、rgdal(資料讀寫)這些包。
本節介紹sf,為後續章節做準備(第5章和第7章分別介紹GEOS和GDAL介面)。
Simple feature geometries(sfg):
The sfg class represents the different simple feature geometry types in R: point, linestring, polygon (and their ‘multi’ equivalents, such as multipoints) or geometry collection.
sfg objects can be created from three base R data types:
- A numeric vector: a single point
- A matrix: a set of points, where each row represents a point, a multipoint or linestring
- A list: a collection of objects such as matrices, multilinestrings or geometry collections
Creat sfg:
st_point(c(5, 2)) # XY point st_point(c(5, 2, 3)) # XYZ point st_point(c(5, 2, 1), dim = "XYM") # XYM point st_point(c(5, 2, 3, 1)) # XYZM point # the rbind function simplifies the creation of matrices ## MULTIPOINT multipoint_matrix = rbind(c(5, 2), c(1, 3), c(3, 4), c(3, 2)) st_multipoint(multipoint_matrix) ## LINESTRING linestring_matrix = rbind(c(1, 5), c(4, 4), c(4, 1), c(2, 2), c(3, 2)) st_linestring(linestring_matrix) ## POLYGON polygon_list = list(rbind(c(1, 5), c(2, 2), c(4, 1), c(4, 4), c(1, 5))) st_polygon(polygon_list) ## POLYGON with a hole polygon_border = rbind(c(1, 5), c(2, 2), c(4, 1), c(4, 4), c(1, 5)) polygon_hole = rbind(c(2, 4), c(3, 4), c(3, 3), c(2, 3), c(2, 4)) polygon_with_hole_list = list(polygon_border, polygon_hole) st_polygon(polygon_with_hole_list) ## MULTILINESTRING multilinestring_list = list(rbind(c(1, 5), c(4, 4), c(4, 1), c(2, 2), c(3, 2)), rbind(c(1, 2), c(2, 4))) st_multilinestring((multilinestring_list)) ## MULTIPOLYGON multipolygon_list = list(list(rbind(c(1, 5), c(2, 2), c(4, 1), c(4, 4), c(1, 5))), list(rbind(c(0, 2), c(1, 2), c(1, 3), c(0, 3), c(0, 2)))) st_multipolygon(multipolygon_list) ## GEOMETRYCOLLECTION gemetrycollection_list = list(st_multipoint(multipoint_matrix), st_linestring(linestring_matrix)) st_geometrycollection(gemetrycollection_list)
Simple feature columns (sfc)
A simple feature geometry column (sfc) is a list of sfg objects,which is additionally able to contain information about the coordinate reference system in use. For instance, to combine two simple features into one object with two features, we can use the st_sfc() function. This is important since
sfc represents the geometry column in sf data frames。
Ansfc
object contains objects of the same geometry type
Thesf
class
Objects of class sf represent such data by combining the attributes (data.frame) with the simple feature geometry column (sfc).
# sfc POINT
point1 = st_point(c(5, 2))
point2 = st_point(c(1, 3))
points_sfc = st_sfc(point1, point2) #我認為相當於merge
points_sfc
# CRS 座標系
st_crs(points_sfc)
# EPSG definition using crs number
points_sfc_wgs = st_sfc(point1, point2, crs = 4326)
st_crs(points_sfc_wgs)
# or using PROJ4STRING definition
st_sfc(point1, point2, crs = "+proj=longlat +datum=WGS84 +no_defs")
> # sf class
> lnd_point = st_point(c(0.1, 51.5)) # sfg object
> lnd_geom = st_sfc(lnd_point, crs = 4326) # sfc object
> lnd_attrib = data.frame( # data.frame object
+ name = "London",
+ temperature = 25,
+ date = as.Date("2017-06-21")
+ )
> lnd_sf = st_sf(lnd_attrib, geometry = lnd_geom) # sf object
> lnd_sf
Simple feature collection with 1 feature and 3 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 0.1 ymin: 51.5 xmax: 0.1 ymax: 51.5
Geodetic CRS: WGS 84
name temperature date geometry
1 London 25 2017-06-21 POINT (0.1 51.5)
> class(lnd_sf)
[1] "sf" "data.frame"
可以看到,他同時屬於兩個類別,既是sf,也是data.frame。
sf讓它可以作為空間資料進行處理,data frame讓它可以作為資料框進行處理。
sfg就是一些向量要素;
sfc就是帶了座標系資訊的sfg;
sf 就是帶了屬性的sfc