1

We will use st_voronoi() to create a Voronoi diagram.

I have three questions.

Point one.

I understand that the argument to st_voronoi() should be the following data.

■SFC format (an error occurs in SF format)

■MULTIPOINT format(Not in POINT form.) Is this understanding correct?

Point two.

If you run the following code, it will be converted to GEOMETRYCOLLECTION. Is there a way to convert this to MULTIPOINT?

Point three.

The original "nz_height" contains the word "elevation". However, the conversion to SFC format has erased that column. I would like to figure out the elevation of each area of the Voronoi diagram. What kind of processing should I do?

If there is anyone who knows about it, please tell us about it.

library(sf)
library(sp)
library(spData)

print(class(nz))

print(class(nz_height))
print(head(nz_height))
plot(st_geometry(nz_height))

#POINT to MULTIPOINT
nz_height_mp <- st_cast(nz_height,"MULTIPOINT")
print(head(nz_height_mp))

#sf to sfc
nz_height_mp_sfc <- st_union(nz_height_mp)
print(class(nz_height_mp_sfc))
print(head(nz_height_mp_sfc))


nz_height_v <-st_voronoi(nz_height_mp_sfc)

print(class(nz_height_v))
print(head(nz_height_v))
plot(st_geometry(nz_height_v),col=0)
6
  • I think everything you need is in the help for st_voronoi - combine point geometries to a single multipoint (not a vector of single-point MULTIPOINT geometries) with c (or possibly st_combine) and extract geometry then st_intersects to match points with polygons. – Spacedman May 18 '20 at 17:27
  • Thank you for answering. If possible, I'd like to know the sample code. – ken iwa May 19 '20 at 0:57
  • The code is in the help text, in the examples section. It shows how to merge points, do the voronoi, then match the points to the polygons. – Spacedman May 19 '20 at 6:03
  • Where can I find help texts? – ken iwa May 19 '20 at 11:41
  • help(st_voronoi), or possibly via RStudio's interface if you are using that. – Spacedman May 19 '20 at 13:06
2

Here's a function that can take an sf data frame with POINT geometry in each row and produce the polygon geometry in the same order:

st_voronoi_point <- function(points){
    ## points must be POINT geometry
    if(!all(st_geometry_type(points) == "POINT")){
        stop("Input not  POINT geometries")
    }
    g = st_combine(st_geometry(points)) # make multipoint
    v = st_voronoi(g)
    v = st_collection_extract(v)
    return(v[unlist(st_intersects(p, v))])
}

p = st_as_sf(data.frame(x=runif(10),y=runif(10),A=letters[1:10]),coords=1:2)
v = st_voronoi_point(p)

v is now the polygon geometry vector that matches the order in the source. If you want you can then do:

pv = st_set_geometry(p, v)

to make a version of p but with the polygons as geometry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.