-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_prepare_neighborhood_data.Rmd
More file actions
120 lines (80 loc) · 4.3 KB
/
01_prepare_neighborhood_data.Rmd
File metadata and controls
120 lines (80 loc) · 4.3 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
```{r, message= FALSE}
library(tidyverse)
library(naniar)
#install.packages(c('s2', 'units','rJava','raster', 'sf','terra', 'sftime','stars', 'gstat','RNetCDF', 'ncmeta', 'areal', 'ncdfgeom', 'leafem', 'leafgl', 'leaflegend', 'leaflet', 'leafsync', 'maptiles', 'tmaptools', 'tmap', 'lwgeom', 'leafpop','satellite', 'mapview'), repos = "https://cloud.r-project.org", dependencies = TRUE)
library(sf)
#library(s2)
#library(units)
```
# Data Import
BMF file from:
https://nccsdata.s3.amazonaws.com/harmonized/bmf/unified/MD_BMF_V1.1.csv
Neighborhood csv file from: https://data.baltimorecity.gov/datasets/baltimore::neighborhood-statistical-area-nsa-boundaries/about
```{r}
BMF <- read_csv("data/MD_BMF_V1.1.csv")
neighborhoods <- read_csv("data/Neighborhood_Statistical_Area_(NSA)_Boundaries.csv")
```
# Get lat and long for shape file
Shape file from: https://data.baltimorecity.gov/datasets/baltimore::neighborhood-statistical-area-nsa-boundaries/about
https://stackoverflow.com/questions/66381795/check-whether-point-coordinate-lies-within-polygon
https://www.statsilk.com/maps/convert-esri-shapefile-map-geojson-format
```{r}
neighborhood_shape <-st_read("data/Neighborhood_Statistical_Area_(NSA)_Boundaries/Neighborhood_Statistical_Area_(NSA)_Boundaries.shp")
```
# Check for missing data
```{r}
BMF_geo <- BMF %>% dplyr::select(EIN, LATITUDE, LONGITUDE)
any(is.na(BMF_geo$LATITUDE)) # no missing location info
any(is.na(BMF_geo$LONGITUDE))
```
# Limit to just Baltimore
Based on coordinates...
```{r}
#CRS <- st_crs(neighborhood_shape$geometry) # get class sf version of coordinates
BMF_sf <- st_as_sf(BMF, coords = c('LONGITUDE', 'LATITUDE'), crs = st_crs(4326)) %>% st_set_crs(4326) # Use LONGITUDE and LATITUDE columns and convert to sf object (with a geometry column), use coordinate reference system 4326
BMF_trans <- st_transform(BMF_sf, 2163) #EPSG:2163, convert lat and long to NAD27 / US National Atlas Equal Area for the org data
neighborhood_tt <- st_transform(neighborhood_shape$geometry, 2163)# convert Baltimore shape file to NAD27 / US National Atlas Equal Area coordinate reference system
```
Look for the intersection of orgs with locations within Baltimore
```{r}
# Make a new column for each org row about if the location is located within the Baltimore shape outline
intersection <- BMF_sf %>% mutate(
intersection = as.integer(st_intersects(BMF_trans, neighborhood_tt )))
# filter for just data within Baltimore dropping other MD orgs
in_balt <- intersection %>% filter(!is.na(intersection)) # just Baltimore locations
```
# Check that it worked
Checking that it worked using this image: https://commons.wikimedia.org/wiki/File:Baltimore_neighborhoods_map.png
```{r include-image, echo=FALSE, out.width="400px"}
knitr::include_graphics("https://upload.wikimedia.org/wikipedia/commons/5/57/Baltimore_neighborhoods_map.png")
```
```{r}
in_balt[1,]$intersection # row for intersection
in_balt[1,]$EIN # org id
in_balt[1,]$ORG_ADDR_FULL # this looks like downtown when you look it up
# how does this look in the neighborhood file?
neighborhood_shape[in_balt[1,]$intersection,]$Name # neighborhood name
#Looks like this is in that location
in_balt[43,]$ORG_ADDR_FULL
neighborhood_shape[in_balt[43,]$intersection,]$Name # neighborhood name
#Looks like this is in that location
in_balt[58,]$ORG_ADDR_FULL
neighborhood_shape[in_balt[58,]$intersection,]$Name # neighborhood name
#Looks like this is in that location
in_balt[65,]$ORG_ADDR_FULL
neighborhood_shape[in_balt[65,]$intersection,]$Name # neighborhood name
#Looks like this is in that location
in_balt[100,]$ORG_ADDR_FULL
neighborhood_shape[in_balt[100,]$intersection,]$Name # neighborhood name
#Looks like this is in that location
```
# Combining it all together
Want the org info, and neighborhood info and shape information together to enable making maps later.
```{r}
neighborhood_shape <-as_tibble(neighborhood_shape) # convert to tibble
neighborhood_shape<-neighborhood_shape %>% mutate(id = row_number()) # add row number column
org_data <-left_join(in_balt, neighborhood_shape, by = c("intersection" = "id")) # look for what row numbers align with the shape file
# save neighborhood R file and org data with neighborhood info
write_rds(neighborhood_shape, file = "data/processed/neighborhood_shape_data.rds")
write_rds(org_data, file = "data/processed/org_data.rds")
```