hypertidy / polymer

general intersections via a triangle pool from disparate polygon inputs
https://hypertidy.github.io/polymer
7 stars 0 forks source link

segment bucket #4

Open mdsumner opened 5 years ago

mdsumner commented 5 years ago

Function to crush sets of SC0 objects together and renormalize indexes.


c.SC0 <- function(...) {
  all_list <- list(...)
  nrs <- integer(length(all_list))
  for (i in seq_along(all_list)) {
    all_list[[i]]$object$id <- i
    nrs[i] <- nrow(all_list[[i]]$vertex)
  }
  ## object here means which element of all_list
  segs <- purrr::map_df(all_list, ~tidyr::unnest(.x$object["topology_"]), .id = "object")
  ## id here means the same (but it didn't exist before)
  #objs <- purrr::map_df(all_list, ~.x$object["id"])

  vert <- purrr::map_df(all_list, ~.x$vertex[c("x_", "y_")])
  vdata <- unjoin::unjoin(vert, .data$x_, .data$y_)
  if (nrow(vdata$data) == nrow(vdata$.idx0)) {
    ## we done, just bind the tables after re-indexing

  } else {
    uverts <- vdata$.idx0
    segs$.vx0 <- match(vdata$data$.idx0[segs$.vx0], vdata$.idx0$.idx0)
    segs$.vx0 <- match(vdata$data$.idx0[segs$.vx1], vdata$.idx0$.idx0)
  }

  topology <- split(segs[c(".vx0", ".vx1")], segs$object)[unique(segs$object)]

  incr <- 0
  for (i in seq_along(topology)) {
    topology[[i]]$.vx0 <- topology[[i]]$.vx0 + incr
    topology[[i]]$.vx1 <- topology[[i]]$.vx1 + incr
    incr <- incr + nrs[i]
    print(i)
  }
  out <- structure(list(object = tibble::tibble(topology_ = topology), vertex = vert, 
                        meta = tibble::tibble(proj = NA_character_, ctime = Sys.time())), class = c("SC0", "sc"))
  out
}

library(silicate)
library(raster)
library(sf)
g <- SC0(sf::st_cast(spex::polygonize(raster(matrix(1:12, 3))),  "LINESTRING"))
l <- SC0(SpatialLines(list(Lines(list(Line(list(rbind(c(0.2, 0.2), c(0.5, 0.5), c(0.6, 0.8), c(0.9, 0.9), c(0.2, 0.3))))), "1"))))
plot(g)
plot(l, add = TRUE, col = 1:4)
plot(c(l, g))

l2 <- SC0(SpatialLines(list(Lines(list(Line(list(rbind(c(0.2, 0.2), c(0.5, 0.5), c(0.6, 0.8), c(0.9, 0.9), c(0.2, 0.3), c(0, 0))))), "1"))))
plot(g)
plot(l2, add = TRUE, col = 1:5)
plot(c(l2, g))

library(trip)
data("walrus818", package = "trip")
x <- SC0(walrus818)
g <- SC0(sf::st_cast(spex::polygonize(raster(walrus818))))
c(x, g)
mdsumner commented 5 years ago

Well that's totally broken and hopeless. Work on a better approach from this example


library(silicate)
library(raster)
library(sf)
g <- SC0(sf::st_cast(spex::polygonize(raster(matrix(1:12, 3))),  "LINESTRING"))

l2 <- SC0(SpatialLines(list(Lines(list(Line(list(rbind(c(0.2, 0.2), c(0.5, 0.5), c(0.6, 0.8), c(0.9, 0.9), c(0.2, 0.3), c(0, 0))))), "1"))))
plot(g)
plot(l2, add = TRUE, col = 1:5)
plot(c(l2, g))