<?php

//-------------------------------------------------------------------------------
// ITML_Module.php
// module for handling (parsing, writing) ITML/itinML files 
//
// written by Bart Meganck, Royal Museum for Central Africa
// bart.meganck@africamuseum.be
//
//
// This program is free & open source, under the GNU General Public License, v.2
// See : http://www.gnu.org/copyleft/gpl.html for more details.
//
//
//-------------------------------------------------------------------------------


class ITML_Module
{

	// array of arrays for storing point data
	private $points_array = array (

		 "id_array" => array(),
		 "unique_id_array" => array(),
		 "name_array" => array(),
		 "latitude_array" => array(),
		 "longitude_array" => array(),
		 "coord_accuracy_array" => array(),
		 "begin_date_array" => array(),
		 "end_date_array" => array()
 
		 );
	
	private $point_count = 0;
	private $return_string;
	private $input_DOM;
	private $point_elements;
	private $latitude;
	private $longitude;
	private $timeperiod_element;
	private $timeinstant_element;

		private $output_DOM;
		private $pointcollection_element;
		private $root_element;
		private $point_element;
		private $point_element_creation;
		private $geodatum_element;
		private $ellipsoid_element;
		private $coordsystem_element;
		private $srs_element;
		private $latitude_element;
		private $latitude_element_creation;
		private $longitude_element;
		private $longitude_element_creation;
		private $gmlpos_element_creation;
		private $gmlpos_element;
		private $gmlTimePeriod_element_creation;
		private $gmlTimePeriod_element;
		private $gmlbegin_element_creation;
		private $gmlbegin_element;
	

