GPX for KML in PHP

I am working on a project in which people download GPX, and I'm trying to convert a GPX file to a KML file, so they have the ability to download in both formats.

I found an XSLT file that supposedly converts GPX to KML, but when I try to convert to php using XSLTProcessor, it gives me some errors saying that some functions were not found. I checked the XSLT file, and these functions are. I am not very familiar with XSLT, so if someone can give me some direction, it will be great.

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

The gpx file is 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 mistakes:

xmlXPathCompOpEval: distCosineLaw xmlXPathCompiledEval: 3

+3
4

XSLT , , MSXML <msxsl:script>, XSLT MSXML.

: :

+2

gpsbabel ( ) . , . , , .

0
    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;
        } 
        ?>
0
source

I wrote this code to convert GPX to KMl, but how to set the 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
source

Source: https://habr.com/ru/post/1791683/


All Articles