EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

R - Calculate Distance & Travel duration (via Google Maps)


Henri-Francois Chadeisson

Director, Sales Engineering • MicroStrategy


Ever wondered how to calculate the distance or travel duration from point A to point B with MicroStrategy ? It's possible with our R integration ! Here is a quick tutorial to implement this in a Cloud environment.

Ever wondered how to calculate the distance or travel duration from point A to point B with Strategy ?
It's possible with our R integration ! Here is a quick tutorial to implement this in a Cloud environment.
 
HowTo
Connect remotely to your server (in this example, Linux, would work for Windows aswell)
Open a SSH console via Putty
Start the R console by typing :           sudo R
Install Google Map package :             install.packages("ggmap")
Select your mirror by entering the ID
Wait for the ggmap installation (dozens of seconds, up to a few minutes)
Load Google Map package in R :       library(ggmap)
Run a test in R Console :                     mapdist("75015, France", "92400, France", mode="driving")

ka02R000000g1KfQAI_0EM440000002Lc6.png

​
This retrieves the the distance and real-time driving time
 
Now, move the attached Rscripts (from "ggmaps.zip") into /opt/mstr/R_Integration_Pack/Rscripts
Or create them from this code, here "MapMinute.R" file


#Strategy_BEGIN
#
#RVAR from -input -string -scalar
#RVAR to -input -string -scalar
#
#RVAR res -output -numeric -scalar  #Metric Expression: RScriptSimple<_RScriptFile="D:\Management\02 Knowledge Sharing\2015_0520 R Integration\Sample R Script\01 Simple\MapDist.R", _InputNames="from, to">(from, to)
if(exists("mstr.WorkingDir")) setwd(mstr.WorkingDir)  #Working Directory if executed by Strategy
#
#Strategy_END

if(!require("ggmap")){ 
  install.packages("ggmap",repos="http://cran.rstudio.com")
  library(ggmap)
} 
list <- mapdist(from, to,mode="driving") 
res <- list$minutes

 

Then in MSTR, use the same function : RScriptSimple<_RScriptFile="MapMinute.R">(From, To)

ka02R000000g1KfQAI_0EM440000002LcB.png

From & To must be valid metrics, with Text format.​​​
  
Next Steps & Thoughts
You can also calculate such metric in a iCube, or in a Datamart Report if you want to store this in the WH.
Also, I think there is a limit of 2500 requests a day, to be considered for large scale demos or POCs
 
ps: Thanks to Madrid, Carlos & Hani Soewandi for the idea & assistance setting this up
Additional script samples
MapKm.R


#Strategy_BEGIN
#
#RVAR from -input -string -scalar
#RVAR to -input -string -scalar
#
#RVAR res -output -numeric -scalar  #Metric Expression: RScriptSimple<_RScriptFile="D:\Management\02 Knowledge Sharing\2015_0520 R Integration\Sample R Script\01 Simple\MapDist.R", _InputNames="from, to">(from, to)
if(exists("mstr.WorkingDir")) setwd(mstr.WorkingDir)  #Working Directory if executed by Strategy
#
#Strategy_END

if(!require("ggmap")){ 
  install.packages("ggmap",repos="http://cran.rstudio.com")
  library(ggmap)
} 
list <- mapdist(from, to,mode="driving") 
res <- list$km

MapDist.R

#Strategy_BEGIN
#
#RVAR from -input -string -scalar
#RVAR to -input -string -scalar
#
#RVAR res -output -numeric -scalar  #Metric Expression: RScriptSimple<_RScriptFile="D:\Management\02 Knowledge Sharing\2015_0520 R Integration\Sample R Script\01 Simple\MapDist.R", _InputNames="from, to">(from, to)
if(exists("mstr.WorkingDir")) setwd(mstr.WorkingDir)  #Working Directory if executed by Strategy
#
#Strategy_END

if(!require("ggmap")){ 
  install.packages("ggmap",repos="http://cran.rstudio.com")
  library(ggmap)
} 
list <- mapdist(from, to,mode="driving") 
res <- list$miles

MapHour.R

#Strategy_BEGIN
#
#RVAR from -input -string -scalar
#RVAR to -input -string -scalar
#
#RVAR res -output -numeric -scalar  #Metric Expression: RScriptSimple<_RScriptFile="D:\Management\02 Knowledge Sharing\2015_0520 R Integration\Sample R Script\01 Simple\MapDist.R", _InputNames="from, to">(from, to)
if(exists("mstr.WorkingDir")) setwd(mstr.WorkingDir)  #Working Directory if executed by Strategy
#
#Strategy_END

if(!require("ggmap")){ 
  install.packages("ggmap",repos="http://cran.rstudio.com")
  library(ggmap)
} 
list <- mapdist(from, to,mode="driving") 
res <- list$hour

 


Comment

0 comments

Details

Knowledge Article

Published:

May 13, 2017

Last Updated:

May 13, 2017