Demo: http://www.diarioc.com.ar/rel2gpx.php?id=3871050
<?php
// PHP rel2gpx. Convert OSM relations to GPX.
// v0.09b 2014 Fabian Gervan. Contact: fagervan at gmail
// La relacion debe tener los miembros WAY ordenados. (Se la puede ordenar con JOSM).
// No es el codigo mas optimo pero funciona.
// uso: rel2gpx.php?id=666
$rel=(int)$_GET['id'];
if ($rel==0) {echo "uso: rel2gpx.php?id=666 ";die;}
$url="http://www.openstreetmap.org/api/0.6/relation/$rel/full";
$xml = file_get_contents($url);
$data = new SimpleXMLElement($xml);
foreach( $data->relation->tag as $row) {
$k=(string)$row['k'];
$v=(string)$row['v'];
$arelacion[$k]=$v;
}
//read relation members
foreach( $data->relation->member as $row) {
$type=(string)$row['type'];
$ref=(string)$row['ref'];
if ($type=='way')
$arelacion['way'][]=$ref;
}
//read relation ways
foreach( $data->way as $way) {
//print_r($way);
$id=(string)$way['id'];
foreach($way->nd as $nd) {
$away[$id][]=(string)$nd['ref'];
}
}
//read all nodes
foreach ($data->node as $node) {
$id=(string)$node['id'];
$lat=(string)$node['lat'];
$lon=(string)$node['lon'];
$anode[$id]=array('lat'=>$lat,'lon'=>$lon);
// se fija si tiene tag para hacer waypoints:
foreach($node->tag as $tag) {
$k=(string)$tag['k'];
$v=(string)$tag['v'];
// si es barrier o tiene name lo carga a waypoint
if ($k=='barrier'){$awpt[$id]=$id; $anode[$id]=array('lat'=>$lat,'lon'=>$lon,'name'=>'puerta');}
if ($k=='name') {$awpt[$id]=$id; $anode[$id]=array('lat'=>$lat,'lon'=>$lon,'name'=>$v);}
}
unset ($id);
unset ($lat);
unset ($lon);
}
foreach ($arelacion['way'] as $way) {
foreach ($away[$way] as $id) {
$asnode[]=$anode[$id];
}
}
if ($awpt)
foreach($awpt as $id) {
$aswpt[]=$anode[$id];
}
gpx($arelacion['name'],$asnode,$aswpt);
function gpx($name='rel',$atrk,$awpt=NULL) {
$name=str_replace(' ','_',$name);
header("Content-type: application/gpx");
header("Content-Disposition: attachment; filename=\"$name.gpx\";" );
echo '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="linux/apache/php/mysql" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">'."\n";
// waypoints
if ($awpt)
foreach($awpt as $row) {
$lat=$row['lat'];
$lon=$row['lon'];
$name=$row['name'];
echo "<wpt lat=\"$lat\" lon=\"$lon\">\n";
echo " <name>$name</name>\n";
echo " <sym>Flag, Red</sym>\n";
echo "</wpt>\n\n";
}
// caminos
echo '<trk>
<name>Camino 001</name>
<trkseg>';
foreach ($atrk as $row) {
echo "<trkpt lat=\"".$row['lat']."\" lon=\"".$row['lon']."\"/>\n";
}
echo "</trkseg></trk>\n\n";
echo '</gpx>';
}
No hay comentarios:
Publicar un comentario