	public function readPointsFromITMLFile($input_file_path) {

		$this->input_DOM = new DOMDocument();
		$this->input_DOM->load("$input_file_path");
	
		// Search all the gml:Point occurences and fill them into points_array
		$this->point_elements = $this->input_DOM->getElementsByTagNameNS("http://www.opengis.net/gml","Point");

		foreach ($this->point_elements as $this->point_element) {
 
			if ($point->hasAttributeNS("http://www.opengis.net/gml","id")) {   

				$this->points_array["id_array"][$this->point_count] = 
					$point->getAttributeNS ( "http://www.opengis.net/gml","id" );

			} // if $point->hasAttributeNS
		
			$this->latitude = $this->point_element->getElementsByTagNameNS("http://www.itineraries","latitude");
			$this->points_array["latitude_array"][$this->point_count] = $this->latitude->item(0)->nodeValue;

			$this->longitude = $this->point_element->getElementsByTagNameNS("http://www.itineraries","longitude");
			$this->points_array["longitude_array"][$this->point_count] = $this->longitude->item(0)->nodeValue;

			$this->timeperiod_element = $this->point_element->getElementsByTagNameNS("http://www.opengis.net/gml","TimePeriod");
			$this->begin = $this->timeperiod_element->item(0);
			$this->timeinstant_element = $this->begin->getElementsByTagNameNS("http://www.opengis.net/gml","TimeInstant");	
			
			$this->points_array["begin_date_array"][$this->point_count] = $this->timeinstant_element->item(0)->nodeValue;
			$this->points_array["end_date_array"][$this->point_count] = $this->timeinstant_element->item(1)->nodeValue;

			$this->point_count++;	 
			
		} // foreach ($points as $point)
		
	return($this->points_array);
		
	} //function readPointsFromITMLFile
	
	
	
	
	public function writePointsToITMLFile($points_array,$output_file_path) { 
	

	
	
		$this->output_DOM = new DOMDocument();
		

		//************************* root element ****************************************

		// level0
		$this->pointcollection_element = $this->output_DOM->createElementNS('http://www.itineraries','itin:PointCollection','');
		$this->root_element = $this->output_DOM->appendChild($this->pointcollection_element); // appended directly to the DOM document


		//************************* gml projection and datum info ***********************
		// level1
		$this->geodatum_element = $this->output_DOM->createElementNS('http://www.opengis.net/gml','gml:GeodeticDatum','WGS84');
		$this->root_element->appendChild($this->geodatum_element);

		// level1
		$this->ellipsoid_element = $this->output_DOM->createElementNS('http://www.opengis.net/gml','gml:Ellipsoid','WGS84');
		$this->root_element->appendChild($this->ellipsoid_element); 

		// level1
		$this->coordsystem_element = $this->output_DOM->createElementNS('http://www.opengis.net/gml','gml:csID','lat/lon');
		$this->root_element->appendChild($this->coordsystem_element);

		// level1
		$this->srs_element = $this->output_DOM->createElementNS('http://www.opengis.net/gml','gml:srsName','EPSG:4326');
		$this->root_element->appendChild($this->srs_element); 


		// fill the parameters for each point in the input
		// foreach ($id_array as $key => $value) {

		for ($this->point_count=0; $this->point_count < count($points_array["unique_id_array"]);$this->point_count++) {

		// level1
		$this->point_element_creation = $this->output_DOM->createElement('gml:Point','');
		$this->point_element = $this->root_element->appendChild($this->point_element_creation); 

		// attribute : id
		$this->point_element->setAttributeNS('http://www.opengis.net/gml','gml:id', $points_array["unique_id_array"][$this->point_count]); 

			// level2
			$this->latitude_element_creation = $this->output_DOM->createElement('itin:latitude',$points_array["latitude_array"][$this->point_count]);
			$this->latitude_element = $this->point_element->appendChild($this->latitude_element_creation); 

			// attributes for latitude
			$this->latitude_element->setAttribute("gml:format", "decimal");
			$this->latitude_element->setAttribute("itin:decimal_accuracy", $points_array["coord_accuracy_array"][$this->point_count]);

			// level2
			$this->longitude_element_creation = $this->output_DOM->createElement('itin:longitude',$points_array["longitude_array"][$this->point_count]);
			$this->longitude_element = $this->point_element->appendChild($this->longitude_element_creation);

			// attributes for longitude
			$this->longitude_element->setAttribute("gml:format", "decimal");
			$this->longitude_element->setAttribute("itin:decimal_accuracy", $points_array["coord_accuracy_array"][$this->point_count]);

			// level2
			$this->gmlpos_element_creation = $this->output_DOM->createElement('gml:pos',$points_array["latitude_array"][$this->point_count]." ".$points_array["longitude_array"][$this->point_count]);
			$this->point_element->appendChild($this->gmlpos_element_creation);

			// level2
			$this->gmlTimePeriod_element_creation = $this->output_DOM->createElement('gml:TimePeriod','');
			$this->gmlTimePeriod_element = $this->point_element->appendChild($this->gmlTimePeriod_element_creation);

				// level3
				$this->gmlbegin_element_creation = $this->output_DOM->createElement('gml:begin','');
				$this->gmlbegin_element = $this->gmlTimePeriod_element->appendChild($this->gmlbegin_element_creation);

					// level4
					$this->gmlTimeInstant_begin_creation = $this->output_DOM->createElement('gml:TimeInstant','');
					$this->gmlTimeInstant_begin = $this->gmlbegin_element->appendChild($this->gmlTimeInstant_begin_creation);

						// level5
						$this->gmlTimePosition_begin = $this->output_DOM->createElement('gml:TimePosition',$points_array["begin_date_array"][$this->point_count]);
						$this->gmlTimeInstant_begin->appendChild($this->gmlTimePosition_begin);

				// level3
				$this->gmlend_element_creation = $this->output_DOM->createElement('gml:end','');
				$this->gmlend_element = $gmlTimePeriod->appendChild($this->gmlend_element_creation);

					// level4
					$this->gmlTimeInstant_end = $this->output_DOM->createElement('gml:TimeInstant','');
					$this->gmlend_element->appendChild($this->gmlTimeInstant_end);

					// level5
					$this->gmlTimePosition_end = $this->output_DOM->createElement('gml:TimePosition',$points_array["end_date_array"][$this->point_count]);
					$this->gmlTimeInstant_end->appendChild($this->gmlTimePosition_end);

		} // for ($this->point_count=1; $this->point_count < count($points_array["id_array"]);$this->point_count++)

   	$this->output_DOM->save ("$output_file_path");
   	return $this->returnstring;

	} // function writePointsToITMLFile
	

} // class ITML_Module


?>