更新时间:2023-01-22 08:40:15
我发现我可以将input$map_click
给出的X-Y(lon-lat)位置重新投影回去.
在这种情况下,我假设输入投影为Lon-Lat,但我认为不一定必须如此.它只需要有Lat-Lon单位.
I have found that I can reproject back the X-Y (lon-lat) position given by input$map_click
.
In this case I assumed the input projection to be Lon-Lat, but I think it doesn't necessarily have to be. It just needs to have Lat-Lon units.
#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