Skip to content

Commit 4b464b1

Browse files
committed
Small fix and demo for #15
1 parent 3f4720a commit 4b464b1

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
library(mapview) # for the popupTables
2+
library(sp)
3+
4+
#' ## Example of using EPSG:28892 Projection
5+
6+
#' Helper function to query proj4 strings for an EPSG code.
7+
#' Not fool proof but works for correct codes.
8+
getEPSG <- function(epsgCode){
9+
res <- httr::GET(sprintf(
10+
"http://epsg.io/?q=%s&format=json",epsgCode))
11+
if(res$status_code==200) {
12+
return(httr::content(res))
13+
} else {
14+
warning(sprintf(
15+
"Error querying EPSG code %s, Server returned %d:%s",
16+
epsgCode, res$status_code, httr::content(res)))
17+
return(NULL)
18+
}
19+
}
20+
21+
getProj4String <- function(epsgCode) {
22+
res <- getEPSG(epsgCode)
23+
if(!is.null(res) && length(res$results)>=1) {
24+
return(res$results[[1]]$proj4)
25+
} else {
26+
return(NULL)
27+
}
28+
}
29+
30+
proj4def.28992 <- getProj4String('28992')
31+
proj4def.28992
32+
proj4def.4326 <- getProj4String('4326')
33+
proj4def.4326
34+
35+
data(meuse)
36+
coordinates(meuse) <- ~x+y
37+
proj4string(meuse) <- proj4def.28992
38+
meuse.4326 <- spTransform(meuse, proj4def.4326)
39+
40+
41+
#' ### Map + Markers in Spherical Mercator
42+
#' Just to verify that everything is correct in 4326
43+
leaflet() %>% addTiles() %>%
44+
addCircleMarkers(data=meuse.4326)
45+
46+
47+
#' ## Now in EPSG:28992
48+
49+
minZoom = 0
50+
maxZoom = 13
51+
52+
# lenght of resultions vector needs to correspond to maxZoom+1-minZoom
53+
# I use the 0.42 multiplyer from the resolutions arg found at http://jsfiddle.net/_Tom_/ueRa8/
54+
resolutions <- 0.42*(2^(maxZoom:minZoom))
55+
56+
crs.28992 <- leafletCRS(
57+
crsClass = "L.Proj.CRS",
58+
code = 'EPSG:28992',
59+
proj4def = proj4def.28992,
60+
resolutions = resolutions)
61+
62+
#' ### Just the markers
63+
#' No need to call setView, leaflet (R package)
64+
#' will auto determine the best initial view based on data.
65+
leaflet(options = leafletOptions(crs = crs.28992,
66+
minZoom = minZoom,
67+
maxZoom = maxZoom)) %>%
68+
addCircleMarkers(data = meuse.4326, popup = popupTable(meuse))
69+
70+
#' ### Markers + Tiles
71+
#' All this is adapted from http://jsfiddle.net/_Tom_/ueRa8/
72+
#' <br/> We will use TMS http://geodata.nationaalgeoregister.nl
73+
74+
crs.28992.forTiles <- leafletCRS(
75+
crsClass = "L.Proj.CRS.TMS",
76+
code = 'EPSG:28992',
77+
proj4def = proj4def.28992,
78+
resolutions = resolutions,
79+
projectedBounds = c(-285401.92, 22598.08, 595401.9199999999, 903401.9199999999))
80+
81+
#' This works but there's a problem when going from Zoom level 9 to 10
82+
#' I've started at zoom level 9, if you zoom out it works perfectly
83+
#' If you zoom in then there's a problem from zoom level 10 onwards
84+
#' but the problem is with the Tile Map Server (TMS) + Proj4 and not the markers.
85+
#' You can verify that with the bottom map which is only tiles
86+
leaflet(options = leafletOptions(crs = crs.28992.forTiles,
87+
minZoom = minZoom,
88+
maxZoom = maxZoom)) %>%
89+
addCircleMarkers(data = meuse.4326, popup = popupTable(meuse)) %>%
90+
setView(5.734745, 50.964112, zoom = 9) %>%
91+
addTiles('http://geodata.nationaalgeoregister.nl/tms/1.0.0/brtachtergrondkaart/{z}/{x}/{y}.png', options = tileOptions(tms=TRUE)) %>%
92+
htmlwidgets::onRender("function(el,t){ var myMap=this; debugger; }")
93+
94+
95+
#' ### Problem with zooming
96+
#' You can see the problem when zooming from level 9 to 10 below.
97+
leaflet(options = leafletOptions(crs = crs.28992.forTiles,
98+
minZoom = minZoom,
99+
maxZoom = maxZoom)) %>%
100+
setView(5.734745, 50.964112, zoom = 9) %>%
101+
addTiles('http://geodata.nationaalgeoregister.nl/tms/1.0.0/brtachtergrondkaart/{z}/{x}/{y}.png', options = tileOptions(tms=TRUE)) %>%
102+
htmlwidgets::onRender("function(el,t){ var myMap=this; debugger; }")

