Customer Banners (Ads) - SpiceUp. AX and SpotfireX Disclaimer



If you find this site useful and you want to support, buy me a coffee   to keep this site alive and without ads.

Shortest path in road using osrm web service

Simple script to find data points along route using OSRM web service

This post is based on a previous one with some tweaks on the code to call the OSRM web service



import clr
clr.AddReference('System.Data')
clr.AddReference('System.Web.Extensions')
import System
from System import DateTime
from System.Data import DataSet, DataTable
from System.IO import StreamReader, StreamWriter, MemoryStream, SeekOrigin
from System.Net import HttpWebRequest,HttpWebResponse
from System.Web.Script.Serialization import JavaScriptSerializer
from Spotfire.Dxp.Data import DataType, DataTableSaveSettings
from Spotfire.Dxp.Data.Import import TextFileDataSource, TextDataReaderSettings

#input parameters
#prepare query string
lat1=str(lat1)
lon1=str(lon1)
lat2=str(lat2)
lon2=str(lon2)
server = "http://router.project-osrm.org"
service = "viaroute"

#query string uri
from System import DateTime
now = DateTime.Now

params = "loc="+lat1+","+lon1+"&loc="+lat2+","+lon2+"&instructions=false&compression=false&geometry=true"
uri = server + "/"+service+"?" + params 
print uri

#get data from web service
webRequest = HttpWebRequest.Create(uri)
response = webRequest.GetResponse()


##automatic proxy settings
#webRequest.Proxy = HttpWebRequest.DefaultWebProxy
#webRequest.Credentials = CredentialCache.DefaultCredentials

#webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials

##manual proxy settings (usually from webplayer)
#webRequest.Proxy = HttpWebRequest.DefaultWebProxy #webRequest.Credentials = CredentialCache.DefaultCredentials #webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials


streamReader = StreamReader(response.GetResponseStream())
jsonData = streamReader.ReadToEnd()
js = JavaScriptSerializer()
dataDict = js.Deserialize(jsonData,object)
totalDistance = dataDict["route_summary"]["total_distance"]
totalTime = dataDict["route_summary"]["total_time"]

print "\n\n\n", totalDistance, totalTime
Document.Properties["totalDistance"] = float(totalDistance)
Document.Properties["totalTime"] = float(totalTime)


#format text data
textData = "lat\tlon\r\n" 
for routePoints in dataDict["route_geometry"]:
     textData += str(routePoints[0]) + "\t" + str(routePoints[1]) + "\r\n"


print "\n\n\n", textData


#pass text data to spotfire table

# make a stream from the string
stream = MemoryStream()
writer = StreamWriter(stream)
writer.Write(textData)
writer.Flush()
stream.Seek(0, SeekOrigin.Begin)

# set up the text data reader
readerSettings = TextDataReaderSettings()
readerSettings.Separator = "\t"
readerSettings.AddColumnNameRow(0)
readerSettings.SetDataType(0, DataType.Real)
readerSettings.SetDataType(1, DataType.Real)

# create a data source to read in the stream
textDataSource = TextFileDataSource(stream, readerSettings)

# add the data into a Data Table in Spotfire
if Document.Data.Tables.Contains("Route Points"):
     Document.Data.Tables["Route Points"].ReplaceData(textDataSource)
else:
     newTable = Document.Data.Tables.Add("Route Points", textDataSource)
     tableSettings = DataTableSaveSettings (newTable, False, False)
     Document.Data.SaveSettings.DataTableSettings.Add(tableSettings)





No comments: