Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mesh3d #33

Open
dcooley opened this issue May 22, 2019 · 3 comments
Open

support mesh3d #33

dcooley opened this issue May 22, 2019 · 3 comments

Comments

@dcooley
Copy link
Collaborator

dcooley commented May 22, 2019

library(quadmesh)
library(raster)
## dummy raster
rr <- setExtent(raster::raster(matrix(sample(1:12), 3)), raster::extent(0, 4, 0, 3))
qm <- quadmesh(rr * 10000)  ## something suitably exaggerated

## convert a 3-column matrix to text embedded triples in json arrays
coord_to_json <- function(x) paste0(unlist(lapply(split(t(x), rep(seq_len(nrow(x)), each = ncol(x))), function(cds) sprintf("[%f,%f,%f]", cds[1], cds[2], cds[3]))), collapse = ",")
## add that first coordinate to the end
coord1 <- function(x) rbind(x, x[1, ])
format_mesh <- function(x, ...) {
  UseMethod("format_mesh")
}
#' format mesh3d into JSON suitable for mapdeck ... WIP 
format_mesh.mesh3d <- function(x, ...) {
  ## switch on x$primitivetype == "quad"/"triangle"
  stopifnot(x$primitivetype == "quad")
  feature_template <- '[{"type":"Feature","properties":{"elevation":0,"fill_colour":"#440154FF","stroke_colour":"#440154FF"},%s}]'
  polygon_template <- '"geometry":{"geometry":{"type":"Polygon","coordinates":[[%s]]}}'
  ## assume the expanded coords for each primitive
  coords <- sprintf(polygon_template, unlist(lapply(seq_len(ncol(qm$ib)), function(iquad)  coord_to_json(coord1(t(qm$vb[1:3, qm$ib[, iquad] ]))))))
  paste0(sprintf(feature_template, coords, collapse = ","), collapse = ",")
}

format_mesh( qm )

spatialwidget:::rcpp_geojson_mesh( qm )
dcooley added a commit that referenced this issue May 22, 2019
@mdsumner
Copy link

I can't get this to compile, but I'll keep trying.

I had to update to github geojsonsf, could you put it in the Remotes field in DESCRIPTION?

Remotes: SymbolixAU/geojsonsf

@SymbolixAU
Copy link
Collaborator

I'm actually going to stash this branch in favour of doing the work in mapdeck, so this example will no longer be valid.

@dcooley
Copy link
Collaborator Author

dcooley commented Jun 11, 2019

reopening because I want to benchmark doing it here and bypassing the mesh_to_sf() step in mapdeck.

Plus, colourvalues now supports lists, so I no longer need the average_z values.

@dcooley dcooley reopened this Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants