Skip to content

Commit 0f1077f

Browse files
committed
support for pmtiles
1 parent b9fff0e commit 0f1077f

3 files changed

Lines changed: 98 additions & 51 deletions

File tree

NEWS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version 0.2.0
33
- tm_fullscreen
44
- layer control improved
55
- tm_geocoder improved
6-
-
6+
- support for PMTiles (via new pacakge tmap.sources)
77

88
version 0.1.0
99
- development version

R/mapgl_layers.R

Lines changed: 97 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -64,72 +64,119 @@ mapgl_polygons = function(a, shpTM, dt, pdt, popup.format, hdt, idt, gp, bbx, fa
6464

6565
rc_text = frc(facet_row, facet_col)
6666

67-
res = select_sf(shpTM, dt)
68-
shp = res$shp
69-
dt = res$dt
67+
shp_is_pointer = inherits(shpTM$shp, "character")
7068

71-
popups = NULL
69+
if (shp_is_pointer) {
70+
if (mode == "mapbox") {
71+
cli::cli_inform("Source shapes not supported in {.str mapbox} mode yet, only in {.str maplibre}")
72+
return(NULL)
73+
}
7274

73-
x = sf::st_sfc(list(sf::st_point(c(mean(bbx[c(1,3)]), bbx[2]))), crs = sf::st_crs(bbx))
74-
y = sf::st_sfc(list(sf::st_point(c(mean(bbx[c(1,3)]), bbx[4]))), crs = sf::st_crs(bbx))
75+
srcname = paste0("layer", pane)
76+
layername1 = paste0(glid, "polygons_fill")
77+
layername2 = paste0(glid, "polygons_border")
7578

76-
gp = impute_gp(gp, dt)
77-
gp = rescale_gp(gp, o$scale_down)
79+
smeta = shpTM$smeta
7880

79-
if (any(nchar(gp$fill) == 9)) {
80-
fill_alpha = split_alpha_channel(gp$fill, alpha = gp$fill_alpha)
81-
gp$fill = fill_alpha$col
82-
gp$fill_alpha = gp$fill_alpha * fill_alpha$opacity
83-
}
84-
if (any(nchar(gp$col) == 9)) {
85-
fill_alpha = split_alpha_channel(gp$col, alpha = gp$col_alpha)
86-
gp$col = fill_alpha$col
87-
gp$col_alpha = gp$fill_alpha * fill_alpha$opacity
88-
}
81+
if (smeta$type != "pmtiles") {
82+
cli::cli_inform("Source shapes other than PMTiles not supported yet")
83+
return(NULL)
84+
}
8985

9086

91-
shp2 = sf::st_sf(unclass(gp[c("fill", "col", "lwd", "fill_alpha", "col_alpha")]), id = 1:length(shp), geometry = shp)
87+
url = smeta$url
9288

93-
if (!is.null(hdt)) {
94-
shp2$hover = hdt$hover[match(dt$tmapID__, hdt$tmapID__)]
95-
shp2$hover = vapply(shp2$hover, htmltools::HTML, FUN.VALUE = character(1))
89+
get_pmt_aes = function(a) {
90+
if (substr(dt[[a]][1], 1, 4) == "ref:") mapgl::get_column(substr(dt[[a]][1], 5, nchar(dt[[a]][1]))) else dt[[a]][1]
91+
}
9692

97-
hdt = mapgl::get_column("hover")
98-
}
93+
aes_f = get_pmt_aes("fill")
94+
aes_fo = get_pmt_aes("fill_alpha")
95+
aes_c = get_pmt_aes("col")
96+
aes_co = get_pmt_aes("col_alpha")
97+
aes_lwd = get_pmt_aes("lwd")
98+
99+
m |> mapgl::add_pmtiles_source(id = glid, url = url) |>
100+
mapgl::add_fill_layer(layername1, source = glid, source_layer = shpTM$smeta$layer,
101+
fill_color = aes_f,
102+
fill_opacity = aes_fo
103+
) |>
104+
mapgl::add_line_layer(layername2, source = glid, source_layer = shpTM$smeta$layer,
105+
line_color = aes_c,
106+
line_opacity = aes_co,
107+
line_width = aes_lwd) |>
108+
assign_mapgl(facet_row, facet_col, facet_page, mode = mode)
99109

100-
if (!is.null(pdt)) {
101-
mtch = match(dt$tmapID__, pdt$tmapID__)
102-
pdt = pdt[mtch][, tmapID__ := NULL]
110+
} else {
111+
res = select_sf(shpTM, dt)
112+
shp = res$shp
113+
dt = res$dt
114+
115+
popups = NULL
116+
117+
x = sf::st_sfc(list(sf::st_point(c(mean(bbx[c(1,3)]), bbx[2]))), crs = sf::st_crs(bbx))
118+
y = sf::st_sfc(list(sf::st_point(c(mean(bbx[c(1,3)]), bbx[4]))), crs = sf::st_crs(bbx))
119+
120+
gp = impute_gp(gp, dt)
121+
gp = rescale_gp(gp, o$scale_down)
122+
123+
if (any(nchar(gp$fill) == 9)) {
124+
fill_alpha = split_alpha_channel(gp$fill, alpha = gp$fill_alpha)
125+
gp$fill = fill_alpha$col
126+
gp$fill_alpha = gp$fill_alpha * fill_alpha$opacity
127+
}
128+
if (any(nchar(gp$col) == 9)) {
129+
fill_alpha = split_alpha_channel(gp$col, alpha = gp$col_alpha)
130+
gp$col = fill_alpha$col
131+
gp$col_alpha = gp$fill_alpha * fill_alpha$opacity
132+
}
133+
134+
135+
shp2 = sf::st_sf(unclass(gp[c("fill", "col", "lwd", "fill_alpha", "col_alpha")]), id = 1:length(shp), geometry = shp)
136+
137+
if (!is.null(hdt)) {
138+
shp2$hover = hdt$hover[match(dt$tmapID__, hdt$tmapID__)]
139+
shp2$hover = vapply(shp2$hover, htmltools::HTML, FUN.VALUE = character(1))
140+
141+
hdt = mapgl::get_column("hover")
142+
}
143+
144+
if (!is.null(pdt)) {
145+
mtch = match(dt$tmapID__, pdt$tmapID__)
146+
pdt = pdt[mtch][, tmapID__ := NULL]
147+
148+
if (is.null(idt) && !is.null(hdt)) {
149+
shp2$popup = view_format_popups_mapgl(id = shp2$hover, titles = names(pdt), values = pdt, format = popup.format)
150+
} else {
151+
shp2$popup = view_format_popups_mapgl(id = shp2$hover, titles = names(pdt), values = pdt, format = popup.format)
152+
}
153+
popup = mapgl::get_column("popup")
103154

104-
if (is.null(idt) && !is.null(hdt)) {
105-
shp2$popup = view_format_popups_mapgl(id = shp2$hover, titles = names(pdt), values = pdt, format = popup.format)
106155
} else {
107-
shp2$popup = view_format_popups_mapgl(id = shp2$hover, titles = names(pdt), values = pdt, format = popup.format)
156+
popup = NULL
108157
}
109-
popup = mapgl::get_column("popup")
110158

111-
} else {
112-
popup = NULL
113-
}
114159

160+
srcname = paste0("layer", pane)
161+
layername1 = paste0(glid, "polygons_fill")
162+
layername2 = paste0(glid, "polygons_border")
163+
164+
nofill = all(gp$fill == o$value.blank$fill)
165+
166+
m |> mapgl::add_source(srcname, data = shp2) |>
167+
mapgl::add_fill_layer(layername1, source = srcname,
168+
fill_color = mapgl::get_column("fill"),
169+
fill_opacity = mapgl::get_column("fill_alpha"),
170+
tooltip = hdt,popup = popup
171+
) |>
172+
mapgl::add_line_layer(layername2, source = srcname,
173+
line_color = mapgl::get_column("col"),
174+
line_opacity = mapgl::get_column("col_alpha"),
175+
line_width = mapgl::get_column("lwd")) |>
176+
assign_mapgl(facet_row, facet_col, facet_page, mode = mode)
115177

116-
srcname = paste0("layer", pane)
117-
layername1 = paste0(glid, "polygons_fill")
118-
layername2 = paste0(glid, "polygons_border")
119178

120-
nofill = all(gp$fill == o$value.blank$fill)
121-
po(layername1, layername2)
122-
m |> mapgl::add_source(srcname, data = shp2) |>
123-
mapgl::add_fill_layer(layername1, source = srcname,
124-
fill_color = mapgl::get_column("fill"),
125-
fill_opacity = mapgl::get_column("fill_alpha"),
126-
tooltip = hdt,popup = popup
127-
) |>
128-
mapgl::add_line_layer(layername2, source = srcname,
129-
line_color = mapgl::get_column("col"),
130-
line_opacity = mapgl::get_column("col_alpha"),
131-
line_width = mapgl::get_column("lwd")) |>
132-
assign_mapgl(facet_row, facet_col, facet_page, mode = mode)
179+
}
133180

134181
mapgl_submit_group(group, c(layername1, layername2), mode)
135182
NULL
56.9 KB
Loading

0 commit comments

Comments
 (0)