source("manuscript/global.R")
library(sf)
library(ggplot2)
library(dplyr)

Geometry Creation

Points

pt <- st_point(c(0,0))
loc <- matrix(rnorm(20), ncol = 2)
pts <-  st_multipoint(loc)
plot(pts,col = "lightblue", pch = 16)
plot(pt,col = "gold", pch = 16, add = TRUE)

Lines

pts <- rbind(c(-1,-1),c(1,1))
ln <- st_linestring(pts)
plot(ln,col = "lightblue", pch = 16)
pts = matrix(rnorm(10), ncol = 2)
lns = st_linestring(pts)
plot(lns,col = "lightblue", pch = 16)
pts1 = matrix(rnorm(10), ncol = 2)
pts2 = matrix(rnorm(10), ncol = 2)
lns = st_multilinestring(list(pts1, pts2))
plot(lns)

Polygons

box1 = rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1))
box2 = rbind(c(-1.5,0), c(0,1.5), c(1.5,0), c(0,-1.5), c(-1.5,0))

pl = st_polygon(list(box1, box2))
plot(pl)
pls = st_multipolygon(list(list(box1), list(box2)))
plot(pls)

Circle

circle <- st_buffer(st_point(c(0,0)),1)
plot(circle)

Geometry Collections

gc <- st_geometrycollection(list(pt, ln, lns))
plot(gc)

SF Objects

sfc <- st_sfc(pt, ln, lns)
sf <- st_sf(
  shape = c("point","line","multiline"),
  geom = sfc
  ) 
plot(sf)
ggplot()+
  geom_sf(data = sf)+
  theme_bw()
st_box1 = st_polygon(list(box1))
st_box2 = st_polygon(list(box2))

sf <- tibble::tribble(
  ~shape, ~geometry,
  "box1", st_box1,
  "box2", st_box2,
  ) %>%
  st_as_sf()
plot(sf)

Geometric Operations

Intersection Lines

l1 <- st_linestring(box1)
l2 <- st_linestring(box2)
pnts <- st_intersection(l1, l2)

plot(l2)
plot(l1, add = T)
plot(pnts, add = T, col = "red", pch = 21)

Conver Hull

Makes a hull around the multilinestring and turns into a polygon.

ml_sf = st_sf(geom = st_sfc(st_multilinestring(list(box1, box2)))) 
st_ch = st_convex_hull(ml_sf)
plot(st_ch,col = "lightblue")
plot(ml_sf, add = T, col = "gold")

Extract Bordering Points

Extracts the bordering points a boundary around the multilinestring and turns into a polygon.

st_bd <- st_boundary(st_ch)
plot(st_ch, col = "lightblue")
plot(st_cast(st_bd,"POINT"),col = "red", add = T)

Union

pl1 = st_polygon(list(box1))
pl2 = st_polygon(list(box2))
plun <- st_union(pl1, pl2)
plot(plun, col = "lightblue", main = "Union")

Intersection

plot(st_intersection(pl1, pl2),col = "lightblue", main = "Intersection")

Difference

pldif <- st_difference(pl1, pl2)
plot(pldif,col = "lightblue", main = "Difference")

Symmetric Difference

symDif <- st_sym_difference(pl1, pl2)
plot(symDif,col = "lightblue", main = "Symmetric Difference")

Crop

box = c(xmin = -1.2, ymin = -1.2, xmax = 1.2, ymax = 1.2)
plot(st_crop(sf, box),col = "lightblue", main = "Crop")

contains

pl3 = pl2*0.5 + 0.2
st_contains(pl2,pl3)
plot(pl2, col = "lightblue")
plot(pl3, add = T,col = "gold")
np <- st_nearest_points(pl3,pldif)
plot(pldif,col = "lightblue")
plot(pl3, add = T,col = "gold")
plot(np, add = T,col = "red")
st_distance(pl3,pldif)
plbuf = st_buffer(plun,0.2)
plot(plbuf, col = "lightblue")
plot(plun, add = T,col = "gold")
plbd <- st_boundary(plun)
plot(plbd, col = "lightblue",lwd = 20)
plot(plun, add = T,col = "gold")
rot = function(a) matrix(c(cos(a), sin(a), -sin(a), cos(a)), 2, 2)
cntrd = st_centroid(plun)
plrot = (plun - cntrd) * rot(pi/6) + cntrd
plot(plun, border = 'lightblue')
plot(plrot, add = TRUE,border = "gold")


bejolithic/Arabesque documentation built on Dec. 19, 2021, 7:43 a.m.