# Run this once to generate individual urban area shape files.

urban_areas <- readOGR("shapefiles", "urban_simpl")
urban_areas$SUA_NAME11 <- sub(" \\-.*", "", urban_areas$SUA_NAME11)

urban_areas <- urban_areas[-grep(pattern="Not in any", urban_areas$SUA_NAME11), ]

for(urban_area in urban_areas$SUA_NAME11) {

  urban_poly <- urban_areas[which(urban_areas$SUA_NAME11 == urban_area), ]
  
  output_folder <- sprintf("shapefiles/urban_areas/%s", urban_area)
  output_name <- sprintf("%s", urban_area)
  
  writeOGR(urban_poly, output_folder, output_name, driver="ESRI Shapefile")
}

