<?php
/*
ABOUT: web service to the NOAA chart catalog via regularly updated UCSD JSON file
USE: http://vislab-ccom.unh.edu/~briana/rnc/?chart=13274
AUTHOR: briana sullivan
ORG: UNH Center for Coastal and Ocean Mapping / Joint Hydrographic Center
DATE: 10/4/12
INPUT: chart number
OUTPUT: JSON formatted data
*/
ini_set('display_errors', 1); 
error_reporting(E_ALL);
include_once 'decodePolylineToArray.php';

$chart = $_GET["chart"];

$url = "http://cordc.ucsd.edu/js/RNC/rnc.php";
// create a new cURL resource
$ch = curl_init();

// set options
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

$buffer = curl_exec($ch);
$chartFound = 0;
// close cURL resource, and free up system resources
curl_close($ch);

if (empty($buffer)){
    print "<p>Sorry, the rnc product catalog is unavailable at the moment<p>";
}
else{
    $rncCat = json_decode($buffer,true);	
	//print_r($rncCat);
	foreach($rncCat as $item){
		if($item["chart_id"] == $chart){
			$chartFound = 1;
			/*foreach($item["panels"] as $panel){
				$enc_poly = $panel["encoded_polygon"];
				$enc_level= $panel["encoded_levels"];				
				var_dump($panel);					
			}*/
			$json = json_encode($item);			
		}
		
	}
	// create the xml variable to build the response
	/*$xml = new SimpleXMLElement('<chart/>');
	// iterate through each chart in the catalog
	foreach($rncCat as $item){
		// find only the chart requested
		if($item->chart_id == $chart){
			$chartFound = 1;
			// iterate through each attribute of the chart
			foreach($item as $key => $value){
				if($key == 'panels'){		
					$panels = $xml->addChild('panels');
					// iterate through each panel belonging to the chart
					foreach($value as $i => $v){
						$panel = $panels->addChild('panel');
						// iterate through each panel attribute						
						foreach($v as $a => $b){
							if($a == 'encoded_polygon'){
								// Store all info for polygon into an array to use when encoded_levels is processed								
								$points = decodePolylineToArray($b);								
							}
							else if($a == 'encoded_levels'){
								// Decode the levels and match up with stored points
								$levels = decodeLevelsToArray($b);
								if(count($points) == count($levels)){
									$pointsEle = $panel->addChild('points');
									for($i=0; $i<count($points); $i++){
										$pointEle = $pointsEle->addChild('point');
										$latlng = $points[$i];
										$pointEle->addChild('latitude',$latlng[0]);
										$pointEle->addChild('longitude',$latlng[1]);
										$pointEle->addChild('zoom_level',$levels[$i]);										
									}
									$pointsEle->addChild('count',count($points));
								}
							}
							else
								$panel->addChild($a,$b);
						}
					}
				}
				else // this is a chart attribute that is not a panel
					$xml->addChild($key,$value);
			}			
			break;
		}		
	}*/
	if($chartFound){
		//$panels->addChild('count',count($panels));	
		//$json = json_encode(new SimpleXMLElement($xml->asXML(),LIBXML_NOCDATA));
		Header('Content-type: application/json');
		print($json);
	}else{
		//Header('Content-type: text/html');
		print("<br/><br/>The chart number (".$chart.") you entered doesn't exitst. Please check your chart number and try again.<br/><br/>Make sure your URL looks like this (with a valid chart number):<br/><br/> http://cordc.ucsd.edu/js/RNC/?chart=");		
		//echo "The chart number you entered ("+$chart+") doesn't exitst. Please check your chart number and try again.";
	}
}
?>