exec.php
8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
include_once(dirname(__FILE__)."/../safe.php");
verificaBlFerramentas(basename(dirname(__FILE__)),$i3geoBlFerramentas,false);
if($statusFerramentas["melhorcaminho"] != true){
exit;
}
//
//faz a busca da função que deve ser executada
//
$retorno = ""; //string que será retornada ao browser via JSON
switch (strtoupper($funcao))
{
case "SHAPE2PONTOS":
$mapa = ms_newMapObj($map_file);
$layer = $mapa->getlayerbyname($tema);
$shapes = retornaShapesSelecionados($layer,$map_file,$mapa);
if(count($shapes) == 0){
$shapes = retornaShapesMapext($layer,$mapa);
}
//quando ponto 0
if($layer->type == 0){
$objLine = $shapes[0]->line(0);
$pontoA = $objLine->point(0);
$objLine = $shapes[count($shapes) - 1]->line(0);
$pontoB = $objLine->point(0);
}
//quando linha 1
if($layer->type == 1){
$objLine = $shapes[0]->line(0);
$pontoA = $objLine->point(0);
$pontoB = $objLine->point($objLine->numpoints - 1);
}
//quando poligono 2
if($layer->type == 2){
$objLine = $shapes[0]->line(0);
$pontoA = $objLine->point(0);
$pontoB = $shapes[0]->getCentroid();
}
$retorno = array(
"ax"=>$pontoA->x,
"ay"=>$pontoA->y,
"bx"=>$pontoB->x,
"by"=>$pontoB->y
);
break;
case "MELHORCAMINHO":
//converte os parametros em um arquivo YAML
$mapa = ms_newMapObj($map_file);
$layer = $mapa->getlayerbyname($_GET["raster"]);
$cost_surface_path = $layer->data;
$prefixo = nomeRandomico(3);
//verifica se o mapa de custo existe
if(file_exists($cost_surface_path)){
$pathresult = $dir_tmp."/melhorcaminho_".nomeRandomico();
//cria a pasta onde os resultados serao armazenados
mkdir ($pathresult,0744);
$pta = $_GET["pta"];
$ptb = $_GET["ptb"];
//parametros para o calculo de melhor caminho e linha reta
$best = array(
"p1"=> array(
"calculation_type" =>"best_path",
"file_prefix" => $prefixo,
"start_coord" => "[$pta]",
"stop_coord" => "[$ptb]"
)
);
$cart = array(
"p2"=> array(
"calculation_type" =>"cartesian_straight_line_cost",
"file_prefix" => $prefixo,
"start_coord" => "[$pta]",
"stop_coord" => "[$ptb]"
)
);
//guarda os processos que serao executados
$processos = array($best,$cart);
//parametros para calculo do buffer
if($buffer > 0){
$buf = array(
"p3"=> array(
"calculation_type" =>"best_path_within_buffer",
"buffer_km" => $_GET["buffer"],
"file_prefix" => $prefixo,
"start_coord" => "[$pta]",
"stop_coord" => "[$ptb]"
)
);
$processos[] = $buf;
}
//parametros para calculo com reclassificacao
$lut = $_GET["lut"];
if($lut != ""){
//pega os valores da lut
$lista = explode("|",$lut);
$novaLut = array();
foreach($lista as $li){
$v = explode(",",$li);
$novaLut[] = "- {min: $v[0], max: $v[1], nv: $v[2]}";
}
if(count($novaLut) == 1){
$novaLut = "\n".implode("",$novaLut);
}
else{
$novaLut = implode("\n",$novaLut);
}
$lut = array(
"p4"=> array(
"calculation_type" =>"best_path_lut",
"lut" => $novaLut,
"file_prefix" => $prefixo,
"start_coord" => "[$pta]",
"stop_coord" => "[$ptb]"
)
);
$processos[] = $lut;
}
//parametros para o calculo quando o usuario escolhe um layer que contem um shapefile
$temausuario = $_GET["temausuario"];
if($temausuario != ""){
//exporta o layer como um shapefile pois pode ser postgis
$shparq = downloadTema2($map_file,$temausuario,$locaplic,$dir_tmp,$postgis_mapa);
$shparq = explode(",",$shparq["arquivos"]);
$shparq = $dir_tmp."/".basename($shparq[0]);
$shp = array(
"p5"=> array(
"calculation_type" =>"informed_path_cost",
"informed_path" => $shparq,
"file_prefix" => $prefixo,
"start_coord" => "[$pta]",
"stop_coord" => "[$ptb]"
)
);
$processos[] = $shp;
}
//monta o array que sera utilizado para gerar o arquivo yaml que sera o input do programa python que faz o calculo
$a = array(
"cost_surface_path" => $cost_surface_path,
"pathresult" => $pathresult,
"processes"=> $processos
);
$y = $pathresult."/input.yaml";
$yaml = yaml_emit($a);
//adapta o formato YAML para que o Python entenda
$yaml = str_replace("---","",$yaml);
$yaml = str_replace("...","",$yaml);
$yaml = str_replace("- p"," p",$yaml);
$yaml = str_replace("'","",$yaml);
$yaml = str_replace('"',"",$yaml);
$yaml = trim($yaml)."\n";
$yaml = str_replace('|-',"",$yaml);
$yaml = str_replace('|2-',"",$yaml);
//salva o arquivo com os parametros
$fp = fopen($y,"w");
fwrite($fp,$yaml);
fclose($fp);
//executa os calculos
exec(dirname(__FILE__)."/better_path.py $y");
//adiciona ao mapa atual os shapefiles criados
include_once("../../classesphp/classe_mapa.php");
$m = new Mapa($map_file);
if(file_exists($pathresult."/".$prefixo."_best_path.shp")){
//adiciona ao mapa best_path
$retorno = $m->adicionaTemaSHP($pathresult."/".$prefixo."_best_path.shp");
$layer = $m->mapa->getlayerbyname($prefixo."_best_path.shp");
$layer->setmetadata("TEMA","Melhor caminho $prefixo");
$layer->setmetadata("DOWNLOAD","SIM");
$layer->setmetadata("TEMALOCAL","SIM");
$classe = $layer->getclass(0);
$estilo = $classe->getstyle(0);
$cor = $estilo->color;
$cor->setRGB(255,0,0);
}
if(file_exists($pathresult."/".$prefixo."_cartesian_straight_line_cost.shp")){
//cartesian_straight_line_cost
$retorno = $m->adicionaTemaSHP($pathresult."/".$prefixo."_cartesian_straight_line_cost.shp");
$layer = $m->mapa->getlayerbyname($prefixo."_cartesian_straight_line_cost.shp");
$layer->setmetadata("TEMA","Linha reta entre os pontos A e B $prefixo");
$layer->setmetadata("DOWNLOAD","SIM");
$layer->setmetadata("TEMALOCAL","SIM");
$classe = $layer->getclass(0);
$estilo = $classe->getstyle(0);
$cor = $estilo->color;
$cor->setRGB(255,0,255);
$estilo->set("width",2);
}
if(file_exists($pathresult."/".$prefixo."_best_path_within_buffer.shp")){
//best_path_within_buffer
$retorno = $m->adicionaTemaSHP($pathresult."/".$prefixo."_best_path_within_buffer.shp");
$layer = $m->mapa->getlayerbyname($prefixo."_best_path_within_buffer.shp");
$layer->setmetadata("TEMA","Melhor caminho dentro do buffer $prefixo");
$layer->setmetadata("DOWNLOAD","SIM");
$layer->setmetadata("TEMALOCAL","SIM");
$classe = $layer->getclass(0);
$estilo = $classe->getstyle(0);
$cor = $estilo->color;
$cor->setRGB(255,255,0);
//buffer
$retorno = $m->adicionaTemaSHP($pathresult."/".$prefixo."_buffer_best_path_within_buffer.shp");
$layer = $m->mapa->getlayerbyname($prefixo."_buffer_best_path_within_buffer.shp");
$layer->setmetadata("TEMA","Buffer $prefixo");
$layer->setmetadata("DOWNLOAD","SIM");
$layer->setmetadata("TEMALOCAL","SIM");
$classe = $layer->getclass(0);
$estilo = $classe->getstyle(0);
$cor = $estilo->color;
$cor->setRGB(-1,-1,-1);
$estilo->set("width",2);
}
if(file_exists($pathresult."/".$prefixo."_best_path_lut.shp")){
//cartesian_straight_line_cost
$retorno = $m->adicionaTemaSHP($pathresult."/".$prefixo."_best_path_lut.shp");
$layer = $m->mapa->getlayerbyname($prefixo."_best_path_lut.shp");
$layer->setmetadata("TEMA","Melhor caminho com reclassificação dos pixels $prefixo");
$layer->setmetadata("DOWNLOAD","SIM");
$layer->setmetadata("TEMALOCAL","SIM");
$classe = $layer->getclass(0);
$estilo = $classe->getstyle(0);
$cor = $estilo->color;
$cor->setRGB(0,255,255);
}
//salva o mapfile
$m->salva();
}
else{
$retorno = "<span style='color:red' >Erro. Arquivo raster não encontrado</span>";
}
$retorno = $pathresult;
break;
case "RELATORIO":
$yaml = yaml_parse_file($_GET["caminho"]."/result.yaml");
$resultados = $yaml["results"];
$retorno = array();
foreach($resultados as $r){
$retorno[$r["item"]] = $r["cost"];
}
break;
}
if(isset($map_file) && isset($postgis_mapa) && $map_file != ""){
restauraCon($map_file,$postgis_mapa);
}
cpjson($retorno);
?>