Skip to content

Commit ff593a7

Browse files
committed
refactor: remove slippymath dependency, ship and use some modified function from it
1 parent bd04e66 commit ff593a7

6 files changed

Lines changed: 143 additions & 12 deletions

File tree

DESCRIPTION

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: maptiles
22
Title: Download and Display Map Tiles
3-
Version: 0.10.0
3+
Version: 0.10.0.9999
44
Authors@R: c(person(given = "Timothée",
55
family = "Giraud",
66
email = "timothee.giraud@cnrs.fr",
@@ -16,7 +16,12 @@ Authors@R: c(person(given = "Timothée",
1616
comment = c(ORCID = "0000-0001-5872-2872")),
1717
person(given = "Hugh A.",
1818
family = "Graham",
19-
role = c("ctb")))
19+
role = c("ctb")),
20+
person(given = "Miles",
21+
family = "McBain",
22+
role = "cph",
23+
comment =
24+
"Slippy map tiles functions, from slippymath package"))
2025
Description: To create maps from tiles, 'maptiles' downloads, composes and
2126
displays tiles from a large number of providers (e.g. 'OpenStreetMap',
2227
'Stadia', 'Esri', 'CARTO', or 'Thunderforest').
@@ -34,10 +39,9 @@ Imports:
3439
png,
3540
terra (>= 1.8-21),
3641
tools,
37-
slippymath,
3842
utils
3943
Suggests:
4044
covr,
4145
tinytest
4246
Encoding: UTF-8
43-
RoxygenNote: 7.3.2
47+
RoxygenNote: 7.3.3

R/get_tiles.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ get_tiles <- function(x,
127127
}
128128

129129
# get tile list
130-
tile_grid <- slippymath::bbox_to_tile_grid(res$bbox_lonlat, zoom)
130+
tile_grid <- bbox_to_tile_grid(res$bbox_lonlat, zoom)
131131

132132
# download images
133133
images <- download_tiles(

R/slippymath.R

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
###############################################################################
2+
## The following code originaly comes from the slippymath package.
3+
## It has been modified and simplified to remove some dependencies (e.g. purrr)
4+
## by maptiles author.
5+
## The original code use the MIT License
6+
###############################################################################
7+
# MIT License
8+
#
9+
# Copyright (c) 2018 Miles McBain
10+
#
11+
# Permission is hereby granted, free of charge, to any person obtaining a copy
12+
# of this software and associated documentation files (the "Software"),
13+
# to deal in the Software without restriction, including without limitation
14+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
15+
# and/or sell copies of the Software, and to permit persons to whom the Software
16+
# is furnished to do so, subject to the following conditions:
17+
#
18+
# The above copyright notice and this permission notice shall be included in
19+
# all copies or substantial portions of the Software.
20+
#
21+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27+
# SOFTWARE.
28+
29+
bbox_to_tile_grid <- function(bbox, zoom = NULL, max_tiles = NULL) {
30+
tile_extent <- bbox_tile_extent(bbox, zoom)
31+
x_tiles <- tile_extent$x_min:tile_extent$x_max
32+
y_tiles <- tile_extent$y_min:tile_extent$y_max
33+
tile_grid <- list(
34+
tiles = expand.grid(x = x_tiles, y = y_tiles),
35+
zoom = zoom
36+
)
37+
tile_grid
38+
}
39+
40+
bbox_tile_extent <- function(bbox, zoom) {
41+
min_tile <- lonlat_to_tilenum(
42+
lat_deg = bbox["ymin"], lon_deg = bbox["xmin"], zoom
43+
)
44+
max_tile <- lonlat_to_tilenum(
45+
lat_deg = bbox["ymax"], lon_deg = bbox["xmax"], zoom
46+
)
47+
list(
48+
x_min = min_tile$x, y_min = max_tile$y, x_max = max_tile$x,
49+
y_max = min_tile$y
50+
)
51+
}
52+
53+
lonlat_to_tilenum <- function(lon_deg, lat_deg, zoom) {
54+
lon_rad <- radians(lon_deg)
55+
lat_rad <- radians(lat_deg)
56+
x <- lon_rad
57+
y <- asinh(tan(lat_rad))
58+
x <- (1 + (x / pi)) / 2
59+
y <- (1 - (y / pi)) / 2
60+
n_tiles <- 2^zoom
61+
xtile <- sm_clamp(floor(x * n_tiles), 0, n_tiles - 1)
62+
ytile <- sm_clamp(floor(y * n_tiles), 0, n_tiles - 1)
63+
list(x = xtile, y = ytile)
64+
}
65+
66+
radians <- function(angle_deg) {
67+
angle_deg * pi / 180
68+
}
69+
70+
degrees <- function(angle_rad) {
71+
(angle_rad * 180) / pi
72+
}
73+
74+
sm_clamp <- function(x, mn, mx) {
75+
x[x < mn] <- mn
76+
x[x > mx] <- mx
77+
x
78+
}
79+
80+
bbox_tile_query <- function(bbox, zoom_levels = 2:18) {
81+
l <- list()
82+
for (i in seq_along(zoom_levels)) {
83+
l[[i]] <- bbox_tile_extent(bbox, zoom = zoom_levels[i])
84+
}
85+
extents_at_zooms <- data.frame(
86+
matrix(
87+
data = unlist(lapply(l, unlist)), ncol = 4, byrow = TRUE,
88+
dimnames = list(seq_along(l), c("x_min", "y_min", "x_max", "y_max"))
89+
)
90+
)
91+
extents_at_zooms$y_dim <- abs(extents_at_zooms$y_max - extents_at_zooms$y_min) + 1
92+
extents_at_zooms$x_dim <- abs(extents_at_zooms$x_max - extents_at_zooms$x_min) + 1
93+
extents_at_zooms$total_tiles <- extents_at_zooms$y_dim * extents_at_zooms$x_dim
94+
extents_at_zooms$zoom <- zoom_levels
95+
extents_at_zooms
96+
}
97+
98+
tile_bbox <- function(x, y, zoom) {
99+
bottom_left <- lonlat_to_merc(t(as.matrix(unlist(tilenum_to_lonlat(x, y + 1, zoom)))))
100+
top_right <- lonlat_to_merc(t(as.matrix(unlist(tilenum_to_lonlat(x + 1, y, zoom)))))
101+
structure(
102+
c(
103+
xmin = bottom_left[[1]], ymin = bottom_left[[2]],
104+
xmax = top_right[[1]], ymax = top_right[[2]]
105+
),
106+
class = "bbox",
107+
crs = sf::st_crs("EPSG:3857")
108+
)
109+
}
110+
111+
tilenum_to_lonlat <- function(x, y, zoom) {
112+
n_tiles <- 2^zoom
113+
lon_rad <- (((x / n_tiles) * 2) - 1) * pi
114+
merc_lat <- (1 - ((y / n_tiles) * 2)) * pi
115+
lat_rad <- atan(sinh(merc_lat))
116+
list(lon = degrees(lon_rad), lat = degrees(lat_rad))
117+
}
118+
119+
lonlat_to_merc <- function(ll) {
120+
A <- 6378137
121+
MAXEXTENT <- 20037508.342789244
122+
xy <- cbind(A * radians(ll[, 1]), A * log(tan((pi * 0.25) + (0.5 * radians(ll[, 2])))))
123+
xy[, 1] <- sm_clamp(xy[, 1], -MAXEXTENT, MAXEXTENT)
124+
xy[, 2] <- sm_clamp(xy[, 2], -MAXEXTENT, MAXEXTENT)
125+
xy
126+
}

R/utils.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ get_extension <- function(q) {
8282
}
8383

8484

85-
8685
# providers parameters
8786
get_param <- function(provider) {
8887
if (is.list(provider) && length(provider) == 4) {
@@ -121,8 +120,9 @@ get_param <- function(provider) {
121120
get_zoom <- function(zoom, bbox_lonlat) {
122121
# select a default zoom level
123122
if (missing(zoom)) {
124-
gz <- slippymath::bbox_tile_query(bbox_lonlat)
125-
zoom <- min(gz[gz$total_tiles %in% 4:10, "zoom"])
123+
gz <- bbox_tile_query(bbox_lonlat)
124+
suitable_zooms <- gz$total_tiles <= 4
125+
zoom <- gz$zoom[max(which(suitable_zooms))]
126126
}
127127
return(zoom)
128128
}
@@ -259,7 +259,7 @@ compose_tiles <- function(tile_grid, images) {
259259
bricks <- vector("list", nrow(tile_grid$tiles))
260260
ext <- unique(tools::file_ext(images))[1]
261261
for (i in seq_along(bricks)) {
262-
bbox <- slippymath::tile_bbox(
262+
bbox <- tile_bbox(
263263
x = tile_grid$tiles$x[i],
264264
y = tile_grid$tiles$y[i],
265265
zoom = tile_grid$zoom

inst/tinytest/test_maptiles.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ expect_warning(maptiles:::get_param("Stamen.Toner"))
115115
expect_error(maptiles:::get_param("Esri.Delorme"))
116116

117117
# get_zoom() ----
118-
expect_equal(maptiles:::get_zoom(bbox_lonlat = nc_bbox), 7)
118+
expect_equal(maptiles:::get_zoom(bbox_lonlat = nc_bbox), 6)
119119
expect_equal(maptiles:::get_zoom(zoom = 8, bbox_lonlat = nc_bbox), 8)
120120

121121

@@ -177,7 +177,7 @@ if (home){
177177
# download_tiles() ----
178178
input <- nc_sf
179179
res <- maptiles:::get_bbox_and_proj(input)
180-
tile_grid <- slippymath::bbox_to_tile_grid(bbox = res$bbox_lonlat, zoom = 6)
180+
tile_grid <- maptiles:::bbox_to_tile_grid(bbox = res$bbox_lonlat, zoom = 6)
181181
param <- param2 <- maptiles:::get_param("OpenStreetMap")
182182
param2$q <- "ppp"
183183
cachedir <- maptiles:::get_cachedir(src = "OSM")
@@ -210,7 +210,7 @@ if (home){
210210

211211
input2 <- nc_sf_centro
212212
res2 <- maptiles:::get_bbox_and_proj(input2)
213-
tile_grid2 <- slippymath::bbox_to_tile_grid(bbox = res2$bbox_lonlat, zoom = 4)
213+
tile_grid2 <- maptiles:::bbox_to_tile_grid(bbox = res2$bbox_lonlat, zoom = 4)
214214
param2 <- maptiles:::get_param("CartoDB.PositronOnlyLabels")
215215
cachedir2 <- maptiles:::get_cachedir(src = "CartoDBxPos")
216216
images2 <- maptiles:::download_tiles(tile_grid = tile_grid2, param = param2,

man/maptiles.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)