Class 12
I have updated answers for all homework tasks.
Review some concepts of vector data:
Geometry creation
POINT and MULTIPOINTLINESTRING and MULTILINESTRINGPOLYGON and MULTIPOLYGON.csv or .xlsx).shp).geojson or .kml)st_drivers(what = "vector") to check all supported typesObserve how many new files saved.
.shp stores the actual geometry (.shx is the index).dbf stores the attribute data (or database).prj stores coordinate reference system (CRS) or projection.library(sf)
library(sdadata)
# Read vector
fname <- system.file('extdata/districts.geojson', package = 'sdadata')
districts <- st_read(fname, quiet = TRUE)
head(districts)Simple feature collection with 6 features and 1 field
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 34.74926 ymin: -4.148158 xmax: 37.42937 ymax: -2.143339
Geodetic CRS: WGS 84
distName geometry
1 Arusha MULTIPOLYGON (((36.82231 -3...
2 Arusha Urban MULTIPOLYGON (((36.63247 -3...
3 Karatu MULTIPOLYGON (((35.89154 -3...
4 Longido MULTIPOLYGON (((36.35742 -3...
5 Meru MULTIPOLYGON (((36.89951 -3...
6 Monduli MULTIPOLYGON (((36.54092 -3...
distName
1 Arusha
2 Arusha Urban
3 Karatu
4 Longido
5 Meru
6 Monduli
Simple feature collection with 1 feature and 1 field
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 36.469 ymin: -3.65315 xmax: 36.88824 ymax: -3.06394
Geodetic CRS: WGS 84
distName geometry
1 Arusha MULTIPOLYGON (((36.82231 -3...
Coordinate Reference System:
User input: WGS 84
wkt:
GEOGCRS["WGS 84",
DATUM["World Geodetic System 1984",
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["geodetic latitude (Lat)",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["geodetic longitude (Lon)",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]]
st_crs(districts)st_transform
(Pseudo-)Cylindrical: Mercator (EPSG:3857), Robinson (ESRI:54030)
Conical: USA Contiguous Albers Equal Area (EPSG:5070)
Planar: Lambert Azimuthal Equal-Area projection (EPSG:3035)
library(rnaturalearth)
library(ggplot2)
world <- ne_countries() %>% select(name)
# Unprojected (Longitude/Latitude)
world %>% ggplot() +
geom_sf(fill = 'aliceblue') +
theme_minimal()
# Change the projection codes to check different projections
world %>% st_transform("EPSG:3035") %>%
ggplot() +
geom_sf(fill = 'aliceblue') +
theme_minimal()If no CRS, we can set set one.
BUT if CRS is not NA, we must use st_transform to reproject.
b266_sfg <- st_point(c(-74.435060, 40.521077))
b266_sfg; class(b266_sfg) # sfg, simple feature geometry
b266_sfc <- st_sfc(b266_sfg)
b266_sfc; class(b266_sfc) # sfc, simple feature column, geospatial already
st_crs(b266_sfc) <- 4326 # set CRS for geopsatial, EPSG code
b266_sfc
b266_pt <- st_sf(b266_sfc) # sf, simple feature collection
b266_pt; class(b266_pt)
attr(b266_pt, "sf_column") # check sfc
b266_geom <- st_geometry(b266_pt) # get sfc
identical(b266_sfc, b266_geom) # st_geometry gets sfc from a sf data.frameTry to create b266_pt with one single pipeline.
multipoint is only one geometry for multiple points.
coords <- rbind(c(40.522340, -74.435752),
c(40.522296, -74.435594),
c(40.522351, -74.435456),
c(40.522058, -74.434914),
c(40.521882, -74.435069),
c(40.521109, -74.434980),
c(40.521150, -74.435071),
c(40.520980, -74.435245),
c(40.520842, -74.435011),
c(40.521023, -74.434842),
c(40.521117, -74.434687),
c(40.521936, -74.434735),
c(40.522093, -74.434554),
c(40.522780, -74.435722))
coords <- coords[, c(2, 1)]
lucy_stone_mpt <- st_multipoint(coords[, ]) %>% # create sfg
st_sfc() %>% st_set_crs(4326) %>% # create sfc
st_sf() # build up sf
lucy_stone_mpt
plot(lucy_stone_mpt %>% st_geometry()) # plotst_as_sf functionmultilinestring is only one geometry for multiple linestrings.
coords <- rbind(c(40.522340, -74.435752),
c(40.522296, -74.435594),
c(40.522351, -74.435456),
c(40.522058, -74.434914),
c(40.521882, -74.435069),
c(40.521109, -74.434980),
c(40.521150, -74.435071),
c(40.520980, -74.435245),
c(40.520842, -74.435011),
c(40.521023, -74.434842),
c(40.521117, -74.434687),
c(40.521936, -74.434735),
c(40.522093, -74.434554),
c(40.522780, -74.435722),
c(40.522340, -74.435752)) # repeat the first point!!
coords <- coords[, c(2, 1)]
lucy_stone_ply <- st_polygon(list(coords)) %>% # create sfc
st_sfc() %>% st_set_crs(4326) %>% # create sfc
st_sf()
lucy_stone_ply
plot(lucy_stone_ply$geometry)st_as_sfc functionst_cast functionLiterally we could convert from any type to any type. But be careful of the order.
POINT, MULTIPOINT, LINESTRING, MULTILISTRING, POLYGON, MULTIPOLYGON.POINT -> MULTIPOINT (good) and the opposite (bad).