Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //defino los parametros de conexion a la base de datos
- $hostname_p = "localhost";
- $dbname_p = "Parcial";
- $user_p = "postgres";
- $pwd_p = "p";
- $ymaxg = "";
- //establezco conexion con la base de datos
- $connection = pg_connect("host=$hostname_p dbname=$dbname_p user=$user_p password=$pwd_p port=5432");
- //Si la conexion no fue realizada, muestra un mensaje y cierra
- if (!$connection) {
- print("<h1>No se conecto a la base de datos</h1>.");
- die();
- }
- //preparo las variables y capturo mediante POST que tablas deseo representar mediante SVG
- $tables_geom = $_POST['tables_geom'];
- $colores = $_POST['colores'];
- $tables=split(",",$tables_geom);
- $colores_capas=split(";",$colores);
- //Funcion para crear la extensión de la capa de forma dinamica, si hay solo una capa o si hay mas de una capa
- function crearExtentDinamico($tables)
- {
- $sql="";
- $sql.="select st_xmin(st_extent(g)) , st_ymin(st_extent(g)) , st_xmax(st_extent(g)), st_ymax(st_extent(g)) from ( ";
- for($i=0;$i<count($tables);$i++)
- {
- if($i==(count($tables)-1))
- {
- $sql.=" select st_extent(st_transform(the_geom,3115)) as g from ".$tables[$i]." ";
- }
- else
- {
- $sql.=" select st_extent(st_transform(the_geom,3115)) as g from ".$tables[$i]." union all";
- }
- }
- $sql.=") as foo;";
- return $sql;
- }
- //funcio que se encarga de crear la cabecera del XML y el objeto SVG, especial enfasis en el viewbox
- function crearXML($conex,$tables)
- {
- global $ymaxg;
- $myresult = pg_exec($conex, crearExtentDinamico($tables) );
- $xmin = pg_result($myresult, 0, 0);
- $ymin = pg_result($myresult, 0, 1);
- $xmax = pg_result($myresult, 0, 2);
- $ymax = pg_result($myresult, 0, 3);
- $ymaxg = $ymax;
- $valor= $xmin." 0 ".($xmax-$xmin)." ".($ymax-$ymin);
- print('<?xml version="1.0" encoding="utf-8" standalone="no"?>'. "n");
- print('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">');
- echo '<svg id="base" width="1500" height="900" viewBox="'.$valor.'" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >'."n";
- }
- //crea las capas dependiendo de el tipo de objeto a representar
- function crearLayers($conex,$layer, $colores)
- {
- global $ymaxg;
- $sql2 = "select type from geometry_columns where f_table_name = '".$layer."';";
- $myresult = pg_exec($conex, $sql2);
- $type_geom = pg_result($myresult, 0, 0);
- //recupera mediante SQL los objetos en su representacion en formato SVG,
- //previamente transformados a otro sistema de referencia ( EPSG:3115)
- $sql3 = "select st_assvg(st_transform(the_geom,3115)) from ".$layer.";";
- $myresult = pg_exec($conex, $sql3);
- switch ($type_geom)
- {
- case "MULTIPOLYGON":
- {
- poligon($myresult,$ymaxg , $colores);
- break;
- }
- case "MULTILINESTRING":
- {
- lineas($myresult,$ymaxg , $colores);
- break;
- }
- case "POINT":
- {
- puntos($myresult,$ymaxg , $colores);
- break;
- }
- }
- }
- //Crea poligonos
- function poligon($result,$ymax, $colores){
- echo '<g transform="matrix(1 0 0 1 0 '.$ymax.')" >'."n";
- for ($lt = 0; $lt < pg_numrows($result); $lt++) {
- echo '<path id="'.$lt.'" fill="'.$colores.'" stroke="rgb(0,0,0)" d="'. pg_result($result,$lt,0). '" />'."n";
- }
- echo '</g>'."n";
- }
- //Crea lineas
- function lineas($result,$ymax , $colores){
- echo '<g transform="matrix(1 0 0 1 0 '.$ymax.')" >'."n";
- for ($lt = 0; $lt < pg_numrows($result); $lt++) {
- echo '<path id="'.$lt.'" fill="none" stroke-width="5" stroke="'.$colores.'" d="'. pg_result($result,$lt,0). '" />'."n";
- }
- echo '</g>'."n";
- }
- //Crea puntos
- function puntos($result,$ymax , $colores){
- echo '<g transform="matrix(1 0 0 1 0 '.$ymax.')" >'."n";
- for ($lt = 0; $lt < pg_numrows($result); $lt++) {
- echo '<circle '.pg_result($result,$lt,0).' r="50" stroke="rgb(0,0,0)" stroke-width="15" fill="'.$colores.'" />'."n";
- }
- echo '</g>'."n";
- }
- //CREAR MAPA
- crearXML($connection,$tables);
- for($i=0;$i<count($tables);$i++)
- {
- crearLayers($connection,$tables[$i],$colores_capas[$i]);
- }
- print('</svg>');
- //destruye los objetos resultantes y cierra la conexion a la base de datos
- unset($myresult);
- pg_close($connection);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement