Skip to content

the path/segment thing #15

@mdsumner

Description

@mdsumner

Building a table of all box coordinates with each row containing its directed face (.vx0 start, .vx1 end - ordered around the box) is seemingly way too hard from the current arrangement.

It should be much more straightforward. I think the problem is that p0, p1 records the orientation of vertices within a face, but there's no orientation of a face with respect to a box (and the path the linked segments take). This must be implicitl at least in the BGM, so record it explicitly in the tables and make it easy to to do.

noba_file  <- bgmfiles::bgmfiles("Nordic")

u <- "https://raw.githubusercontent.com/cecilieha/NoBA/master/corners_neighbours_nordic.txt"
x <- readr::read_tsv(u)
plot(c(x$Lon1, x$Lon2), c(x$Lat1, x$Lat1), type = "n")
segments(x$Lon1, x$Lat1, x$Lon2, x$Lat2)

library(rbgm)

## BGM is a doubly-connected-edge-list
## here in 'related tables' form
dc_edge_list <- bgmfile(noba_file)

library(dplyr)


box_verts <- dc_edge_list$boxes %>% 
     dplyr::select(.bx0, label) %>% 
     inner_join(dc_edge_list$boxesXverts, ".bx0") %>% 
  inner_join(dc_edge_list$vertices, ".vx0") %>% 
  group_by(.bx0) %>% mutate(irow = row_number()) %>% ungroup()

face_vertex_ind <- match(box_verts$.vx0, dc_edge_list$facesXverts$.vx0)
box_verts$face_id <- dc_edge_list$facesXverts$.fx0[face_vertex_ind]
box_verts$face_order <- dc_edge_list$facesXverts$.p0[face_vertex_ind]


face_box_ind <- match(box_verts$face_id, dc_edge_list$faces$.fx0)
box_verts$Neighbour <- ifelse(dc_edge_list$faces$left[face_box_ind] == box_verts$.bx0,dc_edge_list$faces$right, dc_edge_list$faces$left)

ptcentre <- as.data.frame(boxSpatial(dc_edge_list))
coordinates(ptcentre) <- c("insideX", "insideY")
proj4string(ptcentre) <- CRS(dc_edge_list$extra$projection)
library(mapview)
library(sf)
mapview(faceSpatial(dc_edge_list), lwd = 4, color = "pink")  + 
  mapview(ptcentre)
## note too that from here conversion to Spatial is straightforward
## spbabel::sp(box_verts %>% transmute(object_ = .bx0, branch_ = label, x_ = x, y_ = y, island_ = 1, order_ = irow))


## note that .vx0 refers to the start vertex in terms of start =  x1,y1, end = x2,y2
box_verts %>% transmute(irow, .bx0, x1 = x, y1 = y, .vx0) %>% 
  inner_join(box_verts %>% transmute(irow = irow - 1, x2 = x, y2 = y, .bx0)) %>% distinct(.vx0)


%>% 
  left_join(dc_edge_list$facesXverts %>% dplyr::filter(.p0 == 2))


f## the key entity we want is each face, with four coordinates at each end 
## but grouped by box
face_vertex <- dc_edge_list$facesXverts %>% 
  mutate(.p0 = c("start_vertex", "end_vertex")[.p0]) %>% 
  tidyr::spread(.p0, .vx0) %>% 
  inner_join(dc_edge_list$vertices, c("end_vertex" = ".vx0")) %>% 
  rename(x1 = x, y1 = y) %>%
  inner_join(dc_edge_list$vertices, c("start_vertex" = ".vx0")) %>% 
  rename(x2 = x, y2 = y)
face_vertex[c("Lon1", "Lat1")] <- rgdal::project(as.matrix(face_vertex[c("x1", "y1")]), 
                                                 dc_edge_list$extra$projection, inv = TRUE)
face_vertex[c("Lon2", "Lat2")] <- rgdal::project(as.matrix(face_vertex[c("x2", "y2")]), 
                                                 dc_edge_list$extra$projection, inv = TRUE)
## we have one row per face
plot(c(x$Lon1, x$Lon2), c(x$Lat1, x$Lat1), type = "n")
segments(x$Lon1, x$Lat1, x$Lon2, x$Lat2)
with(face_vertex, segments(Lon1, Lat1, Lon2, Lat2, col = "firebrick", lwd = 2))


## faces is fine but we really want box-focus
face_vertex %>% inner_join(dc_edge_list$facesXboxes, c(".fx0" = "iface"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions