Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. setwd("/Users/****/Documents/****/DataSets/PopulationGrid")
  2.  
  3. library(rgdal)
  4. library(sp)
  5. library(rgeos)
  6. library(maptools)
  7. library(ggplot2)
  8. library(plyr)
  9. library(leaflet)
  10.  
  11.  
  12. data = readOGR("Census2011_Small_Areas.shp")
  13. #View(data)
  14.  
  15. #Subset population shapefile to show only Leinster
  16. newdata <- subset(data, data$COUNTYNAME=='Dun Laoghaire-Rathdown' | data$COUNTYNAME=='South Dublin' | data$COUNTYNAME=='Dublin City' | data$COUNTYNAME=='Carlow County' | data$COUNTYNAME=='Dublin County' |
  17. data$COUNTYNAME=='Kildare County' | data$COUNTYNAME=='Kilkenny County' |
  18. data$COUNTYNAME=='Laois County' | data$COUNTYNAME=='Longford County' |
  19. data$COUNTYNAME=='Louth County' | data$COUNTYNAME=='Meath County' |
  20. data$COUNTYNAME=='Offaly County' | data$COUNTYNAME=='Westmeath County' |
  21. data$COUNTYNAME=='Wexford County' | data$COUNTYNAME=='Wicklow County' |
  22. data$COUNTYNAME=='Fingal')
  23.  
  24. shapeData <- spTransform(newdata, CRS("+proj=longlat +datum=WGS84 +no_defs"))
  25.  
  26.  
  27. #Connect to MySQL using credentials
  28. con <- dbConnect(MySQL(), user="****", password="****", dbname="****", host="****")
  29.  
  30. #SQL Query to retrieve healthcentre information from database
  31. query <- dbSendQuery(con, "Select * From disabilitydb.healthcentres;")
  32.  
  33. #fetch data, number of rows=1822
  34. centres <- fetch(query, n=1822)
  35.  
  36. #check that the fetchhas gotten everything
  37. isFinished <- dbHasCompleted(query)
  38.  
  39. #convert data into a data-frame
  40. data.frame(centres)
  41.  
  42. #check data
  43. centres
  44.  
  45. #subset healthcentre data to show only Leinster
  46. leinster <- subset(centres, County =="Wicklow" | County =="Wexford" | County =="Dublin" | County =="Kildare"
  47. | County =="Meath" | County =="Carlow" | County =="Kilkenny"
  48. | County =="Laois" | County =="Offaly" | County =="Westmeath"
  49. | County =="Longford" | County =="Louth")
  50.  
  51. #Create data frame
  52. data = data.frame(
  53. ID = as.numeric(leinster$healthcentre_id),
  54. longitude = as.numeric(leinster$long_xcord),
  55. latitude = as.numeric(leinster$lat_ycord)
  56. )
  57.  
  58. #Create a pop ups for the map
  59. map_popup <- paste0("<strong>HSE Support Centre: </strong>",
  60. leinster$`Service name`,
  61. "<br><strong>Services Provided: </strong>",
  62. leinster$`Service name`,
  63. "<br><strong>Address: </strong>",
  64. leinster$Address)
  65.  
  66. map_popup2 <- paste0("<strong> Area Name: </strong>",
  67. shapeData$EDNAME,
  68. "<br><strong>Population: </strong>",
  69. shapeData$TOTAL2011)
  70.  
  71. heat <- colorNumeric(
  72. c("Yellow", "Red"),
  73. domain = shapeData$TOTAL2011)
  74.  
  75.  
  76. #Create Leaflet map
  77. map <- leaflet() %>% addTiles() %>%
  78. setView(lng = -7.1050, lat=53.2000,zoom=8) %>%
  79.  
  80. addPolygons(data=shapeData,weight=1, popup = map_popup2, fillOpacity = 0.5,
  81. color = ~colorQuantile("YlOrRd", shapeData$TOTAL2011)(TOTAL2011)) %>%
  82.  
  83. addCircleMarkers(data = leinster, lng = ~long_xcord,
  84. lat = ~lat_ycord, radius=.5, color="green") %>%
  85.  
  86. addCircles(data = leinster, lng = ~long_xcord,
  87. lat = ~lat_ycord, popup = map_popup,
  88. radius = 4828.03, fillOpacity = 0.1, #3218.69 = 2 miles
  89. color = 'black', fillColor = 'blue',weight = 2, label=leinster$`Service name`) %>%
  90.  
  91. addLegend("bottomright", pal = heat, values = shapeData$TOTAL2011,
  92. title = "Population(Small Areas)",
  93. opacity = 1)
  94.  
  95. #Print the map
  96. print(map)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement