deckgl icon indicating copy to clipboard operation
deckgl copied to clipboard

sf layers aren't rendering

Open LiRogers opened this issue 5 years ago • 5 comments

Layers generated from sf data objects aren't rendering - just the base map loads.

I tried the examples from here, but none of them worked.

Non sf examples are working fine.

R Version: 4.0.2 deckgl Version: 0.2.7 (CRAN) - although also tried dev version sf version: 0.9-6

LiRogers avatar Dec 02 '20 15:12 LiRogers

@LiRogers Hi, yes you are right. I can reproduce it. It seems that jsonlite changed the way converting sf objects. It creates some kind of GeoJSON from the geometry column now. As a work around you can access the coords in this way:

library(deckgl)
library(sf)

features <- st_as_sf(bart_stations, coords = c("lng", "lat"), crs = 4326)

deckgl(zoom = 9, pitch = 35) %>%
  add_column_layer(
    data = features,
    getPosition = JS("d => d.geometry.coordinates"),
    getElevation = ~exits,
    getFillColor = JS("d => [48, 128, Math.sqrt(d.exits)]"),
    getTooltip = ~name
  )

crazycapivara avatar Dec 02 '20 16:12 crazycapivara

Just replace ~geometry with JS("d => d.geometry.coordinates") in the sf examples.

crazycapivara avatar Dec 02 '20 16:12 crazycapivara

Excellent, thanks!

LiRogers avatar Dec 02 '20 16:12 LiRogers

@crazycapivara somehow JS("d => d.geometry.coordinates") fails to render polygon lines, or maybe I'm not declaring getLineWidth properly?

library(raster)
mli <- getData("GADM", country="MLI", level=2)
mli <- st_as_sf(mli)

props = list(
  getPolygon = JS("d => d.geometry.coordinates"),
  pickable = TRUE,
  stroked = TRUE,
  filled = TRUE,
  wireframe = TRUE,  
  getLineWidth = 2,
  getLineColor = "black",
  getFillColor = "blue"
)

deckgl(
  element_id = "mapgl",
  longitude = -8,
  latitude = 11,
  zoom = 6,
  bearing = 0,
  pitch = 60,
  maxPitch = 89,
) %>%
  add_polygon_layer(data=mli, properties=props)

image

plot(mli[1])

image

mbacou avatar Nov 06 '21 22:11 mbacou

Ignore, figured it needs:


props = list(
  getPolygon = JS("d => d.geometry.coordinates"),
  pickable = TRUE,
  stroked = TRUE,
  filled = TRUE,
  wireframe = TRUE,  
  getLineWidth = 2,
  getLineColor = "black",
  getFillColor = "blue",
  lineWidthMinPixels = 1 # !!!
)

mbacou avatar Nov 06 '21 22:11 mbacou