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
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
server = ""
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)
stream.Seek(0, SeekOrigin.Begin)

# set up the text data reader
readerSettings = TextDataReaderSettings()
readerSettings.Separator = "\t"
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)
     newTable = Document.Data.Tables.Add("Route Points", textDataSource)
     tableSettings = DataTableSaveSettings (newTable, False, False)

No comments: