-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeaflet.Rmd
More file actions
132 lines (114 loc) · 5.13 KB
/
Leaflet.Rmd
File metadata and controls
132 lines (114 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
---
title: "R Leaflet template"
author: "Raghu Sidharthan"
output: html_document
---
[RBasic1](RBasic1),
[RBasic2](RBasic2),
[RBasic3](RBasic3),
[data.table](RDTable),
[dplyr](dplyr),
[tidyr](tidyr),
[RGIS](RGIS),
[Leaflet](Leaflet)
**Libraries to load**
```{r, message=F, warning=F}
library(rgdal)
library(leaflet)
library(htmltools)
library(data.table)
library(rgeos)
library(jsonlite)
```
```{r,results='hide',echo=-1,eval=TRUE}
setwd('C:\\Users\\sidharthanr\\Dropbox\\Resource\\Git\\Resource')
indShp <- readOGR("..\\..\\R\\GIS\\Shape\\India", "IND_adm2")
shpDT <- indShp[indShp@data$NAME_1=='Kerala',]
shpDT@data <- data.table(shpDT@data)
shpDT@data <- shpDT@data[,.(NAME_2,VARNAME_2)]
shpDT@data[,txtLbl := paste(NAME_2,'-',VARNAME_2)]
#shpDT <- gSimplify(shpDT,tol = 0.001)
row.names(shpDT@data) <- row.names(shpDT)
shpDT <- SpatialPolygonsDataFrame(shpDT,shpDT@data)
shpDT_pkd <- shpDT[shpDT@data$NAME_2 %in% c('Palakkad','Malappuram','Thrissur','Ernakulam'),]
sort( sapply(ls(),function(x){object.size(get(x))}))
```
**1. Basic template**
```{r}
leaflet() %>%
addTiles() %>%
addProviderTiles('Esri.WorldStreetMap',group='EsriStreet') %>%
addProviderTiles('Esri.WorldImagery',group='EsriImagery') %>%
addProviderTiles('CartoDB.Positron',group='CartoDB-1') %>%
addPolygons(data=shpDT,group='Kerala',stroke=T,weight = 2,fillOpacity = 0,
label=lapply(shpDT@data$txtLbl, function(x) HTML(x)),
labelOptions = labelOptions(noHide = T)) %>%
addLayersControl(
baseGroups = c('CartoDB-1','EsriStreet','EsriImagery'),
overlayGroups = c('Kerala'),
options = layersControlOptions(collapsed = T) )
```
**2. Example with GeoJSON, Markers**
```{r}
# Reading the GeoJSON File using the jsonlite package
GJFName <- '..\\..\\Data\\PKD_water_road.geojson'
GJFName_LS<- '..\\..\\Data\\PKD_water_road_LS.geojson'
GJFName_PG<- '..\\..\\Data\\PKD_water_road_PG.geojson'
pkdGJ <- readLines(GJFName, warn = FALSE) %>% paste(collapse = "\n") %>% fromJSON(simplifyVector = FALSE)
pkdGJDT <- data.table(fromJSON(GJFName)$features$properties)
pkdGJDT$geomType <- unlist(lapply(pkdGJ$features, function(feat) feat$geometry$type))
# Function to style the features for display
FeatureStyler <- function(feat) {
t_weight = 2;t_opacity = 1;t_fillOpacity = 0;t_color = '#006d2c';t_fillColor = '#41b6c4'
if (feat$geometry$type == 'Polygon') {
t_fillOpacity = 0.5; t_color = '#253494'; t_fillColor = '#41b6c4'
}
else if (feat$geometry$type == 'LineString') t_fillOpacity = 0
feat$properties$style <- list(weight = t_weight, opacity = t_opacity,fillOpacity = t_fillOpacity,
color = t_color, fillColor = t_fillColor,label='sdf', labelOptions = labelOptions(noHide = T))
feat
}
pkdGJ$features <- lapply(pkdGJ$features, FeatureStyler)
# Selector vecors to subset GeoJson Types
selectorTranspo <- (unlist(lapply(pkdGJ$features, function(feat) feat$properties$Highway)) != 'not_hway' |
unlist(lapply(pkdGJ$features, function(feat)!is.null(feat$properties$railway))))
selectorWater <- !(selectorTranspo)
subsetGeoJSON <- function(fullGeoJSON, selectorTemplate) {
temp <- list(); temp$type <- "FeatureCollection"; temp$features <- fullGeoJSON$features[selectorTemplate]
}
pkdGJ_water <- subsetGeoJSON(pkdGJ,selectorWater)
pkdGJ_transpo <- readOGR(GJFName_LS, "OGRGeoJSON")
pkdGJ_transpo <- pkdGJ_transpo[pkdGJ_transpo@data$Highway!='not_hway',]
pkdGJ_transpo@data <- data.table(pkdGJ_transpo@data)[,.(Name,Highway)]
pkdGJ_transpo@data[is.null(Name),Name:='Unknown']
pkdTranCen = data.table(data.frame(gCentroid(pkdGJ_transpo,byid=TRUE)))
pkdTranCen[,Name:=pkdGJ_transpo$Name]
pkdTranCen[,Highway:=pkdGJ_transpo$Highway]
pkdTranCen <- pkdTranCen[Name!='null',]
pkdTranCen <- pkdTranCen[!duplicated(Name) & Highway %in% c('trunk'),]
leaflet() %>% addTiles() %>%
addProviderTiles('Esri.WorldStreetMap',group='EsriStreet') %>%
addProviderTiles('CartoDB.Positron',group='CartoDB-1') %>%
addPolygons(data=shpDT_pkd,group='Cenral Kerala',stroke=T,weight = 2,fillOpacity = 0,
label=lapply(shpDT_pkd@data$txtLbl, function(x) HTML(x)),
labelOptions = labelOptions(noHide = T)) %>%
addGeoJSON(pkdGJ_water, group = 'Water') %>%
addMarkers(lng=76.639003, lat=10.800471, popup="Palakkad Jn.",group='Landmarks') %>%
addPolylines(data=pkdGJ_transpo,group='transpo', weight = 2, color='#c51b8a') %>%
addCircleMarkers(lng = pkdTranCen$x, lat = pkdTranCen$y,
label = as.character((pkdTranCen$Name)),group='transpo',opacity=0,radius = 1,
labelOptions = lapply(1:nrow(pkdTranCen), function(x) {
labelOptions(opacity=1, noHide = T,textOnly = TRUE,textsize='10px',
direction = 'auto', offset=c(20,-15))
})) %>%
setView(lng=76.64, lat=10.8,zoom = 10) %>%
addLayersControl(
baseGroups = c('CartoDB-1','EsriStreet'),
overlayGroups = c('Cenral Kerala','Water','Landmarks','transpo'),
options = layersControlOptions(collapsed = T) )
```
**3. Save to HTML**
```{r,eval=F}
library(htmlwidgets)
saveWidget(mm, file = 'SERPM_MAZ_n_TAZ.html', selfcontained = F)
```