GPX to KML in PHP

3.1k views Asked by At

I am working in a project where people upload GPX and I am trying to convert a GPX file into a KML file, so they have the option of downloading in both formats.

I found a XSLT file that supposedly transforms GPX into KML but when I try to do the conversion in php using XSLTProcessor, it gives me some errors saying that some functions are not found. I checked the XSLT file and those functions are there. I am not very familiar with XSLT so if anyone can give me some direction that would be great.

The xslt file is located here: http://members.home.nl/cybarber/geomatters/FlitspaalGPX2KML.xslt

The gpx file is located here: http://geobetty.com/maps/download/8/archuletas-acres.gpx

Here is the code:

<?php
$gpx = new DOMDocument();
$gpx->loadXML($ride);

$xslsheet = new DOMDocument();
$xslsheet->load(DOCROOT . '/lib/gpx-to-kml.xslt');

$xsl = new XSLTProcessor();
$xsl->importStyleSheet($xslsheet);

$kml = $xsl->transformToXML($gpx); ?>

These are my errors:

xmlXPathCompOpEval: function distCosineLaw not found Unregistered function xmlXPathCompiledEval: 3 objects left on the stack

Among others

4

There are 4 answers

1
Dimitre Novatchev On BEST ANSWER

The XSLT transformation is written especially to be run by MSXML and uses the extension element <msxsl:script> which is implemented only by the MSXML XSLT processor.

Solution: Either:

  1. Run the transformation with MSXML (ver. 3, 4, or 6).

  2. Implement the extension functions for use with your XSLT processor, if that is possible.

  3. Find an early implementation of an XSLT 3.0 XSLT processor. XSLT 3.0 uses XPath 3.0 and in XPath 3.0 the main trigonometric and exponential functions have been made standard functions of the language.

0
pafcu On

You could also try to use gpsbabel (an external program) to do the conversion. This way you will gain automatic support for lots of other formats as well. The downside is that you need to install an external program which may or may not be possible depending on your hosting.

0
AudioBubble On

I have written this code to convert GPX to KMl but how to set style and more dom.

function gpxtokml($path,$id){

    $name_file=$path;
    $point=explode(".",$name_file);
    $namekml=$point[0].'.kml';

    $xml = simplexml_load_file($name_file);$i=0;
    $arry=array();
    foreach($xml->trk->trkseg->trkpt as $trkpt) {

    //$arry[$i++]=$this->xml2array ($trkpt,$out = array());
        foreach ( (array) $trkpt as $index => $node ){
        //$out[$index] = ( is_object ( $node ) );
        if(is_object ( $node )){
        foreach ( (array) $trkpt as $index => $node )
        $out[$index] = $node ;
        continue;
        }else{
        $out[$index] = $node ;
        }
        }
        $arry[$i++]=$out;
    }
    //print_r($arry);exit;
    $retrn=$this->generatekml($arry,true,$namekml,$id);
    return $retrn;
    }

    function xml2array ( $xmlObject, $out = array () )
    {
    foreach ( (array) $xmlObject as $index => $node )
    $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;

    return $out;
    } 
    function generatekml($input,$file,$filename,$id){
        $output="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
        <kml xmlns=\"http://www.opengis.net/kml/2.2\">
        <Document>
        ";
        $i=1;
        //echo '<pre>';print_r($input);exit;
        foreach($input as $key=>$point){

        $name="point ".$i++;
        $description='';
        $lat=$point['@attributes']['lat'];
        $lon=$point['@attributes']['lon'];
        $coordinates=$lat .",".$lon;

        $output.="<Placemark>
        <name>$name</name>
        <description>$description</description>
        <Point>        
        <coordinates>$coordinates</coordinates>
        </Point>
        </Placemark>
        ";
        }        

        $output.="</Document>
        </kml>
        ";
        if($file){

        //header("Content-type: octet/stream");
        //header("Content-disposition: attachment; filename=".$filename.";");
        // header("Content-lenght: ".filesize("files/".$file));
        //echo $output;
        $fl=time().'kml.kml';
        $xmlfile=WWW_ROOT.'kmlfile/'.$fl;
        //echo $this->EventDetail->id=$id;
        //exit;
        //$date['EventDetail']['kmlfile']=time().'kml.kml';
        //$this->EventDetail->save($date['EventDetail'],false);
        $fp = fopen($xmlfile, 'w');
        fwrite($fp, $output);

        fclose($fp);
        //echo time().'kml.kml';
        return $fl; 
        }else{

        }
    }
0
rocoder On
    Please use your gpx file name in program or path of gpx file     

    <?php 
        $name_file="ff72be886cde0672af512bb2c383d422.gpx";
        $point=explode(".",$name_file);
        $namekml=$point[0].'.kml';

        $xml = simplexml_load_file($name_file);$i=0;
        $arry=array();
        foreach($xml->trk->trkseg->trkpt as $trkpt) {

         $arry[$i++]=xml2array ( $trkpt, $out = array () );

        }

        generatekml($arry,true,$namekml);

        function generatekml($input,$file,$filename){
        $output="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
        <kml xmlns=\"http://www.opengis.net/kml/2.2\">
          <Document>
        ";
          $i=1;
foreach($input as $key=>$point){

$name="point ".$i++;
$description=$point['ele'];
$lat=$point['@attributes']['lat'];
$lon=$point['@attributes']['lon'];
$coordinates=$lat .",".$lon;

$output.="<Placemark>
      <name>$name</name>
      <description>$description</description>
      <Point>        
        <coordinates>$coordinates</coordinates>
      </Point>
    </Placemark>
";
}        

$output.="</Document>
        </kml>
        ";
        if($file){

            header("Content-type: octet/stream");
            header("Content-disposition: attachment; filename=".$filename.";");
           // header("Content-lenght: ".filesize("files/".$file));
            print $output;


        }else print $output;
        }




         function xml2array ( $xmlObject, $out = array () )
        {
            foreach ( (array) $xmlObject as $index => $node )
                $out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;

            return $out;
        } 
        ?>