Giter Site home page Giter Site logo

chrisjb / rdistancematrix Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 116.58 MB

Builds isochrones using either the google distance matrix API or the Mapbox API. Contains functions to fetch the population and employment (England and Wales) within the isochrone.

License: Other

R 100.00%
distance-matrix-api isochrone r r-package uk

rdistancematrix's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

rdistancematrix's Issues

Datetime format on departing time

I do not have much experience on handling datatime data. However, my understanding is that as.POSIXct() will convert the time to unix time based on the time zone of the local computer (same as Sys.timezone()). Thus the output time in my computer is in CST (UTC+8), yet not local UK time (UTC+0) and the stop message looks misleading to me.

# enter departing in form: YYYY-MM-DD 08:00:00
if(departing != F & !grepl('[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2,}',departing)) {
stop(paste0('departure time must either be set to FALSE or a time in the format YYYY-MM-DD HH:MM:SS. Note the time is local UK time.'))
} else if(departing != F) {
# if departure time is set, set it to a time in the future (later today or tomorrow depending on whether or not the time has already passed today.
# has the entered time already passed today?
if(Sys.time() > as.POSIXct(departing)) {
stop(paste0('your departure time ', departing, 'is not in the future.'))
} else {
departure_time <- paste0('&departure_time=',as.character(as.numeric(as.POSIXct(departing))),'&traffic_model=', model)
}
} else if(departing == F) {
departure_time <- ''
}

Would it be better to require users to input the timezone? We could still, by default, assume the area of analysis is in the same time zone with the computer. (Would it also be better to use the lubridate package to handle time?)

departing_unix_time <- as.numeric(ymd_hms(departing, tz = Sys.timezone()))

departure_time <- paste0("&departure_time=", departing_unix_time , "&traffic_model=", model))

As a side note, instead of using grepl, strptime may be a better function to validate time input

Error in Distance Matrix Request URL

First of all, thanks for writing this package. I have been looking for scripts to generate isochrone maps with google map API for a long time.

When I tried to generate the isochrone polygon, the make_isochrone function fails. I checked the code and believe the error may be caused by the request URL to the google map Distance Matrix Service. Currently, it only allows 25 origins/destinations in a single request. (https://developers.google.com/maps/documentation/javascript/distancematrix)

But the request URL in distance_from_origins and distance_to_destinations is now requesting 100 points at one time. The functions return a list of NULL and thus make_isochrone fails to work. I am not sure why the line err <- httr::http_error(doc) does not throw an error though.

for(i in 1:(lots)){
origin_txt <- gsub(' ', "+", paste0(origin[(1+100*(i-1)):(100*i)][!is.na(origin[(1+100*(i-1)):(100*i)])],collapse='|'))
if(origin_txt == ''){
break
}
u <- dist_url(origin_txt,dest,mode,departing,model, api_key=api_key)

for(i in 1:(lots)){
# set up string for up to 100 destinations
dest_txt <- gsub(' ',"+",paste0(dest[(1+100*(i-1)):(100*i)][!is.na(dest[(1+100*(i-1)):(100*i)])],collapse='|'))
if(dest_txt == ''){
break
}
u <- dist_url(origin,dest_txt,mode,departing,model,api_key=api_key)

I changed the value 100 to 25 in my local machine and the functions work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.