<?php
header('Content-Type: application/json;charset= utf-8');
ini_set('memory_limit','10048M');
set_time_limit(0);
ini_set('serialize_precision', -1);
require("../../dbinfo.php");
$con = mysqli_connect($sqlname, $username, $password, $db);
mysqli_set_charset($con,"utf8");
if(!$con){exit;}
$res = array();
$sql = "SELECT ASTEXT(SHAPE) AS geom, id, padron, colector, profundidad, tipo,";
$sql .= "  seccion, distmed, distmei, alta_fuent , alta_fecha, etapa, ccz, accion ";
$sql .= " , z_col, via, calle , empresa, tramite, descripcion";
$sql .= " FROM conexiones WHERE activo = 1 ";


$filtroccz = 'CCZ01';

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
	if(isset($_GET["ccz"]))
	{
        $filtroccz = $_GET["ccz"];	
	}
}
$sql .= " AND  ccz = '" . $filtroccz . "'";


$resultado = mysqli_query($con,$sql);
if ($resultado != FALSE){
	if (mysqli_num_rows($resultado) > 0){
                    $features = [];
                        while($row = mysqli_fetch_assoc($resultado))
                        {
                            $str = $row["geom"];
                            $str = substr( $str, 11, (strlen( $str) - 1) - 11);

                            $coordenadas = array();
                            foreach( str_getcsv( $str) as $k => $v) {
                                list( $x, $y) = explode( ' ', $v);
                                    $punto = array();
                                    $punto[0] = sprintf("%.9f", floatval($x));
                                    $punto[1] = sprintf("%.9f", floatval($y));
                                    array_push($coordenadas,$punto);
                            }
                            $feature = array(
                            'type' => 'Feature',
                            'geometry' => array(
                            'type' => 'LineString',
                            'coordinates' => $coordenadas
                            ),
                            'properties' => array(
                                'id'=>$row["id"],
                                'padron'=>$row["padron"],
                                'colector'=>$row["colector"],
                                'profundidad'=>$row["profundidad"],
                                'tipo'=>$row["tipo"],
                                'seccion'=>$row["seccion"],
                                'distmed'=>$row["distmed"],
                                'distmei'=>$row["distmei"],
                                'alta_fuent'=>$row["alta_fuent"],
                                'alta_fecha'=>$row["alta_fecha"],
                                'etapa'=>$row["etapa"],
                                'ccz'=>$row["ccz"],
                                'accion'=>$row["accion"],                              
                                'z_col'=>number_format($row["z_col"],3),
                                'via'=>$row["via"],
                                'calle'=>$row["calle"],
                                'empresa'=>$row["empresa"],
                                'tramite'=>$row["tramite"],
                                'descripcion'=>$row["descripcion"]
                                                )
                                     );
                        //Push each $feature to the empty array $features created earlier
                        array_push($features, $feature);
                        }
	}} 
       
$res = '{"type": "FeatureCollection", "name": "Conexiones","crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },"features":';
$res .= json_encode($features,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$res .= '}';
echo $res; 





                    
?>