Commit e0a21a3909d0df150be82d8cdb53dd718e2d7174

Authored by Edmar Moretti
1 parent c8f5d953

Adicionada a análise de cluster

Showing 1 changed file with 161 additions and 66 deletions   Show diff stats
classesphp/classe_analise.php
@@ -132,76 +132,29 @@ $limitepontos - "TRUE"|"FALSE" limita o resultado ao limite geográfico dos ponto @@ -132,76 +132,29 @@ $limitepontos - "TRUE"|"FALSE" limita o resultado ao limite geográfico dos ponto
132 Include: 132 Include:
133 <class.palette.php> 133 <class.palette.php>
134 */ 134 */
135 - function analiseDistriPt($locaplic,$dir_tmp,$R_path,$numclasses,$tipo,$cori,$corf,$tmpurl,$sigma="",$limitepontos="TRUE") 135 + function analiseDistriPt($locaplic,$dir_tmp,$R_path,$numclasses,$tipo,$cori,$corf,$tmpurl,$sigma="",$limitepontos="TRUE",$tema2="")
136 { 136 {
137 - $prjMapa = $this->mapa->getProjection();  
138 - $prjTema = $this->layer->getProjection();  
139 - $layerPt = $this->layer;  
140 - $layerPt->set("tolerance",0);  
141 - $layerPt->set("template","none.htm");  
142 - $nomefinal = nomeRandomico();  
143 - $nomearq = $this->diretorio."/".$nomefinal;  
144 - $itemspt = pegaItens($layerPt);  
145 - $existesel = "nao";  
146 - if (file_exists(($this->arquivo)."qy"))  
147 - {$this->mapa->loadquery(($this->arquivo)."qy");}  
148 - if ($layerPt->getNumresults() > 0){$existesel = "sim";}  
149 - if ($existesel == "nao")  
150 - {$layerPt->queryByrect($this->mapa->extent);}  
151 - $res_count = $layerPt->getNumresults();  
152 - $pontos = array();  
153 - //pega um shape especifico  
154 - $layerPt->open();  
155 - if (($prjTema != "") && ($prjMapa != $prjTema)) 137 + //
  138 + //pega os dados do tema dois para as funções que o utilizam
  139 + //
  140 + $dados1 = $this->gravaCoordenadasPt($this->nome,$limitepontos);
  141 + $nomearq = $dados1["prefixoarquivo"];
  142 + $dimx = $dados1["dimx"];
  143 + $dimy = $dados1["dimy"];
  144 + if (isset($tema2) && $tema2 != "")
156 { 145 {
157 - $projInObj = ms_newprojectionobj($prjTema);  
158 - $projOutObj = ms_newprojectionobj($prjMapa);  
159 - }  
160 - for ($i = 0; $i < $res_count; $i++)  
161 - {  
162 - $result = $layerPt->getResult($i);  
163 - $shp_index = $result->shapeindex;  
164 - $shape = $layerPt->getshape(-1, $shp_index);  
165 - $lineo = $shape->line(0);  
166 - $pt = $lineo->point(0);  
167 - if (($prjTema != "") && ($prjMapa != $prjTema))  
168 - {  
169 - $pt->project($projInObj, $projOutObj);  
170 - }  
171 - $pontos[] = $pt->x." ".$pt->y."\n";  
172 - $pontosx[] = $pt->x;  
173 - $pontosy[] = $pt->y; 146 + $dados2 = $this->gravaCoordenadasPt($tema2,$limitepontos);
  147 + $nomearq2 = $dados2["prefixoarquivo"];
  148 + $dimx2 = $dados2["dimx"];
  149 + $dimy2 = $dados2["dimy"];
174 } 150 }
175 - $layerPt->close();  
176 - //grava o arquivo com os pontos em x  
177 - $f = fopen($nomearq."x","w");  
178 - foreach ($pontosx as $pt)  
179 - {fwrite($f,$pt."\n");}  
180 - fclose($f);  
181 - //grava o arquivo com os pontos em y  
182 - $f = fopen($nomearq."y","w");  
183 - foreach ($pontosy as $pt)  
184 - {fwrite($f,$pt."\n");}  
185 - fclose($f);  
186 - if ($limitepontos == "TRUE")  
187 - {  
188 - $xi = (min($pontosx));  
189 - $xf = (max($pontosx));  
190 - $yi = (min($pontosy));  
191 - $yf = (max($pontosy));  
192 - }  
193 - else  
194 - {  
195 - $ext = $this->mapa->extent;  
196 - $xi = $ext->minx;  
197 - $xf = $ext->maxx;  
198 - $yi = $ext->miny;  
199 - $yf = $ext->maxy;  
200 - }  
201 - $dimx = "c(".$xi.",".$xf.")";  
202 - $dimy = "c(".$yi.",".$yf.")";  
203 switch ($tipo) 151 switch ($tipo)
204 { 152 {
  153 + //cluster espacial
  154 + case "cluster":
  155 + $this->mapaCluster($nomearq,$nomearq2,$dimx,$dimy,$dir_tmp,$R_path,$locaplic);
  156 + return "ok";
  157 + break;
205 //delaunay e voronoi 158 //delaunay e voronoi
206 case "deldir": 159 case "deldir":
207 $this->mapaDeldir($nomearq,$dir_tmp,$R_path,$locaplic); 160 $this->mapaDeldir($nomearq,$dir_tmp,$R_path,$locaplic);
@@ -390,6 +343,61 @@ $locaplic - Onde fica o I3Geo. @@ -390,6 +343,61 @@ $locaplic - Onde fica o I3Geo.
390 $r = executaR($rcode,$dir_tmp,$R_path); 343 $r = executaR($rcode,$dir_tmp,$R_path);
391 } 344 }
392 /* 345 /*
  346 +function: mapaCluster
  347 +
  348 +Gera um mapa de cluster.
  349 +
  350 +Executa script R para gerar os dados.
  351 +
  352 +parameters:
  353 +$arqpt - Prefixo dos arquivos em disco com os pontos.
  354 +
  355 +$dimx - Range em x no formato R c(-54,-53).
  356 +
  357 +$dimy - Range em y no formato R c(-25,-23).
  358 +
  359 +$dir_tmp - Diretório temporário do mapserver.
  360 +
  361 +$R_path - Onde fica o R.
  362 +
  363 +$locaplic - Onde fica o I3Geo.
  364 +
  365 +$sigma - Bandwidth for kernel smoother in "smooth" option.
  366 +*/
  367 + function mapaCluster($arqpt,$arqpt2,$dimx,$dimy,$dir_tmp,$R_path,$locaplic)
  368 + {
  369 + $gfile_name = nomeRandomico(20);
  370 + $rcode[] = 'dadosx<-scan("'.$arqpt.'x")';
  371 + $rcode[] = 'dadosy<-scan("'.$arqpt.'y")';
  372 + $rcode[] = 'dadosx2<-scan("'.$arqpt2.'x")';
  373 + $rcode[] = 'dadosy2<-scan("'.$arqpt2.'y")';
  374 + $rcode[] = 'd1<-data.frame(cbind(dadosx,dadosy))';
  375 + $rcode[] = 'names(d1)<-(c("x","y"))';
  376 + $rcode[] = 'd2<-data.frame(cbind(dadosx2,dadosy2))';
  377 + $rcode[] = 'names(d2)<-(c("col1","col2"))';
  378 + $rcode[] = 'd2<-as.matrix.data.frame(d2)';
  379 + if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN'))
  380 + {
  381 + $lib = '.libPaths("'.$locaplic.'/pacotes/rlib/win")';
  382 + if(file_exists($locaplic.'/pacotes/rlib/win'))
  383 + $rcode[] = $lib;
  384 + }
  385 + else
  386 + {
  387 + if(file_exists($locaplic."/pacotes/rlib/linux"))
  388 + {
  389 + $lib = '.libPaths("'.$locaplic.'/pacotes/rlib/linux")';
  390 + $rcode[] = $lib;
  391 + }
  392 + }
  393 + $rcode[] = 'library(spatclus)';
  394 + $rcode[] = 'RES <- clus(d1,d2,limx='.$dimx.',limy='.$dimy.',eps=0.2)';
  395 + //var_dump($rcode);
  396 + $r = executaR($rcode,$dir_tmp,$R_path,$gfile_name);
  397 + return "ok";
  398 + }
  399 +
  400 +/*
393 function: mapaKernel 401 function: mapaKernel
394 402
395 Gera um mapa de kernel. 403 Gera um mapa de kernel.
@@ -1291,7 +1299,7 @@ $locaplic - Localização do I3geo. @@ -1291,7 +1299,7 @@ $locaplic - Localização do I3geo.
1291 {$mapt = ms_newMapObj($locaplic."\\aplicmap\\novotema.map");} 1299 {$mapt = ms_newMapObj($locaplic."\\aplicmap\\novotema.map");}
1292 else 1300 else
1293 {$mapt = ms_newMapObj($locaplic."/aplicmap/novotema.map");} 1301 {$mapt = ms_newMapObj($locaplic."/aplicmap/novotema.map");}
1294 - $novolayer = criaLayer($this->mapa,MS_LAYER_POINT,MS_DEFAULT,("Centróide (".$nomecentroide.")"),$metaClasse="SIM"); 1302 + $novolayer = criaLayer($this->mapa,MS_LAYER_POINT,MS_DEFAULT,("Centróide (".$nomeCentroides.")"),$metaClasse="SIM");
1295 $novolayer->set("data",$nomeshp.".shp"); 1303 $novolayer->set("data",$nomeshp.".shp");
1296 $novolayer->setmetadata("DOWNLOAD","SIM"); 1304 $novolayer->setmetadata("DOWNLOAD","SIM");
1297 $novolayer->set("template","none.htm"); 1305 $novolayer->set("template","none.htm");
@@ -2030,6 +2038,93 @@ $operacao - Tipo de análise. @@ -2030,6 +2038,93 @@ $operacao - Tipo de análise.
2030 return($novonomelayer); 2038 return($novonomelayer);
2031 } 2039 }
2032 /* 2040 /*
  2041 +function: gravaCoordenadasPt
  2042 +
  2043 +Lê as coordenadas de um tema pontual e grava em arquivos.
  2044 +
  2045 +Essa função é utilizada nas opções que utilizam o R para cálculos e necessitam ler as coordenadas dos pontos.
  2046 +
  2047 +Parameters:
  2048 +
  2049 +tema - nome do tema com os pontos
  2050 +
  2051 +limitepontos - FALSE para considerar a extensão geográfica do mapa atual e TRUE para considerar como limite as ocorrências pontuais do tema
  2052 +
  2053 +return:
  2054 +
  2055 +array com as dimensões em x e y e nome dos arquivos com x e y gerados.
  2056 +*/
  2057 +function gravaCoordenadasPt($tema,$limitepontos="TRUE")
  2058 +{
  2059 + $prjMapa = $this->mapa->getProjection();
  2060 + $layerPt = $this->mapa->getlayerbyname($tema);
  2061 + $prjTema = $layerPt->getProjection();
  2062 + $layerPt->set("tolerance",0);
  2063 + $layerPt->set("template","none.htm");
  2064 + $nomefinal = nomeRandomico();
  2065 + $nomearq = $this->diretorio."/".$nomefinal;
  2066 + $itemspt = pegaItens($layerPt);
  2067 + $existesel = "nao";
  2068 + if (file_exists(($this->arquivo)."qy"))
  2069 + {$this->mapa->loadquery(($this->arquivo)."qy");}
  2070 + if ($layerPt->getNumresults() > 0){$existesel = "sim";}
  2071 + if ($existesel == "nao")
  2072 + {$layerPt->queryByrect($this->mapa->extent);}
  2073 + $res_count = $layerPt->getNumresults();
  2074 + $pontos = array();
  2075 + //pega um shape especifico
  2076 + $layerPt->open();
  2077 + if (($prjTema != "") && ($prjMapa != $prjTema))
  2078 + {
  2079 + $projInObj = ms_newprojectionobj($prjTema);
  2080 + $projOutObj = ms_newprojectionobj($prjMapa);
  2081 + }
  2082 + for ($i = 0; $i < $res_count; $i++)
  2083 + {
  2084 + $result = $layerPt->getResult($i);
  2085 + $shp_index = $result->shapeindex;
  2086 + $shape = $layerPt->getshape(-1, $shp_index);
  2087 + $lineo = $shape->line(0);
  2088 + $pt = $lineo->point(0);
  2089 + if (($prjTema != "") && ($prjMapa != $prjTema))
  2090 + {
  2091 + $pt->project($projInObj, $projOutObj);
  2092 + }
  2093 + $pontos[] = $pt->x." ".$pt->y."\n";
  2094 + $pontosx[] = $pt->x;
  2095 + $pontosy[] = $pt->y;
  2096 + }
  2097 + $layerPt->close();
  2098 + //grava o arquivo com os pontos em x
  2099 + $f = fopen($nomearq."x","w");
  2100 + foreach ($pontosx as $pt)
  2101 + {fwrite($f,$pt."\n");}
  2102 + fclose($f);
  2103 + //grava o arquivo com os pontos em y
  2104 + $f = fopen($nomearq."y","w");
  2105 + foreach ($pontosy as $pt)
  2106 + {fwrite($f,$pt."\n");}
  2107 + fclose($f);
  2108 + if ($limitepontos == "TRUE")
  2109 + {
  2110 + $xi = (min($pontosx));
  2111 + $xf = (max($pontosx));
  2112 + $yi = (min($pontosy));
  2113 + $yf = (max($pontosy));
  2114 + }
  2115 + else
  2116 + {
  2117 + $ext = $this->mapa->extent;
  2118 + $xi = $ext->minx;
  2119 + $xf = $ext->maxx;
  2120 + $yi = $ext->miny;
  2121 + $yf = $ext->maxy;
  2122 + }
  2123 + $dimx = "c(".$xi.",".$xf.")";
  2124 + $dimy = "c(".$yi.",".$yf.")";
  2125 + return array("dimx"=>$dimx,"dimy"=>$dimy,"arqx"=>($nomearq."x"),"arqy"=>($nomearq."y"),"prefixoarquivo"=>$nomearq);
  2126 +}
  2127 +/*
2033 function unserializeGeo 2128 function unserializeGeo
2034 2129
2035 Deserializa um arquivo de geometrias. 2130 Deserializa um arquivo de geometrias.