overlay                  package:sp                  R Documentation

_s_p_a_t_i_a_l _o_v_e_r_l_a_y _f_o_r _p_o_i_n_t_s, _g_r_i_d_s _a_n_d _r_i_n_g_s

_D_e_s_c_r_i_p_t_i_o_n:

_U_s_a_g_e:

     overlay(x, y, ...)

_A_r_g_u_m_e_n_t_s:

       x: first layer

       y: second layer, put on top of x

     ...: optional arguments; see example below 

_V_a_l_u_e:

     a numerical array of indices of 'x' on locations of 'y', or a
     data.frame with (possibly aggregate) properties of 'x' in units of
     'y'.

_N_o_t_e:

     points on a ring boundary and points corresponding to a ring
     vertex are considered to be inside the ring

_S_e_e _A_l_s_o:

     overlay-methods, point.in.polygon

_E_x_a_m_p_l_e_s:

     r1 = cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 
     180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 
     332618, 332413, 332349))
     r2 = cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 
     179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 
     331133, 331623, 332152, 332357, 332373))
     r3 = cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 
     179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
     c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 
     329783, 329665, 329720, 329933, 330478, 331062, 331086))

     sr1=Polygons(list(Polygon(r1)),"r1")
     sr2=Polygons(list(Polygon(r2)),"r2")
     sr3=Polygons(list(Polygon(r3)),"r3")
     sr=SpatialPolygons(list(sr1,sr2,sr3))
     srdf=SpatialPolygonsDataFrame(sr, data.frame(cbind(1:3,5:3), row.names=c("r1","r2","r3")))

     data(meuse)
     coordinates(meuse) = ~x+y
     data(meuse.grid)
     coordinates(meuse.grid) = ~x+y
     gridded(meuse.grid) = TRUE

     plot(meuse)
     polygon(r1)
     polygon(r2)
     polygon(r3)

     overlay(srdf, meuse)
     overlay(sr, meuse)

     overlay(meuse, srdf, fn = mean)
     overlay(meuse, srdf)
     overlay(as(meuse, "SpatialPoints"), srdf)
     overlay(as(meuse, "SpatialPoints"), sr)

     # same thing, with grid:
     overlay(srdf, meuse.grid)
     overlay(sr, meuse.grid)

     overlay(meuse.grid, srdf, fn = mean)
     overlay(meuse.grid, srdf)
     overlay(as(meuse.grid, "SpatialPoints"), srdf)
     overlay(as(meuse.grid, "SpatialPoints"), sr)

