library(rgdal)
## Loading required package: sp
## rgdal: version: 0.9-2, (SVN revision 526)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
## Path to GDAL shared files: /usr/local/Cellar/gdal/1.11.2_1/share/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
## Path to PROJ.4 shared files: (autodetected)
library(rgeos)
## rgeos version: 0.3-8, (SVN revision 460)
##  GEOS runtime version: 3.4.2-CAPI-1.8.2 r3921 
##  Polygon checking: TRUE
library(data.table)
library(leaflet)
options(warn=-1)
# Read the LSOA polygons (already simplified) geojson

dsn <- ("./data/leedsWgs84.geojson")
leedsShape <- readOGR(dsn=dsn, layer="OGRGeoJSON", stringsAsFactors=FALSE)
## OGR data source with driver: GeoJSON 
## Source: "./data/leedsWgs84.geojson", layer: "OGRGeoJSON"
## with 482 features
## It has 2 fields
# check on number of rows

nrow(leedsShape)
## [1] 482
# Read the csv table (I *love* to use the fread function)

dt_ <- fread("./data/leedsLSOA.csv")
#check on number of rows

nrow(dt_)
## [1] 482
# Column names cleaning and selection

colnames(dt_)[1:2] <- c( "LSOA","Name")
colnames(dt_)[5] <- "Population"
colnames(dt_)[378:379] <-  c("Unemployed","Employed")
dt_ <- dt_[,.(LSOA, Name, Unemployed=as.numeric(Unemployed), Workers= as.numeric(Unemployed) + as.numeric(Employed),
      Employed=as.numeric(Employed), Population= as.numeric(Population))][,
      Unemployed_:= round(Unemployed/Workers*100,2)]
# do we have an identical number of matching elements?

identical(dt_[,LSOA] ,leedsShape@data$LSOA11CD)
## [1] FALSE
# no! - only 440: LSOA are changing to keep the pace with population increase /decrease:

length(intersect(leedsShape@data$LSOA11CD,dt_[,LSOA] ))
## [1] 440
# This is to "merge" the data portion of the geojson to the data we will display
# this tecnique just "works" preserving order and matching
# other tecniques may give errors like "row.names of data and Polygons IDs do not match"

leedsShape@data <- data.frame(leedsShape@data, dt_[match(leedsShape@data[, "LSOA11CD"], dt_[, LSOA]),])

leedsShape@data$LSOA11NM <- leedsShape@data$LSOA <- NULL  # eliminate duplicated or unused columns
# As we may want to play with different colours palettes or binning types, variables etc., 
# a function may be useful

myMap = function(shape,szoom, pal, area,vals,Pops, ...) {
  mbox <- bbox(shape)
  cLong <- round((mbox["x","min"]+mbox["x","max"])/2,4)
  cLat <- round((mbox["y","min"]+mbox["y","max"])/2,4)
  #
  leaflet(data = shape)  %>% setView(cLong, cLat, szoom) %>% addTiles() %>%
    addPolygons(fillColor = pal(vals), 
                weight = 2,
                opacity = 1,
                color = 'white',
                dashArray = '3',
                fillOpacity = .5, 
                popup = sprintf("<strong>%s</strong><br/>%g%%  of %g", area, vals,Pops)
    ) %>%
    addLegend(pal = pal, values = vals, ...)
}

pal = colorBin('YlOrRd', leedsShape@data$Unemployed_, 5)
myMap(leedsShape, szoom=11,pal,vals= leedsShape@data$Unemployed_,area= leedsShape@data$Name,
         Pops= leedsShape@data$Workers, title = '% Unemployment')