From 291a14f707e6805fa2edb1851777a4c9bc8ddc57 Mon Sep 17 00:00:00 2001 From: Edmar Moretti Date: Thu, 31 Jan 2013 19:10:06 +0000 Subject: [PATCH] Inclusão de opção para upload de shapefile e gravação no postgis --- admin/admin.db | Bin 188416 -> 0 bytes admin/html/estat_editor.html | 1 + admin/html/estat_tipo_regiao.html | 2 +- admin/index.html | 2 +- admin/js/estat_editor.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- admin/js/estat_tipo_regiao.js | 6 +++--- admin/php/criabanco.php | 2 ++ admin/php/metaestat_uploadshp_submit.php | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ admin/php/upgradebanco46_47.php | 3 +++ documentacao/diagramas/cadastrodeusuarios.erm | 40 +++++++++++++++++++++++++++------------- 10 files changed, 266 insertions(+), 22 deletions(-) create mode 100755 admin/php/metaestat_uploadshp_submit.php diff --git a/admin/admin.db b/admin/admin.db index e63c675..92c5c8d 100644 Binary files a/admin/admin.db and b/admin/admin.db differ diff --git a/admin/html/estat_editor.html b/admin/html/estat_editor.html index d4f7d4d..2d6e9be 100644 --- a/admin/html/estat_editor.html +++ b/admin/html/estat_editor.html @@ -43,6 +43,7 @@ body { Apenas as tabelas e os dados existentes no esquema i3geo_metaestat podem ser editados

+
diff --git a/admin/html/estat_tipo_regiao.html b/admin/html/estat_tipo_regiao.html index f2abf43..00ed6ff 100755 --- a/admin/html/estat_tipo_regiao.html +++ b/admin/html/estat_tipo_regiao.html @@ -41,7 +41,7 @@ body {

+ value="Adicionar um novo registro" style="left: -5px;" /> Utilize a opção "Editor" no menu superior caso você queira fazer o upload de shapefile para criar uma nova tabela com os dados que serão utilizados em uma determinada região


diff --git a/admin/index.html b/admin/index.html index bbf950b..167be9a 100644 --- a/admin/index.html +++ b/admin/index.html @@ -188,7 +188,7 @@
- Upload de dados + Upload de dados para uma variável (para upload de shapefile, utilize o gerenciador de tabelas)
diff --git a/admin/js/estat_editor.js b/admin/js/estat_editor.js index 65f0f2d..0d86f3c 100644 --- a/admin/js/estat_editor.js +++ b/admin/js/estat_editor.js @@ -81,11 +81,13 @@ i3GEOadmin.editor = { success:function(o){ try { var dados = YAHOO.lang.JSON.parse(o.responseText), - temp = "
Escolha uma tabela: "; - temp += "

" + + "
" + + "Escolha uma tabela existente: " + + ""; - temp += "" + + temp += "" + "

" + "" + "" + @@ -93,6 +95,10 @@ i3GEOadmin.editor = { ""; $i(i3GEOadmin.editor.tabela.onde).innerHTML = temp+"

"; new YAHOO.widget.Button( + "i3GEOadmin_botaoupload", + {onclick:{fn: i3GEOadmin.editor.uploadshp.inicia}} + ); + new YAHOO.widget.Button( "i3GEOadmintabelaMostrar", {onclick:{fn: i3GEOadmin.editor.tabela.mostrar}} ); @@ -399,5 +405,50 @@ i3GEOadmin.editor = { } return false; } + }, + uploadshp: { + inicia: function(){ + var onde = $i("i3GEOadmin_formupload"); + if(onde.innerHTML != ""){ + onde.innerHTML = ""; + return; + } + $i("i3GEOadmin_formupload").innerHTML = i3GEOadmin.editor.uploadshp.formulario(); + new YAHOO.widget.Button( + "i3GEOuploadsubmit", + {onclick:{fn: i3GEOadmin.editor.uploadshp.submit}} + ); + }, + formulario: function(){ + var ins = '
' + + '

shp:

' + + '

shx:

' + + '

dbf:

' + + '

Nome da nova tabela:

' + + '

Código da projeção (SRID):

' + + + '

' + + '' + + '' + + '' + + '

' + + "

Não utilize '_' no nome do arquivo. Apenas letras e números são aceitos!!!

" + + '
'; + return ins; + }, + submit: function(){ + if($i("tabelaDestino").value == ""){ + alert("Digite o nome da tabela a ser criada"); + return; + } + if($i("srid").value == ""){ + alert("Digite o valor do SRID"); + return; + } + $i("i3GEOuploadcodigoconexao").value = $i("i3GEOadmincodigo_estat_conexao").value; + $i("i3GEOuploadesquema").value = $i("i3GEOadminesquema").value; + $i("i3GEOuploadf").submit(); + } } + }; \ No newline at end of file diff --git a/admin/js/estat_tipo_regiao.js b/admin/js/estat_tipo_regiao.js index 6421df7..6a69d9d 100755 --- a/admin/js/estat_tipo_regiao.js +++ b/admin/js/estat_tipo_regiao.js @@ -55,13 +55,13 @@ function montaTabela(dados){ elCell.innerHTML = "
"; }, formatShp = function(elCell, oRecord, oColumn){ - elCell.innerHTML = "
"; + elCell.innerHTML = "
"; }, myColumnDefs = [ {key:"excluir",label:"excluir",formatter:formatExclui}, - {key:"shapefile",label:"shapefile",formatter:formatShp}, + {key:"shapefile",label:"download",formatter:formatShp}, {key:"rel",label:"agregações",formatter:formatRel}, - {key:"mais",label:"editar",formatter:formatMais}, + {key:"mais",label:"propriedades",formatter:formatMais}, {label:"código",key:"codigo_tipo_regiao", formatter:formatTexto}, {label:"Nome",resizeable:true,key:"nome_tipo_regiao", formatter:formatTexto}, {label:"Descrição",resizeable:true,key:"descricao_tipo_regiao", formatter:formatTexto}, diff --git a/admin/php/criabanco.php b/admin/php/criabanco.php index 1333893..8f6f541 100644 --- a/admin/php/criabanco.php +++ b/admin/php/criabanco.php @@ -173,6 +173,8 @@ $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoes VALUES('16', 'admin/php/editortexto', 'editor de textos para edicao de mapfiles')"); $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoes VALUES('17', 'admin/html/usuarios', 'cadastro de usuarios')"); $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoes VALUES('18', 'admin/metaestat/geral', 'permite edicoes mais comuns do sistema de metadados estatisticos')"); + $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoes VALUES('19', 'admin/metaestat/editorbanco', 'permite gerenciar as tabelas do banco')"); + //papeis por operacao $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoespapeis VALUES(1,2)"); $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoespapeis VALUES(1,3)"); diff --git a/admin/php/metaestat_uploadshp_submit.php b/admin/php/metaestat_uploadshp_submit.php new file mode 100755 index 0000000..1c08424 --- /dev/null +++ b/admin/php/metaestat_uploadshp_submit.php @@ -0,0 +1,173 @@ + + + + + + + + + +

+Carregando o arquivo...

"; + $arqshp = $_FILES['i3GEOuploadshp']['tmp_name']; + //verifica nomes e sobe arquivo + verificaNome($_FILES['i3GEOuploadshp']['name'],"shp"); + $nomePrefixo = str_replace(" ","_",removeAcentos(str_replace(".shp","",$_FILES['i3GEOuploadshp']['name']))); + $nomePrefixo = $nomePrefixo."_".(nomeRandomico(4)); + + $Arquivo = $_FILES['i3GEOuploadshp']['tmp_name']; + $status = move_uploaded_file($Arquivo,$dir_tmp."/".$nomePrefixo.".shp"); + if($status != 1) + {echo "

Ocorreu um erro no envio do arquivo SHP";exit;} + $Arquivo = $_FILES['i3GEOuploadshx']['tmp_name']; + $status = move_uploaded_file($Arquivo,$dir_tmp."/".$nomePrefixo.".shx"); + if($status != 1) + {echo "

Ocorreu um erro no envio do arquivo SHX";exit;} + $Arquivo = $_FILES['i3GEOuploaddbf']['tmp_name']; + $status = move_uploaded_file($Arquivo,$dir_tmp."/".$nomePrefixo.".dbf"); + if($status != 1) + {echo "

Ocorreu um erro no envio do arquivo DBF";exit;} + + if(!file_exists($dir_tmp."/".$nomePrefixo.".shp")) + {echo "

Ocorreu algum problema no envio do arquivo ".$dir_tmp."/".$nomePrefixo;paraAguarde();exit;} + $arqshp = $dir_tmp."/".$nomePrefixo.".shp"; + + //pega os parametros de conexao + include("classe_metaestat.php"); + $m = new Metaestat(); + $conexao = $m->listaConexao($_POST["i3GEOuploadcodigoconexao"],true); + //array(5) { ["codigo_estat_conexao"]=> string(1) "1" ["bancodedados"]=> string(8) "geosaude" ["host"]=> string(9) "localhost" ["porta"]=> string(4) "5432" ["usuario"]=> string(8) "postgres" } + //pega as colunas do shapefile + $shapefileObj = ms_newShapefileObj($arqshp,-1); + $numshapes = $shapefileObj->numshapes; + $mapObj = ms_newMapObjFromString("MAP END"); + $layer = ms_newLayerObj($mapObj); + $layer->set("data",$arqshp); + $layer->open(); + $colunas = $layer->getItems(); + + echo "
Numshapes: ". $numshapes; + $tipo = $shapefileObj->type; + echo "
Tipo: ". $tipo; + echo "
Colunas: "; + var_dump($colunas); + + $sqinsert = array(); + + + + //verifica o tipo de coluna + $tipoColuna = array(); + if($numshapes < 10){ + $testar = $numshapes; + } + else{ + $testar = 10; + } + foreach($colunas as $coluna){ + $tipo = "numeric"; + for ($i=0; $i<$testar;$i++){ + $s = $layer->getShape(new resultObj($i)); + $v = $s->getValue($layer,$coluna); + if(!is_numeric($v)){ + $tipo = "varchar"; + } + } + $tipoColuna[$coluna] = $tipo; + } + echo "
Tipos das colunas:

";
+	var_dump($tipoColuna);
+	echo "
"; + //gera o script para criar a tabela + $sqltabela = array(); + $sql = "CREATE TABLE ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]."(gid integer, the_geom geometry"; + foreach($colunas as $coluna){ + $sql .= ",".strtolower($coluna)." ".$tipoColuna[$coluna]; + } + $sql .= ")WITH(OIDS=FALSE)"; + $sqltabela[] = $sql; + $sqltabela[] = "ALTER TABLE ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]." OWNER TO ".$conexao["usuario"]; + $sqltabela[] = "CREATE INDEX ".$_POST["tabelaDestino"]."_indx_thegeom ON ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]." USING gist (the_geom )"; + echo "
Sql tabela:
";
+	var_dump($sqltabela);
+	echo "
"; + //gera o script para inserir os dados + $linhas = array(); + $insert = "INSERT INTO ".$_POST["i3GEOuploadesquema"].".".$_POST["tabelaDestino"]."( gid,".strtolower(implode(",",$colunas)).",the_geom)"; + for ($i=0; $i<$numshapes;$i++){ + $s = $layer->getShape(new resultObj($i)); + $vs = array(); + $vs[] = $i; + foreach($colunas as $coluna){ + if($tipoColuna[$coluna] == "varchar"){ + $vs[] = "'".$s->getValue($layer,$coluna)."'"; + } + else{ + $vs[] = $s->getValue($layer,$coluna); + } + } + $vs[] = "st_geomfromtext('".$s->toWkt()."','".$_POST["srid"]."')"; + $linhas[] = $insert."VALUES(".implode(",",$vs).")"; + } + $layer->close(); + try { + $dbh = new PDO('pgsql:dbname='.$conexao["bancodedados"].';user='.$conexao["usuario"].';password='.$conexao["senha"].';host='.$conexao["host"].';port='.$conexao["porta"]); + } catch (PDOException $e) { + echo 'Connection failed: ' . $e->getMessage(); + } + foreach($sqltabela as $linha){ + try { + $dbh->query($linha); + } catch (PDOException $e) { + echo 'Erro: ' . $e->getMessage(); + } + } + foreach($linhas as $linha){ + try { + $dbh->query($linha); + } catch (PDOException $e) { + echo 'Erro: ' . $e->getMessage(); + } + } + echo "
Feito!!!"; +} +else{ + echo "

