GPX parsing patterns and “standards”

后端 未结 2 1393
渐次进展
渐次进展 2020-12-17 05:53

I would like to continue the discussion from this post: GPX Schema validation problems. To go directly to the point, I want to ask how people are parsing and working with GP

相关标签:
2条回答
  • 2020-12-17 06:38

    i have just worked out a simple gpx parser that i use to scrape tracks and waypoints from a gpx file and save in a database. i have stripped all DB code and left a barebones example.

    <?php
    include('simple_html_dom.php');
    
    if (file_exists('5alqs3.gpx')) {
        $xml = simplexml_load_file('5alqs3.gpx');
        foreach( $xml->children() AS $child ) {
            $name = $child->getName();
            if ($name == 'wpt') {
                print_r($name.'    ');
                echo $child['lat'].' '.$child['lon'].'  ';
                $name = $child->children()->getName();
                if ($name = 'ele') {
                    echo $child->children().'<br/>';
                }
            }
            if ($name == 'trk') {
                foreach( $child->children() AS $grandchild ) {
                    $grandname = $grandchild->getName();
                    if ($grandname == 'name') {
                        echo $grandchild.'<br/>';
                    }
                    if ($grandname == 'trkseg') {
                        foreach( $grandchild->children() AS $greatgrandchild ) {
                            $greatgrandname = $greatgrandchild->getName();
                            print_r($greatgrandname.'  ');
                            //echo '<br/>';
                            if ($greatgrandname == 'trkpt') {
                                echo $greatgrandchild['lat'].' '.$greatgrandchild['lon'];
                                foreach( $greatgrandchild->children() AS $elegreatgrandchild ) {
                                    echo $elegreatgrandchild.'<br/>';
                                }
                            }
                            if ($greatgrandname == 'ele') {
                                print_r($greatgrandchild);
                            }   
                        }
                    }
                }
            } 
        }
    } else {
        exit('Failed to open 5alqs2.gpx.');
    }                
    ?>
    

    you could take this one step further and parse kml from this. check this link out for more on this subject. Creating a kml file from a mysql database with php

    THIS CODE WORKS WITH MULTIPLE TRACKS.

    0 讨论(0)
  • 2020-12-17 06:41

    In my applications I'm using a normal XML parser (e.g. pythons xml.dom.minidom) I'm just using the tag names to access the information I need. As it was mentioned in the other Thread, the thing that makes validation fail is the <xsd:sequence> in the GPX standard definition. While it's true that GPX producing devices should respect the standard, it's actually not that big of a deal on the parsing side (at least with the GPX Files/Devices I worked on).

    If a GPX file contains additional information then it will be dropped (unless you chose to extract the tagnames unknown to you).

    If this method fails because the file is not vaild XML or the GPX uses different tag names for e.g. lat lon information there is no other way to read the file than to write a special importer for this specific use case (I have not seen such files, except for corrupted ones, in the wild)

    0 讨论(0)
提交回复
热议问题