My main motive is to understand the Tree census or record of my hometown Pune city .I am proud to say that Pune city is smart city and I got this data from website .Note - Data is updated on August 2019
This data include many features like location of trees , size canopy , common names etc. I will used this data to hopefully plot it on interactive map using Leaflet library
library(readr)
library(dplyr)
features <-
c(
"id",
"canopy_dia_m",
"condition",
"northing",
"easting",
"common_name",
"botanical_name",
"ward",
"local_name",
"economic_i",
"road_name"
)
p1 <- read_csv("Data/Pune Tree Census August 2019/p1.csv")
p2 <- read_csv("Data/Pune Tree Census August 2019/p2.csv")
p3 <- read_csv("Data/Pune Tree Census August 2019/p3.csv")
p4 <- read_csv("Data/Pune Tree Census August 2019/p4.csv")
p5 <- read_csv("Data/Pune Tree Census August 2019/p5.csv")
# Merging Data and removing raw files
df <- rbind(p1, p2, p3, p4, p5)
df <- select(df, all_of(features))
# deleting files after merging
rm(p1, p2, p3, p4 , p5)
gc()
Removing raw data is important or it will take unnecessary space.There are 40lakhs plus rows and 28 features. I took only features I need for ploting.
Due to large dataset its taking longer time and High computing power map is taking longer time thatโs why I decided to divide data into ward then plot them. Wards are how here city is divide under corporate officials.
dfWard1_20 <- filter(df , ward %in% c(1:20))
dfWard21_40 <- filter(df , ward %in% c(21:40))
dfWard41_60 <- filter(df , ward %in% c(41:60))
dfWard61_77 <- filter(df , ward %in% c(61:77))
paste("Number of Rows:",nrow(df))
## [1] "Number of Rows: 4009623"
rm(df)
gc()
## used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 787094 42.1 8148653 435.2 8928760 476.9
## Vcells 53853172 410.9 275321732 2100.6 358240478 2733.2
##02 Jul 2021
library(leaflet)
plot1 <- addTiles(leaflet())
botName <- unique(dfWard1_20$botanical_name)
for (x in botName) {
df2 <- filter(dfWard1_20, botanical_name == x)
icons <-
icons(
iconUrl = ifelse(
df2$condition == "Healthy",
"icons/banyan_icon.png",
ifelse(
df2$condition == 'Average',
"icons/average.jpg",
ifelse(
df2$condition == "Dead",
"icons/dead2.jpg",
'icons/poor.jpg'
)
)
),
iconWidth = 30,
iconHeight = 55
)
popupMesage <-
data.frame(
popup = paste(
sep = "<br>",
"<b>Botanical Name:</b>" ,
df2$botanical_name,
"<b>Common Name:</b>",
df2$common_name ,
paste("<b>Canopy Diameter:</b>", df2$canopy_dia_m),
paste("<b>Ward:</b>", df2$ward),
paste("<b>Economic use:</b>", df2$economic_i) ,
paste("<b>Street:</b>", df2$road_name) ,
paste("<b>Condition:</b>", df2$condition)
)
)
plot1 <-
plot1 %>% addMarkers(
lat = df2$northing,
lng = df2$easting ,
icon = icons,
clusterOptions = markerClusterOptions(),
popup = popupMesage$popup,
label = paste("Local Name:", df2$local_name, ", Ward:", df2$ward)
)
}
rm(dfWard1_20)
#plot1
plot2 <- addTiles(leaflet())
botName <- unique(dfWard21_40$botanical_name)
for (x in botName) {
df2 <- filter(dfWard21_40, botanical_name == x)
icons <-
icons(
iconUrl = ifelse(
df2$condition == "Healthy",
"icons/banyan_icon.png",
ifelse(
df2$condition == 'Average',
"icons/average.jpg",
ifelse(
df2$condition == "Dead",
"icons/dead2.jpg",
'icons/poor.jpg'
)
)
),
iconWidth = 30,
iconHeight = 55
)
popupMesage <-
data.frame(
popup = paste(
sep = "<br>",
"<b>Botanical Name:</b>" ,
df2$botanical_name,
"<b>Common Name:</b>",
df2$common_name ,
paste("<b>Canopy Diameter:</b>", df2$canopy_dia_m),
paste("<b>Ward:</b>", df2$ward),
paste("<b>Economic use:</b>", df2$economic_i) ,
paste("<b>Street:</b>", df2$road_name) ,
paste("<b>Condition:</b>", df2$condition)
)
)
plot2 <-
plot2 %>% addMarkers(
lat = df2$northing,
lng = df2$easting ,
icon = icons,
clusterOptions = markerClusterOptions(),
popup = popupMesage$popup,
label = paste("Local Name:", df2$local_name, ", Ward:", df2$ward)
)
}
rm(dfWard21_40)
#plot2
plot3 <- addTiles(leaflet())
botName <- unique(dfWard41_60$botanical_name)
for (x in botName) {
df2 <- filter(dfWard41_60, botanical_name == x)
icons <-
icons(
iconUrl = ifelse(
df2$condition == "Healthy",
"icons/banyan_icon.png",
ifelse(
df2$condition == 'Average',
"icons/average.jpg",
ifelse(
df2$condition == "Dead",
"icons/dead2.jpg",
'icons/poor.jpg'
)
)
),
iconWidth = 30,
iconHeight = 55
)
popupMesage <-
data.frame(
popup = paste(
sep = "<br>",
"<b>Botanical Name:</b>" ,
df2$botanical_name,
"<b>Common Name:</b>",
df2$common_name ,
paste("<b>Canopy Diameter:</b>", df2$canopy_dia_m),
paste("<b>Ward:</b>", df2$ward),
paste("<b>Economic use:</b>", df2$economic_i) ,
paste("<b>Street:</b>", df2$road_name) ,
paste("<b>Condition:</b>", df2$condition)
)
)
plot3 <-
plot3 %>% addMarkers(
lat = df2$northing,
lng = df2$easting ,
icon = icons,
clusterOptions = markerClusterOptions(),
popup = popupMesage$popup,
label = paste("Local Name:", df2$local_name, ", Ward:", df2$ward)
)
}
rm(dfWard41_60)
#plot3
plot4 <- addTiles(leaflet())
botName <- unique(dfWard61_77$botanical_name)
for (x in botName) {
df2 <- filter(dfWard61_77, botanical_name == x)
icons <-
icons(
iconUrl = ifelse(
df2$condition == "Healthy",
"icons/banyan_icon.png",
ifelse(
df2$condition == 'Average',
"icons/average.jpg",
ifelse(
df2$condition == "Dead",
"icons/dead2.jpg",
'icons/poor.jpg'
)
)
),
iconWidth = 30,
iconHeight = 55
)
popupMesage <-
data.frame(
popup = paste(
sep = "<br>",
"<b>Botanical Name:</b>" ,
df2$botanical_name,
"<b>Common Name:</b>",
df2$common_name ,
paste("<b>Canopy Diameter:</b>", df2$canopy_dia_m),
paste("<b>Ward:</b>", df2$ward),
paste("<b>Economic use:</b>", df2$economic_i) ,
paste("<b>Street:</b>", df2$road_name) ,
paste("<b>Condition:</b>", df2$condition)
)
)
plot4 <-
plot4 %>% addMarkers(
lat = df2$northing,
lng = df2$easting ,
icon = icons,
clusterOptions = markerClusterOptions(),
popup = popupMesage$popup,
label = paste("Local Name:", df2$local_name, ", Ward:", df2$ward)
)
}
rm(dfWard61_77)
#plot4
library(shiny)
library(leaflet)
ui <- shinyUI(fluidPage(
titlePanel("Pune Tree Census August 2019"),
sidebarLayout(
sidebarPanel(
selectInput(
"ward",
"Select Ward Range",
c(select = "Ward 1-20", "Ward 21-40", "Ward 41-60", "Ward 61-77")
),
submitButton("Submit")
),
mainPanel(leafletOutput('mymap'))
)
))
## This version of bslib is designed to work with rmarkdown version 2.7 or higher.
server <- function(input, output) {
output$mymap <- renderLeaflet({
ifelse(input$ward == "Ward 1-20",
plot1,
ifelse(
input$ward == "Ward 21-40",
plot2,
ifelse(input$ward == "Ward 41-60", plot3, plot4)
))
})
}
shinyApp(ui, server)
#paste("Ward: 1 - 20")
#plot1
#paste("Ward: 21 - 40")
#plot2
#paste("Ward: 41 - 60")
#plot3
#paste("Ward: 61- 77")
#plot1