Skip to content

Commit

Permalink
install fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hodge committed Jul 9, 2019
1 parent 3dd5808 commit cf34717
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 377 deletions.
1 change: 1 addition & 0 deletions propeR/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ rsconnect/
output/
#ignore otp
otp/
ignore/
#html files
GUIDE.html
README.html
Expand Down
5 changes: 1 addition & 4 deletions propeR/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(choropleth)
export(cleanGTFS)
export(importGeojsonData)
export(importLocationData)
export(isochrone)
export(isochroneMulti)
export(isochroneMultiIntersect)
export(isochroneMultiIntersectSensitivity)
export(isochroneMultiIntersectTime)
export(isochroneTime)
export(locationValidatorIsochrone)
export(nominatimNodeSearch)
Expand All @@ -23,6 +19,7 @@ export(pointToPoint)
export(pointToPointLoop)
export(pointToPointNearest)
export(pointToPointTime)
export(postcodeComplete)
export(postcodeToDecimalDegrees)
export(postcodeToDecimalDegrees_backup)
export(stopFIX)
113 changes: 100 additions & 13 deletions propeR/R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
##' @param loncol The title of the header of the column containing the longitudinal values, default is "lon"
##' @param latcol The title of the header of the column containing the latitudinal values, default is "lat"
##' @param postcodecol The title of the header of the column containing the postcode values, default is "postcode"
##' @param deleteRow Specify whether to delete the row if no postcode is found, defaults to T
##' @param autoComplete Specify whether to use autocomplete if postcode is not valid
##' @return R dataframe of location points
##' @author Michael Hodge
##' @examples originPoints <- importLocationData('C:\Users\User\Documents\origins.csv', idcol = "name", loncol = "lon", latcol = "lat")
Expand All @@ -15,7 +17,9 @@ importLocationData <- function(src,
idcol = "name",
loncol = "lon",
latcol = "lat",
postcodecol = "postcode") {
postcodecol = "postcode",
deleteRow = T,
autoComplete = F) {

data_points <-
read.csv(src, sep = ",", as.is = TRUE) # Opens file box
Expand All @@ -27,6 +31,27 @@ importLocationData <- function(src,

data_points$name <- as.character(data_points$name)

rows_to_delete = c()

postcodeAutoComplete <- function(postcode, num_char) {

lat_lon = c()
new_postcode_content <- propeR::postcodeComplete(substr(postcode, 1, nchar(postcode)-num_char))
new_postcode <- new_postcode_content$result[[1]]
new_postcode <- gsub('\\s+', '', new_postcode)

pc_content <- propeR::postcodeToDecimalDegrees(new_postcode)
if (pc_content$status == 'no_match' || is.null(pc_content$data$latitude) || pc_content$status == '404') {
pc_content <- propeR::postcodeToDecimalDegrees_backup(new_postcode)
lat_lon <- pc_content$result$latitude
lat_lon <- cbind(lat_lon,pc_content$result$longitude)
} else {
lat_lon <- as.double(pc_content$data$latitude)
lat_lon <- cbind(lat_lon,as.double(pc_content$data$longitude))
}
lat_lon
}

if ("lat" %in% colnames(data_points))
{
if (!("lon" %in% colnames(data_points))) {
Expand Down Expand Up @@ -62,32 +87,85 @@ importLocationData <- function(src,
the location will be removed from the list.\n"
)

data_points$postcode <- gsub('\\s+', '', data_points$postcode)
pb <- progress::progress_bar$new(
format = " Postcode to lat and lon conversion complete for call :what [:bar] :percent eta: :eta",
total = nrow(data_points), clear = FALSE, width= 100)

for (i in 1:nrow(data_points)) {

data_points$postcode[i] <- gsub('\\s+', '', data_points$postcode[i])

pc_content <-
propeR::postcodeToDecimalDegrees(data_points$postcode[i])
if (pc_content$status == 'no_match') {
pc_content <-
propeR::postcodeToDecimalDegrees_backup(data_points$postcode[i])
if (pc_content$status == '404') {
warning(
"Warning: Postcode ",
data_points$postcode[i],
" for location ",
data_points$name[i],
" cannot be convert to a latitude and longitude.
This location shall be removed from the list.\n"
)
data_points <- data_points[-i,]

if(autoComplete == T){
lat_lon <- postcodeAutoComplete(data_points$postcode[i], 1)
if (is.null(lat_lon[1])){
lat_lon <- lat_lon <- postcodeAutoComplete(data_points$postcode[i], 2)
if (is.null(lat_lon[1])){
lat_lon <- lat_lon <- postcodeAutoComplete(data_points$postcode[i], 3)
if (is.null(lat_lon[1])){
rows_to_delete = rbind(i,rows_to_delete)
data_points$lat[i] <- 'NA'
data_points$lon[i] <- 'NA'
} else {
data_points$lat[i] <- lat_lon[1]
data_points$lon[i] <- lat_lon[2]
}
} else {
data_points$lat[i] <- lat_lon[1]
data_points$lon[i] <- lat_lon[2]
}
} else {
data_points$lat[i] <- lat_lon[1]
data_points$lon[i] <- lat_lon[2]
}
} else {
rows_to_delete = rbind(i,rows_to_delete)
data_points$lat[i] <- 'NA'
data_points$lon[i] <- 'NA'
}
} else {
data_points$lat[i] <- pc_content$result$latitude
data_points$lon[i] <- pc_content$result$longitude
}
} else {
data_points$lat[i] <- as.double(pc_content$data$latitude)
data_points$lon[i] <- as.double(pc_content$data$longitude)
if (is.null(pc_content$data$latitude) || is.null(pc_content$data$longitude)){

if(autoComplete == T){
lat_lon <- postcodeAutoComplete(data_points$postcode[i], 1)
if (is.null(lat_lon[1])){
lat_lon <- lat_lon <- postcodeAutoComplete(data_points$postcode[i], 2)
if (is.null(lat_lon[1])){
lat_lon <- lat_lon <- postcodeAutoComplete(data_points$postcode[i], 3)
if (is.null(lat_lon[1])){
rows_to_delete = rbind(i,rows_to_delete)
data_points$lat[i] <- 'NA'
data_points$lon[i] <- 'NA'
} else {
data_points$lat[i] <- lat_lon[1]
data_points$lon[i] <- lat_lon[2]
}
} else {
data_points$lat[i] <- lat_lon[1]
data_points$lon[i] <- lat_lon[2]
}
} else {
data_points$lat[i] <- lat_lon[1]
data_points$lon[i] <- lat_lon[2]
}
}

} else {
data_points$lat[i] <- as.double(pc_content$data$latitude)
data_points$lon[i] <- as.double(pc_content$data$longitude)
}
}
pb$tick(tokens = list(what = i))
}
} else {
stop(
Expand All @@ -99,12 +177,21 @@ importLocationData <- function(src,
)
)
}

}



# data_points <-
# data_points[order(data_points$name),] # Sort by name
data_points <-
as.data.frame(data_points) # Converts to dataframes and create lat, lon field needed by otp

if (deleteRow == T && length(rows_to_delete) > 0){
message(paste0('Number of rows removed from ', length(data_points), ' was ',length(rows_to_delete)))
data_points <- data_points[-rows_to_delete,]
}

data_points$lat_lon <-
with(data_points, paste0(lat, ",", lon)) # Adds a lat_lon column as needed by otp
data_points
Expand Down
16 changes: 15 additions & 1 deletion propeR/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ postcodeToDecimalDegrees <- function(postcode) {
postcodeToDecimalDegrees_backup <- function(postcode) {
r <-
httr::GET(paste0("http://api.postcodes.io/postcodes/", postcode))
httr::warn_for_status(r)
httr::content(r)
}

##' Postcode autocomplete
##'
##' Generates a postcode from a partial postcode
##'
##' @param postcode UK postcode
##' @return The latitude and longitude in decimal degrees.
##' @author Michael Hodge
##' @examples postcodeToDecimalDegrees_backup('NP10 8XG')
##' @export
postcodeComplete <- function(postcode) {
r <-
httr::GET(paste0("http://api.postcodes.io/postcodes/", postcode,'/autocomplete'))
httr::content(r)
}

Expand Down
92 changes: 0 additions & 92 deletions propeR/man/choropleth.Rd

This file was deleted.

7 changes: 6 additions & 1 deletion propeR/man/importLocationData.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf34717

Please sign in to comment.