|
| 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; }") |
0 commit comments