Erro ao enviar o arquivo. Talvez o tamanho do arquivo seja maior do que o permitido.

"; +} +function verificaNome($nome,$ext){ + $nome = strtolower($nome); + $lista = explode(".",$nome); + $extensao = $lista[count($lista) - 1]; + if($extensao != $ext){ + echo "Nome de arquivo inválido."; + exit; + } +} +?> + + \ No newline at end of file diff --git a/admin/php/upgradebanco46_47.php b/admin/php/upgradebanco46_47.php index 459a3c6..3c85a61 100644 --- a/admin/php/upgradebanco46_47.php +++ b/admin/php/upgradebanco46_47.php @@ -176,6 +176,9 @@ if(!in_array($teste,17)) $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoes VALUES('17', 'admin/html/usuarios', 'cadastro de usuarios')"); if(!in_array($teste,18)) $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoes VALUES('18', 'admin/metaestat/geral', 'permite edicoes mais comuns do sistema de metadados estatisticos')"); +if(!in_array($teste,19)) + $dbhw->query("INSERT INTO ".$esquemaadmin."i3geousr_operacoes VALUES('19', 'admin/metaestat/editorbanco', 'permite gerenciar as tabelas do banco')"); + $teste = lista("select * from ".$esquemaadmin."i3geousr_operacoes","id_operacao","id_papel"); diff --git a/documentacao/diagramas/cadastrodeusuarios.erm b/documentacao/diagramas/cadastrodeusuarios.erm index 3533e80..70a6fa6 100644 --- a/documentacao/diagramas/cadastrodeusuarios.erm +++ b/documentacao/diagramas/cadastrodeusuarios.erm @@ -110,7 +110,7 @@ false 2012-07-19 01:51:14 - 2012-11-28 13:56:34 + 2013-01-31 12:16:31 Project Name @@ -656,7 +656,7 @@ 0 - 1 + 4 2 1..n 1 @@ -673,7 +673,7 @@ 1 - 4 + 1 2 1..n 1 @@ -699,7 +699,7 @@ 7 0 - 0 + 1 @@ -732,7 +732,7 @@ 8 11 - 1 + 0 @@ -787,7 +787,7 @@ 2 - 6 + 4 3 1..n 1 @@ -804,7 +804,7 @@ 3 - 4 + 6 3 1..n 1 @@ -830,7 +830,7 @@ 9 16 - 2 + 3 @@ -863,7 +863,7 @@ 10 11 - 3 + 2 @@ -1045,7 +1045,7 @@ 4 - 1 + 7 5 1..n 1 @@ -1062,7 +1062,7 @@ 5 - 7 + 1 5 1..n 1 @@ -1088,7 +1088,7 @@ 14 0 - 4 + 5 @@ -1121,7 +1121,7 @@ 15 19 - 5 + 4 @@ -2054,6 +2054,20 @@ permite edicoes mais comuns do sistema de metadados estatisticos + + + 16 + 19 + + + 17 + admin/metaestat/editorbanco + + + 18 + permite gerenciar as tabelas do banco de dados metaestat + + 0 -- libgit2 0.21.2