inst/htmlwidgets/leaflet.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,18 @@ function getCRS(crsOptions) {
163163
if (crsOptions.options && crsOptions.options.bounds) {
164164
crsOptions.options.bounds = _leaflet2.default.bounds(crsOptions.options.bounds);
165165
}
166+
if (crsOptions.options && crsOptions.options.transformation) {
167+
crsOptions.options.transformation = _leaflet2.default.Transformation(crsOptions.options.transformation[0], crsOptions.options.transformation[1], crsOptions.options.transformation[2], crsOptions.options.transformation[3]);
168+
}
166169
crs = new _proj4leaflet2.default.CRS(crsOptions.code, crsOptions.proj4def, crsOptions.options);
167170
break;
168171
case "L.Proj.CRS.TMS":
169172
if (crsOptions.options && crsOptions.options.bounds) {
170173
crsOptions.options.bounds = _leaflet2.default.bounds(crsOptions.options.bounds);
171174
}
175+
if (crsOptions.options && crsOptions.options.transformation) {
176+
crsOptions.options.transformation = _leaflet2.default.Transformation(crsOptions.options.transformation[0], crsOptions.options.transformation[1], crsOptions.options.transformation[2], crsOptions.options.transformation[3]);
177+
}
172178
crs = new _proj4leaflet2.default.CRS.TMS(crsOptions.code, crsOptions.proj4def, crsOptions.projectedBounds, crsOptions.options);
173179
break;
174180
}

javascript/src/crs_utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,31 @@ export function getCRS(crsOptions) {
2222
if(crsOptions.options && crsOptions.options.bounds) {
2323
crsOptions.options.bounds = L.bounds(crsOptions.options.bounds);
2424
}
25+
if(crsOptions.options && crsOptions.options.transformation) {
26+
crsOptions.options.transformation =
27+
L.Transformation(
28+
crsOptions.options.transformation[0],
29+
crsOptions.options.transformation[1],
30+
crsOptions.options.transformation[2],
31+
crsOptions.options.transformation[3]
32+
);
33+
}
2534
crs = new Proj4Leaflet.CRS(crsOptions.code, crsOptions.proj4def,
2635
crsOptions.options);
2736
break;
2837
case "L.Proj.CRS.TMS":
2938
if(crsOptions.options && crsOptions.options.bounds) {
3039
crsOptions.options.bounds = L.bounds(crsOptions.options.bounds);
3140
}
41+
if(crsOptions.options && crsOptions.options.transformation) {
42+
crsOptions.options.transformation =
43+
L.Transformation(
44+
crsOptions.options.transformation[0],
45+
crsOptions.options.transformation[1],
46+
crsOptions.options.transformation[2],
47+
crsOptions.options.transformation[3]
48+
);
49+
}
3250
crs = new Proj4Leaflet.CRS.TMS(crsOptions.code, crsOptions.proj4def,
3351
crsOptions.projectedBounds, crsOptions.options);
3452
break;

0 commit comments

Comments
 (0)