RBasic1, RBasic2, RBasic3, data.table, dplyr, tidyr, RGIS, Leaflet
Libraries to load
library(rgdal)
library(leaflet)
library(htmltools)
library(data.table)
library(rgeos)
library(jsonlite)
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
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
# 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")
## OGR data source with driver: GeoJSON
## Source: "..\..\Data\PKD_water_road_LS.geojson", layer: "OGRGeoJSON"
## with 547 features
## It has 43 fields
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']
## Name Highway
## 1: Palghat-Ottappalam-Shornur Road secondary
## 2: Palakkad-Mannarkkad-Calicut Road trunk
## 3: Palakkad - Pollachi Road primary
## 4: Palakkad - Pollachi Road primary
## 5: Court Road secondary
## ---
## 172: Mankara-Chakkanthara Road secondary
## 173: Palakkad - Coimbatore Road trunk
## 174: Palakkad - Coimbatore Road secondary
## 175: null secondary
## 176: null secondary
pkdTranCen = data.table(data.frame(gCentroid(pkdGJ_transpo,byid=TRUE)))
pkdTranCen[,Name:=pkdGJ_transpo$Name]
## x y Name
## 1: 76.49779 10.78757 Palghat-Ottappalam-Shornur Road
## 2: 76.65878 10.78357 Palakkad-Mannarkkad-Calicut Road
## 3: 76.77845 10.75611 Palakkad - Pollachi Road
## 4: 76.71359 10.76165 Palakkad - Pollachi Road
## 5: 76.65786 10.77053 Court Road
## ---
## 172: 76.63423 10.77090 Mankara-Chakkanthara Road
## 173: 76.67618 10.77260 Palakkad - Coimbatore Road
## 174: 76.67300 10.77251 Palakkad - Coimbatore Road
## 175: 76.65158 10.77753 null
## 176: 76.65308 10.77838 null
pkdTranCen[,Highway:=pkdGJ_transpo$Highway]
## x y Name Highway
## 1: 76.49779 10.78757 Palghat-Ottappalam-Shornur Road secondary
## 2: 76.65878 10.78357 Palakkad-Mannarkkad-Calicut Road trunk
## 3: 76.77845 10.75611 Palakkad - Pollachi Road primary
## 4: 76.71359 10.76165 Palakkad - Pollachi Road primary
## 5: 76.65786 10.77053 Court Road secondary
## ---
## 172: 76.63423 10.77090 Mankara-Chakkanthara Road secondary
## 173: 76.67618 10.77260 Palakkad - Coimbatore Road trunk
## 174: 76.67300 10.77251 Palakkad - Coimbatore Road secondary
## 175: 76.65158 10.77753 null secondary
## 176: 76.65308 10.77838 null secondary
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
library(htmlwidgets)
saveWidget(mm, file = 'SERPM_MAZ_n_TAZ.html', selfcontained = F)