Combine Voronoi polygons and maps

前端 未结 1 1940
后悔当初
后悔当初 2020-12-14 10:53

I would like to combine Voronoi polygons with map, in order to use this later for spatial analysis. I have number of points and shapefile that i want to combine and then sav

相关标签:
1条回答
  • 2020-12-14 11:08

    Slightly modified function, takes an additional spatial polygons argument and extends to that box:

    voronoipolygons <- function(x,poly) {
      require(deldir)
      if (.hasSlot(x, 'coords')) {
        crds <- x@coords  
      } else crds <- x
      bb = bbox(poly)
      rw = as.numeric(t(bb))
      z <- deldir(crds[,1], crds[,2],rw=rw)
      w <- tile.list(z)
      polys <- vector(mode='list', length=length(w))
      require(sp)
      for (i in seq(along=polys)) {
        pcrds <- cbind(w[[i]]$x, w[[i]]$y)
        pcrds <- rbind(pcrds, pcrds[1,])
        polys[[i]] <- Polygons(list(Polygon(pcrds)), ID=as.character(i))
      }
      SP <- SpatialPolygons(polys)
    
      voronoi <- SpatialPolygonsDataFrame(SP, data=data.frame(x=crds[,1],
                                                              y=crds[,2], row.names=sapply(slot(SP, 'polygons'), 
                                                                                           function(x) slot(x, 'ID'))))
    
      return(voronoi)
    
    }
    

    Then do:

    pzn.coords<-voronoipolygons(coords,pznall)
    library(rgeos)
    gg = gIntersection(pznall,pzn.coords,byid=TRUE)
    plot(gg)
    

    Note that gg is a SpatialPolygons object, and you might get a warning about mismatched proj4 strings. You may need to assign the proj4 string to one or other of the objects.

    0 讨论(0)
提交回复
热议问题