From 5faef42eb74f8af67eba360e47e7bd8f2ef57d64 Mon Sep 17 00:00:00 2001 From: Edmar Moretti Date: Sun, 18 Sep 2011 21:10:18 +0000 Subject: [PATCH] Adaptação da biblioteca TME para funcionamento no i3Geo --- pacotes/tme/TME_Engine.php | 6 +++--- pacotes/tme/TME_i3geo.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pacotes/tme/TME_i3geo_DataConnector.php | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 306 insertions(+), 3 deletions(-) create mode 100644 pacotes/tme/TME_i3geo.php create mode 100644 pacotes/tme/TME_i3geo_DataConnector.php diff --git a/pacotes/tme/TME_Engine.php b/pacotes/tme/TME_Engine.php index 68aa9ad..bcb3775 100644 --- a/pacotes/tme/TME_Engine.php +++ b/pacotes/tme/TME_Engine.php @@ -352,7 +352,7 @@ class ThematicMap . " " . PHP_EOL; // End of shared style $kmlFolder = " " . PHP_EOL - . " Years" . PHP_EOL + . " Colunas" . PHP_EOL . " 1" . PHP_EOL; if ($this->timeType == 'series') { @@ -597,7 +597,7 @@ class ThematicMap // Add title $kml .= " " . PHP_EOL - . " Title" . PHP_EOL + . " Título" . PHP_EOL . " " . PHP_EOL . " files/brand.png" . PHP_EOL . " " . PHP_EOL @@ -609,7 +609,7 @@ class ThematicMap // Add legend if ($this->showLegend && $this->colourType == 'scale') { $kml .= " " . PHP_EOL - . " Legend" . PHP_EOL + . " Legenda" . PHP_EOL . " " . PHP_EOL . " files/legend.png" . PHP_EOL . " " . PHP_EOL diff --git a/pacotes/tme/TME_i3geo.php b/pacotes/tme/TME_i3geo.php new file mode 100644 index 0000000..497c539 --- /dev/null +++ b/pacotes/tme/TME_i3geo.php @@ -0,0 +1,59 @@ + +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 3 of the GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// http://www.gnu.org/licenses/ +// +// +//adaptado para o i3Geo por Edmar Moretti + +// This file shows how maps can be created with the DataConnector +// and ThematicMap classes. + + +// Can be changed to another data connector class +require_once ('TME_i3geo_DataConnector.php'); + +// Include engine class +require_once ('TME_Engine.php'); +if(!isset($_GET["sid"])) +{echo "Erro. Acesso não permitido";exit;} +$dataConnector = new DataConnector($_GET["sid"]); +$colunas = str_replace(","," ",$_GET["colunasvalor"]); +$colunas = explode(" ",$colunas); +$dataStore = $dataConnector->getDataStore($_GET["nomelayer"],$colunas,$_GET["colunanomeregiao"],$_GET["ext"],$_GET["titulo"],$_GET["descricao"]); +//choropleth,prism,bar,symbol +$ano = ""; +$tipo = "slider"; +if(count($colunas) == 1){ + $ano = $colunas[0]; + $tipo = "year"; +} + +$parameters = array( 'mapType' => 'bar', + 'indicator' => 'valores', + 'year' => $ano, + 'classification' => 'equal', + 'mapTitle' => 'Título do Mapa', + 'timeType' => $tipo //para mais de um ano, escolha slider ou series + ); + +// Create thematic map object +$map = new ThematicMap($dataStore, $parameters); +$file = $map->getKML(); + +echo "

