Why Fake Data?
One of the more interesting bits to the project is being able to generate fake GPS tracks to feed to the API. We need to be able to demonstrate the real-time aspects of the solution without having to have someone drive around and collect the data. Also it would be nice be able to generate tracks based on who we are demoing to. Perhaps a simulated drive from the iVision office to a clients office.So, the goal is to be able to generate fake GPS tracks that we can feed to the iGOR application while demoing the real-time updating of the maps. We also need to be able to generate these tracks from any two points, and they should follow the streets and roads available in a logical way.
Google driving directions seems like a good place to start, and I found a great tool to help. GPS Babel is a free application that can take the data generated from Google Maps (and many other sources) and generate a standard GPX file.
So we want a page in our application that looks something like this:
We will have the user enter their start and destination addresses into Google Maps hosted in a browser control. They will then copy the link generated from the directions into the text box and press the Go button.
This will save the resulting HTML to a file that can be picked up by GPS Babel and converted into GPX. A lot more straight forward than it sounds, I promise.
First thing to do is download/install GPS Babel (follow the link above). Make a note of the location you install it as you will need it later. Now in our web browser control we can set our url to http://map.google.com.
Now lets wire up our Go button event like this:
private void cmdGo_Click(object sender, EventArgs e)
{
try
{
_isBuilding = true;
var newURI = new Uri(txtTrackURI.Text + "&output=js");
webBrowser.ScriptErrorsSuppressed = true;
webBrowser.Url = newURI;
}
catch (Exception)
{
MessageBox.Show("Link does not appear to be valid. Please chech the track uri and try again.",
"Invalid Track URI", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
_isBuilding = false;
}
}
As you can see we set a form level bool to true (I will come back to this later), take the text from the textbox and append a query parameter. This parameter tells Google to output the resulting page in a special format that can be imported by GPS Babel. We then update the web browser controls uri to this new location. Lets take a look at the Document Complete event of our control:
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (_isBuilding)
{
var gpsBabelLocation = Properties.Settings.Default.GPSBabelLocation + @"\gpsbabel.exe";
var tempFolder = Properties.Settings.Default.LocalTempFolder;
var localMapFilePath = tempFolder + @"\map.txt";
var localGPXFilePath = tempFolder + @"\gpx_data.gpx";
var fs = new FileStream(localMapFilePath, FileMode.Create, FileAccess.Write, FileShare.None);
var file = new StreamWriter(fs, Encoding.ASCII);
file.WriteLine(webBrowser.DocumentText);
file.Close();
var p = new Process
{
StartInfo =
{
FileName = gpsBabelLocation,
Arguments = @"-t -i google -f " + localMapFilePath + @" -o gpx,suppresswhite=0,logpoint=0,humminbirdextensions=0,garminextensions=0 -F " + localGPXFilePath,
UseShellExecute = false
}
};
p.Start();
p.WaitForExit();
TextReader tr = new StreamReader(localGPXFilePath);
var viewer = new GPXViewer { Doc = tr.ReadToEnd() };
tr.Close();
viewer.ShowDialog();
webBrowser.GoBack();
txtTrackURI.Text = null;
}
_isBuilding = false;
}
Ah, there is that _isBuilding again. As you can see we are using it to control whether or not we process the web controls contents on document complete. As the user is generating their track, this event gets fired several times and we only want to process it when the user has hit the Go button.
The first thing we do is load up the location of the GPS Babel exe and the location of a temp folder (these are stored via the Settings tab which I will cover in a different post). We also set up the location of the map.txt file which is the input into GPS Babel, and gpx_data.gpx which is the output from the conversion process.
Next we save the contents of the web browser control to our input file (map.txt). Now we create a new Process object to kick off the GPS Babel exe. We give it the exe location and a set of command line arguments to control how GPS Babel executes and where to store the output. Once the conversion process is complete we read in the results from the gpx_data.gpx file into a string and pass it to another form to display the output.
My next post will cover the gpx viewer form and saving our file to Azure cloud storage for later retrieval.

No comments:
Post a Comment