Commit 9c81c6853808662f957d1213b70ac0609e8f97a6

Authored by Edmar Moretti
1 parent e1144583

Inclusão das funções de geração de grade Delaunay e diagrama de Voronoi na opção…

… de análise de distribuição de pontos. Além dos métodos criados em classe_analise.php, foi acrescentado um novo botão na ferramenta de análise de distribuição de pontos
classesphp/classe_analise.php
@@ -148,8 +148,11 @@ Include: @@ -148,8 +148,11 @@ Include:
148 $pontos = array(); 148 $pontos = array();
149 //pega um shape especifico 149 //pega um shape especifico
150 $layerPt->open(); 150 $layerPt->open();
151 - $projInObj = ms_newprojectionobj($prjTema);  
152 - $projOutObj = ms_newprojectionobj($prjMapa); 151 + if (($prjTema != "") && ($prjMapa != $prjTema))
  152 + {
  153 + $projInObj = ms_newprojectionobj($prjTema);
  154 + $projOutObj = ms_newprojectionobj($prjMapa);
  155 + }
153 for ($i = 0; $i < $res_count; $i++) 156 for ($i = 0; $i < $res_count; $i++)
154 { 157 {
155 $result = $layerPt->getResult($i); 158 $result = $layerPt->getResult($i);
@@ -167,12 +170,12 @@ Include: @@ -167,12 +170,12 @@ Include:
167 } 170 }
168 $layerPt->close(); 171 $layerPt->close();
169 //grava o arquivo com os pontos em x 172 //grava o arquivo com os pontos em x
170 - $f = fopen($nomearq."x",w); 173 + $f = fopen($nomearq."x","w");
171 foreach ($pontosx as $pt) 174 foreach ($pontosx as $pt)
172 {fwrite($f,$pt."\n");} 175 {fwrite($f,$pt."\n");}
173 fclose($f); 176 fclose($f);
174 //grava o arquivo com os pontos em y 177 //grava o arquivo com os pontos em y
175 - $f = fopen($nomearq."y",w); 178 + $f = fopen($nomearq."y","w");
176 foreach ($pontosy as $pt) 179 foreach ($pontosy as $pt)
177 {fwrite($f,$pt."\n");} 180 {fwrite($f,$pt."\n");}
178 fclose($f); 181 fclose($f);
@@ -195,6 +198,13 @@ Include: @@ -195,6 +198,13 @@ Include:
195 $dimy = "c(".$yi.",".$yf.")"; 198 $dimy = "c(".$yi.",".$yf.")";
196 switch ($tipo) 199 switch ($tipo)
197 { 200 {
  201 + //delaunay e voronoi
  202 + case "deldir":
  203 + $this->mapaDeldir($nomearq,$dir_tmp,$R_path,$locaplic);
  204 + $this->deldirDir2shp($nomearq."dirsgs",$dir_tmp,$locaplic);
  205 + $this->deldirDel2shp($nomearq."delsgs",$dir_tmp,$locaplic);
  206 + return "ok";
  207 + break;
198 case "kernel": 208 case "kernel":
199 $this->mapaKernel($nomearq,$dimx,$dimy,$dir_tmp,$R_path,$locaplic,$sigma); 209 $this->mapaKernel($nomearq,$dimx,$dimy,$dir_tmp,$R_path,$locaplic,$sigma);
200 break; 210 break;
@@ -543,6 +553,295 @@ $locaplic - Onde fica o I3Geo. @@ -543,6 +553,295 @@ $locaplic - Onde fica o I3Geo.
543 return "ok"; 553 return "ok";
544 } 554 }
545 /* 555 /*
  556 +function: mapaDeldir
  557 +
  558 +Calcula a triangulação de Delaunay e diagrama de Voronoi.
  559 +
  560 +Para funcionar, é necessário a instalação da biblioteca deldir do R.
  561 +
  562 +http://cran.r-project.org/web/packages/deldir
  563 +
  564 +parameters:
  565 +
  566 +$arqpt - Prefixo dos arquivos em disco com os pontos.
  567 +
  568 +$dir_tmp - Diretório temporário do mapserver.
  569 +
  570 +$R_path - Onde fica o R.
  571 +
  572 +$locaplic - Onde fica o I3Geo.
  573 +*/
  574 + function mapaDeldir($arqpt,$dir_tmp,$R_path,$locaplic)
  575 + {
  576 + $gfile_name = nomeRandomico(20);
  577 + $rcode[] = 'dadosx<-scan("'.$arqpt.'x")';
  578 + $rcode[] = 'dadosy<-scan("'.$arqpt.'y")';
  579 + if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN'))
  580 + {
  581 + $lib = '.libPaths("'.$locaplic.'/pacotes/rlib/win")';
  582 + if(file_exists($locaplic.'/pacotes/rlib/win'))
  583 + $rcode[] = $lib;
  584 + }
  585 + else
  586 + {
  587 + if(file_exists($locaplic."/pacotes/rlib/linux"))
  588 + {
  589 + $lib = '.libPaths("'.$locaplic.'/pacotes/rlib/linux")';
  590 + $rcode[] = $lib;
  591 + }
  592 + }
  593 + $rcode[] = 'library(deldir)';
  594 + $rcode[] = 'pt <- deldir(dadosx, dadosy)';
  595 + $rcode[] = 'write.csv(pt$delsgs,file="'.$arqpt.'delsgs")';
  596 + $rcode[] = 'write.csv(pt$dirsgs,file="'.$arqpt.'dirsgs")';
  597 + $r = executaR($rcode,$dir_tmp,$R_path,$gfile_name);
  598 + return "ok";
  599 + }
  600 +/*
  601 +function deldirDel2shp
  602 +
  603 +Lê um arquivo CSV gerado pelo software R com os dados referentes à triangulação de Delaunay.
  604 +
  605 +O arquivo CSV é lido e convertido em um shape file que é então adicionado ao mapa.
  606 +
  607 +Parameters:
  608 +
  609 +$nomearq - nome do arquivo CSV
  610 +
  611 +$dir_tmp - diretório temporário do Mapserver
  612 +
  613 +$locaplic - diretório da aplicação i3geo
  614 +*/
  615 + function deldirDel2shp($nomearq,$dir_tmp,$locaplic)
  616 + {
  617 + if (file_exists($nomearq))
  618 + {
  619 + require_once "../pacotes/phpxbase/api_conversion.php";
  620 + //define o nome do novo shapefile que será criado
  621 + $nomefinal = nomeRandomico();
  622 + $nomeshp = $this->diretorio."/".$nomefinal;
  623 + //cria o shape file
  624 + $novoshpf = ms_newShapefileObj($nomeshp, MS_SHP_ARC);
  625 + // cria o dbf
  626 + $def[] = array("x1","N","12","5");
  627 + $def[] = array("y1","N","12","5");
  628 + $def[] = array("x2","N","12","5");
  629 + $def[] = array("y2","N","12","5");
  630 + $def[] = array("ind1","N","5","0");
  631 + $def[] = array("ind2","N","5","0");
  632 + $db = xbase_create($nomeshp.".dbf", $def);
  633 + $dbname = $nomeshp.".dbf";
  634 + //le o arquivo linha a linha, pulando a primeira
  635 + //acrescenta os pontos no shape file formando as linhas
  636 + $abre = fopen($nomearq, "r");
  637 + $buffer = fgets($abre);
  638 + $poligonos = array();
  639 + while (!feof($abre))
  640 + {
  641 + $buffer = fgets($abre);
  642 + $i = explode(",",$buffer);
  643 + if(is_array($i))
  644 + {
  645 + $i1 = floatval($i[1]);
  646 + $i2 = floatval($i[2]);
  647 + $i3 = floatval($i[3]);
  648 + $i4 = floatval($i[4]);
  649 + $i5 = floatval($i[5]);
  650 + $i6 = floatval($i[6]);
  651 + $poPoint1 = ms_newpointobj();
  652 + $poPoint1->setXY($i1,$i2);
  653 + $poPoint2 = ms_newpointobj();
  654 + $poPoint2->setXY($i3, $i4);
  655 + $linha = ms_newLineObj();
  656 + $linha->add($poPoint1);
  657 + $linha->add($poPoint2);
  658 + $ShapeObj = ms_newShapeObj(MS_SHAPE_LINE);
  659 + $ShapeObj->add($linha);
  660 +
  661 + $novoshpf->addShape($ShapeObj);
  662 + $registro = array($i1,$i2,$i3,$i4,$i5,$i6);
  663 + xbase_add_record($db,$registro);
  664 + $linha->free();
  665 + $ShapeObj->free();
  666 + }
  667 + }
  668 + $novoshpf->free();
  669 + xbase_close($db);
  670 + fclose($abre);
  671 + //adiciona no mapa atual o novo tema
  672 + $novolayer = criaLayer($this->mapa,MS_LAYER_LINE,MS_DEFAULT,("Delaunay (".$nomefinal.")"),$metaClasse="SIM");
  673 + $novolayer->set("data",$nomeshp.".shp");
  674 + $novolayer->setmetadata("DOWNLOAD","SIM");
  675 + $novolayer->set("template","none.htm");
  676 + $classe = $novolayer->getclass(0);
  677 + $estilo = $classe->getstyle(0);
  678 + $estilo->set("symbolname","linha");
  679 + $estilo->set("size",2);
  680 + $cor = $estilo->color;
  681 + $cor->setrgb(255,50,0);
  682 + }
  683 + }
  684 +/*
  685 +function deldirDir2shp
  686 +
  687 +Lê um arquivo CSV gerado pelo software R com os dados referentes ao diagrama de Voronoi.
  688 +
  689 +O arquivo CSV é lido e convertido em um shape file que é então adicionado ao mapa.
  690 +
  691 +Parameters:
  692 +
  693 +$nomearq - nome do arquivo CSV
  694 +
  695 +$dir_tmp - diretório temporário do Mapserver
  696 +
  697 +$locaplic - diretório da aplicação i3geo
  698 +*/
  699 + function deldirDir2shp($nomearq,$dir_tmp,$locaplic)
  700 + {
  701 + if (file_exists($nomearq))
  702 + {
  703 + require_once "../pacotes/phpxbase/api_conversion.php";
  704 + //
  705 + //define os nomes dos novos shapefiles que serão criados
  706 + //
  707 + $nomeLinhas = nomeRandomico();
  708 + $nomePoligonos = nomeRandomico();
  709 + $nomeshpLinhas = $this->diretorio."/".$nomeLinhas;
  710 + $nomeshpPoligonos = $this->diretorio."/".$nomePoligonos;
  711 + //cria o shape file
  712 + $novoshpLinhas = ms_newShapefileObj($nomeshpLinhas, MS_SHP_ARC);
  713 + $novoshpPoligonos = ms_newShapefileObj($nomeshpPoligonos, MS_SHP_POLYGON);
  714 + //
  715 + // cria o dbf para o shapefile linear
  716 + //
  717 + $def[] = array("x1","N","12","5");
  718 + $def[] = array("y1","N","12","5");
  719 + $def[] = array("x2","N","12","5");
  720 + $def[] = array("y2","N","12","5");
  721 + $def[] = array("ind1","N","5","0");
  722 + $def[] = array("ind2","N","5","0");
  723 + $def[] = array("b1","C","6");
  724 + $def[] = array("b2","C","6");
  725 + $dbLinhas = xbase_create($nomeshpLinhas.".dbf", $def);
  726 + $dbnameLinhas = $nomeshpLinhas.".dbf";
  727 + //
  728 + // cria o dbf para o shapefile poligonal
  729 + //
  730 + $def = array();
  731 + $def[] = array("area","N","12","5");
  732 + $dbPoligonos = xbase_create($nomeshpPoligonos.".dbf", $def);
  733 + $dbnamePoligonos = $nomeshpPoligonos.".dbf";
  734 + //
  735 + //constrói as linhas do diagrama
  736 + //
  737 + //le o arquivo linha a linha, pulando a primeira
  738 + //acrescenta os pontos no shape file formando as linhas
  739 + //cria o array para criar os polígonos
  740 + //
  741 + $abre = fopen($nomearq, "r");
  742 + $buffer = fgets($abre);
  743 + $borda = array();//guarda os pontos que ficam na borda
  744 + $poligonos = array();
  745 + while (!feof($abre))
  746 + {
  747 + $buffer = fgets($abre);
  748 + $i = explode(",",$buffer);
  749 + if(is_array($i))
  750 + {
  751 + $i1 = floatval($i[1]);
  752 + $i2 = floatval($i[2]);
  753 + $i3 = floatval($i[3]);
  754 + $i4 = floatval($i[4]);
  755 + $i5 = floatval($i[5]);
  756 + $i6 = floatval($i[6]);
  757 + $poPoint1 = ms_newpointobj();
  758 + $poPoint1->setXY($i1,$i2);
  759 + $poPoint2 = ms_newpointobj();
  760 + $poPoint2->setXY($i3, $i4);
  761 + if(trim($i[7]) == "TRUE")
  762 + {$borda[] = $poPoint1;}
  763 + if(trim($i[8]) == "TRUE")
  764 + {$borda[] = $poPoint2;}
  765 + $linha = ms_newLineObj();
  766 + $linha->add($poPoint1);
  767 + $linha->add($poPoint2);
  768 + if($poligonos[$i[5]])
  769 + $poligonos[$i[5]] = array_merge(array($linha),$poligonos[$i[5]]);
  770 + else
  771 + $poligonos[$i[5]] = array($linha);
  772 + if($poligonos[$i[6]])
  773 + $poligonos[$i[6]] = array_merge(array($linha),$poligonos[$i[6]]);
  774 + else
  775 + $poligonos[$i[6]] = array($linha);
  776 + $ShapeObj = ms_newShapeObj(MS_SHAPE_LINE);
  777 + $ShapeObj->add($linha);
  778 + $novoshpLinhas->addShape($ShapeObj);
  779 + $registro = array($i1,$i2,$i3,$i4,$i5,$i6,$i[7],$i[8]);
  780 + xbase_add_record($dbLinhas,$registro);
  781 + $ShapeObj->free();
  782 + }
  783 + }
  784 + //
  785 + //adiciona os poligonos
  786 + //
  787 + foreach ($poligonos as $p)
  788 + {
  789 + $ShapeObjp = ms_newShapeObj(MS_SHAPE_LINE);
  790 + foreach ($p as $o)
  791 + {$ShapeObjp->add($o);}
  792 + $ns = $ShapeObjp->convexhull();
  793 + $novoshpPoligonos->addShape($ns);
  794 + $registro = array($ns->getArea());
  795 + xbase_add_record($dbPoligonos,$registro);
  796 + $ShapeObjp->free();
  797 + }
  798 + $novoshpPoligonos->free();
  799 + xbase_close($dbPoligonos);
  800 + //
  801 + //adiciona o layer com os polígonos no mapa
  802 + //
  803 + $novolayerp = criaLayer($this->mapa,MS_LAYER_POLYGON,MS_DEFAULT,("Voronoi - poligonos (".$nomePoligonos.")"),$metaClasse="SIM");
  804 + $novolayerp->set("data",$nomeshpPoligonos.".shp");
  805 + $novolayerp->setmetadata("DOWNLOAD","SIM");
  806 + $novolayerp->set("template","none.htm");
  807 + $classe = $novolayerp->getclass(0);
  808 + $estilo = $classe->getstyle(0);
  809 + $cor = $estilo->color;
  810 + $cor->setrgb(240,240,240);
  811 + //
  812 + //adiciona no mapa atual o novo tema com as linhas do diagrama
  813 + //
  814 + if (count($borda > 2))
  815 + {
  816 + $linha = ms_newLineObj();
  817 + foreach ($borda as $ponto)
  818 + {
  819 + $linha->add($ponto);
  820 + }
  821 + $ShapeObj = ms_newShapeObj(MS_SHAPE_LINE);
  822 + $ShapeObj->add($linha);
  823 + $novoshpLinhas->addShape($ShapeObj->convexhull());
  824 + $registro = array(0,0,0,0,0,0,0,0);
  825 + xbase_add_record($dbLinhas,$registro);
  826 + $linha->free();
  827 + $ShapeObj->free();
  828 + }
  829 + $novoshpLinhas->free();
  830 + xbase_close($dbLinhas);
  831 + fclose($abre);
  832 + $novolayer = criaLayer($this->mapa,MS_LAYER_LINE,MS_DEFAULT,("Voronoi (".$nomeLinhas.")"),$metaClasse="SIM");
  833 + $novolayer->set("data",$nomeshpLinhas.".shp");
  834 + $novolayer->setmetadata("DOWNLOAD","SIM");
  835 + $novolayer->set("template","none.htm");
  836 + $classe = $novolayer->getclass(0);
  837 + $estilo = $classe->getstyle(0);
  838 + $estilo->set("symbolname","linha");
  839 + $estilo->set("size",4);
  840 + $cor = $estilo->color;
  841 + $cor->setrgb(255,210,0);
  842 + }
  843 + }
  844 +/*
546 function: pontoEmPoligono 845 function: pontoEmPoligono
547 846
548 Cruza um tema pontual com temas poligonais ou raster. 847 Cruza um tema pontual com temas poligonais ou raster.
ferramentas/pontosdistri/index.js
@@ -41,7 +41,10 @@ comboTemasPt(&quot;temasPt&quot;,function(retorno) @@ -41,7 +41,10 @@ comboTemasPt(&quot;temasPt&quot;,function(retorno)
41 ins += "<br><br><div style=top:0px;left:0px;text-align:left; onclick='analiseDensidade()' ><input id=botao1 size=35 type=button value='densidade de pontos' /></div><br>" 41 ins += "<br><br><div style=top:0px;left:0px;text-align:left; onclick='analiseDensidade()' ><input id=botao1 size=35 type=button value='densidade de pontos' /></div><br>"
42 ins += "<div style=top:0px;left:0px;text-align:left; onclick='analiseDistancia()' ><input id=botao2 size=35 type=button value='dist&acirc;ncia entre pontos' /></div><br>" 42 ins += "<div style=top:0px;left:0px;text-align:left; onclick='analiseDistancia()' ><input id=botao2 size=35 type=button value='dist&acirc;ncia entre pontos' /></div><br>"
43 ins += "<div style='padding:1px;left:0px;border: 1px solid gray;'><div style=top:0px;left:0px;text-align:left; onclick='analiseKernel()' ><input id=botaokernel size=35 type=button value='Kernel' /></div><div>Desvio padr&atilde;o (sigma):<input class='digitar' type=text size=3 value='' id=sigma /></div></div><br>" 43 ins += "<div style='padding:1px;left:0px;border: 1px solid gray;'><div style=top:0px;left:0px;text-align:left; onclick='analiseKernel()' ><input id=botaokernel size=35 type=button value='Kernel' /></div><div>Desvio padr&atilde;o (sigma):<input class='digitar' type=text size=3 value='' id=sigma /></div></div><br>"
  44 + ins += "<div style=top:0px;left:0px;text-align:left; onclick='analiseDeldir()' ><input id=botaodeldir size=35 type=button value='Delaunay/Voronoi' /></div><br>"
  45 +
44 ins += "<div style=top:0px;left:0px;text-align:left; onclick='analiseRelatorio()' ><input id=botao3 size=35 type=button value='relat&oacute;rio' /></div><br>" 46 ins += "<div style=top:0px;left:0px;text-align:left; onclick='analiseRelatorio()' ><input id=botao3 size=35 type=button value='relat&oacute;rio' /></div><br>"
  47 +
45 $i("guia1obj").innerHTML = ins; 48 $i("guia1obj").innerHTML = ins;
46 YAHOO.example.init = function () 49 YAHOO.example.init = function ()
47 { 50 {
@@ -52,6 +55,7 @@ comboTemasPt(&quot;temasPt&quot;,function(retorno) @@ -52,6 +55,7 @@ comboTemasPt(&quot;temasPt&quot;,function(retorno)
52 new YAHOO.widget.Button("botao3"); 55 new YAHOO.widget.Button("botao3");
53 new YAHOO.widget.Button("botao4"); 56 new YAHOO.widget.Button("botao4");
54 new YAHOO.widget.Button("botaokernel"); 57 new YAHOO.widget.Button("botaokernel");
  58 + new YAHOO.widget.Button("botaodeldir");
55 } 59 }
56 YAHOO.util.Event.onContentReady("botao1", onPushButtonsMarkupReady); 60 YAHOO.util.Event.onContentReady("botao1", onPushButtonsMarkupReady);
57 }() 61 }()
@@ -88,6 +92,23 @@ function mostracores(retorno) @@ -88,6 +92,23 @@ function mostracores(retorno)
88 function corj(obj) 92 function corj(obj)
89 {window.parent.abreCor("wdocai",obj)} 93 {window.parent.abreCor("wdocai",obj)}
90 94
  95 +function analiseDeldir()
  96 +{
  97 + aguarde("block")
  98 + var temp = function()
  99 + {
  100 + aguarde("none");
  101 + window.parent.ajaxredesenha("")
  102 + }
  103 + var tema = $i("temasPt").value
  104 + if(tema == ""){alert("Escolha um tema");aguarde("none");return}
  105 + var cp = new cpaint();
  106 + var p = g_locaplic+"/classesphp/mapa_controle.php?g_sid="+g_sid+"&funcao=analiseDistriPt&tema="+tema+"&tipo=deldir&numclasses=&cori=&corf=&sigma=&limitepontos="
  107 + //cp.set_debug(2)
  108 + cp.set_response_type("JSON");
  109 + cp.call(p,"analiseDeldir",temp);
  110 +}
  111 +
91 function analiseKernel() 112 function analiseKernel()
92 { 113 {
93 var n = $i("numclasses").value 114 var n = $i("numclasses").value