$file"; + +?> \ No newline at end of file diff --git a/pacotes/tme/TME_i3geo_DataConnector.php b/pacotes/tme/TME_i3geo_DataConnector.php new file mode 100644 index 0000000..e04c1ad --- /dev/null +++ b/pacotes/tme/TME_i3geo_DataConnector.php @@ -0,0 +1,244 @@ + +// This program is free software; you can redistribute it and/or +// modify it under the terms of version 3 of the GNU General +// Public License as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// http://www.gnu.org/licenses/ +// +//adaptado para o i3Geo por Edmar Moretti + + + +// Set database access information as constants +// Should be stored outside of the Web directory +//DEFINE ('DB_USER', 'myuser'); +//DEFINE ('DB_PASSWORD', 'mypass'); +//DEFINE ('DB_HOST', 'localhost'); +//DEFINE ('DB_NAME', 'tme'); + + +class DataConnector +{ + public $featureTable = 'country_simpl'; + public $indicatorTable = 'indicator'; + public $valuesTable = 'indicator_values'; + public $map_file; + public $postgis_mapa; + private $dbc; + + // Constructor + function __construct($sid) + { + if (!function_exists('ms_GetVersion')) + { + if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN')) + { + if(!@dl('php_mapscript_48.dll')) + dl('php_mapscript.dll'); + } + else + {dl('php_mapscript.so');} + } + include("../../classesphp/carrega_ext.php"); + //verificação de segurança + $_SESSION = array(); + session_name("i3GeoPHP"); + session_id($sid); + session_start(); + if(@$_SESSION["fingerprint"]) + { + $f = explode(",",$_SESSION["fingerprint"]); + if (md5('I3GEOSEC' . $_SERVER['HTTP_USER_AGENT'] . session_id()) != $f[0] && !in_array($_GET["telaR"],$f) ) + {exit;} + } + else + {exit;} + if(!isset($_SESSION["map_file"])) + {exit;} + $this->map_file = $_SESSION["map_file"]; + $this->postgis_mapa = $_SESSION["postgis_mapa"]; + } + // Fetch all indicators + function getIndicators(){ + $sql = 'SELECT id, name, description, source FROM indicator ORDER BY name'; + $result = $this->dbc->query($sql); + + // Loop thorough all indicators + while($row = $result->fetch_row()) + { + // Add values to array + $indicators[] = array('id' => $row[0], + 'name' => $row[1], + 'description' => $row[2], + 'source' => $row[3]); + } + + // Make JSON encoded array + $json = json_encode(array('indicators' => $indicators)); + return $json; + } + + // Fetch all avaialbe years for one indicator + function getIndicatorYears($indicatorID){ + $sql = "SELECT DISTINCT year FROM indicator_values WHERE variable=$indicatorID ORDER BY year"; + $result = $this->dbc->query($sql); + + // Loop thorough all years + while($row = $result->fetch_row()) + { + // Add values to array + $years[] = array('year' => $row[0]); + } + // Make JSON encoded array + $json = json_encode(array('years' => $years)); + return $json; + } + + // Make data store + function getDataStore($nomelayer,$colunasvalor,$colunanomeregiao,$ext,$titulo,$descricao){ //$indicatorID, $year, $region){ + include("../../classesphp/funcoes_gerais.php"); + $versao = versao(); + $versao = $versao["principal"]; + $mapa = ms_newMapObj($this->map_file); + $layer = $mapa->getlayerbyname($nomelayer); + if($ext && $ext != ""){ + $e = explode(" ",$ext); + $extatual = $mapa->extent; + $extatual->setextent((min($e[0],$e[2])),(min($e[1],$e[3])),(max($e[0],$e[2])),(max($e[1],$e[3]))); + } + $layer->set("template","none.html"); + $existesel = "nao"; + if (($this->postgis_mapa != "") && ($this->postgis_mapa != " ")) + { + if ($layer->connectiontype == MS_POSTGIS) + { + $lcon = $layer->connection; + if (($lcon == " ") || ($lcon == "") || (in_array($lcon,array_keys($this->postgis_mapa)))) + { + if(($lcon == " ") || ($lcon == "")) + {$layer->set("connection",$this->postgis_mapa);} + else + {$layer->set("connection",$this->postgis_mapa[$lcon]);} + } + } + } + $itens = pegaItens($layer,$mapa); + carregaquery2($this->map_file,$layer,$mapa); + if ($layer->getNumresults() > 0){$existesel = "sim";} + if ($existesel == "nao") + {$layer->querybyrect($mapa->extent);} + $layer->open(); + $res_count = $layer->getNumresults(); + $dataStore = array(); + $dataStore['indicators']['valores'] = array( + "name"=>$titulo, + "description"=>$descricao, + "source"=>"", + "decimals"=>2, + "max"=>0, + "min"=>0 + ); + for ($i = 0; $i < $res_count; $i++) + { + $valitem = array(); + if($versao == 6) + {$shape = $layer->getShape($layer->getResult($i));} + else{$shape = $layer->getFeature($layer->getResult($i)->shapeindex);} + $pt = $shape->getCentroid(); + $dataStore['features'][$i] = array( + "featureID"=>$i, + "name"=>$shape->values[$colunanomeregiao], + "lon"=>$pt->x, + "lat"=>$pt->y, + "wkt"=>$shape->toWkt() + ); + //[0] é o ano + foreach($colunasvalor as $colunavalor){ + $valor = number_format($shape->values[$colunavalor], 2, '.', ''); + $dataStore['indicators']['valores']['values'][$colunavalor][$i] = $valor; + $indicatorYears[$colunavalor] = $colunavalor; + $todosV[] = $valor; + } + } + $fechou = $layer->close(); + $dataStore['indicators']['valores']['years'] = $indicatorYears; + $dataStore['indicators']['valores']['max'] = max($todosV); + $dataStore['indicators']['valores']['min'] = min($todosV); + echo "

