Task 1. Produce a map showing the centroids of each municipality in just the state of Säo Paulo, and add the outer boundary of Säo Paulo state.

  1. Open municipalities and filter for SP State
municipalities <- read_sf("C:/Users/accou/Downloads/Spatial Week 3/municipalities/municipalities.shp")

municipalities_sp <- municipalities %>% filter(UF=="SP")
  1. Generate Municipalities Centroids and Plot
municipalities_centroids <- municipalities_sp %>% st_centroid()
municipalities_centroids %>% ggplot()+geom_sf()

  1. Dissolve SP State Polygons to creat SP State Border
SP_border <- municipalities_sp %>% st_union()
SP_border %>% ggplot()+geom_sf()

  1. Plot SP Centroids and sp border together
SP_border %>% ggplot()+geom_sf()+
  geom_sf(data = municipalities_centroids)+
    theme_classic() +
  labs(title = "Sao Paulo and Their Centroids")

### Task 2: What is the mean human development index of municipalities in each state of Brazil?

1.HDI mean by state

br_states <- municipalities %>%
  group_by(UF) %>%
  summarise(IDHM_mean = mean(IDHM_10))
  1. Plotting results
br_states %>% ggplot()+
  geom_sf(aes(fill=IDHM_mean))+
  theme_classic()+scale_fill_continuous(low = "red",high="green")+
labs(title = "Brazilian Sates by Mean Human Development Index")

Task 3: Produce a polygon/shape file mapping the area of the municipality ‘Gaucha do Norte’ that is in the indegenous territory “Parque do Xingu”

indegenous <- read_sf("C:/Users/accou/Downloads/Spatial Week 3/BC250_Terra_Indigena_A/BC250_Terra_Indigena_A.shp")
## Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, : GDAL
## Message 1: C:\Users\accou\Downloads\Spatial Week
## 3\BC250_Terra_Indigena_A\BC250_Terra_Indigena_A.shp contains polygon(s) with
## rings with invalid winding order. Autocorrecting them, but that shapefile
## should be corrected using ogr2ogr for example.
br_states %>% ggplot()+geom_sf() + geom_sf(data = indegenous,fill = "red")

  1. Shapefile for Gaucha do Norte e Xingu
Xingu <- indegenous %>% filter(nome == "Parque do Xingu") %>% st_transform(4326)
Gaucha <- municipalities %>% filter(NOME== "GAUCHA DO NORTE") %>% st_transform(4326)
  1. Plotting shapefile to see overlap
Gaucha %>% ggplot()+ geom_sf(fill = "red") + geom_sf(data=Xingu,fill="blue",alpha= 0.5)

  1. Create shapefile for interactions and plot it
intersection <- Gaucha %>% st_intersection(Xingu)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
intersection %>% ggplot()+geom_sf(fill = "dark green")

5. Plotting together

Gaucha %>% ggplot()+geom_sf(fill = "red",alpha=0.5) + geom_sf(data=Xingu, fill="blue",alpha=0.5) + 
                                  geom_sf(data = intersection,fill = "black")

Intersection area

st_area(intersection)
## 8109249396 [m^2]

Task4 : Inther state of Acre (AC), which two social housing (MCMV) projects are closest to each other? Create a 20km buffer around each housing project.

  1. Load housing shapefile
Housing <- read_sf("C:/Users/accou/Downloads/Spatial Week 3/MCMV_new/MCMV_new.shp")
  1. Select housing in AC State and plot the results
Housing_AC <- Housing %>% filter(UF=="AC")
br_states %>% filter(UF == "AC") %>% ggplot()+ geom_sf() + geom_sf(data = Housing_AC)

3. Calculating distance between housing points and view results

distance <- Housing_AC %>% 
  st_transform(29189) %>%
  st_distance() %>%
  as.data.frame()
  1. Generate buffer around housing points
Housing_AC %>% st_transform(29189) %>% 
  st_buffer(10000) %>% 
  ggplot()+geom_sf(fill = "dark green")

  1. Plotting it all together
br_states %>% filter(UF=="AC") %>% ggplot() + geom_sf()+
  geom_sf(data=(Housing_AC %>% st_transform(29189) %>% st_buffer(10000)),fill = "dark green")+
  geom_sf(data=Housing_AC,color ="red")

Task5 : Across Brazil, which municipalities have the lowest and highest number of MCMV housing units (UH) in its territory? Create a map of the distribution of total housing units by municipality.

  1. Spatial join between municipalities and housing
mun_Housing_units <- municipalities %>% st_join(Housing)
  1. Calculate total of housing units in each municipality
mun_Housing_units <- mun_Housing_units %>% 
  group_by(COD_MUN,NOME) %>% 
  summarize(UH=sum(UH,na.rm = T)) %>% 
  ungroup()
## `summarise()` has grouped output by 'COD_MUN'. You can override using the
## `.groups` argument.
  1. Select municipality with most Housing Units
mun_Housing_units %>% arrange(-UH) %>% slice(1) %>% pull(NOME)
## [1] "RIO DE JANEIRO"
  1. Plot the distribution of total housing units by municipality
mun_Housing_units %>% ggplot() + 
  geom_sf(aes(fill=UH),col=NA) +
  scale_fill_gradient(low="#ccece6",high = "dark green",trans = "log")+
  theme_classic()+
labs(title = "Total Housing Units by Municipality")
## Warning in scale_fill_gradient(low = "#ccece6", high = "dark green", trans =
## "log"): log-2.718282 transformation introduced infinite values.