Can't plot a R interactive map comtainning my shapefile and my raster data in power BI

Hello!
I'm trying to load my script in power BI, which is:


library(ggplot2)
library(ggmap)
library(plotly)
library(raster)
library(sf)
library(rgdal)
library(rgeos)
library(mapview)

setwd("C:/Users/Daniel/Desktop/IDGeo/Earth analytics course_Learn data science/data/week 4")

ndwi <- raster("california/SJER/vector_data/2017_06_04_NDWI_CLIP.tif")

pivos <- readOGR(dsn = "california/SJER/vector_data",
"aneis_anapaula_0904")

M2<- mapView(ndwi, alpha.regions = 0.50, na.color = "transparent")
m4 <- mapview(pivos, lwd = 1.5, color = "gray40")

M2 + m4


ndwi is my raster data
pivos is my shapefile

In R it works perfectly and returns an interactive map containing my raster data plus myshapefile.

Anyone knows what could be happening?
Or, anyone could suggest another way to plot raster + shapefile interactive maps in power BI using R?
Thanks very much, best regards.

I believe that {mapview} is not compatible with Power BI.

Are you absolutely positively determined on using Power BI? Your piece of code looks rather static, and could possibly be rendered as html via leaflet.

Hello! Yes, I need to use Power BI...
Do you have any idea? To plot a raster + shapefile, allowing to zoom them?
Thanks very much!

What I have done in the past was to create a TopoJSON (not very difficult in {sf} workflow) out of the shapefile and then used it to create a choropleth map. There is a walkthrough to it on the Power BI pages.

It might be something along these lines (note that your example is not quite reproducible, so I can not make sure).

yer_shapefile <- sf::st_read("yer_shapefile.shp") # you will want to be more specific :)
yer_raster <- raster:.raster("your_raster.tif") %>% # original raster
   sf:.st_as_sf() # grid - without values - as a sf object

geojsonio::topojson_write(yer_r_object, # the sf object - first raster, then shapefile
                          file = "whatever.topojson", # or what not...
                          object_name = "whatever") # or what not...

In Power BI you would need to re-link the data to empty scaffolding created from the topojson.

This should work, but it is kinda hassle, IMHO.

An alternative would be generating the plot in R via {leaflet} and "plotting" it from Power BI as a chunk of HTML.

Hi, thanks for your attention! Well, a choropleth map won't work for what I need to demonstrate...
About the HTML chunk, I think it could work..
Should I generate a Rmardown file and input it in power BI ?
Again, thanks very much!

It need not be a rmd file: consider this example (drawing heavily on my earlier answer to a related question).

The last line is what matters, the rest is just a piece of reproducible leaflet

Once you have the html file saved you can embed the code into Power BI (there should be a ton of walkthroughs for that around...)

library(tidyverse)
library(leaflet)
library(tmap) # for the Metro dataset
library(sf)

data("metro") # from tmap

metro <- metro %>% 
  select(pop = pop2020) %>% 
  mutate(pop = pop / max(pop)) # normalise the values

mapa <- st_read("./data/maps/ne_50m_admin_0_countries.shp") # replace with your path :)

pal <- colorNumeric(palette = "Reds",  domain = mapa$GDP_MD_EST)

asdf <- 
  leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  setView(lng = 14.46, lat = 50.07, zoom = 1) %>%
  addPolygons(data = mapa, 
              fillColor = ~pal(mapa$GDP_MD_EST),
              fillOpacity = 0.75,
              stroke = F) %>% 
  addCircleMarkers(data = metro,
                   radius = 20 * metro$pop,
                   stroke = F)

htmlwidgets::saveWidget(asdf, "asdf.html") # put something more specific than asdf here :)

Hello! Thanks very much for your script..
I'm quite beginner in R and I don't know exactly where should I put my raster data in this script...
should I add another object using the raster library?

Again,
thank you!

Try this code; as I don't have access to your filesystem I am coding blind. So bear with me if there is a missing pipe or the result needs some tweaking to make colors right or what not... :slight_smile:

library(tidyverse)
library(htmlwidgets)
library(raster)
library(sf)

# these are your files from code above, so I assume them to work...
pivos <- st_read("california/SJER/vector_data/aneis_anapaula_0904.shp")
ndwi  <- raster("california/SJER/vector_data/2017_06_04_NDWI_CLIP.tif")

chrt <- leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>% # a basemap; not required but nice...
  addPolygons(data = pivos, 
              fillOpacity = 0,
              stroke = T) %>%
  addRasterImage(ndwi)

saveWidget(chrt, "chart.html")

Hi, thanks again and again... however, only the basemap is plotted... The raster and shp don't load =(
I received those waring messages only:
Warning messages:
1: sf layer is not long-lat data
2: sf layer has inconsistent datum (+proj=utm +zone=23 +south +ellps=GRS80 +units=m +no_defs).
Need '+proj=longlat +datum=WGS84'

I've checked the crs and it is like:

crs(pivos)
[1] "+proj=utm +zone=23 +south +ellps=GRS80 +units=m +no_defs"
crs(ndwi)
CRS arguments:
+proj=utm +zone=23 +south +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs

Both are in the same CRS, do you have any idea about wjat could be happening?

Thanks very much, you are very kind!!!

Sorry, I can't really help you without the files in question :frowning:

The best I can think of is

pivos <- st_read("california/SJER/vector_data/aneis_anapaula_0904.shp") %>%
  st_transform(4326) # i.e. WGS84 

Hi @jlacko ! Now the HTML is perfectly working!
I'm not able to display it in power BI yet, but I will find a way (if you have any, please suggest, haha) .
Anyway, you solved my firts problem, thank you!

Glad to be of service! :slight_smile: