<?php
//
// synchronizacja do serwera postgres GIS
//

include_once('se-lib/DB_Dump.php');
include_once('se-lib/UsersHelper.php');



function get_data($url) {
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}


function SYNC_GIS_GEOPORTAL_IMPORT() {
	global $db,$gdb;

	$mysql_zas_id=36;
	$gis_zas_id=13102;
	$sql_cr="";
	
	$db = DB::getDB();
	$gdb= DB::getDB($gis_zas_id);
	
	$tbl_dest="WFS_DzKat";
	
	
		DEBUG_S(-3,'db',$db);
		DEBUG_S(-3,'gis',$gdb);
		
	
	//			 http://sdi.geoportal.gov.pl/wfs_dzkat/wfservice.aspx?service=wfs&request=getfeature&bbox=50.855,20.695,50.911,20.805&version=1.1.0&typename=Dzialki&srsname=EPSG:4326
	//2014-06-05 http://sdi.geoportal.gov.pl/wfs_dzkat/wfservice.aspx?request=getfeature&version=1.1.0&service=wfs&typename=Dzialki&srsName=EPSG:4326&bbox=21.1,52.6,21.2,52.7

	
	
$url="http://sdi.geoportal.gov.pl/wfs_dzkat/wfservice.aspx?request=getFeature&version=1.1.0&service=WFS&typename=gmgml:";
$url="http://sdi.geoportal.gov.pl/wfs_dzkat/wfservice.aspx?request=getfeature&version=1.1.0&service=wfs";

$layers[]='Dzialki';
$srsName='EPSG:4326';
$bbox_y0=51.00; //dol
$bbox_x0=21.00; //lewo
$bbox_y1=52.14; //gora
$bbox_x1=22.83;//prawo
$step_y=0.01;
$step_x=0.01;


//dla gdanska
$bbox_y0=54.29; //dol
$bbox_x0=18.43; //lewo
$bbox_y1=54.50; //gora
$bbox_x1=19.00;//prawo
$step_y=0.05;
$step_x=0.05;


/*
$srsName='EPSG:2180';
$bbox_y0=378316; //dol
$bbox_x0=423036; //lewo
$bbox_y1=379316; //gora
$bbox_x1=424036;//prawo
$step_y=50;
$step_x=50;
*/

//lg=174213,770680,,876438,108559
//<gml:lowerCorner>49.0023866380461 14.1229300045899</gml:lowerCorner>
//<gml:upperCorner>54.8357888684533 24.1457361773594</gml:upperCorner>
//EPSG:2180 378316,423036
//54.13000,18.3156,54.3226,18.5427

foreach($layers as $layer) {


	for($y=$bbox_y0;$y<=$bbox_y1;$y=$y+$step_y) {
		
		for($x=$bbox_x0;$x<=$bbox_x1;$x=$x+$step_x) {		
		
			$url_get=$url."&typename=".$layer.'&srsName='.$srsName.'&bbox='.$y.','.$x.','.($y+$step_y).','.($x+$step_x);
			echo "<br>".$url_get;
			//break 2 ;


echo "<br> bede jechac ".$url_get;
			
			
			
			$xml_=get_data($url_get);
			//$xmlarray = array(); // this will hold the flattened data 
			
			
			
			
			$element = simplexml_load_string($xml_);
			$element->registerXPathNamespace('gmgml', 'http://www.intergraph.com/geomedia/gml');
			$element->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
			//$element->registerXPathNamespace('p', 'http://example.org');
			
			$positions = $element->xpath('/gmgml:FeatureCollection[1]/gml:featureMember/gmgml:Dzialki');
			//$count[]=$positions->count();
			$i=0;
			foreach ($positions as $pos) {
			
			  $pos_ = $pos->xpath('./gmgml:ID');
			  foreach ($pos_ as $pos__) {
			  
			  	$seq[]=1;
			  	$exp[$i]['ID']=$pos__->__toString();
			  	
			 // 	$result[] = $pos__->__toString();
			  	$i++;
			  	
			  }
			
			 
			}
			
			$cols=array('IDENTYFIKATOR'=>'gmgml:IDENTYFIKATOR','POWIERZCHNIA'=>'gmgml:POWIERZCHNIA','posList'=>'gmgml:GEOMETRIA_DEG/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList'); //todo jak zrobic atrubuty ? 'EPSG'=>'gmgml:GEOMETRIA_DEG/gml:Polygon@'
			
			foreach($cols as $column=>$xpath) {
			  foreach($seq as $ind=>$val) {
				$pos_=$element->xpath('/gmgml:FeatureCollection/gml:featureMember['.$ind.']/gmgml:Dzialki/'.$xpath);
					//echo "<br> exp[".$ind. "][".$column."]=";
					  foreach($pos_ as $pos__) {
				  	     $exp[$ind][$column]=$pos__->__toString();
				  	   //  echo " .. a string to ".$pos__->__toString();
				  	  }
			  }
			}
			
			//var_dump($result);
			
			//DEBUG_S(-3,'x',array($url_get,$xml_,$xml,$element,$result,$ind,$count,$exp),__FILE__,__FUNCTION__,__LINE__);
			
			
			
			foreach($exp as $exp_) {
			
				$WKT_a=explode(" ", $exp_['posList']);
				$y_=false;
				$WKT='';
				$WKT_=array();
				$i=0;
				foreach($WKT_a as $ref) {
				    if(!$y_) $y_=$ref;
				       else { 
				       	$WKT_[]=$ref." ".$y_;
				       	$y_=false;
				       }
				$i++;
				}
				
				$WKT=implode(",", $WKT_);
				//print_r($WKT);
				
				$sql='delete from  "'.$tbl_dest.'" where "REM_SYSTEM"=\''.$url.'\' and "REM_ID"=\''.$exp_['ID'].'\'';
				$gdb->query($sql);
			
				
				$sql='insert  into "'.$tbl_dest.'" ( "REM_SYSTEM","REM_ID","IDENTYFIKATOR","POWIERZCHNIA","posList","A_RECORD_CREATE_DATE" ) values 
				(\''.$url.'\',\''.$exp_['ID'].'\',\''.$exp_['IDENTYFIKATOR'].'\' , \''.$exp_['POWIERZCHNIA'].'\',ST_GeomFromEWKT(\'SRID=4326;POLYGON(('.$WKT.'))\') ,now() ) 
				';
				echo "\n<br>".$sql;	
				$gdb->query($sql);
			}
			
			unset($exp);

		//	die();



		}
	}
flush();
//break 2;
}



			


echo "<br> pojechalem";


echo "</pre>";

	

}