gpt4 book ai didi

r - 在 R 中识别传单中光栅的点击位置

转载 作者:行者123 更新时间:2023-12-04 10:33:56 25 4
gpt4 key购买 nike

我正在绘制一个大型的经纬度 NetCDF raster在 R leaflet map 使用 shinydashboard .当我单击 map 时,会出现一个弹出窗口并显示单击的栅格点的行、列、纬度位置和值。 (请参阅下面的可重现代码)

问题是,如果光栅足够大,我会遇到光栅的变化。例如,在这里我点击了一个应该有值的点,但结果是识别的点是上面的点。

enter image description here

我相信这与 leaflet 使用的栅格有关。被投影,而我用来识别点的原始数据是 Lat-Lon,因为单击的点由 leaflet 返回为 Lat-Lon| .我无法使用投影文件( depth ),因为它的单位是米,而不是度!
即使我试图将这些米重新投影到度数,我也得到了转变。

这是代码的基本可运行示例:

#Libraries
library(leaflet)
library(raster)
library(shinydashboard)
library(shiny)

#Input data
download.file("https://www.dropbox.com/s/y9ekjod2pt09rvv/test.nc?dl=0", destfile="test.nc")
inputFile = "test.nc"
inputVarName = "Depth"
lldepth <- raster(inputFile, varname=inputVarName)
lldepth[Which(lldepth<=0, cells=T)] <- NA #Set all cells <=0 to NA
ext <- extent(lldepth)
resol <- res(lldepth)
projection(lldepth) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

#Project for leaflet
depth <- projectRasterForLeaflet(lldepth)

#Prepare UI
sbwidth=200
sidebar <- dashboardSidebar(width=sbwidth)
body <- dashboardBody(
box( #https://stackoverflow.com/questions/31278938/how-can-i-make-my-shiny-leafletoutput-have-height-100-while-inside-a-navbarpa
div(class="outer",width = NULL, solidHeader = TRUE, tags$style(type = "text/css", paste0(".outer {position: fixed; top: 50px; left: ", sbwidth, "px; right: 0; bottom: 0px; overflow: hidden; padding: 0}")),
leafletOutput("map", width = "100%", height = "100%")
)
)
)
ui <- dashboardPage(
dashboardHeader(title = "A title"),
sidebar,
body
)
#
#Server instance
server <- function(input, output, session) {
output$map <- renderLeaflet({#Set extent
leaflet() %>%
fitBounds(ext[1], ext[3], ext[2], ext[4])
})

observe({#Observer to show Popups on click
click <- input$map_click
if (!is.null(click)) {
showpos(x=click$lng, y=click$lat)
}
})

showpos <- function(x=NULL, y=NULL) {#Show popup on clicks
#Translate Lat-Lon to cell number using the unprojected raster
#This is because the projected raster is not in degrees, we cannot use it!
cell <- cellFromXY(lldepth, c(x, y))
if (!is.na(cell)) {#If the click is inside the raster...
xy <- xyFromCell(lldepth, cell) #Get the center of the cell
x <- xy[1]
y <- xy[2]
#Get row and column, to print later
rc <- rowColFromCell(lldepth, cell)
#Get value of the given cell
val = depth[cell]
content <- paste0("X=",rc[2],
"; Y=",rc[1],
"; Lon=", round(x, 5),
"; Lat=", round(y, 5),
"; Depth=", round(val, 1), " m")
proxy <- leafletProxy("map")
#add Popup
proxy %>% clearPopups() %>% addPopups(x, y, popup = content)
#add rectangles for testing
proxy %>% clearShapes() %>% addRectangles(x-resol[1]/2, y-resol[2]/2, x+resol[1]/2, y+resol[2]/2)
}
}

#Plot the raster
leafletProxy("map") %>%
addRasterImage(depth, opacity=0.8, project=FALSE, group="Example", layerId="Example", colors=colorNumeric(terrain.colors(10), values(depth), na.color = "black"))
}


print(shinyApp(ui, server))

如果栅格很大,如何正确识别点?

编辑:
我还想提供一些(可能)相关文档或问题的附加链接:
  • Raster image seems to be shifted using leaflet for R
  • R for leaflet redirect when clicking on raster image
  • https://gis.stackexchange.com/questions/183918/is-it-possible-to-use-a-rasterclick-event-within-an-interactive-leaflet-map
  • marker mouse click event in R leaflet for shiny
  • 最佳答案

    我发现我可以重新投影 input$map_click 给出的 X-Y(经纬度)位置.
    在这种情况下,我假设输入投影是 Lon-Lat,但我认为它不一定是。它只需要有纬度单位。

    #Set projections
    inputProj <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
    leafletProj <- "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +nadgrids=@null +wktext +no_defs"
    #Note that for some reason "+nadgrids=@null +wktext" is very important
    #as hinted to by other questions and answers linked in my question.
    xy <- SpatialPoints(data.frame(x,y))
    proj4string(xy) <- inputProj
    xy <- as.data.frame(spTransform(xy, leafletProj))
    #Get the cell number from the newly transformed metric X and Y.
    cell <- cellFromXY(depth, c(xy$x, xy$y))

    #At this point, you can also retrace back the center of the cell in
    #leaflet coordinates, starting from the cell number!
    xy <- SpatialPoints(xyFromCell(depth, cell))
    proj4string(xy) <- leafletProj
    xy <- as.data.frame(spTransform(xy, inputProj))
    #Here XY will again be in lat-lon, if you projection says so,
    #indicating the center of the clicked cell

    关于r - 在 R 中识别传单中光栅的点击位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37523323/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com