# Run this once to generate individual electorate shape files.  Does not handle Antony Green's KML files.

years <- c(2013, 2010, 2007, 2004)
file_str <- c("COM20111216_ELB_region", "CED_2011_AUST", "CED07aAUST_region", "CED04aAUST_region")
seat_var <- c("ELECT_DIV", "CED_NAME", "NAME_2007", "NAME_2004")

for (ct in 1:length(years)) {
  electorates <- readOGR("shapefiles", file_str[ct])
  seats <- unique(electorates[[seat_var[ct]]])
  for (seat in seats) {
    seat_poly <- electorates[which(electorates[[seat_var[ct]]] == seat), ]
    
    output_folder <- sprintf("shapefiles/seats/%s", seat)
    output_name <- sprintf("%d", years[ct])
    
    writeOGR(seat_poly, output_folder, output_name, driver="ESRI Shapefile")
  }
}
