Skip to content

Commit 727dd47

Browse files
committed
- Upgraded Simple Graticule and also add Graticule plugin (Fixes #12).
1 parent 8eb9daa commit 727dd47

File tree

11 files changed

+254
-13
lines changed

11 files changed

+254
-13
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export(addCircleMarkers)
1515
export(addCircles)
1616
export(addControl)
1717
export(addGeoJSON)
18+
export(addGraticule)
1819
export(addLabelOnlyMarkers)
1920
export(addLayersControl)
2021
export(addLegend)

R/plugin-graticule.R

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
leafletGraticuleDependencies <- function() {
2+
list(
3+
htmltools::htmlDependency(
4+
"leaflet-graticule",
5+
"0.1.0",
6+
system.file("htmlwidgets/plugins/Leaflet.Graticule", package = "leaflet"),
7+
script = c("L.Graticule.js", "Graticule-binding.js")
8+
)
9+
)
10+
}
11+
12+
#' Add a Graticule on the map
13+
#' see \url{https://github.com/turban/Leaflet.Graticule}
14+
#'
15+
#' @param map a map widget object
16+
#' @param interval The spacing in map units between horizontal and vertical lines.
17+
#' @param style path options for the generated lines. See \link{http://leafletjs.com/reference.html#path-options}
18+
#' @param layerId the layer id
19+
#' @param group the name of the group this layer belongs to.
20+
#' @examples
21+
#' library(leaflet)
22+
#'
23+
#' leaf <- leaflet() %>%
24+
#' addTiles() %>%
25+
#' addGraticule()
26+
#'
27+
#' @export
28+
addGraticule <- function(
29+
map,
30+
interval = 20,
31+
style = list(color= '#333', weight= 1),
32+
layerId = NULL,
33+
group=NULL
34+
) {
35+
map$dependencies <- c(map$dependencies, leafletGraticuleDependencies())
36+
invokeMethod(
37+
map
38+
, getMapData(map)
39+
, 'addGraticule'
40+
, interval
41+
, style
42+
, layerId
43+
, group
44+
)
45+
}

R/plugin-simplegraticule.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ leafletSimpleGraticuleDependencies <- function() {
55
"0.1.0",
66
system.file("htmlwidgets/plugins/Leaflet.SimpleGraticule", package = "leaflet"),
77
script = c("L.SimpleGraticule.js", "SimpleGraticule-binding.js"),
8-
stylesheet = "L.SimpleGraticule.js"
8+
stylesheet = "L.SimpleGraticule.css"
99
)
1010
)
1111
}
@@ -17,12 +17,15 @@ leafletSimpleGraticuleDependencies <- function() {
1717
#' @param interval The spacing in map units between horizontal and vertical lines.
1818
#' @param showOriginLabel true Whether or not to show '(0,0)' at the origin.
1919
#' @param redraw on which map event to redraw the graticule. On move is default but moveend can be smoother.
20+
#' @param hidden hide on start
21+
#' @param zoomIntervals use different intervals in different zoom levels. If not specified, all zoom levels use value in interval option.
22+
2023
#' @param layerId the layer id
2124
#' @param group the name of the group this layer belongs to.
2225
#' @examples
2326
#' library(leaflet)
2427
#'
25-
#' leaf <- leaflet() %>%
28+
#' leaflet() %>%
2629
#' addTiles() %>%
2730
#' addSimpleGraticule()
2831
#'
@@ -32,6 +35,8 @@ addSimpleGraticule <- function(
3235
interval = 20,
3336
showOriginLabel = TRUE,
3437
redraw = 'move',
38+
hidden = FALSE,
39+
zoomIntervals = list(),
3540
layerId = NULL,
3641
group=NULL
3742
) {
@@ -43,6 +48,8 @@ addSimpleGraticule <- function(
4348
, interval
4449
, showOriginLabel
4550
, redraw
51+
, hidden
52+
, zoomIntervals
4653
, layerId
4754
, group
4855
)

inst/examples/graticule.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library(leaflet)
2+
# Default
3+
l <- leaflet() %>% addTiles() %>% setView(0,0,1)
4+
5+
l %>% addGraticule()
6+
7+
# Custom Params
8+
l %>% addGraticule(interval = 40, style = list(color='#FF0000', weight=1))
9+
10+
# on a toggleable Layer
11+
l %>%
12+
addGraticule(group="graticule") %>%
13+
addLayersControl(
14+
overlayGroups = c("graticule"),
15+
options = layersControlOptions(collapsed = FALSE)
16+
)

inst/examples/simpleGraticule.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
library(leaflet)
22
# Default
3-
leaflet() %>% addTiles() %>% addSimpleGraticule()
3+
l <- leaflet() %>% addTiles() %>% setView(0,0,1)
4+
5+
l %>% addSimpleGraticule()
46

57
# Custom Params
6-
leaflet() %>% addTiles() %>% addSimpleGraticule(interval = 40, showOriginLabel = F)
8+
l %>% addSimpleGraticule(interval = 40, showOriginLabel = F)
79

810
# Custom Resolution + Custom Date and on a toggleable Layer
9-
leaflet() %>% addTiles() %>%
11+
l %>%
1012
addSimpleGraticule(interval=40,
1113
showOriginLabel = F,
1214
group="graticule") %>%
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LeafletWidget.methods.addGraticule = function(interval, style, layerId, group) {
2+
(function() {
3+
this.layerManager.addLayer(
4+
L.graticule({
5+
interval: interval,
6+
style: style
7+
}),
8+
'shape', layerId, group);
9+
}).call(this);
10+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Graticule plugin for Leaflet powered maps.
3+
*/
4+
L.Graticule = L.GeoJSON.extend({
5+
6+
options: {
7+
style: {
8+
color: '#333',
9+
weight: 1
10+
},
11+
interval: 20
12+
},
13+
14+
initialize: function (options) {
15+
L.Util.setOptions(this, options);
16+
this._layers = {};
17+
18+
if (this.options.sphere) {
19+
this.addData(this._getFrame());
20+
} else {
21+
this.addData(this._getGraticule());
22+
}
23+
},
24+
25+
_getFrame: function() {
26+
return { "type": "Polygon",
27+
"coordinates": [
28+
this._getMeridian(-180).concat(this._getMeridian(180).reverse())
29+
]
30+
};
31+
},
32+
33+
_getGraticule: function () {
34+
var features = [], interval = this.options.interval;
35+
36+
// Meridians
37+
for (var lng = 0; lng <= 180; lng = lng + interval) {
38+
features.push(this._getFeature(this._getMeridian(lng), {
39+
"name": (lng) ? lng.toString() + "° E" : "Prime meridian"
40+
}));
41+
if (lng !== 0) {
42+
features.push(this._getFeature(this._getMeridian(-lng), {
43+
"name": lng.toString() + "° W"
44+
}));
45+
}
46+
}
47+
48+
// Parallels
49+
for (var lat = 0; lat <= 90; lat = lat + interval) {
50+
features.push(this._getFeature(this._getParallel(lat), {
51+
"name": (lat) ? lat.toString() + "° N" : "Equator"
52+
}));
53+
if (lat !== 0) {
54+
features.push(this._getFeature(this._getParallel(-lat), {
55+
"name": lat.toString() + "° S"
56+
}));
57+
}
58+
}
59+
60+
return {
61+
"type": "FeatureCollection",
62+
"features": features
63+
};
64+
},
65+
66+
_getMeridian: function (lng) {
67+
lng = this._lngFix(lng);
68+
var coords = [];
69+
for (var lat = -90; lat <= 90; lat++) {
70+
coords.push([lng, lat]);
71+
}
72+
return coords;
73+
},
74+
75+
_getParallel: function (lat) {
76+
var coords = [];
77+
for (var lng = -180; lng <= 180; lng++) {
78+
coords.push([this._lngFix(lng), lat]);
79+
}
80+
return coords;
81+
},
82+
83+
_getFeature: function (coords, prop) {
84+
return {
85+
"type": "Feature",
86+
"geometry": {
87+
"type": "LineString",
88+
"coordinates": coords
89+
},
90+
"properties": prop
91+
};
92+
},
93+
94+
_lngFix: function (lng) {
95+
if (lng >= 180) return 179.999999;
96+
if (lng <= -180) return -179.999999;
97+
return lng;
98+
}
99+
100+
});
101+
102+
L.graticule = function (options) {
103+
return new L.Graticule(options);
104+
};

inst/htmlwidgets/plugins/Leaflet.SimpleGraticule/L.SimpleGraticule.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ L.SimpleGraticule = L.LayerGroup.extend({
88
interval: 20,
99
showOriginLabel: true,
1010
redraw: 'move',
11-
hidden: false
11+
hidden: false,
12+
zoomIntervals : []
1213
},
1314

1415
lineStyle: {
1516
stroke: true,
1617
color: '#111',
1718
opacity: 0.6,
1819
weight: 1,
19-
clickable: false
20+
interactive: false,
21+
clickable: false //legacy support
2022
},
2123

2224
initialize: function(options) {
@@ -54,6 +56,16 @@ L.SimpleGraticule = L.LayerGroup.extend({
5456
this.clearLayers();
5557

5658
if (!this.options.hidden) {
59+
60+
var currentZoom = this._map.getZoom();
61+
62+
for(var i = 0 ; i < this.options.zoomIntervals.length ; i++) {
63+
if(currentZoom >= this.options.zoomIntervals[i].start && currentZoom <= this.options.zoomIntervals[i].end){
64+
this.options.interval = this.options.zoomIntervals[i].interval;
65+
break;
66+
}
67+
}
68+
5769
this.constructLines(this.getMins(), this.getLineCounts());
5870

5971
if (this.options.showOriginLabel) {
@@ -128,7 +140,8 @@ L.SimpleGraticule = L.LayerGroup.extend({
128140
}
129141

130142
return L.marker(latLng, {
131-
clickable: false,
143+
interactive: false,
144+
clickable: false, //legacy support
132145
icon: L.divIcon({
133146
iconSize: [0, 0],
134147
className: 'leaflet-grid-label',
@@ -139,7 +152,8 @@ L.SimpleGraticule = L.LayerGroup.extend({
139152

140153
addOriginLabel: function() {
141154
return L.marker([0, 0], {
142-
clickable: false,
155+
interactive: false,
156+
clickable: false, //legacy support
143157
icon: L.divIcon({
144158
iconSize: [0, 0],
145159
className: 'leaflet-grid-label',

inst/htmlwidgets/plugins/Leaflet.SimpleGraticule/SimpleGraticule-binding.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
LeafletWidget.methods.addSimpleGraticule = function(interval, showOriginLabel, redraw, layerId, group) {
1+
LeafletWidget.methods.addSimpleGraticule = function(interval, showOriginLabel,
2+
redraw, hidden, zoomIntervals, layerId, group) {
23
(function() {
34
this.layerManager.addLayer(
45
L.simpleGraticule({
56
interval: interval,
67
showOriginLabel: showOriginLabel,
7-
redraw: redraw
8+
redraw: redraw,
9+
hidden: hidden,
10+
zoomIntervals: zoomIntervals
811
}),
912
'shape', layerId, group);
1013
}).call(this);

man/addGraticule.Rd

Lines changed: 34 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)