data-raw/convert.R

#-----------------------------------------------------------------------------
# Convert the raw shapefiles into R spatial objects with the required
# co-ordinate reference system
#
# epsg:3113 projection for brisbane region
# http://www.bom.gov.au/water/wdtf/documentation/schema-control-lists/srs-name-wdtf03.htm
# http://spatialreference.org/ref/epsg/3113/
#-----------------------------------------------------------------------------
rm(list=ls())
library(rgdal)
library(dplyr)
library(magrittr)
library(tidyr)
library(pryr)

exdir <- "tmp"


#' IDs of a SpatialLinesDataFrame (spldf) should be
#'  - from 0..rows-1
#'  - should be sequential
get_IDs <- function(spldf) {
    rows  <- nrow(spldf@data)
    lines <- spldf@lines
    IDs   <- as.numeric(sapply(lines, FUN=function(x) {x@ID}))
    stopifnot(min(IDs)==0)
    stopifnot(max(IDs)==(rows-1))
    stopifnot(length(unique(diff(IDs))) == 1) # sequential integers
    IDs
}


#-----------------------------------------------------------------------------
# 20160811
#-----------------------------------------------------------------------------
# Load the data
unzip("20160811/AIMBIKEWAYSECTIONS2016.zip", exdir=exdir, overwrite=TRUE)
null_geoms   <- rgdal::readOGR(exdir, "AIM_BIKEWAY_SECTIONS_2016", dropNULLGeometries = FALSE)
bikeways2016 <- rgdal::readOGR(exdir, "AIM_BIKEWAY_SECTIONS_2016", dropNULLGeometries = TRUE )
bikeways2016 <- spTransform(bikeways2016, CRS("+proj=longlat +init=epsg:3113"))

# keep a standard set of columns
bikeways2016@data %<>% select(plant_num  = PLANT_STRU,
                              type       = TRAFFIC_TY,
                              material   = SECTION_TY,
                              location   = LOCATION,
                              road_pos   = ON_OFF_ROA,
                              path_width = WIDTH,
                              suburb     = SUBURB,
                              owner      = OWNER,
                              length     = Shape_Leng,
                              name       = BIKEWAY_NA)

# Fix plant_nums to be integers and bike path names to be clean factor
bikeways2016@data %<>% mutate(
    plant_num = tidyr::extract_numeric(as.character(plant_num)) %>% as.integer,
    name      = as.character(name),
    name      = ifelse(name=='0', NA, name),
    name      = as.factor(name)
)

# row names of @data should match the IDs in the @lines
rownames(bikeways2016@data) <- vapply(bikeways2016@lines, FUN=function(x) {x@ID}, FUN.VALUE = character(1))

save(bikeways2016, file="../data/bikeways2016.rda", compress="xz")

# Clean up
file.remove(list.files(exdir, full.names = TRUE), exdir)


#-----------------------------------------------------------------------------
# 2015
#-----------------------------------------------------------------------------
# Load the data
unzip("20150624/AIMBIKEWAYSECTIONS2015.zip", exdir=exdir, overwrite=TRUE)
bikeways2015 <- rgdal::readOGR(exdir, "AIM_BIKEWAY_SECTIONS_2015")
bikeways2015 <- spTransform(bikeways2015, CRS("+proj=longlat +init=epsg:3113"))

# keep a standard set of columns
bikeways2015@data %<>% select(plant_num  = PLANT_NUM,
                              type       = TRAFFIC,
                              material   = MATERIAL,
                              location   = LOCATION,
                              road_pos   = BK_ON_OFF,
                              path_width = PATH_WIDTH,
                              suburb     = SUBURB,
                              owner      = OWNER,
                              length     = Shape_Leng,
                              name       = BK_NAME)

# Fix plant_nums to be integers and bike path names to be clean factor
bikeways2015@data %<>% mutate(
    plant_num = tidyr::extract_numeric(as.character(plant_num)),
    name      = as.character(name),
    name      = ifelse(name=='0', NA, name),
    name      = as.factor(name)
)

# row names of @data should match the IDs in the @lines
rownames(bikeways2015@data) <- get_IDs(bikeways2015)

save(bikeways2015, file="../data/bikeways2015.rda", compress="xz")

# Clean up
file.remove(list.files(exdir, full.names = TRUE), exdir)


#-----------------------------------------------------------------------------
# 20140704
#-----------------------------------------------------------------------------
# Load the data
unzip("20140704/dataset_aim_bikeway_sections.zip", exdir=exdir, overwrite=TRUE)
bikeways2014 <- rgdal::readOGR(exdir, "AIM_BIKEWAY_SECTIONS")
bikeways2014 <- spTransform(bikeways2014, CRS("+proj=longlat +init=epsg:3113"))

# keep a standard set of columns
bikeways2014@data %<>% select(plant_num   = PLANT_NUM,
                               type       = TRAFFIC,
                               material   = SECTION,
                               location   = LOCATION,
                               road_pos   = BK_ON_OFF,
                               path_width = PATH_WIDTH,
                               suburb     = SUBURB,
                               owner      = OWNER,
                               length     = Shape_Leng,
                               name       = ROUTE_NAME)

