Jump to content
We've recently updated our Privacy Statement, available here. ×
  • Travel Distance Matrix Data Function for Spotfire


    This data function calculates driving distances between a specific Earth point and all points in a data table, providing travel time and distance based on transportation infrastructure

    Introduction

    This data function is a geospatial function to calculate the driving distances between a single point on Earth to all points in a data table. Latitude and longitude data must be provided for the originating point, as well as the destination points in the data table.

    The data function provides travel time and travel distance based on transportation infrastructures such as roads and ferries and is not a simple distance calculation as the crow flies. The user needs to have access to Spotfire Geoanalytics Builder REST API Service and Spotfire either through trial or license.

    Data Function

    # [TERR] Driving Distance Matrix
    # Neil Kanungo, 2020
    # TIBCO Software
    
    # Description:
    #   Get the driving distance and times from the central lat/long point to all points
    #   in another data table
    
    # Inputs:
    #   start.lon - Longitude column of the Trade Area Center data table
    #   start.lat - Latitude column of the Trade Area Center data table
    #   destinations.df - Data Table with all hospital info
    
    # Outputs:
    #   output.df - Distance matrix data from Trade Area Center to all hospitals within it
    
    
    #----------------------------------------------
    
    # Load libraries
    suppressMessages(suppressWarnings(library(jsonlite,warn.conflicts=FALSE,quietly=TRUE)))
    
    # Set API Key (password) for running this data function with TIBCO Geoanalytics Builder
    api.key <-"ENTER YOUR API KEY HERE" #contact Arnaud Varin (avarin@tibco.com) for a key
    
    # Build Destination lat/long table
    destinations.df <- cbind(destinations.lon, destinations.lat)
    
    # Get starting coords
    start.lon <- round(start.lon[1],2)
    start.lat <- round(start.lat[1],2)
    
    # Create null Segments Data Frame
    mySegments <- data.frame(
      Number=integer(),
      StartName=character(),
      EndName=character(),
      DrivingDistance.Km=character(),
      DrivingDistance.Meters=integer(),
      DrivingDistance.Miles=character(),
      DrivingDistance.Feet=integer(),
      DrivingTime.hh_mm=character(),
      DrivingTime.seconds=integer(),
      StartLocation.Latitude=double(),
      StartLocation.Longitude=double(),
      EndLocation.Latitude=double(),
      EndLocation.Longitude=double()
    )
    
    
    if(nrow(destinations.df)<1){
      output.df <- cbind(destinations.df,mySegments)
    } else {
    
    # Set REST API Request Parameters
    start.point <- paste0(start.lon,",",start.lat)
    start.name <- "MyCenter"
    
      
    # Determine number of REST API calls needed (max 15 waypoints per call)
    max.waypoints <- 15
    iterations <- ceiling(nrow(destinations.df)/max.waypoints)
    
    for (iter in 1:iterations){
        
        i.start <- iter*max.waypoints - max.waypoints + 1
        i.end <- iter*max.waypoints
        
        if(nrow(destinations.df)<i.end){
          i.end<-nrow(destinations.df)
        }
        
        waypoint.string <- ""
        for (i in i.start:i.end){
          print(i)
          this.string <- paste0("n:point",i,";ll:",destinations.df$HQ_LONGITUDE[i],",",destinations.df$HQ_LATITUDE[i],"|")
          waypoint.string <- paste0(waypoint.string,this.string)
        }
        waypoint.string <- substr(waypoint.string,1,nchar(waypoint.string)-1) #trim off last character
        
        
        #Construct API Request url for Trade Area
        myURL<-paste0(
          "https://geowebservices.maporama.com/itinerary/distance/onetomany.json", #base url
          "?maporamakey=",api.key, #parameter for API Key
          "&startname=",start.name,
          "&startpoint=",start.point,
          "&travelmode=","vehicle",
          "&unitsystem=","metric",
          "&avoidtollroad=","false",
          "&avoidhighways=","false",
          "&waypoints=",waypoint.string
        )
        
        # Make API Call
        api.result <- fromJSON(txt=myURL,flatten=TRUE)
        
        # Keep segments part of results list
        mySegments <- rbind(mySegments,api.result$Segments)
    }
    
    # Bind segments to destinations and output
    output.df <- cbind(destinations.df,mySegments)
    
    }
     

    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...