r-spatial / mapedit

Interactive editing of spatial data in R
https://www.r-spatial.org/r/2019/03/31/mapedit_leafpm.html
Other
218 stars 33 forks source link

Coordinates using editMap() with addRasterRGB() are wrong #130

Closed TiagoOlivoto closed 7 months ago

TiagoOlivoto commented 10 months ago

Hi! Thanks for developing such an useful tool!

I have noticed a strange issue while using editMap() and addRasterRGB(). You can try the following by drawing a polygon in any area of the raster 'rgb'

require(raster)
require(stars)
require(plainview)
require(leaflet)
library(leafem)
library(sf)
rgb <- plainview::poppendorf

works fine (using addGeoRaster())

res <- 
leaflet() %>%
  addTiles(group = "OpenStreetMap") %>%
  addGeoRaster(rgb) |> 
  editMap()

polygons_spv <- sf::st_transform(res$finished$geometry, crs = sf::st_crs(rgb))

plot(rgb[[1]])
lines(as.matrix(polygons_spv[[1]]))

wrong coordinates when using addRasterRGB()

res <- 
  leaflet() %>%
  addTiles(group = "OpenStreetMap") %>%
  addRasterRGB(rgb, 4,3,2, group = "True colours") |>
  editMap()

polygons_spv <- sf::st_transform(res$finished$geometry, crs = sf::st_crs(rgb))

plot(rgb[[1]])
lines(as.matrix(polygons_spv[[1]]))

This make to me extract the pixels from the wrong location when using editMap() to create polygons in the image. Can you please help with this? Thanks

tim-salabim commented 10 months ago

Hm, I can't seem to reproduce this... Can you post some of the resulting plots? And could you give us your sessionInfo()?

TiagoOlivoto commented 10 months ago

Thanks for the prompt response!

Just to keep you up to date. I use editMap() in my package pliman to analyze orthomosaics. Generally, I need high zoom levels (~ 0.2 m) to select the cropping area correctly.

When using addGeoRaster(), the coordinates seems to be correct

Packages and raster image

library(mapedit)
require(plainview)
require(leaflet)
library(leafem)
library(sf)
library(terra)
library(stars)
rgb <- terra::rast(plainview::poppendorf)

works fine (using addGeoRaster())

res <- 
  leaflet() %>%
  addTiles(group = "OpenStreetMap") %>%
  addGeoRaster(rgb |> st_as_stars()) |> 
  addMouseCoordinates() |> 
  editMap(editor = "leafpm")

# crop the raster and show the draw rectangle
polyn <- res$finished$geometry
polygons_spv <- sf::st_transform(polyn, crs = sf::st_crs(rgb))
polygons_ext <- terra::vect(polygons_spv)
ext <- terra::buffer(polygons_ext, 100) |> terra::ext()
rgb_cr <- terra::crop(rgb, ext)
plot(rgb_cr[[1]])
lines(as.matrix(polygons_spv[[1]]))

image

wrong coordinates when using addRasterRGB() Using the addRasterRGB, I can see that the draw rectangle in the cropped raster is not in the same position as I observed when editing. After a detailed look, I noticed that the coordinates shown in the viewer are not the same for the same pixel compared to addGeoRaster(). Maybe a bug with addRasterRGB()?

res <- 
  leaflet() %>%
  addTiles(group = "OpenStreetMap") %>%
  addRasterRGB(rgb |> st_as_stars()) |> 
  addMouseCoordinates() |> 
  editMap(editor = "leafpm")

polyn <- res$finished$geometry
polygons_spv <- sf::st_transform(polyn, crs = sf::st_crs(rgb))
polygons_ext <- terra::vect(polygons_spv)
ext <- terra::buffer(polygons_ext, 100) |> terra::ext()
rgb_cr <- terra::crop(rgb, ext)
plot(rgb_cr[[1]])
lines(as.matrix(polygons_spv[[1]]))

image

Section Info

sessionInfo()
R Under development (unstable) (2023-10-13 r85327 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22621)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.utf8  LC_CTYPE=Portuguese_Brazil.utf8   
[3] LC_MONETARY=Portuguese_Brazil.utf8 LC_NUMERIC=C                      
[5] LC_TIME=Portuguese_Brazil.utf8    

time zone: America/Sao_Paulo
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stars_0.6-4     abind_1.4-5     terra_1.7-55    sf_1.0-14       leafem_0.2.3   
[6] plainview_0.2.1 mapedit_0.6.0  

loaded via a namespace (and not attached):
 [1] utf8_1.2.3         generics_0.1.3     class_7.3-22       KernSmooth_2.23-22
 [5] lattice_0.21-9     digest_0.6.33      magrittr_2.0.3     grid_4.4.0        
 [9] fastmap_1.1.1      e1071_1.7-13       DBI_1.1.3          promises_1.2.1    
[13] fansi_1.0.4        crosstalk_1.2.0    viridisLite_0.4.2  codetools_0.2-19  
[17] cli_3.6.1          shiny_1.7.5.1      rlang_1.1.1        units_0.8-4       
[21] ellipsis_0.3.2     base64enc_0.1-3    parallel_4.4.0     tools_4.4.0       
[25] raster_3.6-26      dplyr_1.1.3        httpuv_1.6.11      vctrs_0.6.3       
[29] R6_2.5.1           mime_0.12          png_0.1-8          proxy_0.4-27      
[33] lifecycle_1.0.3    classInt_0.4-10    leaflet_2.2.0      htmlwidgets_1.6.2 
[37] pkgconfig_2.0.3    pillar_1.9.0       later_1.3.1        glue_1.6.2        
[41] Rcpp_1.0.11        tibble_3.2.1       tidyselect_1.2.0   rstudioapi_0.15.0 
[45] xtable_1.8-4       htmltools_0.5.6.1  compiler_4.4.0     sp_2.1-1          

Thanks in advance!

TiagoOlivoto commented 7 months ago

I fixed this issue by using viewRGB() within pliman. So, I believe I can now close this.