# Fix plant_nums to be integers and bike path names to be clean factor
bikeways2014@data %<>% mutate(
    # change plant_num to an integer
    plant_num = as.character(plant_num),
    plant_num = ifelse(plant_num=='BK9028S153'   , 'BK9028S00153', plant_num),
    plant_num = ifelse(plant_num=='BK9028S0035'  , 'BK9028S00035', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000238', 'BK9070S00238', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000237', 'BK9070S00237', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000236', 'BK9070S00236', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000232', 'BK9070S00232', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000231', 'BK9070S00231', plant_num),
    plant_num = as.integer(tidyr::extract_numeric(plant_num)),
    # clean-up the named bikeways
    name      = as.character(name),
    name      = ifelse(name=='0', NA, name),
    name      = as.factor(name)
)

# Clean up some bad naming. "On ROAD" => "ON ROAD"
bikeways2014@data %<>% mutate(
    road_pos = as.character(road_pos),
    road_pos = ifelse(road_pos=='On ROAD', 'ON ROAD', road_pos),
    road_pos = as.factor(road_pos)
)


# row names of @data should match the IDs in the @lines
rownames(bikeways2014@data) <- get_IDs(bikeways2014)

save(bikeways2014, file="../data/bikeways2014.rda", compress="xz")

# cleanup
file.remove(list.files(exdir, full.names = TRUE), exdir)


#-----------------------------------------------------------------------------
# 20140129
# This bikeway data is lacking a lot of the metadata
#-----------------------------------------------------------------------------
# Load the data
unzip("20140129/AIM_BIKEWAY_SECTIONS.zip", exdir=exdir, overwrite=TRUE)
bikeways2014a <- rgdal::readOGR(file.path(exdir, "SHP"), "AIM_BIKEWAY_SECTIONS")
bikeways2014a <- spTransform(bikeways2014a, CRS("+proj=longlat +init=epsg:3113"))

# keep a standard set of columns
bikeways2014a@data %<>% select(plant_num  = SECTION_ID,
                               road_pos   = ON_OFF,
                               suburb     = SUBURB,
                               name       = LINK_NAME)

# Fix plant_nums to be integers and bike path names to be clean factor
bikeways2014a@data %<>% mutate(
    plant_num = as.character(plant_num),
    plant_num = ifelse(plant_num=='BK9038'       , 'BK9038S00000', plant_num),
    plant_num = ifelse(plant_num=='BK9067'       , 'BK9067S00000', plant_num),
    plant_num = ifelse(plant_num=='BK9028S153'   , 'BK9028S00153', plant_num),
    plant_num = ifelse(plant_num=='BK9028S0035'  , 'BK9028S00035', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000238', 'BK9070S00238', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000237', 'BK9070S00237', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000236', 'BK9070S00236', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000232', 'BK9070S00232', plant_num),
    plant_num = ifelse(plant_num=='BK9070S000231', 'BK9070S00231', plant_num),
    plant_num = as.integer(tidyr::extract_numeric(plant_num)),
    name      = as.character(name),
    name      = ifelse(name=='0', NA, name),
    name      = as.factor(name)
)

# Clean up some bad naming. "On ROAD" => "ON ROAD"
bikeways2014a@data %<>% mutate(
    road_pos = as.character(road_pos),
    road_pos = ifelse(road_pos=='On ROAD', 'ON ROAD', road_pos),
    road_pos = as.factor(road_pos)
)

# row names of @data should match the IDs in the @lines
rownames(bikeways2014a@data) <- get_IDs(bikeways2014a)

save(bikeways2014a, file="../data/bikeways2014a.rda", compress="xz")

# cleanup
file.remove(list.files(file.path(exdir, "SHP"), full.names = TRUE), file.path(exdir, "SHP"), exdir)


#-----------------------------------------------------------------------------
# 20111006
#-----------------------------------------------------------------------------
# Load the data
unzip("20111006/tmpez2sPi044243.zip", exdir=exdir, overwrite=TRUE)
bikeways2011 <- rgdal::readOGR(file.path(exdir, "BIKEWAYS Shapefiles"), "TRN_BIKEWAY_SECTION_LL")
bikeways2011 <- spTransform(bikeways2011, CRS("+proj=longlat +init=epsg:3113"))

# keep a standard set of columns
bikeways2011@data %<>% select(plant_num  = PLANT_STRU,
                              type       = TRAFFIC__1,
                              material   = SECTION__1,
                              location   = LOCATIONS1,
                              road_pos   = BIKEWAY__1,
                              path_width = BIKEWAY_WI,
                              suburb     = SUBURB,
                              owner      = OWNER,
                              length     = SHAPE_LEN)

# Fix plant_nums to be integers and bike path names to be clean factor
bikeways2011@data %<>% mutate(
    plant_num = as.character(plant_num),
    plant_num = ifelse(plant_num=='BK9028S153' , 'BK9028S00153', plant_num),
    plant_num = ifelse(plant_num=='BK9028S0035', 'BK9028S00035', plant_num),
    plant_num = tidyr::extract_numeric(plant_num)
)

# row names of @data should match the IDs in the @lines
rownames(bikeways2011@data) <- get_IDs(bikeways2011)

save(bikeways2011, file="../data/bikeways2011.rda", compress="xz")

# cleanup
file.remove(list.files(file.path(exdir, "BIKEWAYS Shapefiles"), full.names = TRUE), file.path(exdir, "BIKEWAYS Shapefiles"), exdir)



if (FALSE) {
    library(leaflet)
    library(dplyr)

    bikeways <- bikeways2015 ; dim(bikeways); leaflet(bikeways[bikeways$SUBURB=='BRISBANE CITY',]) %>% addTiles() %>% addPolylines()
    bikeways <- bikeways2014; dim(bikeways); leaflet(bikeways[bikeways$SUBURB=='BRISBANE CITY',]) %>% addTiles() %>% addPolylines()
    bikeways <- bikeways2014a; dim(bikeways); leaflet(bikeways[bikeways$SUBURB=='BRISBANE CITY',]) %>% addTiles() %>% addPolylines()
    bikeways <- bikeways2011 ; dim(bikeways); leaflet(bikeways[bikeways$SUBURB=='BRISBANE CITY',]) %>% addTiles() %>% addPolylines()

}
coolbutuseless/BrisbaneBikeways documentation built on May 12, 2017, 9:46 a.m.