Skip to content

Commit 01844b1

Browse files
committed
Initial Support for Proj4Leaflet plugin.
1 parent 15aecde commit 01844b1

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export(addMiniMap)
2525
export(addPolygons)
2626
export(addPolylines)
2727
export(addPopups)
28+
export(addProj4Leaflet)
2829
export(addProviderTiles)
2930
export(addRasterImage)
3031
export(addRectangles)

inst/examples/proj4Leaflet.R

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
library(leaflet)
2+
3+
#' h1. Leaflet now with Proj4Leaflet support.
4+
5+
#' This is very very hackish for now.<br/>
6+
#' <P>But as HTMLWidget's factory/initialize method has no way to pass params,
7+
#' and the CRS can only be specified at Map creation time (even in 1.0), the only
8+
#' way to support Proj4Leaflet is by destroying the default map and creating a new one.
9+
#' I haven't thought all the details here. But this is the first hack at it.
10+
#' A lot of stuff in the onRender can be moved to R, I just wanted something quick and dirty.
11+
#' </P><br/><br/>Thoughts/Comments ?<br/><br/>
12+
13+
#' The code is adaptation of [this](https://github.com/turban/Leaflet.Graticule/blob/master/examples/mollweide.html) sans the graticule part.<br/><br/>
14+
extJS <- htmltools::htmlDependency("countries", "1.0.0",
15+
src = c(href = "https://cdn.rawgit.com/turban/Leaflet.Graticule/master/examples/lib/"),
16+
script = "countries-110m.js"
17+
)
18+
19+
addExtJS <- function(map) {
20+
map$dependencies <- c(map$dependencies, list(extJS))
21+
map
22+
}
23+
24+
leaflet() %>% addTiles() %>%
25+
addProj4Leaflet() %>% # Add Proj4Leaflet plugin JSes.
26+
addExtJS() %>% # Load external GeoJson File
27+
htmlwidgets::onRender("
28+
function(el, x) {
29+
30+
// remove the original map
31+
var oldMap = this;
32+
oldMap.remove();
33+
34+
// Sphere Mollweide: http://spatialreference.org/ref/esri/53009/
35+
var crs = new L.Proj.CRS('ESRI:53009',
36+
'+proj=moll +lon_0=0 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs',
37+
{
38+
resolutions: [65536, 32768, 16384, 8192, 4096, 2048]
39+
}
40+
);
41+
42+
// create a new map in place of old map using new CRS
43+
var newMap = L.map(el.id, {
44+
crs: crs,
45+
maxZoom: 5
46+
});
47+
48+
L.geoJson(countries, {
49+
style: {
50+
color: '#000',
51+
weight: 0.5,
52+
opacity: 1,
53+
fillColor: '#fff',
54+
fillOpacity: 1
55+
}
56+
}).addTo(newMap);
57+
newMap.fitWorld();
58+
59+
//debugger;
60+
}
61+
")
62+
63+
leaflet() %>% addTiles() %>%
64+
addProj4Leaflet() %>% # Add Proj4Leaflet plugin JSes.
65+
addExtJS() %>% # Load external GeoJson File
66+
# Graticule not strictly necessary just aesthetics
67+
addGraticule(style=
68+
list( color= '#777',
69+
weight= 1, opacity= 0.5)) %>%
70+
htmlwidgets::onRender("
71+
function(el, x) {
72+
73+
// remove the original map
74+
var oldMap = this;
75+
oldMap.remove();
76+
77+
// Sphere Mollweide: http://spatialreference.org/ref/esri/53009/
78+
var crs = new L.Proj.CRS('ESRI:53009',
79+
'+proj=moll +lon_0=0 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs',
80+
{
81+
resolutions: [65536, 32768, 16384, 8192, 4096, 2048]
82+
}
83+
);
84+
85+
// create a new map in place of old map using new CRS
86+
var newMap = L.map(el.id, {
87+
crs: crs,
88+
maxZoom: 5
89+
});
90+
91+
L.graticule({
92+
sphere: true,
93+
style: {
94+
color: '#777',
95+
opacity: 1,
96+
fillColor: '#ccf',
97+
fillOpacity: 1,
98+
weight: 2
99+
}
100+
}).addTo(newMap);
101+
102+
103+
L.graticule({
104+
style: {
105+
color: '#777',
106+
weight: 1,
107+
opacity: 0.5
108+
}
109+
}).addTo(newMap);
110+
111+
112+
L.geoJson(countries, {
113+
style: {
114+
color: '#000',
115+
weight: 0.5,
116+
opacity: 1,
117+
fillColor: '#fff',
118+
fillOpacity: 1
119+
}
120+
}).addTo(newMap);
121+
newMap.fitWorld();
122+
123+
//debugger;
124+
}
125+
")

man/addProj4Leaflet.Rd

Lines changed: 15 additions & 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)