-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.atualizarpontos.php
376 lines (328 loc) · 11.7 KB
/
exec.atualizarpontos.php
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
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','1');
require_once('classes/experimento.class.php');
require_once('classes/conexao.class.php');
$conexao = new Conexao;
$conn = $conexao->Conectar();
$Experimento = new Experimento();
$Experimento->conn = $conn;
$idexperimento = $_REQUEST['id'];
$filtro = $_REQUEST['filtro'];
$idponto = explode(",",$_REQUEST['idponto']);
$idstatus = $_REQUEST['idstatus'];
$latinf = $_REQUEST['latinf'];
$longinf = $_REQUEST['longinf'];
// print_r($_REQUEST);
if(dirname(__FILE__) != '/var/www/html/rafael/modelr'){
$baseUrl = '../';
} else {
$baseUrl = '';
}
if(empty($_REQUEST['mult'])){
$mult = false;
}
else {
if($_REQUEST['mult'] == 'false'){
$mult = false;
} else {
$mult = true;
}
}
if(empty($_REQUEST['statusOnly'])){
$statusOnly = false;
}
else {
if($_REQUEST['statusOnly'] == 'false'){
$statusOnly = false;
} else {
$statusOnly = true;
}
}
$lista = $idponto;
// if(count($idponto) == 1){
// $lista = $idponto[0];
// } else {
// $lista = $idponto;
// }
$MSGCODIGO = 19;
if ($latinf === '0' || $longinf === '0') {
echo '1';
foreach($lista as $idponto){
$sql = "update modelr.occurrence set idstatusoccurrence=$idstatus,lat2=$latinf, long2=$longinf ";
$sql.=" where
idoccurrence = $idponto";
$res = pg_exec($conn,$sql);
}
}
else if ((($latinf != 'undefined') && (!empty($latinf))) || (($longinf != 'undefined') && (!empty($longinf))))
{
echo '2';
//$Experimento->excluirPonto($idexperimento,$idponto,$idstatus,$latinf,$longinf);
foreach($lista as $idponto){
$sql = "update modelr.occurrence set idstatusoccurrence=$idstatus,lat2=$latinf, long2=$longinf ";
$sql.=" where
idoccurrence = $idponto";
$res = pg_exec($conn,$sql);
}
}
else if($mult == true || $statusOnly == true){
echo '3';
foreach($lista as $idponto){
$sql = "update modelr.occurrence set idstatusoccurrence=$idstatus ";
$sql.=" where
idoccurrence = $idponto";
$res = pg_exec($conn,$sql);
}
$filtro = $idstatus;
}
else
{
echo 'else';
//$lista = $_REQUEST['table_records'];
// FORA DO LIMITE DO BRASIL
// // libera o experimento para a modelagem
// if (($idstatus == '17') || ($idstatus=='4'))
// {
// $sql = "update modelr.experiment set idstatusexperiment = 2 where idexperiment = ".$idexperimento;
// $res = pg_exec($conn,$sql);
// }
// coodenada zerada
if (($idstatus=='13') || ($idstatus=='99'))
{
$sql = "update modelr.occurrence set idstatusoccurrence=13 where
lat = 0 or long = 0 and
idexperiment = ".$idexperimento;
$MSGCODIGO = 73;
$res = pg_exec($conn,$sql);
}
// georreferenciar coordenadas
if (($idstatus=='3') || ($idstatus=='99'))
{
echo 'georreferenciar';
$sql = "select * from modelr.occurrence where idexperiment = ".$idexperimento." and idstatusoccurrence <> 13 and lat is null and long is null";
$res = pg_exec($conn,$sql);
// echo $sql;
// echo '<br>';
while ($row = pg_fetch_array($res))
{
// print_r($row);
// echo '<br>';
$sql2 = "select * from base_geografica.\"municipios_2014\" shape where LOWER(shape.nm_mun) = '".strtolower($row['minorarea'])."'";
// echo $sql2;
// echo '<br>';
$res2 = pg_exec($conn,$sql2);
while ($row3 = pg_fetch_array($res2))
{
// print_r($row3);
// echo '<br>';
$sql4 = "update modelr.occurrence set idstatusoccurrence=5, lat2 = ".$row3['y_mun'].", long2 = ".$row3['x_mun']."
where idoccurrence = ".$row['idoccurrence'];
// echo $sql4;
// echo '<br>';
$res4 = pg_exec($conn,$sql4);
}
}
$sql5 = "select * from modelr.occurrence where idexperiment = ".$idexperimento." and lat is null and long is null and lat2 is null and long2 is null";
$res5 = pg_exec($conn,$sql5);
// echo $sql;
// echo '<br>';
while ($row5 = pg_fetch_array($res5))
{
$sql6 = "update modelr.occurrence set idstatusoccurrence=3 where idoccurrence = ".$row5['idoccurrence'];
$res6 = pg_exec($conn,$sql6);
}
}
// fora do limite do brasil
if (($idstatus=='10') || ($idstatus=='99'))
{
$sql = "update modelr.occurrence set idstatusoccurrence=10 where
idexperiment = ".$idexperimento." and idstatusoccurrence <> 13 and
idoccurrence in (
select idoccurrence from modelr.occurrence o,
base_geografica.\"shp_limite_brasil_250MIL\" shape
where
not contains(GeomFromEWKT(shape.geom),GeomFromEWKT('SRID=4326;POINT(' || o.long || ' ' || o.lat || ')'))
)";
$MSGCODIGO = 74;
$res = pg_exec($conn,$sql);
}
// fora do municipio de origem
if (($idstatus=='2') || ($idstatus=='99'))
{
$sql = "update modelr.occurrence set idstatusoccurrence=2 where
idexperiment = ".$idexperimento." and
(idstatusoccurrence <> 13 and idstatusoccurrence <> 10) and
idoccurrence in (
select idoccurrence from modelr.occurrence o,
base_geografica.\"municipios_2014\" shape
where
contains(GeomFromEWKT(shape.geom),GeomFromEWKT('SRID=4326;POINT(' || o.long || ' ' || o.lat || ')' ))
and (shape.nm_uf <> majorarea
or shape.nm_mun <> minorarea)
and majorarea <> '' and minorarea <> ''
)";
//shape.pais <> country
$res = pg_exec($conn,$sql);
//teste inverter coordenada
// pago o que foi marcado como idstatusocurrence = 2
$sql2 = 'select * from modelr.occurrence where (idstatusoccurrence = 2 or idstatusoccurrence = 8 or idstatusoccurrence = 10 or idstatusoccurrence = 11 or idstatusoccurrence = 19) and
idexperiment = '.$idexperimento;
$res2 = pg_exec($conn,$sql2);
while ($row = pg_fetch_array($res2))
{
$sql3 = "select * from modelr.occurrence o,
base_geografica.\"municipios_2014\" shape
where
idoccurrence = ".$row['idoccurrence']." and
contains(GeomFromEWKT(shape.geom),GeomFromEWKT('SRID=4326;POINT(' || o.lat || ' ' || o.long || ')' ))
and (shape.nm_uf = majorarea
and shape.nm_mun = minorarea)";
// shape.pais = country
$res3 = pg_exec($conn,$sql3);
while ($row2 = pg_fetch_array($res3))
{
// troco o status para 4 // normal e inverto a coordenada
$sql4 = "update modelr.occurrence set idstatusoccurrence=4, lat2 = ".$row2['long'].", long2 = ".$row2['lat']."
where idoccurrence = ".$row2['idoccurrence'];
$res4 = pg_exec($conn,$sql4);
}
}
//teste trocar sinal de coordenada (latitude)
$sql5 = 'select * from modelr.occurrence where (idstatusoccurrence = 2 or idstatusoccurrence = 8 or idstatusoccurrence = 10 or idstatusoccurrence = 11 or idstatusoccurrence = 19) and
idexperiment = '.$idexperimento.' and lat > 0';
$res5 = pg_exec($conn,$sql5);
while ($row3 = pg_fetch_array($res5))
{
$sql6 = "select * from modelr.occurrence o,
base_geografica.\"municipios_2014\" shape
where
idoccurrence = ".$row3['idoccurrence']." and
contains(GeomFromEWKT(shape.geom),GeomFromEWKT('SRID=4326;POINT(' || o.long || ' ' || -1 * o.lat || ')' ))
and (shape.nm_uf = majorarea
and shape.nm_mun = minorarea)";
// shape.pais = country
$res6 = pg_exec($conn,$sql6);
while ($row4 = pg_fetch_array($res6))
{
// troco o status para 4 // normal e inverto a coordenada
$sql7 = "update modelr.occurrence set idstatusoccurrence=4, lat2 = -1 * ".$row4['lat']."
where idoccurrence = ".$row4['idoccurrence'];
$res7 = pg_exec($conn,$sql7);
}
}
//teste trocar sinal de coordenada (longitude)
$sql8 = 'select * from modelr.occurrence where (idstatusoccurrence = 2 or idstatusoccurrence = 8 or idstatusoccurrence = 10 or idstatusoccurrence = 11 or idstatusoccurrence = 19) and
idexperiment = '.$idexperimento.' and long > 0';
$res8 = pg_exec($conn,$sql8);
while ($row5 = pg_fetch_array($res8))
{
$sql9 = "select * from modelr.occurrence o,
base_geografica.\"municipios_2014\" shape
where
idoccurrence = ".$row5['idoccurrence']." and
contains(GeomFromEWKT(shape.geom),GeomFromEWKT('SRID=4326;POINT(' || -1 * o.long || ' ' || o.lat || ')' ))
and (shape.nm_uf = majorarea
and shape.nm_mun = minorarea)";
// shape.pais = country
$res9 = pg_exec($conn,$sql9);
while ($row6 = pg_fetch_array($res9))
{
// troco o status para 4 // normal e inverto a coordenada
$sql10 = "update modelr.occurrence set idstatusoccurrence=4, long2 = -1 * ".$row6['long']."
where idoccurrence = ".$row6['idoccurrence'];
$res10 = pg_exec($conn,$sql10);
}
}
//teste trocar sinal de coordenada (latitude e longitude)
$sql11 = 'select * from modelr.occurrence where (idstatusoccurrence = 2 or idstatusoccurrence = 8 or idstatusoccurrence = 10 or idstatusoccurrence = 11 or idstatusoccurrence = 19) and
idexperiment = '.$idexperimento.' and (long > 0) and (lat > 0)';
$res11 = pg_exec($conn,$sql11);
while ($row7 = pg_fetch_array($res11))
{
$sql12 = "select * from modelr.occurrence o,
base_geografica.\"municipios_2014\" shape
where
idoccurrence = ".$row7['idoccurrence']." and
contains(GeomFromEWKT(shape.geom),GeomFromEWKT('SRID=4326;POINT(' || -1 * o.long || ' ' || -1 * o.lat || ')' ))
and (shape.nm_uf = majorarea
and shape.nm_mun = minorarea)";
// shape.pais = country
$res12 = pg_exec($conn,$sql12);
while ($row8 = pg_fetch_array($res12))
{
// echo "entrou";
// print_r($row8);
// exit;
// troco o status para 4 // normal e inverto a coordenada
$sql13 = "update modelr.occurrence set idstatusoccurrence=4, lat2 = -1 * ".$row8['lat'].", long2 = -1 * ".$row8['long']."
where idoccurrence = ".$row8['idoccurrence'];
$res13 = pg_exec($conn,$sql13);
}
}
$MSGCODIGO = 73;
}
//marcar pontos com verificados pelo filtro
$sql = "update modelr.occurrence set idstatusoccurrence=19 where
idstatusoccurrence=8 and idexperiment = ".$idexperimento;
$res = pg_exec($conn,$sql);
if ($idstatus=='99')
{
// pontos duplicados
$Experimento->marcarduplicados($idexperimento);
$MSGCODIGO = 75;
}
if ($idstatus=='99')
{
// pontos duplicados
$Experimento->marcarduplicatas($idexperimento);
$MSGCODIGO = 75;
}
}
//criar extent
if($idstatus == '4' || $idstatus == '17'){
if (!file_exists($baseUrl . "temp/" . $idexperimento )) {
mkdir($baseUrl . "temp/" . $idexperimento , 0777, true);
}
#ocorrencias.csv
$ocorrenciasCSVPath = $baseUrl . 'temp/'. $idexperimento . '/ocorrencias.csv';
$file = fopen($ocorrenciasCSVPath, 'w');
fputcsv($file, array("taxon","lon","lat"), ";");
$ws = file_get_contents("https://model-r.jbrj.gov.br/modelr-web/ws/?id=" . $idexperimento);
$json = json_decode($ws);
$occurrenceList = $json[0]->occurrences;
$count = 0;
foreach($occurrenceList as $occurrence){
$item = [];
if($occurrence->idstatusoccurrence == 4 || $occurrence->idstatusoccurrence == 17){
if($occurrence->lat_ajustada != "") $lat = $occurrence->lat_ajustada;
else $lat = $occurrence->lat;
if($occurrence->lon_ajustada != "") $lon = $occurrence->lon_ajustada;
else $lon = $occurrence->lon;
array_push($item,$occurrence->taxon,$lon,$lat);
fputcsv($file, $item, ";");
$count = $count + 1;
}
}
fclose($file);
$ocorrenciasCSVPath = $baseUrl . 'temp/'. $idexperimento . '/ocorrencias.csv';
exec("Rscript R/extent-points.r " . $idexperimento . ' ' . $ocorrenciasCSVPath, $a, $b);
$oeste = str_replace('xmin',"",$a[1]);
$oeste = str_replace(' ',"",$oeste);
$oeste = str_replace(':',"",$oeste);
$leste = str_replace("xmax","",$a[2]);
$leste = str_replace(" ","",$leste);
$leste = str_replace(":","",$leste);
$sul = str_replace("ymin","",$a[3]);
$sul = str_replace(" ","",$sul);
$sul = str_replace(":","",$sul);
$norte = str_replace("ymax","",$a[4]);
$norte = str_replace(" ","",$norte);
$norte = str_replace(":","",$norte);
$extensao = $leste.';'.$oeste.';'.$norte.';'.$sul;
$result = $Experimento->incluirExtensao($idexperimento, $extensao);
$result = $Experimento->incluirProjecao($idexperimento, $extensao);
}
header("Location: cadexperimento.php?op=A&MSGCODIGO=$MSGCODIGO&tab=10&pag=2&id=$idexperimento&filtro=$filtro");
?>