- I have two polygons, an outer polygon and an inner polygon
- The inner polygon is a polygon
pol1(red) for a parameter with avalue = 10 - The outer polygon is a polygon
pol2(blue) for a parameter with avalue = 20 - I need to draw a polygon for a
value = 13that would lie somewhere betweenpol1andpol2 - This is almost like buffering but not quite since buffering would only give me a constant distance.This is some sort of interpolation.
- I think the solution would be like calculating distances from the odes in the inner polygon
pol1to the nearest edge ofpol2 - Then interpolating the distance for
value = 13for each node. - Then calculate the new points for a polygon
pol3forvalue= 13. - I could tediously try to code this but i imagine there may be a quicker/simpler solution Is there any tool that can help me to solve this. I would like Something that can be used with sf objects in R.
The code below is just for producing pol1 and pol2 which I hope someone can use to create a solution.
(In reality i have a more complex sf objects read from .shp files so I am thankful if you have example with such files )
library(sf)
#polygon1 value=10
lon = c(21, 22,23,24,22,21)
lat = c(1,2,1,2,3,1)
Poly_Coord_df = data.frame(lon, lat)
pol1 = st_polygon(
list(
cbind(
Poly_Coord_df$lon,
Poly_Coord_df$lat)
)
)
#polygon2 value =20
lon = c(21, 20,22,25,22,21)
lat = c(0,3,5,4,-3,0)
Poly_Coord_df = data.frame(lon, lat)
pol2 = st_polygon(
list(
cbind(
Poly_Coord_df$lon,
Poly_Coord_df$lat)
)
)
plot(pol2,border="blue")
plot(pol1,border="red",add=T)
# How to create pol3 with value = 13?


One option is to go through inverse distance weighted interpolation and "contour" the resulting raster. For idw to work, we first need a set of locations and a grid that defines resulting locations. For input locations we'll first segementize polygon lines so we would have bit more than just corner locations, then cast to
POINTs. For the grid we'll first build a polygon that covers interpolation area and input polygon lines; this will be turned intostarsraster. This also definesidw()output objects, which we can pass tostars::st_contour()to get polygons or linestings.Resulting
sfobject:Edit: other interpolation methods
IDW is just one of many interpolations methods; there's a good chance that some others are either faster and/or deliver more suitable results so it would probably be a good idea to look into Kriging methods (same
gstatpackage) and maybe few others. E.g. one super-simple approach would be k-Nearest Neighbour Classification to detect the distance boundary between 2 different point classes:Created on 2023-08-18 with reprex v2.0.2