PHP 中的 GPX 到 KML

发布于 2024-10-17 03:23:20 字数 972 浏览 1 评论 0原文

我正在从事一个项目,人们上传 GPX,我试图将 GPX 文件转换为 KML 文件,因此他们可以选择以两种格式下载。

我发现一个 XSLT 文件据说可以将 GPX 转换为 KML,但是当我尝试使用 XSLTProcessor 在 php 中进行转换时,它给了我一些错误,指出某些函数未找到。我检查了 XSLT 文件,这些函数都在那里。我对 XSLT 不太熟悉,所以如果有人能给我一些指导,那就太好了。

xslt 文件位于此处: http://members.home.nl/cybarber/geomatters/ FlitspaalGPX2KML.xslt

gpx 文件位于此处:http://geobetty .com/maps/download/8/archuletas-acres.gpx

这是代码:

<?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); ?>

这些是我的错误:

xmlXPathCompOpEval:找不到函数 distCosineLaw 未注册的函数 xmlXPathCompiledEval:堆栈上剩余 3 个

对象

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风和你 2024-10-24 03:23:20

XSLT 转换是专门为由 MSXML 运行而编写的,并使用仅由 MSXML XSLT 处理器实现的扩展元素

解决方案:要么:

  1. 使用 MSXML 运行转换(版本 3、4 或 6)。

  2. 实现扩展函数,以便与您的 XSLT 处理器一起使用(如果可能)。

  3. 查找 XSLT 3.0 XSLT 处理器的早期实现。 XSLT 3.0 使用 XPath 3.0 并且在 XPath 3.0 中主要三角函数和指数函数已成为标准函数

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.

独﹏钓一江月 2024-10-24 03:23:20

您还可以尝试使用 gpsbabel (外部程序)进行转换。这样您也将获得对许多其他格式的自动支持。缺点是您需要安装外部程序,这可能会也可能不会,具体取决于您的主机。

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.

半步萧音过轻尘 2024-10-24 03:23:20
    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;
        } 
        ?>
    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;
        } 
        ?>
无人问我粥可暖 2024-10-24 03:23:20

我编写了这段代码来将 GPX 转换为 KMl,但如何设置样式和更多 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{

        }
    }

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{

        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文