Buffers and centroids Zev Ross President, ZevRoss Spatial Analysis - - PowerPoint PPT Presentation

buffers and centroids
SMART_READER_LITE
LIVE PREVIEW

Buffers and centroids Zev Ross President, ZevRoss Spatial Analysis - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

DataCamp Spatial Analysis with sf and raster in R

Buffers and centroids

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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)

slide-4
SLIDE 4

DataCamp Spatial Analysis with sf and raster in R

Compute centroids with st_centroid()

> poly <- st_read("poly.shp") > poly_cent <- st_centroid(poly)

slide-5
SLIDE 5

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-6
SLIDE 6

DataCamp Spatial Analysis with sf and raster in R

Bounding boxes, dissolve features and create a convex hull

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-7
SLIDE 7

DataCamp Spatial Analysis with sf and raster in R

Defining a region

Rectangular bounding box Tighter polygon with a convex hull

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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 + )

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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 ...

slide-12
SLIDE 12

DataCamp Spatial Analysis with sf and raster in R

Two separate polygons

slide-13
SLIDE 13

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 ...

slide-14
SLIDE 14

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...

slide-15
SLIDE 15

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...

slide-16
SLIDE 16

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)

slide-17
SLIDE 17

DataCamp Spatial Analysis with sf and raster in R

Compute a tight bounding box called a convex hull

> chull <- st_convex_hull(boro_union)

slide-18
SLIDE 18

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-19
SLIDE 19

DataCamp Spatial Analysis with sf and raster in R

Multi-layer geoprocessing and relationships

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-20
SLIDE 20

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)

slide-21
SLIDE 21

DataCamp Spatial Analysis with sf and raster in R

Spatial join

slide-22
SLIDE 22

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...

slide-23
SLIDE 23

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...

slide-24
SLIDE 24

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)

slide-25
SLIDE 25

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()

slide-26
SLIDE 26

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")

slide-27
SLIDE 27

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")

slide-28
SLIDE 28

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)

slide-29
SLIDE 29

DataCamp Spatial Analysis with sf and raster in R

Computing distance between features

slide-30
SLIDE 30

DataCamp Spatial Analysis with sf and raster in R

Compute distance with st_distance()

> dist1 <- st_distance(tract_cent, roads) > roads$dist <- dist1[1, ]

slide-31
SLIDE 31

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R

slide-32
SLIDE 32

DataCamp Spatial Analysis with sf and raster in R

Geoprocessing with rasters

SPATIAL ANALYSIS WITH SF AND RASTER IN R

Zev Ross

President, ZevRoss Spatial Analysis

slide-33
SLIDE 33

DataCamp Spatial Analysis with sf and raster in R

Example: Elevation raster and polygons

slide-34
SLIDE 34

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)

slide-35
SLIDE 35

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)

slide-36
SLIDE 36

DataCamp Spatial Analysis with sf and raster in R

Mask and crop!

> el_crop <- crop(elevation, polys) > el_mask <- mask(el_crop, mask = polys) > plot(el_mask)

slide-37
SLIDE 37

DataCamp Spatial Analysis with sf and raster in R

Extract values with extract()

With points the raster values under each point are returned With polygons you have two options controlled by the fun argument: Return all values in each polygon (fun = NULL) Supply a summary function (e.g., fun = mean)

slide-38
SLIDE 38

DataCamp Spatial Analysis with sf and raster in R

Extract elevation values for the polygons

# For polys fun = NULL will return all values > vals <- extract(elevation, polys, fun = mean) > vals # [,1] # [1,] 471.1458 # hill polygon # [2,] 203.4773 # lake polygon

slide-39
SLIDE 39

DataCamp Spatial Analysis with sf and raster in R

Raster math with overlay()

Here we have two rasters:

  • 1. The elevation raster
  • 2. The raster with multiplier values
slide-40
SLIDE 40

DataCamp Spatial Analysis with sf and raster in R

Raster math

> f <- function(rast1, rast2) rast1 * rast2 > alt_ele <- overlay(elevation, multiplier, fun = f)

slide-41
SLIDE 41

DataCamp Spatial Analysis with sf and raster in R

Result of overlay

Elevation values have been multiplied by either 1.25 or 0.75 depending on their

  • location. Otherwise raster values are set to NA.
slide-42
SLIDE 42

DataCamp Spatial Analysis with sf and raster in R

Let's practice!

SPATIAL ANALYSIS WITH SF AND RASTER IN R