[Linpha-cvs] SF.net SVN: linpha: [4844] trunk/linpha2
Status: Inactive
Brought to you by:
bzrudi
From: <bz...@us...> - 2008-01-31 19:54:36
|
Revision: 4844 http://linpha.svn.sourceforge.net/linpha/?rev=4844&view=rev Author: bzrudi Date: 2008-01-31 11:54:28 -0800 (Thu, 31 Jan 2008) Log Message: ----------- First draft for the XML parser class (xml_parser.class.php) Modified Paths: -------------- trunk/linpha2/ChangeLog trunk/linpha2/lib/classes/linpha.metadata.class.php trunk/linpha2/lib/plugins/maps/module.maps.php Added Paths: ----------- trunk/linpha2/lib/plugins/maps/xml_parser.class.php Removed Paths: ------------- trunk/linpha2/lib/plugins/maps/garmingpx_parser.class.php trunk/linpha2/lib/plugins/maps/gpx.php Modified: trunk/linpha2/ChangeLog =================================================================== --- trunk/linpha2/ChangeLog 2008-01-31 13:01:02 UTC (rev 4843) +++ trunk/linpha2/ChangeLog 2008-01-31 19:54:28 UTC (rev 4844) @@ -6,7 +6,8 @@ - code cleanups - still work in progress - btw - installer looks fine - + * First draft for the XML parser class (xml_parser.class.php) + 2008-01-30 flo * installer - improved layout Modified: trunk/linpha2/lib/classes/linpha.metadata.class.php =================================================================== --- trunk/linpha2/lib/classes/linpha.metadata.class.php 2008-01-31 13:01:02 UTC (rev 4843) +++ trunk/linpha2/lib/classes/linpha.metadata.class.php 2008-01-31 19:54:28 UTC (rev 4844) @@ -29,7 +29,6 @@ */ class LinMetaData { - public $defined_fields, $available_fields; protected $objMetaTool; private $cachedFieldNames, $cachedFieldIds, $cachedSelectedFieldNames; @@ -1029,6 +1028,15 @@ } /** + * Parse GPX data from file + */ + public function parseGpxFile($filename) + { + $parser = new GpxParser($filename); + + } + + /** * Simple wrapper method for saveMetaData() * @param string $what meta type exif,iptc,xmp * @param string $filename filename @@ -1193,5 +1201,6 @@ return $rotate; } } + /* vi: set ts=4 sw=4 sts=4 */ ?> Deleted: trunk/linpha2/lib/plugins/maps/garmingpx_parser.class.php =================================================================== --- trunk/linpha2/lib/plugins/maps/garmingpx_parser.class.php 2008-01-31 13:01:02 UTC (rev 4843) +++ trunk/linpha2/lib/plugins/maps/garmingpx_parser.class.php 2008-01-31 19:54:28 UTC (rev 4844) @@ -1,84 +0,0 @@ -<?php -/* -* Copyright (c) 2004 Heiko Rutenbeck <bz...@tu...> -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -if(!defined('TOP_DIR')) { define('TOP_DIR','../../'); } - -/** -* Class to parse Garmin GPX files and return values for -* latitiude, longitude, elevation, time and more. Makes use of libxml -* to read XML data -> requires php >= 4.2.0! -* -* @author bzrudi -*/ -class GpxParser -{ - -var $xmldata; //protected -var $parsedata; //protected -var $filename; //protected - -/** - * Constructor - */ -function GpxParser($filename) -{ - include_once(TOP_DIR.'/plugins/maps/libxml/IsterXmlSimpleXMLImpl.php'); - $xmlreader = new IsterXmlSimpleXMLImpl; - $this->xmldata = $xmlreader->load_file($filename); - $this->filename = $filename; -} - -/** - * This method checks whether file contains waypoints (wpt) or trackpoints - * (trkpt) or even routepoints (rtp) and takes good care of - * how we handle them. - * - * @param none - * @return array returns only XmlData and filetype - * @author bzrudi - */ -function getGpxFileType() -{ - $this->parsedata = file_get_contents($this->filename); - - if(strpos($this->parsedata, 'trkpt')) - { - echo "TRACKFILE DETECTED"; - return array($this->xmldata, 'trkpt'); - } - elseif(strpos($this->parsedata, 'rtept')) - { - echo "ROUTEFILE DETECTED"; - return array($this->xmldata, 'rtept'); - } - elseif(strpos($this->parsedata, 'wpt')) - { - echo "WAYFILE DETECTED"; - return array($this->xmldata, 'wpt'); - } - else - { - echo "CANNOT DETECT INPUT FILE FORMAT"; - return array('dummy', 'failed'); - } -} - -} // end class -?> - Deleted: trunk/linpha2/lib/plugins/maps/gpx.php =================================================================== --- trunk/linpha2/lib/plugins/maps/gpx.php 2008-01-31 13:01:02 UTC (rev 4843) +++ trunk/linpha2/lib/plugins/maps/gpx.php 2008-01-31 19:54:28 UTC (rev 4844) @@ -1,99 +0,0 @@ -<?php -/** - * This method parses an XML file (currently in Garmin GPX format) and saves - * all available information in the DB - * - * @param string filename - * @author bzrudi - */ -function parseXMLDataFile($filename) -{ - -$gpxparser = new GpxParser($filename); -list($xmldata, $filetype) = $gpxparser->getGpxFileType(); - - switch($filetype) - { - case "trkpt": - { - foreach( $xmldata->gpx->trk->trkseg->trkpt as $trkinfo) - { - $latlon = $trkinfo->attributes(); //array(lat,lon) - - $lat = $latlon['lat']; - $lon = $latlon['lon']; - $ele = $trkinfo->ele->CDATA(); - $time = $trkinfo->time->CDATA(); - - //echo"LAT:".$lat."LON:".$lon."ELE:".$ele."<br>"; - - $location = new Location($lat, $lon, '<div class="someCssClass">' . - 'LAT: '.$lat.'<br>' . - 'LON: '.$lon.'<br>' . - 'HÖHE: '.$ele.'m<br>' . - 'DATE: '.$time.'</div>'); - $this->cmap->addLocation($location); - } - break; - } - - case "rtept": - { - foreach( $xmldata->gpx->rte->rtept as $pointinfo) - { - $latlon = $pointinfo->attributes(); //array(lat,lon) - - $lat = $latlon['lat']; - $lon = $latlon['lon']; - //$ele = $pointinfo->ele->CDATA(); - //$name = $pointinfo->name->CDATA(); - - //echo"LAT:".$lat."LON:".$lon."ELE:".$ele."NAME:".$name."<br>"; - - $location = new Location($lat, $lon, '<div class="someCssClass">' . - 'LAT: '.$lat.'<br>' . - 'LON: '.$lon.'<br>' . - 'HÖHE: na<br>' . - 'Name: na</div>'); - $this->cmap->addLocation($location); - } - break; - } - - case "wpt": - { - foreach( $xmldata->gpx->wpt as $wpointinfo) - { - $latlon = $wpointinfo->attributes(); //array(lat,lon) - - $lat = $latlon['lat']; - $lon = $latlon['lon']; - $ele = $wpointinfo->ele->CDATA(); - $name = $wpointinfo->name->CDATA(); - $cmt = $wpointinfo->cmt->CDATA(); - //$desc = $wpointinfo->desc->CDATA(); - $sym = $wpointinfo->sym->CDATA(); - - //echo"LAT: ".$lat."LON: ".$lon."ELE: ".$ele."NAME: ".$name." " . - // "CMT: ".$cmt."SYMBOL: ".$sym."<br>"; - - $location = new Location($lat, $lon, '<div class="someCssClass">' . - 'LAT: '.$lat.'<br>' . - 'LON: '.$lon.'<br>' . - 'ELE: '.$ele.'m<br>' . - 'NAM: '.$name.'<br>' . - 'CMT: '.$cmt.'<br>' . - 'SYM: '.$sym.'<br></div>'); - $this->cmap->addLocation($location); - } - break; - } - case "failed": - { - echo "CANNOT DETECT INPUT FILE FORMAT"; - } - } -} - - - Modified: trunk/linpha2/lib/plugins/maps/module.maps.php =================================================================== --- trunk/linpha2/lib/plugins/maps/module.maps.php 2008-01-31 13:01:02 UTC (rev 4843) +++ trunk/linpha2/lib/plugins/maps/module.maps.php 2008-01-31 19:54:28 UTC (rev 4844) @@ -23,10 +23,13 @@ * @package Plugins */ if(!defined('LINPHA_DIR')) { exit(1); } +include_once LINPHA_DIR.'/lib/plugins/maps/xml_parser.class.php'; -/* $MetaData = new LinMetaData(); - $MetaData->importGeoData();*/ -/** +$GPXData = new LinXmlParser(); +$array_with_gpx = $GPXData->readXmlFile('/tmp/sample.gpx'); +echo "<pre>", print_r($array_with_gpx), "</pre>"; + +/** * xml load/save stuff */ if(isset($_GET['xml'])) Copied: trunk/linpha2/lib/plugins/maps/xml_parser.class.php (from rev 4841, trunk/linpha2/lib/plugins/maps/garmingpx_parser.class.php) =================================================================== --- trunk/linpha2/lib/plugins/maps/xml_parser.class.php (rev 0) +++ trunk/linpha2/lib/plugins/maps/xml_parser.class.php 2008-01-31 19:54:28 UTC (rev 4844) @@ -0,0 +1,137 @@ +<?php +/* + * Copyright (c) 2004 Heiko Rutenbeck <bz...@tu...> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +if(!defined('TOP_DIR')) { define('TOP_DIR','../../'); } + +/** + * Class to parse Garmin GPX files and return values for + * latitiude, longitude, elevation, time and more. + * @author bzrudi + */ +class LinXmlParser +{ + private $data, $parser, $filename, $values, $index_array; + private $data_range, $gpx, $xml_data; + + function __construct() + { + + } + /** + * This method reads all trackpoints and related data from an xml file and + * returns them as an array, example: + * [685] => Array + * ( + * [trkpt] => Array + * ( + * [lon] => 7.715998599305749 + * [lat] => 50.1166464574635 + * ) + * + * [ele] => 191.92 + * [time] => 2007-12-23T13:31:50Z + * [extension] => + * [flags] => 0 + * ) + * + * @param string $filename + * @return array with all trackpoints + */ + public function readXmlFile($filename) + { + /** + * open and parse xml file for Track data + */ + $this->data = file_get_contents($filename); + $this->parser = xml_parser_create(); + + /** + * lowercase tags + */ + xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); + xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); + + /** + * This function returns two arrays, one for the index and one for + * the values. + */ + xml_parse_into_struct($this->parser, $this->data, $this->values, $this->index_array); + xml_parser_free($this->parser); + + /** + * Loop through index array to find all occurencies of "trkpt" + */ + foreach($this->index_array AS $key => $val) + { + if($key == "trkpt") // hit + { + $this->data_range = $val; + + for($i=0; $i < count($this->data_range); $i+=2) + { + $offset = $this->data_range[$i]; + $len = $this->data_range[$i + 1] - $offset; + $this->xml_data[] = $this->parseXmlArray(array_slice($this->values, $offset, $len)); + } + } + else + { + continue; + } + } + return $this->xml_data; + } + + /** + * This is just an helper/wrapper method for readXmlFile() and is called + * for each trkpoint data entry from readXmlFile(). + * + * @param array $sliced_arrav + * @return unknown + */ + private function parseXmlArray($sliced_array) + { + for($i=0; $i < count($sliced_array); $i++) + { + /** + * Trkpt data is somewhat other to handle than ele and time data, + * as the return is an array. + */ + if($sliced_array[$i]["tag"] == "trkpt") + { + $this->gpx[$sliced_array[$i]["tag"]] = @$sliced_array[$i]["attributes"]; + } + + /** + * This is anything beetween the <trkpt> and </trkpt> tag + */ + else + { + $this->gpx[$sliced_array[$i]["tag"]] = @$sliced_array[$i]["value"]; + } + } + + //return new LinXmlParser($this->gpx); + return $this->gpx; + + } +} + +/* vi: set ts=4 sw=4 sts=4 */ +?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |