/**
  * Update the current walk with passed in form data
  * This also handles GPX data
  */
 public function updateWalk(array $formData)
 {
     $this->loadWalk($formData['id']);
     // Update all basic fields
     // Fields that can't be saved are just ignored
     // Invalid fields throw an exception - display this to the user and continue
     foreach ($formData as $name => $value) {
         try {
             if ($name == "suggestedBy") {
                 $this->walk->suggestedBy = Leader::getLeader($value);
             } else {
                 $this->walk->{$name} = $value;
             }
         } catch (UnexpectedValueException $e) {
             echo "<p>";
             var_dump($name);
             var_dump($value);
             var_dump($e->getMessage());
             echo "</p>";
         }
     }
     // Handle the route file upload
     $file = JRequest::getVar('jform', array(), 'files', 'array');
     if (!empty($file) && $file['error']['route'] == UPLOAD_ERR_OK) {
         // We've been given a GPX file. Try to parse it.
         $gpx = DOMDocument::load($file['tmp_name']['route']);
         if ($gpx) {
             // Check for a GPX element at the root
             if ($gpx->getElementsByTagName("gpx")->length == 1) {
                 // Get the route ID if we have an existing route
                 if (isset($this->walk->route)) {
                     $routeID = $this->walk->route->id;
                 } else {
                     $routeID = null;
                 }
                 // TODO: Turn on or off overwriting of existing properties
                 if (isset($this->walk->route)) {
                     $route = $this->walk->route;
                 } else {
                     $route = new Route($this->walk);
                 }
                 $route->readGPX($gpx);
                 $route->uploadedBy = JFactory::getUser()->id;
                 $route->uploadedDateTime = time();
                 $this->walk->setRoute($route, true);
                 // Store this route for later requests
                 JFactory::getApplication()->setUserState("uploadedroute", serialize($route));
                 JFactory::getApplication()->setUserState("deleteroute", false);
             } else {
                 echo "Must have only one GPX tag";
             }
         } else {
             echo "Not a GPX file";
         }
         // Return to the form
     } else {
         // Restore previously uploaded file from state
         $route = unserialize(JFactory::getApplication()->getUserState("uploadedroute"));
         if ($route) {
             $route->setWalk($this->walk);
             $this->walk->setRoute($route);
         }
     }
     // If we have a route, allow the user to set options on it
     if (isset($this->walk->route)) {
         $this->walk->route->visibility = $formData['routeVisibility'];
     }
 }
<?php

define("_JEXEC", true);
define("JPATH_BASE", "/home/peter/swg/website/public_html");
require_once "../swg.php";
include_once "../Models/Walk.php";
$walk = new Walk();
$route = new Route($walk);
$route->readGPX(DOMDocument::loadXML(file_get_contents($argv[1])));
$walk->setRoute($route);
echo "Distance: " . $walk->miles . " miles (" . $walk->distanceGrade . ")\n";
if ($walk->isLinear) {
    echo "Linear";
} else {
    echo "Circular";
}
echo "\n";
echo "Start: " . $walk->startGridRef . " (" . $walk->startPlaceName . ")\n";
echo "End: " . $walk->endGridRef . " (" . $walk->endPlaceName . ")\n";
$route->save();