'Cesium Draw and Measure tools for 2D maps?

I am using Cesium to display a strictly 2D map in the browser (wrapped in React using the Resium library).

I am interested in giving the user the option to draw lines\polygons, and to measure the distances between two points or the area withing the polygon.

Basially, I want precisely this OpenLayeres example, but in Cesium: https://openlayers.org/en/latest/examples/measure.html

How would one go about doing it?

Thank you



Solution 1:[1]

Using lapply you could achieve your desired result like so:

df <- data.frame(
  P1 = c(0L, 2L),
  P2 = c("3 Coins", "4"),
  P3 = c("2", "-2 Coins"),
  P4 = c(1L, 4L)
)

df[] <- lapply(df, function(x) {x[grepl("coin", tolower(x), fixed = TRUE)] <- 0; x})

df
#>   P1 P2 P3 P4
#> 1  0  0  2  1
#> 2  2  4  0  4

Solution 2:[2]

Here is a tidyverse solution. The string pattern is detected by stringr::str_detect that returns TRUE/FALSE and this logical value is used inside if_else. You can then use the superseded mutate_all function or the new method of using mutate along with across to transform the values.

library(tidyverse)

df <- data.frame(
  P1 = c("0", "2"),
  P2 = c("3 Coins", "4"),
  P3 = c("2", "-2 Coins"),
  P4 = c("1", "4")
)

df 
#>   P1      P2       P3 P4
#> 1  0 3 Coins        2  1
#> 2  2       4 -2 Coins  4

df %>%
  mutate(
    dplyr::across(
      .cols = everything(), 
      .fns = ~ dplyr::if_else(stringr::str_detect(.x, "Coins"), "0", .x)
    )
  )
#>   P1 P2 P3 P4
#> 1  0  0  2  1
#> 2  2  4  0  4

Created on 2022-01-21 by the reprex package (v2.0.1)

Solution 3:[3]

We may directly convert to numeric with as.numeric, which converts the elements with characters to NA which can be changed to 0

df[] <- as.numeric(as.matrix(df))
df[is.na(df)] <- 0

-output

> df
  P1 P2 P3 P4
1  0  0  2  1
2  2  4  0  4

data

df <- structure(list(P1 = c("0", "2"), P2 = c("3 Coins", "4"), P3 = c("2", 
"-2 Coins"), P4 = c("1", "4")), class = "data.frame", row.names = c(NA, 
-2L))

Solution 4:[4]

So many solutions! Here is one more using str_contains function from sjmisc package in combination with dplyr:

library(sjmisc)
library(dplyr)
df %>% 
  rowwise() %>% 
  mutate(across(everything(), ~ifelse(str_contains(., "Coins"), "0", .)))
     P1 P2    P3       P4
  <dbl> <chr> <chr> <dbl>
1     0 0     2         1
2     2 4     0         4

Solution 5:[5]

An easy way would be to convert df into a matrix, use grepl to subset the required indices, set them to 0 then convert df back to data.frame:

df <- as.matrix(df)
df[grepl(pattern = "coins", x = df)] <- 0

df <- as.data.frame(df)

df

#>   p1 p2 p3 p4
#> 1  0  0 -2  1
#> 2  2  4  0  4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 stefan
Solution 2 Claudiu Papasteri
Solution 3 akrun
Solution 4 TarJae
Solution 5 Mwavu