<?php
$_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';


//when used in /Library/Server/Web/Data/Sites/bzyk-biale-plamy-php-temp/SE/dev on biuro.biall-net.pl
if( file_exists ( "../../se-lib/bootstrap.php"))
require("../../se-lib/bootstrap.php");
//when used in  in native SE
else require("../../../../SE-production-git/SE/se-lib/bootstrap.php");

Lib::loadClass("Vendor_Geophp");


 
$cables_from_joins_to_PE=simplexml_load_file('cables_from_joins_to_PE.xml');
$cables_from_joins_to_PE->registerXPathNamespace('bp', 'https://procesy5.pl/biale_plamy-schema.xsd');
/*
	
	<?xml version="1.0" encoding="UTF-8"?>
	<cables_from_joins_to_PE xmlns:bp="https://procesy5.pl/biale_plamy-schema.xsd">
	<cable_to_PE_to_be_calculated ID_Way="3"
                                 Joins="4"
                                 Join_X="18.7896425"
                                 Join_Y="51.698063399083">
		      <not_found Pos="1"
		                 X="18.636657356658"
		                 Y="51.253416226324"
		                 srch_X="18.7896425"
		                 srch_Y="51.698063399083"
		                 diff_X="0.15298514334200064"
		                 diff_Y="0.444647172758998"/>
        <found_in_path_in_edge_joins_filled_path diff_X="-0.00006424369499669069"
                                               diff_Y="0.0013892901829990478"
                                               position="5">
         <bp:Path_Point Pos="5" X="18.960657743695" Y="51.612137008899"/>
         <detect_next_PE Pos_found="5"
                         Next_PE_Pos="20"
                         Prev_PE_Pos=""
                         Going_to_next="20">
            <bp:Path_Point Pos="5" X="18.960657743695" Y="51.612137008899"/>
            <bp:Path_Point Pos="6" X="18.960110541501" Y="51.612105643896"/>
            <bp:Path_Point Pos="7" X="18.959774447246" Y="51.612086379359"/>
            <bp:Path_Point Pos="8" X="18.959754322927" Y="51.612085225856"/>
            <bp:Path_Point Pos="9" X="18.959645426785" Y="51.612078984054"/>
            <bp:Path_Point Pos="10" X="18.959455431877" Y="51.612068093763"/>
            <bp:Path_Point Pos="11" X="18.959126615445" Y="51.612049246382"/>
            <bp:Path_Point Pos="12" X="18.958655233785" Y="51.612022227326"/>
            <bp:Path_Point Pos="13" X="18.958386790309" Y="51.612006840454"/>
            <bp:Path_Point Pos="14" X="18.957723493448" Y="51.611968821038"/>
            <bp:Path_Point Pos="15" X="18.957421446031" Y="51.611951508026"/>
            <bp:Path_Point Pos="16" X="18.9573044" Y="51.611944799082"/>
            <bp:Path_Point Pos="17" X="18.956794400864" Y="51.611932376808"/>
            <bp:Path_Point Pos="18" X="18.9566352" Y="51.611928499082"/>
            <bp:Path_Point Pos="19" X="18.955813488168" Y="51.611951817689"/>
            <bp:Path_Point Pos="20" X="18.955739759724" Y="51.611953909961"/>
            <LINESTRING>LINESTRING (18.960657743695 51.612137008899, 18.960110541501 51.612105643896, 18.959774447246 51.612086379359, 18.959754322927 51.612085225856, 18.959645426785 51.612078984054, 18.959455431877 51.612068093763, 18.959126615445 51.612049246382, 18.958655233785 51.612022227326, 18.958386790309 51.612006840454, 18.957723493448 51.611968821038, 18.957421446031 51.611951508026, 18.9573044 51.611944799082, 18.956794400864 51.611932376808, 18.9566352 51.611928499082, 18.955813488168 51.611951817689, 18.955739759724 51.611953909961)</LINESTRING>
         </detect_next_PE>
                          
	
	 */
//print_r($edge_joins);

$cables_from_joins_to_PE_arr=array();
$id_j=0;
foreach($cables_from_joins_to_PE->xpath('cable_to_PE_to_be_calculated') as $child)  { //cable_to_PE_to_be_calculated
 // echo "\n<br>";
	   //$attrs=$child->attributes("ID_Way");
	   foreach($child->attributes() as $a=>$b) {
		   //	echo "#87Child";print_r($b[0]);
		   if($a=='ID_Way') $ID_Way=$b[0];
		    if($a=='Joins') $Joins=$b[0];
		    if($a=='ID_Point') $ID_Point=$b[0];

		   //if($a=='Count') $Count=$b[0];
	   }
	  //echo $child->getName() . ": " . " ID_Way: ".$ID_Way. " : ";
	   settype($ID_Way, "integer");
	   settype($Joins, "integer");
	   settype($ID_Point, "integer");

	   foreach($child->xpath('found_in_path_in_edge_joins_filled_path') as $Path_Point_xml)  {
		//   detect_next_PE/LINESTRING
			
		   $id_j++;
		   $LINESTRING=$Path_Point_xml->xpath('detect_next_PE/LINESTRING/text()');
		   $LINESTRING=$LINESTRING[0];
		   settype($LINESTRING, "string");
		   $cables_from_joins_to_PE_arr[$id_j]['LINESTRING']=$LINESTRING;
		   $cables_from_joins_to_PE_arr[$id_j]['Joins']=$Joins;
		   $cables_from_joins_to_PE_arr[$id_j]['ID_Way']=$ID_Way;
		   $cables_from_joins_to_PE_arr[$id_j]['ID_Point']=$ID_Point;
		   
		   
		    foreach($Path_Point_xml->xpath('detect_next_PE') as $detect_next_PE ) {
		   
			   		foreach($detect_next_PE->attributes() as $a=>$b) {
				   		if($a=='len') $len=$b[0];
				   		if($a=='PE_ID_Join') $PE_ID_Join=$b[0];		   
		   			}
		   			settype($len, "integer");
		   			settype($PE_ID_Join, "integer");
		   			
		   	   $cables_from_joins_to_PE_arr[$id_j]['len']=$len;
			   $cables_from_joins_to_PE_arr[$id_j]['PE_ID_Join']=$PE_ID_Join;
			   }
			}
}

echo "\n<br>#150 cables_from_joins_to_PE_arr";
print_r($cables_from_joins_to_PE_arr);


 DB::getPDO()->query("truncate table Rozdzielcza_test_bzyk_Cables_to_PE");

unset($ID_Way);

foreach($cables_from_joins_to_PE_arr as $id_j=>$Path_Point_val) {
		
		$sql_Path_Point_val="insert into Rozdzielcza_test_bzyk_Cables_to_PE (the_geom,ID_Way,ID_Point,len,PE_ID_Join)
		     values (ST_GeomFromText('".$Path_Point_val['LINESTRING']."'),'".$Path_Point_val['ID_Way']."',
		     '".$Path_Point_val['ID_Point']."' ,'".$Path_Point_val['len']."','".$Path_Point_val['PE_ID_Join']."'  )   ";
		    echo "#216Query path ins:: ".$sql_Path_Point_val."  ";
		    //if(strlen($edge_joins_filled_paths[$ID_Way]['asText'])>2)
		    if(strlen($Path_Point_val['LINESTRING'])>1) 
		      DB::getPDO()->query($sql_Path_Point_val);
		    else echo " ERROR blad danych!!! dla ID_Point:".$Path_Point_val['ID_Point'];
		    //else echo "#296 path nie wygenerowalo sie !! dla ".$ID_Way." i pkt ".$pkt_A." , ".$pkt_B." \n";
			
	
}