";
+		var_dump($dataStore);
+		return $dataStore;
+        /*
+		$sqlregion = '';
+        if ($region) $sqlregion = "region = $region AND";
+        $sqlyear = '';
+        if ($year) $sqlyear = "AND year = $year";
+    	// Add features - exclude Antarctica
+    	$sql = "SELECT un AS featureID, name, lon, lat, AsText(geom) AS wkt
+                FROM $this->featureTable
+                WHERE $sqlregion un != 10
+                ORDER BY featureID";
+
+    	$features = $this->dbc->query($sql);
+    	while($row = $features->fetch_array(MYSQLI_ASSOC))
+    	{
+    		// First field should be feature id
+    		$featureID = array_shift($row);
+    		// Add feature to dataStore
+    		$dataStore['features'][$featureID] = $row;
+    	}
+
+
+    		$indicatorYears = array();
+
+            // Select indicator metadata
+    		$sql = "SELECT name, description, source, decimals,
+                        (SELECT ROUND(MAX(value),decimals) FROM indicator_values, $this->featureTable WHERE variable=$indicatorID AND indicator_values.area=un $sqlyear) AS max,
+                        (SELECT ROUND(MIN(value),decimals) FROM indicator_values, $this->featureTable WHERE variable=$indicatorID AND indicator_values.area=un $sqlyear) AS min
+                    FROM $this->indicatorTable
+                    WHERE id=$indicatorID";
+
+    		$result = $this->dbc->query($sql);
+    		$indicator = $result->fetch_assoc();
+    		$precision = $indicator['decimals'];
+
+    		// Add indicator to dataStore
+    		$dataStore['indicators'][$indicatorID] = $indicator;
+
+    		// Select indicator values (only values that have features)
+    		$sql = "SELECT indvalues.area AS featureID, indvalues.year, indvalues.value
+                    FROM $this->valuesTable AS indvalues, $this->featureTable
+                    WHERE indvalues.variable=$indicatorID
+                    AND indvalues.area=un
+                    $sqlyear
+                    ORDER BY indvalues.value"; // Needed for qunatiles calculation
+
+            $result = $this->dbc->query($sql);
+
+    		// Add indicator values to dataStore
+    		while($row = $result->fetch_row())
+    		{
+                $dataStore['indicators'][$indicatorID]['values'][$row[1]][$row[0]] = number_format($row[2], $precision, '.', '');
+
+                // Find all years with values (could also be a separate sql for better performance)
+                $indicatorYears[$row[1]] = $row[1];
+    		}
+            sort($indicatorYears);
+    		$dataStore['indicators'][$indicatorID]['years'] = $indicatorYears;
+    	return $dataStore;
+		*/
+    }
+}
+
+
+?>
\ No newline at end of file
--
libgit2 0.21.2