lvalnegri

test_mapping.R

May 16th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.08 KB | None | 0 0
  1. pkg <- c('data.table', 'htmltools', 'leaflet', 'rgdal', 'rgeos', 'RMySQL')
  2. invisible( lapply(pkg, require, character.only = TRUE) )
  3.  
  4. # read OA shapefiles and calculate centroids
  5. bng.proj = '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs'
  6. shp.path <- './data/boundaries/shp'
  7. shp.fn <- 'OA'
  8. shp <- readOGR(shp.path, shp.fn)
  9. centroids <- gCentroid(shp, byid = TRUE)
  10. centroids <- cbind(shp@data, centroids@coords)
  11. centroids <- as.data.table(centroids)
  12. setnames(centroids, 'id', 'X9012')
  13. setkey(centroids, 'X9012')
  14.  
  15. # read geo data
  16. file.name <- ' Geo'
  17. data.path <- "./dataset/data/"
  18. dt.geo <- fread(paste(data.path, file.name, '.csv', sep = ''), select = c('parent_unid', 'oa11'), na.strings = '' )
  19. dt.geo <- dt.geo[!is.na(oa11)]
  20. setnames(dt.geo, c('X9007', 'X9012'))
  21. dt.geo <- unique(dt.geo)
  22. setkey(dt.geo, 'X9007')
  23. # this is necessary (for testing purposes!!!) because to each patient_id there could be associated multiple OAs
  24. dt.geo.unqiue <- dt.geo[, .SD[1], X9007]
  25.  
  26. # read procedures data and merge with previous
  27. db_conn <- dbConnect(MySQL(), group = 'shiny', dbname = 'PCI')
  28. dt <- data.table(dbGetQuery(db_conn, "SELECT D3010 AS datefield, X1010 AS HSP_id, X2010, X2020, X9007 FROM dataset"))
  29. dbDisconnect(db_conn)
  30. setkey(dt, 'X9007')
  31. # dt1 <- dt[dt.geo.unqiue]
  32. dt2 <- dt.geo.unqiue[dt][!is.na(X9012)]
  33.  
  34.  
  35. ## results for single hospital
  36.  
  37. # choroplet
  38. qeb.map <- dt2[HSP_id == 'QEB', .N, X9012]
  39. shp.qeb <- subset(shp, id %in% qeb.map[, X9012])
  40. shp.qeb <- merge(shp.qeb, qeb.map, by.x = 'id', by.y = 'X9012')
  41. shp.qeb.bbox <- bbox(shp.qeb)
  42. pal <- colorNumeric('YlOrRd', shp.qeb$N)
  43. leaflet(shp.qeb) %>%
  44.     fitBounds(shp.qeb.bbox[1], shp.qeb.bbox[2], shp.qeb.bbox[3], shp.qeb.bbox[4] ) %>%
  45.     addTiles('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png') %>%
  46.     addPolygons(
  47.         stroke = TRUE,
  48.         color = '#444444',
  49.         opacity = 0.8,
  50.         weight = 0.6,
  51.         smoothFactor = 0.5,
  52.         fill = TRUE,
  53.         fillColor = ~pal(N),
  54.         fillOpacity = 0.4,
  55.         highlight = highlightOptions(
  56.             weight = 3,
  57.             color = '#666',
  58.             dashArray = "",
  59.             fillOpacity = 0.7,
  60.             bringToFront = TRUE),
  61.         label = lapply(paste0('N. Patients: <b>', shp.qeb$N, '</b>'), HTML),
  62.         labelOptions = labelOptions(
  63.             style = list("font-weight" = "normal", padding = "3px 8px"),
  64.             textsize = "12px",
  65.             direction = "auto"
  66.         )
  67.     ) %>%
  68.     addLegend(
  69.         pal = pal,
  70.         values = ~N,
  71.         title = 'N Patients',
  72.         position = 'bottomright',
  73.         opacity = 0.8
  74.     )
  75.  
  76. # hotspot (http://rpubs.com/bhaskarvk/leaflet-heat)
  77. qeb.hot <- dt2[HSP_id == 'QEB', .N, .(date.year = paste0('y', substr(datefield, 1, 4) ), X9012) ]
  78. qeb.hot <- merge(qeb.hot, centroids, by = 'X9012')
  79.  
  80. # total
  81. qeb.hot.tot <- qeb.hot[, .(N = sum(N)), .(X_Lon = x, Y_Lat = y) ]
  82. mp <- leaflet(qeb.hot.tot) %>%
  83.         fitBounds(shp.qeb.bbox[1], shp.qeb.bbox[2], shp.qeb.bbox[3], shp.qeb.bbox[4] ) %>%
  84.         addTiles('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png') %>%
  85.         addHeatmap(
  86.             lng = ~X_Lon, lat = ~Y_Lat,
  87.             intensity = ~N,
  88.             blur = 20, max = 0.05, radius = 15
  89.         )
  90.  
  91.  
  92. # by year
  93. qeb.hot.y <- dcast.data.table(qeb.hot, x + y ~ date.year, value.var = 'N')
  94. mp <- leaflet() %>%
  95.         fitBounds(shp.qeb.bbox[1], shp.qeb.bbox[2], shp.qeb.bbox[3], shp.qeb.bbox[4] ) %>%
  96.         addTiles('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png')
  97. for(idx in 3:17){
  98.     m <- colnames(qeb.hot.y)[idx]
  99.     t <- qeb.hot.y[, c(1:2, idx), with = FALSE]
  100.     setnames(t, c('X_Lon', 'Y_Lat', 'N'))
  101.     mp <- mp %>%
  102.             addHeatmap(data = t[!is.na(N)],
  103.                 layerId = m, group = m,
  104.                 lng = ~X_Lon, lat = ~Y_Lat,
  105.                 blur = 20, max = 0.05, radius = 15
  106.             )
  107. }
  108. mp <- mp %>%
  109.         addLayersControl(
  110.             baseGroups = colnames(qeb.hot.y)[3:17],
  111.             options = layersControlOptions(collapsed = TRUE)
  112.         )
Advertisement
Add Comment
Please, Sign In to add comment