Skip to main content
added 185 characters in body
Source Link
Sam
  • 1.6k
  • 2
  • 16
  • 37

Answer for Simple Features:

sf package draws on Geometry Engine Open Source, and as such can access the list of commands such as st_within etc.

One such command, st_difference, will do the job:

require(sf)

# make a square simple feature
s <- rbind(c(1,1), c(1,5), c(5,5), c(5,1), c(1,1))
s.sf <-st_sfc(st_polygon(list(s)))
s.pol = st_sf(ID = "sq", s.sf)

# make a smaller triangle simple feature
s2 <- rbind(c(2,2), c(3,4), c(4,2), c(2,2))
s2.sf <-st_sfc(st_polygon(list(s2)))
s2.pol = st_sf(ID = "tr", s2.sf)

# find the 'difference', i.e. reverse of st_intersection
t <- st_difference(s.pol,s2.pol)

plot(t) 

# have a look at the new geometry, a well known text format with exterior followed by hole
st_geometry(t)[[1]]
POLYGON((1 1, 1 5, 5 5, 5 1, 1 1), (2 2, 4 2, 3 4, 2 2))

also see towards the bottom of this article

can also be done by coercing Sp to sf with st_as_sf. Heed the warnings as attributes can be tricky to manage!

Answer for Simple Features:

sf package draws on Geometry Engine Open Source, and as such can access the list of commands such as st_within etc.

One such command, st_difference, will do the job:

require(sf)

# make a square simple feature
s <- rbind(c(1,1), c(1,5), c(5,5), c(5,1), c(1,1))
s.sf <-st_sfc(st_polygon(list(s)))
s.pol = st_sf(ID = "sq", s.sf)

# make a smaller triangle simple feature
s2 <- rbind(c(2,2), c(3,4), c(4,2), c(2,2))
s2.sf <-st_sfc(st_polygon(list(s2)))
s2.pol = st_sf(ID = "tr", s2.sf)

# find the 'difference', i.e. reverse of st_intersection
t <- st_difference(s.pol,s2.pol)

plot(t)

also see towards the bottom of this article

can also be done by coercing Sp to sf with st_as_sf. Heed the warnings as attributes can be tricky to manage!

Answer for Simple Features:

sf package draws on Geometry Engine Open Source, and as such can access the list of commands such as st_within etc.

One such command, st_difference, will do the job:

require(sf)

# make a square simple feature
s <- rbind(c(1,1), c(1,5), c(5,5), c(5,1), c(1,1))
s.sf <-st_sfc(st_polygon(list(s)))
s.pol = st_sf(ID = "sq", s.sf)

# make a smaller triangle simple feature
s2 <- rbind(c(2,2), c(3,4), c(4,2), c(2,2))
s2.sf <-st_sfc(st_polygon(list(s2)))
s2.pol = st_sf(ID = "tr", s2.sf)

# find the 'difference', i.e. reverse of st_intersection
t <- st_difference(s.pol,s2.pol)

plot(t) 

# have a look at the new geometry, a well known text format with exterior followed by hole
st_geometry(t)[[1]]
POLYGON((1 1, 1 5, 5 5, 5 1, 1 1), (2 2, 4 2, 3 4, 2 2))

also see towards the bottom of this article

can also be done by coercing Sp to sf with st_as_sf. Heed the warnings as attributes can be tricky to manage!

Source Link
Sam
  • 1.6k
  • 2
  • 16
  • 37

Answer for Simple Features:

sf package draws on Geometry Engine Open Source, and as such can access the list of commands such as st_within etc.

One such command, st_difference, will do the job:

require(sf)

# make a square simple feature
s <- rbind(c(1,1), c(1,5), c(5,5), c(5,1), c(1,1))
s.sf <-st_sfc(st_polygon(list(s)))
s.pol = st_sf(ID = "sq", s.sf)

# make a smaller triangle simple feature
s2 <- rbind(c(2,2), c(3,4), c(4,2), c(2,2))
s2.sf <-st_sfc(st_polygon(list(s2)))
s2.pol = st_sf(ID = "tr", s2.sf)

# find the 'difference', i.e. reverse of st_intersection
t <- st_difference(s.pol,s2.pol)

plot(t)

also see towards the bottom of this article

can also be done by coercing Sp to sf with st_as_sf. Heed the warnings as attributes can be tricky to manage!