knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

The approaches required for Spatial data include the following.

Maybe this ?can be done more directly, to get the entire table of vertices classified by branch

This has to be able to be done dynamically, since a projection transformation or choice of vertex attributes can change the definition of "unique".

extract_ always works with the geometry _join always works with the attributes

apply_sp <- function(x) { index <- vector("list", nrow(x)) for (i in seq_along(index)) index[[i]] <- sptable(x[i, ]) %>% mutate(object_ = i) bind_rows(index) }

New approach for gris.

library(spbabel) ## devtools::install_github("mdsumner/spbabel")
library(dplyr)
source("R/grist.r")
library(maptools)
data(wrld_simpl)
#install.packages("rworldxtra")
library(rworldxtra)
data(countriesHigh)
# library(spdplyr)
# d1 <- countriesHigh %>% filter(SOVEREIGNT == "Antarctica")
# map1 <- sptable(d1) 
# 
# gg <- gris2(d1, map1)
# gg$s <- grist(gg)
#split(map1, map1$branch_)

d <- countriesHigh
d1<- as.data.frame(d) %>% mutate(object_ = row_number())
map1 <- sptable(d) 

x <- gris2(d1, map1)

verts <- with(x, o %>% filter(NAME == "Japan") %>% select(object_) %>% inner_join(b) %>% inner_join(bXv) %>% inner_join(v))
library(ggplot2)
ggplot(verts) + aes(x = x_, y = y_, fill = branch_, group = branch_) + geom_polygon()


## trips should be indexing from the original data frame
load("~/AMP2015/Data/tracks/EAnt_SES.RData")

library(trip)
library(dplyr)
d <- as_data_frame(ses_tracks) %>% mutate(vertex_ = row_number())

tr <- 
  d %>% select(ref, gmt, vertex_) %>% distinct(ref, gmt, .keep_all = TRUE) %>%  arrange(ref, gmt)

#library(magrittr)
library(ggplot2)
plot_tr <- function(x) {
  dd <- tr %>% inner_join(d %>% select(LON, LAT, vertex_), "vertex_")
  ggplot(dd[1:1000, ]) + aes(x = LON, y = LAT, group = ref, colour = ref) + geom_line()

}



## say we add the segments, then subset
x$s <- grist(x)
xa <- list(o =x$o %>% filter(NAME == "Japan"))
xa$b <- inner_join(x$b, xa$o %>% select(object_))
xa$bXv <- inner_join(x$bXv, xa$b %>% select(branch_))
xa$v <- inner_join(x$v, xa$bXv %>% select(vertex_)) %>% distinct()
xa$s <- inner_join(x$s, xa$b %>% select(branch_))

verts <- with(xa, o %>% select(object_) %>% inner_join(b) %>% inner_join(bXv) %>% inner_join(v))
library(ggplot2)
ggplot(verts) + aes(x = x_, y = y_, fill = branch_, group = branch_) + geom_polygon()

p <- xa$v %>% dplyr::select(x_, y_) %>% as.matrix()
S <- seg2struct(xa) %>% as.matrix()

p <- x$v %>% dplyr::select(x_, y_) %>% as.matrix()
p <- gris:::llh2xyz(cbind(p, 0))
S <- seg2struct(x) %>% as.matrix()

library(rgl)
lines3d(cbind(p, 0)[t(S), ])
tr <- RTriangle::triangulate(p = RTriangle::pslg(xa$v %>% dplyr::select(x_, y_) %>% as.matrix(), 
                                                 S = seg2struct(xa) %>% as.matrix()),                                               a = 0.1)

plot(tr$P, pch = ".")
apply(tr$T, 1, function(a) lines(tr$P[a, ]))

Try conversion from other formats.

library(spatstat)
  # polygon with hole
  ho <- owin(poly=list(list(x=c(0,1,1,0), y=c(0,0,1,1)),
                       list(x=c(0.6,0.4,0.4,0.6), y=c(0.2,0.2,0.4,0.4))))

as_df_bdry <- function(x) {
  bind_rows(lapply(x$bdry, function(xb) data_frame(x_ = xb$x, y_ = xb$y)), .id = "branch_")
}


as_df_bdry(ho)
as_df_branch_bdry(ho)


r-gris/nf documentation built on May 26, 2019, 1:33 p.m.