Advertisement
Guest User

Untitled

a guest
Oct 31st, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2.  
  3. //defino los parametros de conexion a la base de datos
  4.  
  5. $hostname_p = "localhost";
  6. $dbname_p = "Parcial";
  7. $user_p = "postgres";
  8. $pwd_p = "p";
  9. $ymaxg = "";
  10.  
  11. //establezco conexion con la base de datos
  12. $connection = pg_connect("host=$hostname_p dbname=$dbname_p user=$user_p password=$pwd_p port=5432");
  13.  
  14. //Si la conexion no fue realizada, muestra un mensaje y cierra
  15. if (!$connection) {
  16. print("<h1>No se conecto a la base de datos</h1>.");
  17. die();
  18. }
  19.  
  20.  
  21. //preparo las variables y capturo mediante POST que tablas deseo representar mediante SVG
  22. $tables_geom = $_POST['tables_geom'];
  23. $colores = $_POST['colores'];
  24. $tables=split(",",$tables_geom);
  25. $colores_capas=split(";",$colores);
  26.  
  27.  
  28.  
  29. //Funcion para crear la extensión de la capa de forma dinamica, si hay solo una capa o si hay mas de una capa
  30. function crearExtentDinamico($tables)
  31. {
  32. $sql="";
  33. $sql.="select st_xmin(st_extent(g)) , st_ymin(st_extent(g)) , st_xmax(st_extent(g)), st_ymax(st_extent(g)) from ( ";
  34.  
  35. for($i=0;$i<count($tables);$i++)
  36. {
  37. if($i==(count($tables)-1))
  38. {
  39. $sql.=" select st_extent(st_transform(the_geom,3115)) as g from ".$tables[$i]." ";
  40. }
  41. else
  42. {
  43. $sql.=" select st_extent(st_transform(the_geom,3115)) as g from ".$tables[$i]." union all";
  44. }
  45. }
  46. $sql.=") as foo;";
  47.  
  48. return $sql;
  49. }
  50.  
  51.  
  52. //funcio que se encarga de crear la cabecera del XML y el objeto SVG, especial enfasis en el viewbox
  53. function crearXML($conex,$tables)
  54. {
  55. global $ymaxg;
  56.  
  57. $myresult = pg_exec($conex, crearExtentDinamico($tables) );
  58. $xmin = pg_result($myresult, 0, 0);
  59. $ymin = pg_result($myresult, 0, 1);
  60. $xmax = pg_result($myresult, 0, 2);
  61. $ymax = pg_result($myresult, 0, 3);
  62.  
  63. $ymaxg = $ymax;
  64.  
  65. $valor= $xmin." 0 ".($xmax-$xmin)." ".($ymax-$ymin);
  66. print('<?xml version="1.0" encoding="utf-8" standalone="no"?>'. "n");
  67. print('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">');
  68. 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";
  69.  
  70. }
  71.  
  72. //crea las capas dependiendo de el tipo de objeto a representar
  73. function crearLayers($conex,$layer, $colores)
  74. {
  75. global $ymaxg;
  76. $sql2 = "select type from geometry_columns where f_table_name = '".$layer."';";
  77. $myresult = pg_exec($conex, $sql2);
  78. $type_geom = pg_result($myresult, 0, 0);
  79.  
  80. //recupera mediante SQL los objetos en su representacion en formato SVG,
  81. //previamente transformados a otro sistema de referencia ( EPSG:3115)
  82.  
  83. $sql3 = "select st_assvg(st_transform(the_geom,3115)) from ".$layer.";";
  84. $myresult = pg_exec($conex, $sql3);
  85.  
  86. switch ($type_geom)
  87. {
  88. case "MULTIPOLYGON":
  89. {
  90. poligon($myresult,$ymaxg , $colores);
  91. break;
  92. }
  93. case "MULTILINESTRING":
  94. {
  95. lineas($myresult,$ymaxg , $colores);
  96. break;
  97. }
  98. case "POINT":
  99. {
  100. puntos($myresult,$ymaxg , $colores);
  101. break;
  102. }
  103. }
  104.  
  105. }
  106.  
  107.  
  108. //Crea poligonos
  109. function poligon($result,$ymax, $colores){
  110. echo '<g transform="matrix(1 0 0 1 0 '.$ymax.')" >'."n";
  111. for ($lt = 0; $lt < pg_numrows($result); $lt++) {
  112. echo '<path id="'.$lt.'" fill="'.$colores.'" stroke="rgb(0,0,0)" d="'. pg_result($result,$lt,0). '" />'."n";
  113. }
  114. echo '</g>'."n";
  115. }
  116.  
  117.  
  118. //Crea lineas
  119. function lineas($result,$ymax , $colores){
  120. echo '<g transform="matrix(1 0 0 1 0 '.$ymax.')" >'."n";
  121. for ($lt = 0; $lt < pg_numrows($result); $lt++) {
  122. echo '<path id="'.$lt.'" fill="none" stroke-width="5" stroke="'.$colores.'" d="'. pg_result($result,$lt,0). '" />'."n";
  123. }
  124. echo '</g>'."n";
  125. }
  126.  
  127.  
  128. //Crea puntos
  129. function puntos($result,$ymax , $colores){
  130. echo '<g transform="matrix(1 0 0 1 0 '.$ymax.')" >'."n";
  131. for ($lt = 0; $lt < pg_numrows($result); $lt++) {
  132. echo '<circle '.pg_result($result,$lt,0).' r="50" stroke="rgb(0,0,0)" stroke-width="15" fill="'.$colores.'" />'."n";
  133. }
  134. echo '</g>'."n";
  135. }
  136.  
  137.  
  138.  
  139. //CREAR MAPA
  140.  
  141. crearXML($connection,$tables);
  142.  
  143. for($i=0;$i<count($tables);$i++)
  144. {
  145. crearLayers($connection,$tables[$i],$colores_capas[$i]);
  146. }
  147. print('</svg>');
  148.  
  149.  
  150. //destruye los objetos resultantes y cierra la conexion a la base de datos
  151. unset($myresult);
  152. pg_close($connection);
  153.  
  154. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement