-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_map.R
More file actions
59 lines (51 loc) · 2.5 KB
/
generate_map.R
File metadata and controls
59 lines (51 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
library(rgdal)
library(rgeos)
library(leaflet)
library(dplyr)
library(htmlwidgets)
setwd("~/Desktop/Data_Incubator")
#download grocery stores file
#url<-"http://data.cityofchicago.org/api/views/53t8-wyrc/rows.csv?accessType=DOWNLOAD"
#download_dir<-getwd()
#dest_name<-"grocery_stores.csv"
#download.file(url, dest_name)
gf<-read.csv("chicago_green_roof.txt")
gs<-read.csv("grocery_stores.csv")
#filter for grocery stores > 10,000 sq feet (used in definition of food desert)
large_store<-filter(gs,SQUARE.FEET>10000)
#download zipcodes:
url <- "http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_zcta510_500k.zip"
download_dir<-getwd()
dest_name<-"all_zip.zip"
download.file(url, dest_name)
unzip(dest_name, exdir=download_dir, junkpaths=TRUE)
filename<-list.files(download_dir, pattern=".shp", full.names=FALSE)
filename<-gsub(".shp", "", filename)
zip_codes<-readOGR(download_dir, "cb_2014_us_zcta510_500k")
chi_zips<-c(60647,60639,60707,60622,60651,60611,60638,60652,60626,60621,60645,60631,60646,60628,60660,60640,60625,60641,60657,60615,60636,60649,60617,60643,60633,60643,60612,60604,60656,60624,60655,60644,60603,60605,60653,60609,60666,60618,60616,60602,60601,60608,60607,60661,60606,60614,60827,60630,60642,60659,60707,60634,60613,60610,60654,60632,60623,60629,60620,60637,60619)
chicago <- zip_codes[zip_codes$ZCTA5CE10 %in% chi_zips,]
interest<-subset(chicago, grepl('^(60636)|(60628)|(60644).*', chicago$ZCTA5CE10,perl=T))
#epsg:4326 transform
chicago<-spTransform(chicago, CRS("+init=epsg:4326"))
interest<-spTransform(interest, CRS("+init=epsg:4326"))
chicago_data<-chicago@data[,c("GEOID10", "ALAND10")]
interest_data<-interest@data[,c("GEOID10", "ALAND10")]
#gSimplify
chicago<-gSimplify(chicago,tol=0.01, topologyPreserve=TRUE)
interest<-gSimplify(interest,tol=0.01, topologyPreserve=TRUE)
#create SpatialPolygonsDataFrame
chicago<-SpatialPolygonsDataFrame(chicago, data=chicago_data)
interest<-SpatialPolygonsDataFrame(interest, data=interest_data)
#leaflet map
m <- leaflet(gf) %>%
addTiles() %>%
setView(lng =-87.7738, lat =41.78798, zoom = 10) %>%
addPolygons(data=chicago,weight=2,fillOpacity = 0.15,color="blue")%>%
addPolygons(data=interest,weight=2,fillOpacity = 0.6,color="turquoise")%>%
addCircles(~LONGITUDE, ~LATITUDE, weight =4, radius=20,
color="darkgreen", stroke = TRUE, fillOpacity = 0.8) %>%
addCircles(large_store$LONGITUDE, large_store$LATITUDE, weight = 4, radius=20,
color="purple", stroke = TRUE, fillOpacity = 0.8)
m
#save map
saveWidget(m, file="chicago_map.html")