DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Buffers and centroids Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R Use a projected coordinate reference system for spatial analysis As a general rule your layers should use a projected CRS With multiple layers they should have the same CRS
DataCamp Spatial Analysis with sf and raster in R Buffer vectors with st_buffer() The dist argument controls the radius and is in CRS units. # Buffer five trees, this is feet > trees_buf <- st_buffer(trees5, + 5000)
DataCamp Spatial Analysis with sf and raster in R Compute centroids with st_centroid() > poly <- st_read("poly.shp") > poly_cent <- st_centroid(poly)
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Bounding boxes, dissolve features and create a convex hull Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R Defining a region Rectangular bounding box Tighter polygon with a convex hull
DataCamp Spatial Analysis with sf and raster in R Compute the coordinates of the bounding box with st_bbox() # Read data in and get the bbox coords > poly <- st_read("poly.shp") # Compute the bounding box coordinates > st_bbox(poly) xmin ymin xmax ymax 913090.8 120053.5 1067383.5 272932.1
DataCamp Spatial Analysis with sf and raster in R Create a bounding box # Bounding box of counties in red > poly_grd <- st_make_grid( + poly, n = 1 + ) # Bounding box of centroids in green > cent_grd <- st_make_grid( + poly_cent, n = 1 + )
DataCamp Spatial Analysis with sf and raster in R Dissolve features with st_union() For polygons you dissolve multiple features into a single feature For points you cluster individual points into a MULTIPOINT geometry This is required for some computations including convex hull
DataCamp Spatial Analysis with sf and raster in R Two polygons in the data frame # Simple feature collection with 2 features and 1 field # geometry type: POLYGON # dimension: XY # bbox: xmin: 971113.7 ymin: 146981.3 xmax: 1030504 ymax: 259547.8 # epsg (SRID): NA # proj4string: +proj=lcc +lat_1=40.66666666666666 +lat_2=41.033333 ... # BoroName geometry # 1 Manhattan POLYGON ((971113.66006219 1... # 2 Brooklyn POLYGON ((972454.529951439 ...
DataCamp Spatial Analysis with sf and raster in R Two separate polygons
DataCamp Spatial Analysis with sf and raster in R Dissolve into a single polygon with st_union() > grds_union <- st_union(polys) > grds_union # Geometry set for 1 feature # geometry type: POLYGON # dimension: XY # bbox: xmin: 971113.7 ... # epsg (SRID): NA # proj4string: +proj=lcc ... # POLYGON ((1010061.70003446 ...
DataCamp Spatial Analysis with sf and raster in R Our point data frame starts with five individual points Here we have a data frame of five points (the centroids from our counties). The geometry type is POINT . > select(boro_cent, BoroName, geometry) # Simple feature collection with 5 features and 1 field # geometry type: POINT # dimension: XY # bbox: xmin: 941777.1 ymin: 150931 xmax: 1034598 ymax: 249928.1 # epsg (SRID): NA # proj4string: +proj=lcc +lat_1=40.66666666666666 ... # BoroName geometry # 1 Manhattan POINT (993372.855589884 222... # 2 The Bronx POINT (1021130.02634457 249... # 3 Staten Island POINT (941777.089270765 150... # 4 Brooklyn POINT (999165.45349374 1739... # 5 Queens POINT (1034598.34861245 196...
DataCamp Spatial Analysis with sf and raster in R With st_union() points get bundled into a single grouped feature > boro_union <- st_union(boro_cent) > boro_union # Geometry set for 1 feature # geometry type: MULTIPOINT # dimension: XY # bbox: xmin: 941777.1 ymin: 150931 xmax: 1034598 ymax: 249928.1 # epsg (SRID): NA # proj4string: +proj=lcc +lat_1=40.66666666666666 ... # MULTIPOINT (941777.089270765 150931.02608931, 9...
DataCamp Spatial Analysis with sf and raster in R They look the same though Five points (5 features) vs 1 MULTIPOINT (1 feature with five pieces)
DataCamp Spatial Analysis with sf and raster in R Compute a tight bounding box called a convex hull > chull <- st_convex_hull(boro_union)
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Multi-layer geoprocessing and relationships Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R Multi-layer spatial analysis Linking features from multiple layers (e.g., spatial join) Determine relationships between features from different layers (e.g., intersect, distance)
DataCamp Spatial Analysis with sf and raster in R Spatial join
DataCamp Spatial Analysis with sf and raster in R No tract ID in the roads data frame The only attributes are fullname and geometry . > head(roads) # Simple feature collection with 6 features and 2 fields # geometry type: MULTILINESTRING # dimension: XY # bbox: xmin: 283409.6 ymin: 13674360 xmax: 415302.7 ... # epsg (SRID): 32712 # proj4string: +proj=utm +zone=12 +south +datum=WGS84 +units=m +no_defs # fullname linearid geom # 3 15400 N 1106093299255 MULTILINESTRING ((368965.05... # 4 W Thomas Rd N 1103680827068 MULTILINESTRING ((359947.09... # 5 W Thomas Rd S 1103680826832 MULTILINESTRING ((359642.75... # 6 W Lost Creek Dr E 1101629369359 MULTILINESTRING ((360798.66... # 7 W Lost Creek Dr W 1101629369529 MULTILINESTRING ((358095.55... # 8 I- 10 1101629193616 MULTILINESTRING ((283409.56...
DataCamp Spatial Analysis with sf and raster in R Use st_join() to conduct the spatial join > roads <- st_join(roads, tracts) > head(roads) # Simple feature collection with 6 features and 3 fields # geometry type: MULTILINESTRING # dimension: XY # bbox: xmin: 358095.6 ymin: 13704750 xmax: 368965.1 ... # epsg (SRID): 32712 # proj4string: +proj=utm +zone=12 +south +datum=WGS84 +units=m +no_defs # fullname linearid geoid geom # 3 15400 N 1106093299255 04013050605 MULTILINESTRING ((368965.0... # 3.1 15400 N 1106093299255 04013061031 MULTILINESTRING ((368965.0... # 4 W Thomas Rd N 1103680827068 04013050605 MULTILINESTRING ((359947.0... # 5 W Thomas Rd S 1103680826832 04013050605 MULTILINESTRING ((359642.7... # 6 W Lost Creek Dr E 1101629369359 04013050605 MULTILINESTRING ((360798.6... # 7 W Lost Creek Dr W 1101629369529 04013050605 MULTILINESTRING ((358095.5...
DataCamp Spatial Analysis with sf and raster in R Color-code the plot based on the new attribute > plot(roads["geoid"]) > plot(st_geometry(tracts), border = "black", add = TRUE)
DataCamp Spatial Analysis with sf and raster in R Computing relationships between multiple layers Which roads intersect with the grey polygon -- st_intersects() Which roads are completely contained by the polygon -- st_contains()
DataCamp Spatial Analysis with sf and raster in R Looking at intersection with st_intersects() # Result is a list > intersects <- st_intersects(tract, roads)[[1]] # The index number of roads that intersect > head(intersects) [1] 1 2 6 12 15 19 > plot(st_geometry(roads[intersects,]), add = TRUE, col = "red")
DataCamp Spatial Analysis with sf and raster in R Looking at contains with st_contains() > contains <- st_contains(tract, roads)[[1]] > plot(st_geometry(roads[contains,]), add = TRUE, col = "blue")
DataCamp Spatial Analysis with sf and raster in R Clip features with st_intersection() # Clip the roads to the tract polygon > clipped <- st_intersection(tract, roads) > plot(st_geometry(tract), col = "grey", border = "white") > plot(st_geometry(clipped), add = TRUE)
DataCamp Spatial Analysis with sf and raster in R Computing distance between features
DataCamp Spatial Analysis with sf and raster in R Compute distance with st_distance() > dist1 <- st_distance(tract_cent, roads) > roads$dist <- dist1[1, ]
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Let's practice!
DataCamp Spatial Analysis with sf and raster in R SPATIAL ANALYSIS WITH SF AND RASTER IN R Geoprocessing with rasters Zev Ross President, ZevRoss Spatial Analysis
DataCamp Spatial Analysis with sf and raster in R Example: Elevation raster and polygons
DataCamp Spatial Analysis with sf and raster in R Mask your raster with mask() > polys <- as(polys, "Spatial") > el_mask <- mask(elevation, mask = polys) > plot(el_mask)
DataCamp Spatial Analysis with sf and raster in R Crop your raster with crop() > el_crop <- crop(elevation, polys) > plot(el_crop) > plot(polys, add = TRUE)
Recommend
More recommend