classe_shp.php
8.63 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/*
Title: Shape
Manipulação de shapefile.
About: Licença
I3Geo Interface Integrada de Ferramentas de Geoprocessamento para Internet
Direitos Autorais Reservados (c) 2006 Ministério do Meio Ambiente Brasil
Desenvolvedor: Edmar Moretti edmar.moretti@mma.gov.br
Este programa é software livre; você pode redistribuí-lo
e/ou modificá-lo sob os termos da Licença Pública Geral
GNU conforme publicada pela Free Software Foundation;
tanto a versão 2 da Licença.
Este programa é distribuído na expectativa de que seja útil,
porém, SEM NENHUMA GARANTIA; nem mesmo a garantia implícita
de COMERCIABILIDADE OU ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA.
Consulte a Licença Pública Geral do GNU para mais detalhes.
Você deve ter recebido uma cópia da Licença Pública Geral do
GNU junto com este programa; se não, escreva para a
Free Software Foundation, Inc., no endereço
59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
File: i3geo/classesphp/classe_shp.php
19/6/2007
*/
/*
Class: SHP
*/
class SHP
{
/*
Variable: $mapa
Objeto mapa
*/
protected $mapa;
/*
Variable: $arquivo
Arquivo map file
*/
protected $arquivo;
/*
Variable: $layer
Objeto layer
*/
protected $layer;
/*
Variable: $nome
Nome do layer
*/
protected $nome;
/*
function: __construct
Cria um objeto map e seta a variavel tema
parameters:
$map_file - Endereço do mapfile no servidor.
$tema - nome do tema
*/
function __construct($map_file,$tema="")
{
//error_reporting(E_ALL);
$this->mapa = ms_newMapObj($map_file);
$this->arquivo = $map_file;
$this->tema = $tema;
if ($tema != "")
{
$this->layer = $this->mapa->getlayerbyname($tema);
$this->nome = $tema;
}
}
/*
function: salva
Salva o mapfile atual
*/
function salva()
{
if (connection_aborted()){exit();}
$this->mapa->save($this->arquivo);
}
/*
function: criaSHPvazio
Cria um shape file do tipo pontual vazio no diretório local
return:
Nome do tema criado.
*/
function criaSHPvazio()
{
require_once "../pacotes/phpxbase/api_conversion.php";
$diretorio = dirname($this->arquivo);
$tipol = MS_SHP_POINT;
$novonomelayer = nomeRandomico();
$nomeshp = $diretorio."/".$novonomelayer;
$l = criaLayer($this->mapa,MS_LAYER_POINT,MS_DEFAULT,"Ins","SIM");
$novoshpf = ms_newShapefileObj($nomeshp, $tipol);
$novoshpf->free();
$def[] = array("ID","C","50");
$db = xbase_create($nomeshp.".dbf", $def);
xbase_close($db);
$novoshpf = ms_newShapefileObj($nomeshp.".shp", -2);
$novoshpf->free();
$l->setmetadata("tema",$novonomelayer." pontos");
$l->setmetadata("TEMALOCAL","SIM");
$l->setmetadata("DOWNLOAD","sim");
$l->set("data",$nomeshp);
$l->set("name",$novonomelayer);
$classe = $l->getclass(0);
$estilo = $classe->getstyle(0);
$estilo->set("symbolname","ponto");
$estilo->set("size",6);
$cor = $estilo->color;
$cor->setrgb(255,210,0);
$cor = $estilo->outlinecolor;
$cor->setrgb(255,0,0);
return($novonomelayer);
}
/*
function: insereSHP
Insere um ponto em um shape file no diretório local
parameters:
$xy - X e y do novo ponto, separados por espaços. Pode ser mais de um ponto.
$projecao - código epsg da projeção das coordenadas
*/
function insereSHP($xy,$projecao)
{
require_once "../pacotes/phpxbase/api_conversion.php";
$xy = explode(" ",$xy);
$data = $this->layer->data;
$data = explode(".shp",$data);
$data = $data[0];
$items = pegaItens($this->layer);
$dbname = $data.".dbf";
$db=xbase_open($dbname,2);
for($i=0;$i<(count($xy) / 2);$i++)
{
$reg = array();
foreach ($items as $ni)
{$reg[] = "-";}
xbase_add_record($db,$reg);
}
xbase_close($db);
if (@$shapefileObj = ms_newShapefileObj($data,-2))
{
for($i=0;$i<(count($xy));$i=$i+2)
{
$poPoint = ms_newpointobj();
$poPoint->setXY($xy[$i],$xy[$i+1]);
if($projecao != "")
{
$projOutObj = ms_newprojectionobj("proj=latlong");
$projInObj = ms_newprojectionobj("init=epsg:".$projecao);
$poPoint->project($projInObj, $projOutObj);
}
$shapefileObj->addpoint($poPoint);
}
$shapefileObj->free();
return("ok");
}
else
{return("erro");}
}
/*
function: insereSHPgrafico
Insere um ponto em um shape file no diretório local como um grafico
parameters:
$tipo - Tipo de gráfico
$x - Coordenada x.
$y - Coordenada Y.
$itens - Lista de itens
$imgurl - Endereço da imagem atual
$width - Largura do gráfico
$inclinacao - Inclinação do gráfico
$shadow_height - Tamanho da sombra do gráfico
Include:
<classe_atributos.php>, <graficopizza.php>
*/
function insereSHPgrafico($tipo,$x,$y,$itens,$imgurl,$width,$inclinacao,$shadow_height)
{
//nome do novo tema
$temaedit = nomeRandomico();
//pega os valores
include_once("classe_atributos.php");
$m = new Atributos($this->arquivo,$this->tema);
$shape = $m->identificaQBP($this->nome,$x,$y,$this->arquivo,0,"","shape");
if ((is_array($shape)) && ($shape[0] == " "))
{
return("erro.Nenhum valor encontrado");
}
else
{
$itens = explode("*",$itens);
foreach ($itens as $i)
{
$ii = explode(",",$i);
$v = $shape->values[$ii[0]];
if (!is_numeric($v))
{return("erro. Dados nao numericos.");}
$valor[] = $v;
$cor[] = $ii[1].",".$ii[2].",".$ii[3];
}
$data = implode("*",$valor);
$cores = implode("*",$cor);
if ($tipo == "pizza")
{
//gera a figura
include_once("graficopizza.php");
$res = graficopizza($data,$width,$inclinacao,$shadow_height,$cores,$imgurl,$this->arquivo,$temaedit);
$img = explode(",",$res);
}
//insere simbolo
$nomes = nomeRandomico();
$nId = ms_newsymbolobj($this->mapa, "foto");
$oSymbol = $this->mapa->getsymbolobjectbyid($nId);
$oSymbol->set("inmapfile", MS_TRUE);
$oSymbol->set("type",MS_SYMBOL_PIXMAP);
$oSymbol->setimagepath($img[0]);
$oSymbol->set("name",$nomes);
$pinlayer = criaLayer($this->mapa,MS_LAYER_POINT,MS_DEFAULT,"Gráfico (".$temaedit.")","SIM");
$c = $pinlayer->getclass(0);
$e = $c->getstyle(0);
$pinlayer->set("name",$temaedit);
$c->set("name","");
if(!isset($tamanho)){$tamanho = 50;}
$e->set("size",$tamanho);
$e->set("symbolname",$nomes);
$pinlayer->set("transparency",MS_GD_ALPHA);
$shp = ms_newshapeobj(MS_SHAPE_POINT);
$lin = ms_newlineobj();
$lin->addxy($x,$y);
$shp->add($lin);
$pinlayer->addfeature($shp);
$this->salva();
return("ok");
}
}
/*
function: listaPontosShape
Lista as coordenadas dos pontos de um shapefile
return:
string - xy
*/
function listaPontosShape()
{
$this->layer->open();
$this->layer->whichShapes($this->mapa->extent);
$xy = array();
while ($shape = $this->layer->nextShape())
{
$lin = $shape->line(0);
$pt = $lin->point(0);
$xy[] = array("x"=>$pt->x,"y"=>$pt->y);
}
$this->layer->close();
return $xy;
}
/*
function: shpPT2shp
Cria um tema linear ou poligonal com base em pontos de um tema pontual.
parameters:
$locaplic - Localização do I3geo
$para - linha|poligono
*/
function shpPT2shp($locaplic,$para)
{
//para manipular dbf
require_once "../pacotes/phpxbase/api_conversion.php";
$this->layer->set("template","none.htm");
$diretorio = dirname($this->arquivo);
$tipol = MS_SHP_ARC;
$tipos = MS_SHAPE_LINE;
if ($para == "poligono")
{
$tipol = MS_SHP_POLYGON;
$tipos = MS_SHAPE_POLYGON;
}
$novonomelayer = nomeRandomico();
$nomeshp = $diretorio."/".$novonomelayer;
$items = pegaItens($this->layer);
// cria o dbf
$def = array();
foreach ($items as $ni)
{
$temp = strtoupper($ni);
$def[] = array($temp,"C","254");
}
$db = xbase_create($nomeshp.".dbf", $def);
$dbname = $nomeshp.".dbf";
$reg = array();
$novoshpf = ms_newShapefileObj($nomeshp.".shp", $tipol);
$this->layer->open();
$this->layer->whichShapes($this->mapa->extent);
$linha = ms_newLineObj();
while ($shape = $this->layer->nextShape())
{
$lin = $shape->line(0);
$pt = $lin->point(0);
$linha->add($pt);
}
if ($para == "poligono")
{
$linha->add($linha->point(0));
}
$shape = ms_newShapeObj($tipos);
$shape->add($linha);
foreach ($items as $ni)
{$reg[] = "-";}
$novoshpf->addShape($shape);
xbase_add_record($db,$reg);
$novoshpf->free();
$this->layer->close();
xbase_close($db);
//cria o novo layer
$layer = criaLayer($this->mapa,MS_LAYER_LINE,MS_DEFAULT,"",$metaClasse="SIM");
$layer->set("data",$nomeshp);
$layer->setmetadata("tema",$novonomelayer." (linear)");
$layer->setmetadata("download","sim");
$layer->setmetadata("temalocal","sim");
$classe = $layer->getclass(0);
$estilo = $classe->getstyle(0);
$estilo->set("symbolname","linha");
if ($para == "poligono")
{
$layer->set("type",MS_LAYER_POLYGON);
$layer->set("transparency","50");
$layer->setmetadata("tema",$novonomelayer." (poligonal)");
}
return("ok");
}
}
?>