You are here: Re: Making a file from a long Google URL « PHP Programming Language « IT news, forums, messages
Re: Making a file from a long Google URL

Posted by petersprc on 07/07/07 18:54

Here's a script which will convert the URL to an ITN:

<?

error_reporting(E_ALL);

function gmapToItn($url, & $errors)
{
$ok = true;
$errors = array();

if (!(($parts = parse_url($url)) &&
isset($parts['query']))) {
$errors[] = 'Invalid URL.';
$ok = false;
} else {
parse_str($parts['query'], $params);
$query = '';
$saddr = isset($params['saddr']) ?
trim($params['saddr']) : '';
if ($saddr == '') {
$errors[] = 'Parameter \'saddr\' not specified.';
$ok = false;
} else {
$query .= $saddr;
$daddr = isset($params['daddr']) ?
trim($params['daddr']) : '';
if ($daddr != '') {
if ($query != '') $query .= ' to:';
$query .= $daddr;
}
}
}

if ($ok) {
$route = array();
$locs = explode('to:', $query);
foreach ($locs as $loc) {
$ptn = '/^\s*([\+-]?\d+(?:.\d+)?)' .
'\s*,\s*([\+-]?\d+(?:.\d+)?)\s*$/';
if (preg_match($ptn, $loc, $m)) {
// Bare coordinates like '44.500840,-70.671660'
$route[] = array('', $m[1], $m[2]);
} else {
$ptn = '/^\s*(.*?)\s*@\s*([\+-]?\d+(?:.\d+)?)' .
'\s*,\s*([\+-]?\d+(?:.\d+)?)\s*$/';
if (!preg_match($ptn, $loc, $m)) {
$errors[] = "No coordinates specified for " .
"location \"$loc\".";
$ok = false;
} else {
// String like 'Stationsstraat @50.879940, 4.092990'
$route[] = array($m[1], $m[2], $m[3]);
}
}
}
}

// Create ITN

if ($ok) {
$itn = '';
for ($i = 0; $i < count($route); $i++) {
$loc = $route[$i];
$x = preg_replace('/[\+\-]/', '', $loc[1]);
if (($p = strpos($x, '.')) !== false) {
$x = substr($x, 0, $p) . substr($x, $p + 1, 5);
}
$y = preg_replace('/[\+\-]/', '', $loc[2]);
if (($p = strpos($y, '.')) !== false) {
$y = substr($y, 0, $p) . substr($y, $p + 1, 5);
}
$itn .= "$y|$x|{$loc[0]}";
if ($i == 0) {
$itn .= '|4|';
} elseif ($i == count($route) - 1) {
$itn .= '|3|';
} else {
$itn .= '|1|';
}
$itn .= "\r\n";
}

$fileName = preg_replace("/[^a-z0-9_\-\.\(\)']/i", '',
$route[0][0]);
if (count($route) > 1) {
$fileName .= ' to ';
$last = $route[count($route) - 1][0];
$fileName .= preg_replace("/[^a-z0-9_\-\.\(\)']/i", '',
$last) . '.itn';
}
}

if ($ok) {
return array(
'params' => $params,
'route' => $route,
'itn' => $itn,
'bytes' => strlen(pack('a*', $itn)),
'fileName' => $fileName,
);
}

return false;
}

function run()
{
if (isset($_REQUEST['url'])) {
$url = trim($_REQUEST['url']);
} else {
$url = 'http://maps.google.com/maps?
f=d&hl=en&geocode=&saddr=Stationsstraat+%4050.879940,+4.092990+to
%3AKleemputtenstraat+%4050.873610,+4.091480+to%3AMuilenstraat+
%4050.878540,+4.098430&daddr=Meersstraat+
%4050.879560,+4.090310&sll=50.876638,4.09436&sspn=0.011915,0.011415&ie=UTF8&om=1&ll=50.877207,4.09436&spn=0.011915,0.011415&z=16';
}

$res = gmapToItn($url, $errors);

if (!$res) {
echo "Failed to create ITN file.<br><br>";
foreach ($errors as $err) {
echo "Error: $err<br>";
}
} else {
header('Content-Type: application/octet-stream');
header("Content-Length: {$res['bytes']}");
header('Content-Disposition: attachment; filename="' .
"{$res['fileName']}\"; size={$res['bytes']};");
echo $res['itn'];
}
}

run();

?>

On Jul 7, 8:26 am, houghi <hou...@houghi.org.invalid> wrote:
> I have noticed that Google has a new feature¹ on their maps directions²
> What I would like it to turn the routs you create qhith google into a
> Tomtom iternerary.itn file.
>
> I have such a script already running on my local machine³ in bash/perl,
> but it would be nice if this were possible to do with php. Perhaps I
> bite of more then I can chew as I do not have much experience in php
> scripting, however here is the idea.
>
> 1) Make a route with google maps. Do not forget to move the start and
> end point, so you get logtitude and latitude
> 2) Paste the URL into my site (or anybody elses)
> 3) copy the file.itn onto your tomtom
>
> As a sample here susch a URLhttp://tinyurl.com/2ubzloor the long
> version:http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=Stationsstraat+%...
>
> This should be turned into:
> 5087994|409299|Stationsstraat|4|
> 5087361|409148|Kleemputtenstraat|1|
> 5087854|409843|Muilenstraat|1|
> 5087956|409031|Meersstraat|3|
>
> This means doing a few things in about the following order:
> 1) Cut up the URL, so you get
> Stationsstraat+%4050.879940,+4.092990+to%3AKleemputtenstraat+%4050.873610,+4.091
> 480+to%3AMuilenstraat+%4050.878540,+4.098430
> Meersstraat+%4050.879560,+4.090310
> Next turn it into
> Stationsstraat 50.879940 4.092990
> Kleemputtenstraat 50.873610 4.091480
> Muilenstraat 50.878540 4.098430
> Meersstraat 50.879560 4.090310
> Then into
> 5087994|409299|Stationsstraat|4|
> 5087361|409148|Kleemputtenstraat|1|
> 5087854|409843|Muilenstraat|1|
> 5087956|409031|Meersstraat|3|
>
> and last but not least give the output as
> Stationsstraat_Meersstraat.itn, so it can be saved imediatly
>
> Any help would be apriciated.
>
> ¹http://tinyurl.com/2f8xa7
> ²http://maps.google.com/maps?f=d
> ³http://houghi.org/tomtom/
>
> houghi
> -->>>> Run the following from the bashprompt if you have the kernel sources
>
> for I in `find /usr/src/linux/ -name *.c`; \
> do A=`grep -i -A 1 -B 1 fuck $I`;if [ "$A" != "" ]; \
> then printf "$I \n$A \n\n"; fi ;done|less

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация