在R传单地图上叠加图像 [英] Overlay image on R leaflet map

查看:114
本文介绍了在R传单地图上叠加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.PNG图像,该图像从根本上表示是栅格,但不是栅格格式.这些是提供给我的,否则我自己将它们生成为栅格并避免出现此问题.

I have a .PNG image which fundamentally represents is raster, but is not in a raster format. These are provide to me, otherwise I would generate them as rasters myself and avoid this problem.

我想将图像覆盖在R中的传单底图上.图像覆盖将仅为用户提供一个参考,以在区域周围绘制边界框,并向数据库查询在其中生成栅格的原始数据.该地区.

I would like to overlay the image on a leaflet basemap in R. The image overlay will just provide a reference for the user to draw a bounding box around a region, and query a database for the raw data that generated the raster in that region.

在Python的传单实现中,以这种方式可以进行图像叠加

In Python's implementation of leaflet the image overlay is possible in this manner

center = [0,0]
zoom = 2

m = Map(center=center, zoom=zoom)
layer = ImageOverlay(url="filename.png", bounds=(( min_lat, min_lon), 
                                             (max_lat, max_lon)))
m.add_layer(layer)
return m

因此,到目前为止,在R中似乎要执行此操作,我需要一个栅格对象,然后使用addRasterImage(),该对象似乎将栅格转换为RGB图像,然后将其覆盖在传单地图上.我最初有一张图像,只想将其添加为图层,而不是需要栅格格式.谢谢.

Thus far it looks like to do this in R, I need a raster object that then use addRasterImage(), which appears to converts the raster to a RGB image and then overlays it on the leaflet map. I have an image initially and just want add it as a layer rather than a requiring a raster format. Thank you.

推荐答案

如果您需要添加图像并且以后不更改它(bbox或源代码),那么您应该能够使用htmlwidgets来访问本机传单. js:

If you need to append the image and not change it afterwards (bbox or source), then you should be able to do that using htmlwidgets to access native leaflet.js:

htmlwidget :: onRender函数可用于添加自定义行为 使用本机Javascript访问传单地图.这是什么 高级用例,并且需要您了解Javascript.使用onRender 您可以使用定义的任何API自定义地图的行为 在Leaflet.js文档中. (摘自传单文档)

The htmlwidget::onRender function can be used to add custom behavior to the leaflet map using native Javascript. This is a some what advanced use case and requires you to know Javascript. Using onRender you can customize your map’s behavior using any of the APIs as defined in the Leaflet.js documentation. (from leaflet r documentation)

通过显示更广泛的本机功能,可以为您提供更多的灵活性.添加图像叠加非常简单(示例)并且类似于python执行.从r和js文档版本采用的一个简单示例可能看起来像:

This gives you a bit more flexibility by revealing the more extensive native functionality. To add an image overlay is rather straightforward (example) and similar to the python implementation. A simple example adopted from the r and js documentation versions might look like:

library(leaflet)

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=-74.22655, lat=40.712216, popup="Bottom Right Corner") %>%
  htmlwidgets::onRender("
      function(el, x) {
        console.log(this);
        var myMap = this;
        var imageUrl = 'http://www.lib.utexas.edu/maps/historical/newark_nj_1922.jpg';
        var imageBounds = [[40.712216, -74.22655], [40.773941, -74.12544]];

        L.imageOverlay(imageUrl, imageBounds).addTo(myMap);
      }
      ")
m  # Print the map

这篇关于在R传单地图上叠加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