Paste the contents of a .kml file below:

Unit: meters feet both (2 graphs)

$xml"; if (0 == preg_match("|(.*?)|s", $xml, $matches)) { $elv = "none"; } else { $elv = $matches[1]; if (elv > 1E+10) $elv = "max"; if (elv < -1E+10) $elv = "min"; } return $elv; } function getHtml($url) { // The following implementation is not adviced due to security concerns. //return file_get_contents($url); // For PHPs compiled with cURL support, this is the preferred method. $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($ch); curl_close ($ch); return $result; } $debug = 0; $kml = $_POST['kml']; $unit = $_POST['unit']; if (!$unit) $unit = "both"; // Must use 1 or 0 so that these vars can be use in JavaScript below. $doFeet = (strcasecmp($unit, "feet") == 0 || strcasecmp($unit, "both") == 0) ? 1 : 0; $doMeters = (strcasecmp($unit, "meters") == 0 || strcasecmp($unit, "both") == 0) ? 1 : 0; if ($debug) echo "

unit = $unit, doFeet = $doFeet, doMeters = $doMeters

"; $data = array(); $minElevation_m = 0; $minElevation_ft = 0; $maxElevation_m = 0; $maxElevation_ft = 0; $totalGain_m = 0; $totalGain_ft = 0; $totalLoss_m = 0; $totalLoss_ft = 0; if ($debug) echo "

" . htmlspecialchars('kml = ' . $kml) . "

"; // Processes \r\n's first so they aren't converted twice. $order = array("\r\n", "\n", "\r"); $kml = str_replace($order, "", $kml); if (0 == preg_match("|(.*?)|s", $kml, $matches)) { $title = ""; } else { $title = $matches[1]; $title = str_replace(".kml", "", $title); } if ($debug) echo "

title = $title

"; if (0 == preg_match_all("|(.*)|s", $kml, $matches)) { echo "No '<coordinates>' node found in kml file. Cannot generate graph."; echo ""; exit(-1); } else { for ($mm = 0; $mm < count($matches[0]); $mm++) { $coordinates = $matches[1][$mm] . " "; } if ($debug) echo "

" . htmlspecialchars('coordinates = ' . $coordinates) . "

"; $last_m = 0; $last_ft = 0; if ($debug) echo "

coordinates =
"; preg_match_all("|(.*?),(.*?),(.*?) |s", $coordinates, $matches); for ($mm = 0; $mm < count($matches[0]); $mm++) { $lat = $matches[1][$mm]; $long = $matches[2][$mm]; $kml_elv = $matches[3][$mm]; if ($doMeters) { $usgs_elv_m = getUsgsElevation($lat, $long, 0); } if ($doFeet) { $usgs_elv_ft = getUsgsElevation($lat, $long, 1); } if (0 == $mm) { // Init stats. if ($doMeters) { $minElevation_m = $usgs_elv_m; $maxElevation_m = $usgs_elv_m; $totalGain_m = 0; $totalLoss_m = 0; $last_m = $usgs_elv_m; } if ($doFeet) { $minElevation_ft = $usgs_elv_ft; $maxElevation_ft = $usgs_elv_ft; $totalGain_ft = 0; $totalLoss_ft = 0; $last_ft = $usgs_elv_ft; } } else { if ($doMeters) { if ($usgs_elv_m > $maxElevation_m) $maxElevation_m = $usgs_elv_m; if ($usgs_elv_m < $minElevation_m) $minElevation_m = $usgs_elv_m; $diff = $usgs_elv_m - $last_m; if ($diff > 0) { // Went up. $totalGain_m += $diff; } else { // Went down. $totalLoss_m -= $diff; } if ($debug) echo "

last = $last_m, new = $usgs_elv_m, diff = $diff, totGain = $totalGain_m, totLoss = $totalLoss_m

"; $last_m = $usgs_elv_m; } if ($doFeet) { if ($usgs_elv_ft > $maxElevation_ft) $maxElevation_ft = $usgs_elv_ft; if ($usgs_elv_ft < $minElevation_ft) $minElevation_ft = $usgs_elv_ft; $diff = $usgs_elv_ft - $last_ft; if ($diff > 0) { // Went up. $totalGain_ft += $diff; } else { // Went down. $totalLoss_ft -= $diff; } if ($debug) echo "

last = $last_ft, new = $usgs_elv_ft, diff = $diff, totGain = $totalGain_ft, totLoss = $totalLoss_ft

"; $last_ft = $usgs_elv_ft; } } if ($debug) echo "(lat = $lat, long = $long, kml_elv = $kml_elv, usgs_elv_m = $usgs_elv_m, usgs_elv_ft = $usgs_elv_ft)
"; $data[] = array( 'dist' => $mm, 'lat' => $lat, 'long' => $long, 'kml_elv' => $kml_elv, 'usgs_elv_m' => $usgs_elv_m, 'usgs_elv_ft' => $usgs_elv_ft ); } if ($debug) echo "

"; } // Build JS data structure. $jsData = "["; $notFirst = 0; foreach ($data as $dd) { if ($notFirst) { $jsData .= ","; } $notFirst = 1; $jsData .= "{"; $jsData .= "dist:" . $dd['dist']. ","; $jsData .= "lat:" . $dd['lat']. ","; $jsData .= "long:" . $dd['long']. ","; $jsData .= "kml_elv:" . $dd['kml_elv']. ","; if ($doMeters) { $jsData .= "usgs_elv_m:" . $dd['usgs_elv_m']; if ($doFeet) { $jsData .= ","; } } if ($doFeet) { $jsData .= "usgs_elv_ft:" . $dd['usgs_elv_ft']; } $jsData .= "}"; } $jsData .= "]"; echo << END; if ($doMeters) { echo << END; if ($doFeet) { echo << END; } } if ($doFeet) { echo <<
END; } echo << END; } ?>