rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
808 stars 507 forks source link

addRasterImage with crsClass = "L.CRS.Simple" #371

Open tim-salabim opened 7 years ago

tim-salabim commented 7 years ago

I cannot get addRasterImage to work on a map with crsClass = "L.CRS.Simple"

library(mapview)

rst <- poppendorf[[5]]

leaflet(options = leafletOptions(minZoom = -100,
                                 crs = leafletCRS(crsClass = 
                                                    "L.CRS.Simple"))) %>% 
  addRasterImage(rst, project = FALSE) %>%
  addMouseCoordinates()

I can see through the mouseCoordinates that the view of the map is roughly set to the correct extent when hovering over the empty map (compare to e.g. mapview(poppendorf[[5]]) )

Any ideas how to get this working? This may also be related to https://github.com/environmentalinformatics-marburg/mapview/issues/57 where canvas rendering does not work with L.CRS.Simple

bhaskarvk commented 7 years ago

I think this is because currently leaflet re projects all raster data to 3857, I think the code should be changed to re project to what ever is the target projection (L.CRS.Simple) in this case.

tim-salabim commented 7 years ago

For L.CRS.Simple no re-projection needs to happen AFAIK, that's the beauty of it. I agree, the automatic projection to 3857 should not happen, especially when we can set argument project = FALSE.

jcheng5 commented 7 years ago

It's not as simple as that. When project=FALSE there's no reprojection of the raster image itself. However, there is an attempt to project the bounds from whatever it is to EPSG3857 and then (back) to WGS84. For the case here, that shouldn't break the rendering; the end result should be visible at these bounds: addRectangles(11.1417132622267, 49.7692718887291, 11.2529678227801, 49.6987360507454), but they're not (if you add that line to your example then you will see the rectangle but still no raster image).

I think the reason nothing shows up at all is due to the first issue listed under the "common gotchas" heading here: http://leafletjs.com/examples/crs-simple/crs-simple.html My code that's hardcoded to EPSG3857 is here: https://github.com/rstudio/leaflet/blob/b9736ecbe3f4941c92d37506c7fd9d6923f22768/javascript/src/methods.js#L882-L899

I'm not exactly sure how to rewrite that code to work with L.CRS.Simple but ICRS.latLngToPoint seems like a good place to start.

jcheng5 commented 7 years ago

It also seems to be the case that Leaflet canvas tiles don't work at zoom levels less than 0. At least that's what I'm seeing.

library(leaflet)
leaflet(options = leafletOptions(minZoom = -100, crs = leafletCRS(crsClass = "L.CRS.EPSG4326"))) %>%
  htmlwidgets::onRender(JS("function(el, data) {
    window.map = this;

    var canvasTiles = L.tileLayer.canvas({ minZoom: -9999 });
    canvasTiles.drawTile = function(canvas, tilePoint, zoom) {
      var ctx = canvas.getContext('2d');
      ctx.font = '20px serif';
      ctx.textBaseline = 'top';
      ctx.textAlign = 'start';
      ctx.fillText(tilePoint.x + ',' + tilePoint.y, 0, 0);
      ctx.strokeRect(0, 0, canvasTiles.options.tileSize, canvasTiles.options.tileSize);
      console.log('draw at ' + zoom)
    };
    canvasTiles.addTo(this);
  }"))
bhaskarvk commented 7 years ago

FWIW, Proj4Leaflet has L.Proj.ImageOverlay which accepts bounds in projected coordinates. I wish I could spend more time on this but I'm a very busy w/ Time Dimension.

tim-salabim commented 7 years ago

do we need to have the tiling for L.CRS.Simple? If not, this http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html might provide some hints to a solution. Which seems to work well enough without tiling.

bhaskarvk commented 6 years ago

There should be an easy way to not project to EPSG3857 and back to WSG84 in case the crs used is L.CRS.Simple. I'll see if this can be done in a relatively sane way.

jamesdalg commented 6 years ago

Tiling is important when the purpose is to view really large images, which is why many users look to leaflet and why there are plugins built into the original package to handle square images (e.g. deep zoom, zoomify). I would vote that this is an important issue and will bring a lot to development to the project if this bottleneck can be overcome. An image overlay doesn't quite give the resolution and speed to view large images like tiles do, so it doesn't work for some applications (e.g. microscopy, large plots) without it.

tim-salabim commented 6 years ago

@bhaskarvk with regard to your comment above can you elaborate a little on this? I may only need a push in the right direction to get it working... hopefully

tim-salabim commented 6 years ago

@jamesdalg just to make sure I understand correctly, you are referring to non-geographical imagery using L.CRS.Simple (i.e. without a background map) right?