Borrowed from benbestphd.com/landscape-ecology-labs/ since original downloads not working at County of Santa Barbara GIS Spatial Catalog:
data/cosb.countyofsb.org/
:
city_boundary_no_ocean.shp
county_bnd.shp
Create regions from city boundaries and the county remainder (ie city
=“Outside Cities”). The spatial file will be output to data/regions.geojson
.
library(tidyverse)
library(sf)
library(sp)
library(rgeos)
library(maptools)
library(geojsonio)
library(leaflet)
regions_json = 'data/regions.geojson'
cities_shp = 'data/cosb.countyofsb.org/city_boundary_no_ocean.shp'
county_shp = 'data/cosb.countyofsb.org/county_bnd.shp'
if (!file.exists(regions_json)){
# read shapefiles
cities = read_sf(cities_shp) %>% as('Spatial')
county = read_sf(county_shp) %>% as('Spatial')
# subtract cities from county
not_cities = county %>%
gDifference(cities) %>%
SpatialPolygonsDataFrame(data.frame(city='Outside Cities')) %>%
spChFIDs('ID9')
# fix cities for binding
cities@data$city = cities@data$CITY
cities@data = subset(cities@data, select=city)
row.names(cities@data) = row.names(cities)
# regions: bind cities and not cities
regions = spRbind(cities, not_cities)
# add region id
regions@data$region_id = 1:9
# calculate area
sqft_to_acres = 1/43560
regions@data$area_acres = gArea(regions, byid=T) * sqft_to_acres
# write layer
write_csv(regions@data, 'layers/regions.csv')
# write regions to geosjon
geojson_write(regions, file=regions_json, convert_wgs84=T)
}
Created:
Let’s show a prettier map of the regions:
# read regions from geojson file
regions = geojson_read(regions_json, what='sp')
# setup color palette and labels
pal = colorNumeric('Spectral', regions$region_id, reverse=F)
labels <- sprintf(
"%d: <strong>%s</strong><br/>",
regions$region_id, regions$city) %>%
lapply(htmltools::HTML)
# show interactive map
leaflet(regions) %>%
# http://leaflet-extras.github.io/leaflet-providers/preview/
addProviderTiles(providers$Stamen.TonerLite) %>%
addPolygons(
fillColor = ~pal(region_id),
fillOpacity = 0.5,
color = ~pal(region_id),
opacity = 0.7,
weight = 2,
highlight = highlightOptions(
fillOpacity = 0.7,
weight = 5,
opacity = 0.9,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto"))
Show table of regions.
library(DT)
regions@data %>% datatable()
This Detailed County Map (3´ x 4´) from Santa Barbara County Planning and Development - Maps and Imagery - Recently Added or Updated has some interesting political / administrative criteria for considering alternative regions: