classe_selecao.php
26.3 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
<?php
/*
Title: classe_selecao.php
Seleção de elementos de um tema.
Adiciona, remove, etc.
Licenca:
GPL2
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@gmail.com
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;
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 copia 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.
Arquivo:
i3geo/classesphp/classe_selecao.php
*/
/*
Classe: Selecao
*/
class Selecao
{
/*
Variavel: $mapa
Objeto mapa
*/
public $mapa;
/*
Variavel: $arquivo
Arquivo map file
*/
protected $arquivo;
/*
Variavel: $layer
Objeto layer
*/
protected $layer;
/*
Variavel: $nome
Nome do layer
*/
protected $nome;
/*
Variavel: $qyfile
Nome do arquivo de seleção (.qy)
*/
public $qyfile;
/*
Variavel: $projO
Objeto projection original do mapa. Obtido apenas na interface Googlemaps
*/
public $projO;
/*
Variavel: $v
Versão atual do Mapserver (primeiro dígito)
*/
public $v;
/*
Function: __construct
Cria um objeto Selecao
O tipo de interface usada pelo mapa é obtido do metadata "interface". Se for a interface Googlemaps, é feita a alteração temporária da projeção do mapa.
parameters:
$map_file - Endereço do mapfile no servidor.
$tema - nome do tema
$ext - extensão geográfica do mapa
*/
function __construct($map_file,$tema="",$ext="")
{
include_once(dirname(__FILE__)."/funcoes_gerais.php");
$this->v = versao();
$this->v = $this->v["principal"];
$this->qyfile = str_replace(".map",".qy",$map_file);
if($tema != "")
{$this->qyfileTema = dirname($map_file)."/".$tema.".php";}
else
{$this->qyfileTema = "";}
$this->mapa = ms_newMapObj($map_file);
$this->arquivo = $map_file;
if($tema != "" && @$this->mapa->getlayerbyname($tema))
$this->layer = $this->mapa->getlayerbyname($tema);
$this->nome = $tema;
$c = $this->mapa->numlayers;
for ($i=0;$i < $c;++$i)
{
$l = $this->mapa->getlayer($i);
$l->set("template","none.htm");
}
if($ext && $ext != ""){
$e = explode(" ",$ext);
$extatual = $this->mapa->extent;
$extatual->setextent((min($e[0],$e[2])),(min($e[1],$e[3])),(max($e[0],$e[2])),(max($e[1],$e[3])));
}
if($this->mapa->getmetadata("interface") == "googlemaps"){
$this->projO = $this->mapa->getProjection();
$this->mapa->setProjection(pegaProjecaoDefault("proj4"));
}
}
/*
function: salva
Salva o mapfile atual
*/
function salva()
{
if (connection_aborted()){exit();}
if($this->mapa->getmetadata("interface") == "googlemaps")
{$this->mapa->setProjection($this->projO);}
$this->mapa->save($this->arquivo);
}
/*
function: nSel
Retorna o número de elementos selecionados
*/
function nSel(){
return $this->layer->getNumresults();
}
/*
function: selecaoPorPoligono
Seleciona os elementos de um tema baseado em um conjunto de pontos que formarão um polígono.
parameters:
$tipo - Tipo de operação adiciona|retira|inverte|limpa|novo
$xs - lista de coordenadas x separadas por virgula
$ys - lista de coordenadas y separadas por virgula
$wkt - string wkt opcional ao uso de xs e ys
$buffer - buffer que será aplicado
*/
function selecaoPorPoligono($tipo,$xs="",$ys="",$wkt="",$buffer=0)
{
if(!$this->layer){return "erro";}
$this->layer->set("tolerance",0);
if ($tipo == "novo")
{
$this->selecaoLimpa();
$tipo = "adiciona";
}
if ($tipo == "limpa")
{return($this->selecaoLimpa());}
if ($tipo == "inverte")
{return($this->selecaoInverte());}
$tipoLayer = $this->layer->type;
if (file_exists($this->qyfile))
{$this->mapa->loadquery($this->qyfile);}
$indxlayer = $this->layer->index;
$res_count = $this->layer->getNumresults();
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema))
{$shp_atual = $this->unserializeQ($this->qyfileTema);}
$shpi = array();
//transforma os pontos em shape
if($wkt != ""){
$s = ms_shapeObjFromWkt($wkt);
}
else{
$s = ms_newShapeObj(MS_SHAPE_POLYGON);
$linha = ms_newLineObj();
$xs = explode(",",$xs);
$ys = explode(",",$ys);
for($i=0;$i<(count($xs));++$i)
{
$linha->addxy($xs[$i],$ys[$i]);
}
$linha->addxy($xs[0],$ys[0]);
$s->add($linha);
}
if($buffer > 0){
$s = $this->bufferShape($s,$buffer);
}
$this->layer->querybyshape($s);
$res_count = $this->layer->getNumresults();
for ($i = 0; $i < $res_count; ++$i)
{
$result = $this->layer->getResult($i);
$shpi[] = $result->shapeindex;
}
$this->layer->set("status",MS_DEFAULT);
if (($tipo == "adiciona") && (count($shpi) > 0))
{return($this->selecaoAdiciona($shpi,$shp_atual));}
if (($tipo == "retira") && (count ($shp_atual) > 0))
{return($this->selecaoRetira($shpi,$shp_atual));}
return("Nada selecionado.");
}
/*
function: selecaoTema
Seleciona os elementos de um tema baseado nos elementos selecionados em outro.
parameters:
$temao - Tema que será processado.
$tipo - Tipo de operação adiciona|retira|inverte|limpa|novo
$buffer - Opcional
*/
function selecaoTema($temao,$tipo,$buffer=0){
if(!$this->layer){return "erro";}
$this->layer->set("tolerance",0);
if ($tipo == "novo"){
$this->selecaoLimpa();
$tipo = "adiciona";
}
if ($tipo == "limpa"){
return($this->selecaoLimpa());
}
if ($tipo == "inverte"){
return($this->selecaoInverte());
}
$layero = $this->mapa->getlayerbyname($temao);
if ($layero->type == MS_LAYER_LINE || $layero->type == 1){
return("erro. O tema de sobreposicao nao pode ser do tipo linear.");
}
$tipoLayer = $this->layer->type;
$tipoLayero = $layero->type;
$this->layer->set("template","none.htm");
$layero->set("template","none.htm");
//if (file_exists($this->qyfile))
//{$this->mapa->loadquery($this->qyfile);}
$indxlayer = $this->layer->index;
carregaquery2($this->arquivo,$layero,$this->mapa);
$res_count = $this->layer->getNumresults();
$res_counto = $layero->getNumresults();
if ($res_counto == 0){
return false;
}
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema)){
$shp_atual = $this->unserializeQ($this->qyfileTema);
}
$shpi = array();
$i = $layero->index;
$selecao = "";
if ($layero->data != ""){
$sopen = $layero->open();
if($sopen == MS_FAILURE){return "erro";}
$res_count = $layero->getNumresults();
for ($i = 0; $i < $res_count; ++$i)
{
if($this->v >= 6)
{$sh = $layero->getShape($layero->getResult($i));}
else{
$result = $layero->getResult($i);
$s = $result->shapeindex;
$sh = $layero->getfeature($s,-1);
}
if($buffer > 0){
$sh = $this->bufferShape($sh,$buffer);
}
$tiposh = $sh->type;
if ($tiposh == 2){
$ident = @$this->layer->querybyshape($sh);
}
if ($tiposh == 0)
{
$lin = $sh->line(0);
$npt = $lin->numpoints;
if ($npt == 1)
{
$ptlin = $lin->point(0);
$ident = $this->layer->queryByPoint($ptlin, 1, 0);
}
if ($npt > 1)
{
for ($k = 0; $k < $npt; $k++)
{
$ptlin = $lin->point($k);
$s = $this->layer->queryByPoint($ptlin, 1, 0);
if ($s != 1)
{
$res_countl = $this->layer->getNumresults();
for ($kk = 0; $kk < $res_countl; $kk++)
{
$result = $this->layer->getResult($kk);
$shpi[] = $result->shapeindex;
}
}
}
$ident = 1;
}
}
if ($ident != 1)
{
$res_countl = $this->layer->getNumresults();
for ($ii = 0; $ii < $res_countl; $ii++)
{
$result = $this->layer->getResult($ii);
$shpi[] = $result->shapeindex;
}
}
}
$layero->close();
if (count($shpi)>0){$selecao = "ok";}
}
if (($selecao != "ok") && ($layero->data == ""))
{
$layero->queryByRect($this->mapa->extent);
$sopen = $layero->open();
if($sopen == MS_FAILURE){return "erro";}
$conta = $layero->getNumresults();
for ($k = 0; $k < $conta; $k++)
{
if($this->v >= 6)
{$s = @$layero->getShape($layero->getResult($k));}
else
{$s = @$layero->getfeature($k,-1);}
if($buffer > 0){
$s = $this->bufferShape($s,$buffer);
}
if($s){
if ($s->type == 2)
{
$this->layer->querybyshape($s);
$res_count = $this->layer->getNumresults();
for ($i = 0; $i < $res_count; ++$i)
{
$result = $this->layer->getResult($i);
$shpi[] = $result->shapeindex;
}
}
else
{
$lin = $s->line(0);
$npt = $lin->numpoints;
for ($c = 0; $c < $npt; $c++)
{
$pt = $lin->point($c);
$this->layer->set("tolerance",0);
$this->layer->set("toleranceunits",6);
if (($this->layer->type == MS_POINT) || ($this->layer->type == MS_LINE))
{
$this->layer->set("tolerance",5);
$ident = $this->layer->queryByPoint($pt, 1, 0);
}
else
{$ident = $this->layer->queryByPoint($pt, 1, 0);}
if ($ident != 1)
{
$res_countl = $this->layer->getNumresults();
for ($ii = 0; $ii < $res_countl; $ii++)
{
$result = $this->layer->getResult($ii);
if ($result->shapeindex != "")
{$shpi[] = $result->shapeindex;}
}
}
}
}
}
}
$layero->close();
}
if (($tipo == "adiciona") && (count($shpi) > 0))
{return($this->selecaoAdiciona($shpi,$shp_atual));}
if (($tipo == "retira") && (count ($shp_atual) > 0))
{return($this->selecaoRetira($shpi,$shp_atual));}
}
/*
function: selecaoAtributos
Seleção simples por atributo. Não permite composição de atributos, porém, se valor for igual a vazio "",
a string existente em "operador" será incluida como está no filtro. Para que isso funcione, a string
deve estar no padrão utilizado pelo mapserver.
parameters:
$tipo - Tipo de operação adiciona|retira|inverte|limpa|novo
$item - Item que será consultado.
$operador - Operador.
$valor - Valor.
*/
function selecaoAtributos($tipo,$item,$operador,$valor)
{
if ($tipo == "novo")
{
$this->selecaoLimpa();
$tipo = "adiciona";
}
if ($tipo == "limpa")
{return($this->selecaoLimpa());}
if ($tipo == "inverte")
{return($this->selecaoInverte());}
if(!$this->layer){return "erro";}
if($valor != "")
{
$operador = explode(",",$operador);
$operador = $operador[1];
}
$this->layer->set("template","none.htm");
$indxlayer = $this->layer->index;
/*
if (file_exists($this->qyfile))
{$this->mapa->loadquery($this->qyfile);}
$res_count = $this->layer->getNumresults();
$shp_atual = array();
for ($i = 0; $i < $res_count;++$i)
{
$rc = $this->layer->getResult($i);
$shp_atual[] = $rc->shapeindex;
}
$this->mapa->freequery($indxlayer);
*/
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema))
{$shp_atual = $this->unserializeQ($this->qyfileTema);}
$shpi = array();
if($this->layer->connectiontype == MS_POSTGIS)
{
if($valor != "")
{$this->layer->querybyattributes($item,$item." ".$operador." '".$valor."' ",1);}
else
{$this->layer->querybyattributes($item,$operador,1);}
}
else
{
if($valor != "")
{
if(!is_numeric($valor))
{$this->layer->querybyattributes($item,'("['.$item.']"'.$operador.'"'.$valor.'")',1);}
else
{$this->layer->querybyattributes($item,'(['.$item.']'.$operador.' '.$valor.' )',1);}
}
else
{
$this->layer->querybyattributes($item,$operador,1);
}
}
$res_count = $this->layer->getNumresults();
$shpi = array();
for ($i = 0; $i < $res_count; ++$i)
{
$result = $this->layer->getResult($i);
$shpi[] = $result->shapeindex;
}
if ($tipo == "adiciona")
{return($this->selecaoAdiciona($shpi,$shp_atual));}
if ($tipo == "retira")
{return($this->selecaoRetira($shpi,$shp_atual));}
return("ok");
}
/*
function: selecaoAtributos2
Seleção por atributo. Permite composição de atributos.
parameters:
$filtro - Expressão de seleção
$tipo - Tipo de operação adiciona|retira|inverte|limpa|novo
*/
function selecaoAtributos2($filtro,$tipo)
{
$items = pegaItens($this->layer);
if ($tipo == "novo")
{
$this->selecaoLimpa();
$tipo = "adiciona";
}
if ($tipo == "limpa")
{return($this->selecaoLimpa());}
if ($tipo == "inverte")
{return($this->selecaoInverte());}
if(!$this->layer){return "erro";}
$this->layer->set("template","none.htm");
$indxlayer = $this->layer->index;
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema))
{$shp_atual = $this->unserializeQ($this->qyfileTema);}
$shpi = array();
$filtro = str_replace("|","'",$filtro);
if ($this->layer->connectiontype == MS_POSTGIS)
{
$filtro = str_replace("'[","",$filtro);
$filtro = str_replace("[","",$filtro);
$filtro = str_replace("]'","",$filtro);
$filtro = str_replace("]","",$filtro);
$filtro = str_replace("("," ",$filtro);
$filtro = str_replace(")"," ",$filtro);
}
$teste = $this->layer->querybyattributes($items[0],$filtro,1);
if($teste != MS_SUCCESS){
$teste = $this->layer->queryByAttributes($itens[0], mb_convert_encoding($filtro,"ISO-8859-1","UTF-8"), 1);
}
if($teste != MS_SUCCESS){
$teste = $this->layer->queryByAttributes($itens[0], mb_convert_encoding($filtro,"UTF-8","ISO-8859-1"), 1);
}
$res_count = $this->layer->getNumresults();
$shpi = array();
for ($i = 0; $i < $res_count; ++$i)
{
$result = $this->layer->getResult($i);
$shpi[] = $result->shapeindex;
}
if ($tipo == "adiciona")
{return($this->selecaoAdiciona($shpi,$shp_atual));}
if ($tipo == "retira")
{return($this->selecaoRetira($shpi,$shp_atual));}
return("ok");
}
/*
function: selecaoPT
Seleciona por ponto.
parameters:
$xy - X e Y separados por vírgula.
$tipo - Tipo de operação adiciona|retira|inverte|limpa|novo
*/
function selecaoPT($xy,$tipo,$tolerancia)
{
if ($tipo == "novo")
{
$this->selecaoLimpa();
$tipo = "adiciona";
}
if ($tipo == "limpa")
{return ($this->selecaoLimpa());}
if ($tipo == "inverte")
{return ($this->selecaoInverte());}
if(!$this->layer){return "erro";}
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema))
{$shp_atual = $this->unserializeQ($this->qyfileTema);}
$shpi = array();
$c = explode(" ",$xy);
$pt = ms_newPointObj();
$pt->setXY($c[0], $c[1]);
if ($tolerancia == 0)
{
$this->layer->set("tolerance",0);
$this->layer->set("toleranceunits",6);
if (($this->layer->type == MS_LAYER_POINT) || ($this->layer->type == MS_LAYER_LINE))
{
$this->layer->set("tolerance",5);
$ident = @$this->layer->queryByPoint($pt, 1, 0);
}
else
{$ident = @$this->layer->queryByPoint($pt, 1, 0);}
}
else
{
error_reporting(0);
$projInObj = ms_newprojectionobj("proj=longlat,ellps=WGS84,datum=WGS84,no_defs");
$projOutObj = ms_newprojectionobj("proj=poly,ellps=GRS67,lat_0=0,lon_0=".$pt->x.",x_0=5000000,y_0=10000000");
$poPoint = ms_newpointobj();
$poPoint->setXY($pt->x, $pt->y);
//$dd1 = ms_newpointobj();
//$dd1->setXY($rect->minx, $rect->miny);
$poPoint->project($projInObj, $projOutObj);
$dd2 = ms_newpointobj();
$dd2->setXY(($poPoint->x + $tolerancia), $poPoint->y);
$dd2->project($projOutObj,$projInObj);
$d = $pt->distanceToPoint($dd2);
if ($d < 0){$d = $d * -1;}
//calcula a distancia 29100
//gera o buffer
$s = ms_newShapeObj(MS_SHAPE_POINT);
$linha = ms_newLineObj();
$linha->add($pt);
$s->add($linha);
$buffer = $s->buffer($d);
$ident = @$this->layer->queryByShape($buffer);
}
if ($ident != 1)
{
$res_count = $this->layer->getNumresults();
$shpi = array();
for ($i = 0; $i < $res_count; ++$i)
{
$result = $this->layer->getResult($i);
$shpi[] = $result->shapeindex;
}
}
if ($tipo == "adiciona")
{return($this->selecaoAdiciona($shpi,$shp_atual));}
if ($tipo == "retira")
{return($this->selecaoRetira($shpi,$shp_atual));}
}
/*
function: selecaoLimpa
Limpa a seleção do tema.
*/
function selecaoLimpa($apagaQyfile=false)
{
//apaga o arquivo do i3geo com os ids selecionados
if(file_exists($this->qyfileTema))
{unlink($this->qyfileTema);}
if ($this->nome != "" && $apagaQyfile == false) //limpa de um tema
{
if(!$this->layer){return "erro";}
if (file_exists($this->qyfile))
{
$this->mapa->loadquery($this->qyfile);
$indxlayer = $this->layer->index;
$this->mapa->freequery($indxlayer);
$this->mapa->savequery($this->qyfile);
}
}
else //limpa de todos os temas
{
if (file_exists($this->qyfile))
{unlink ($this->qyfile);}
}
return("ok");
}
/*
function: selecaoInverte
Inverte seleção do tema.
*/
function selecaoInverte()
{
if(!$this->layer){return "erro";}
$items = pegaItens($this->layer);
if (file_exists($this->qyfile))
{$this->mapa->loadquery($this->qyfile);}
$indxlayer = $this->layer->index;
/*
$res_count = $this->layer->getNumresults();
$shp_atual = array();
for ($i = 0; $i < $res_count;++$i)
{
$rc = $this->layer->getResult($i);
$shp_atual[] = $rc->shapeindex;
}
$this->mapa->freequery($indxlayer);
*/
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema))
{$shp_atual = $this->unserializeQ($this->qyfileTema);}
$this->layer->queryByrect($this->mapa->extent);
$res_count = $this->layer->getNumresults();
$shp_todos = array();
for ($i = 0; $i < $res_count;++$i)
{
$rc = $this->layer->getResult($i);
$shp_todos[] = $rc->shapeindex;
}
$shp = array_diff($shp_todos,$shp_atual);
$this->mapa->freequery($indxlayer);
foreach ($shp as $indx)
{$this->mapa->querybyindex($indxlayer,-1,$indx,MS_TRUE);}
$this->mapa->savequery($this->qyfile);
$this->serializeQ($this->qyfileTema,$shp);
return("ok");
}
/*
function: selecaoAdiciona
Adiciona elementos na seleção do tema.
parameters:
$shpi - Indices dos registros novos.
$shp_atual - Indices dos elementos já selecionados.
*/
function selecaoAdiciona($shpi,$shp_atual)
{
error_reporting(0);
if(!$this->layer){return "erro";}
$indxlayer = $this->layer->index;
$shp = array_merge($shpi,$shp_atual);
$shp = array_unique($shp);
$this->mapa->freequery($indxlayer);
foreach ($shp as $indx)
{@$this->mapa->querybyindex($indxlayer,-1,$indx,MS_TRUE);}
//echo $this->layer->getNumresults();
$this->mapa->savequery($this->qyfile);
$this->serializeQ($this->qyfileTema,$shp);
return("ok");
}
/*
function: selecaoRetira
Retira elementos na seleção do tema.
parameters:
$shpi - Indices dos registros que serão retirados.
$shp_atual - Indices dos elementos já selecionados.
*/
function selecaoRetira($shpi,$shp_atual)
{
if(!$this->layer){return "erro";}
$indxlayer = $this->layer->index;
$this->mapa->freequery($indxlayer);
$shp = array_diff($shp_atual,$shpi);
$shp = array_unique($shp);
$this->mapa->freequery($indxlayer);
foreach ($shp as $indx)
{$this->mapa->querybyindex($indxlayer,-1,$indx,MS_TRUE);}
$this->mapa->savequery($this->qyfile);
$this->serializeQ($this->qyfileTema,$shp);
return("ok");
}
/*
function: incluiSel
Inclui uma lista de ids na seleção do tema.
Salva o arquivo .qy adicionando os novos registros
parameters:
$ids - Ids separados por vírgula correspondendo aos registros.
*/
function incluiSel($ids)
{
if(!$this->layer){return "erro";}
if (file_exists($this->qyfile))
{$this->mapa->loadquery($this->qyfile);}
$ids = explode(",",$ids);
$indxlayer = $this->layer->index;
$ids = array_unique($ids);
foreach ($ids as $i)
{$this->mapa->queryByIndex($indxlayer, -1, $i);}
$this->mapa->savequery($this->qyfile);
$this->serializeQ($this->qyfileTema,$ids);
return("ok");
}
/*
function: selecao2tema
Exporta elementos selecionados de um tema em shape file e adiciona no mapa atual.
parameters:
$locaplic - localização do I3geo
$dir_tmp - localização do diretório temporário
*/
function selecao2tema($locaplic,$dir_tmp)
{
if(!$this->layer){return "erro";}
$this->layer->setfilter("");
$nomeshp = criaSHP($this->nome,$this->arquivo,$locaplic,$dir_tmp,true,"",false);
$novolayer = criaLayer($this->mapa,$this->layer->type,MS_DEFAULT,"Seleção de ".(pegaNome($this->layer))." (".$this->nome.")",$metaClasse="SIM");
if($this->layer->getprojection() != ""){
$novolayer->setprojection($this->layer->getprojection());
}
$novolayer->set("data",$nomeshp.".shp");
$novolayer->set("name",basename($nomeshp));
$down = "nao";
$down = $this->layer->getmetadata("download");
if ($down == ""){
$down = "sim";
}
$novolayer->setmetadata("DOWNLOAD",$down);
$tipo = $this->layer->type;
if ($this->layer->getmetadata("TABELA") != ''){
$novolayer->setmetadata("TABELA","NAO");
}
$novolayer->setmetadata("TEMALOCAL","SIM");
$novolayer->setfilter("");
return("ok");
}
/*
function: selecaoEXT
Seleciona por extensão geográfica.
parameters:
$tipo - Tipo de operação adiciona|retira|inverte|limpa|novo
*/
function selecaoEXT($tipo)
{
if ($tipo == "novo")
{
$this->selecaoLimpa();
$tipo = "adiciona";
}
if(!$this->layer){return "erro";}
$this->layer->set("tolerance",0);
if ($tipo == "limpa")
{return ($this->selecaoLimpa());}
if ($tipo == "inverte")
{return ($this->selecaoInverte());}
if (file_exists($this->qyfile))
{$this->mapa->loadquery($this->qyfile);}
$indxlayer = $this->layer->index;
/*
$res_count = $this->layer->getNumresults();
$shp_atual = array();
for ($i = 0; $i < $res_count;++$i)
{
$rc = $this->layer->getResult($i);
$shp_atual[] = $rc->shapeindex;
}
$this->mapa->freequery($indxlayer);
*/
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema))
{$shp_atual = $this->unserializeQ($this->qyfileTema);}
$shpi = array();
$rect = $this->mapa->extent;
$ident = @$this->layer->queryByRect($rect);
if ($ident != 1)
{
$res_count = $this->layer->getNumresults();
$shpi = array();
for ($i = 0; $i < $res_count; ++$i)
{
$result = $this->layer->getResult($i);
$shpi[] = $result->shapeindex;
}
}
if ($tipo == "adiciona")
{return($this->selecaoAdiciona($shpi,$shp_atual));}
if ($tipo == "retira")
{return($this->selecaoRetira($shpi,$shp_atual));}
}
/*
function: selecaoBOX
Seleciona por retângulo.
parameters:
$tipo - Tipo de operação adiciona|retira|inverte|limpa|novo
$ext - coordenadas separadas por espaços no estilo xmin ymin xmax ymax
*/
function selecaoBOX($tipo,$ext)
{
if ($tipo == "novo")
{
$this->selecaoLimpa();
$tipo = "adiciona";
}
if(!$this->layer){return "erro";}
$this->layer->set("tolerance",0);
if ($tipo == "limpa")
{return ($this->selecaoLimpa());}
if ($tipo == "inverte")
{return ($this->selecaoInverte());}
if (file_exists($this->qyfile))
{$this->mapa->loadquery($this->qyfile);}
$indxlayer = $this->layer->index;
/*
$res_count = $this->layer->getNumresults();
$shp_atual = array();
for ($i = 0; $i < $res_count;++$i)
{
$rc = $this->layer->getResult($i);
$shp_atual[] = $rc->shapeindex;
}
$this->mapa->freequery($indxlayer);
*/
$shp_atual = array();
if($this->qyfileTema != "" && file_exists($this->qyfileTema))
{$shp_atual = $this->unserializeQ($this->qyfileTema);}
$shpi = array();
$temp = explode(" ",$ext);
$rect = ms_newRectObj();
$rect->set("minx",(min(array($temp[0],$temp[2]))));
$rect->set("miny",(min(array($temp[1],$temp[3]))));
$rect->set("maxx",(max(array($temp[0],$temp[2]))));
$rect->set("maxy",(max(array($temp[1],$temp[3]))));
$ident = $this->layer->queryByRect($rect);
if ($ident != 1)
{
$res_count = $this->layer->getNumresults();
$shpi = array();
for ($i = 0; $i < $res_count; ++$i)
{
$result = $this->layer->getResult($i);
$shpi[] = $result->shapeindex;
}
}
if ($tipo == "adiciona")
{return($this->selecaoAdiciona($shpi,$shp_atual));}
if ($tipo == "retira")
{return($this->selecaoRetira($shpi,$shp_atual));}
}
/*
function unserializeQ
Deserializa um arquivo.
Parametros:
$arquivo - arquivo que será processado
*/
function unserializeQ($arq)
{
$handle = fopen ($arq, "r");
$conteudo = fread ($handle, filesize ($arq));
fclose ($handle);
return(unserialize($conteudo));
}
/*
function serializeQ
Serializa um arquivo.
Parametros:
$arquivo - arquivo que será processado
$geos - array com os dados
*/
function serializeQ($arq,$geos)
{
if (file_exists($arq))
{unlink($arq);}
$fp = fopen($arq,"w");
$r = serialize($geos);
fwrite($fp,$r);
fclose($fp);
}
/*
function projetaDistancia
Projeta um valor de distancia em metros para dd
Parametros:
$shape - objeto usado para calcular o centro da projecao
$distancia - distancia em metros
*/
function projetaDistancia($shape,$distancia){
error_reporting(0);
$pt = $shape->getCentroid();
$projInObj = ms_newprojectionobj("proj=longlat,ellps=WGS84,datum=WGS84,no_defs");
$projOutObj = ms_newprojectionobj("proj=poly,ellps=GRS67,lat_0=0,lon_0=".$pt->x.",x_0=5000000,y_0=10000000");
$poPoint = ms_newpointobj();
$poPoint->setXY($pt->x, $pt->y);
$poPoint->project($projInObj, $projOutObj);
$dd2 = ms_newpointobj();
$dd2->setXY(($poPoint->x + $distancia), $poPoint->y);
$dd2->project($projOutObj,$projInObj);
$d = $pt->distanceToPoint($dd2);
if ($d < 0){$d = $d * -1;}
return $d;
}
/*
function bufferShape
Buffer de um shape em metros
Parametros:
$shape - objeto usado para calcular o centro da projecao
$distancia - distancia em metros
*/
function bufferShape($shape,$distancia){
$d = $this->projetaDistancia($shape,$distancia);
$b = $shape->buffer($d);
return $b;
}
}
?>