#include "grass/gis.h" #include "grass/Vect.h" #include "grass/display.h" #include "grass/raster.h" #include "grass/colors.h" #include #include #include "libreria.h" #include "math.h" int crea_db(char* nombre_tabla,int *ordenacion,struct Map_info *map); int main (int argc,char *argv[]){ struct GModule *module; //Definición del módulo struct Map_info map,map2; //Mapas struct Option *input, *output,*crit,*des; //Opciones de usuario struct line_pnts *linea,*linea1,*linea2; //Secuencias de puntos struct line_cats *cats; int lev,n1,n2,criterio,desemb,busc; int i,ii,l,k,l1,l2,l3,l4,ll,n_lines,n_nodes,fin,p,prev; char *mapset, err_msg[200],buf[1024],*name,*name2; double *recta,*sinuosidad,*longit,long2,recta2,sinu2; int *orden,*sigue1,*sigue2,*tocado,*coming,*desem,*att,*n_lineas,*fuente1,*fuente2,*fuente3; double x,y; char *colorin; //Variables de la base de datos char *db_nombre, *driver_nombre, *ordenc; dbDriver *driver; dbHandle handle; dbTable *tabla; dbColumn *columna; dbString orden_sql; ordenc=(char *) malloc(200); colorin=(char *) malloc(11); G_gisinit (argv[0]); // Gestión de la linea de comandos module = G_define_module(); module->description ="Ordena una red de drenaje generada artificialmente"; input = G_define_option(); input->key = "input"; input->type = TYPE_STRING; input->required = YES; input->description = "Nombre del mapa vectorial que vas a abrir"; input->gisprompt = "viejo,vectorial,mapa vectorial"; output = G_define_option(); output->key = "output"; output->type = TYPE_STRING; output->required = YES; output->description = "Nombre del mapa vectorial que vas a crear"; output->gisprompt = "nuevo,vectorial,mapa vectorial"; crit = G_define_option(); crit->key = "crit"; crit->type = TYPE_INTEGER; crit->required = YES; crit->description = "Criterio de ordenación (1:Strahler 2:Shreve 3:Crea tabla)"; crit->gisprompt = ""; des = G_define_option(); des->key = "desembocadura"; des->type = TYPE_INTEGER; des->required = YES; des->description = "cat del tramo de desembocadura de la red"; des->gisprompt = ""; if (G_parser(argc,argv)) exit(1); criterio=atoi(crit->answer); desemb=atoi(des->answer); //Inicialización de comandos gráficos if (R_open_driver()!=0) G_fatal_error("No hay monitor\n"); D_setup(0);//R_standard_color(0); G_setup_plot(D_get_d_north(),D_get_d_south(),D_get_d_west(),D_get_d_east(),D_move_abs,D_cont_abs); //COMENZAR LA CREACION DE LA BASE DE DATOS //Establecer parámetros de la conexión driver_nombre=db_get_default_driver_name(); db_nombre=db_get_default_database_name(); //Abrimos driver=db_start_driver(driver_nombre); db_init_handle(&handle); db_set_handle(&handle,db_nombre,NULL); if(db_open_database(driver,&handle)!=DB_OK){exit(ERROR);} //Generar y comprobar la orden SQL de creación de tabla db_init_string(&orden_sql); db_zero_string (&orden_sql); sprintf(ordenc,"create table %s ( cat integer, ",output->answer); db_append_string(&orden_sql,ordenc); db_append_string(&orden_sql,"orden integer, longitud double precision, GRASSRGB varchar(11) );"); ordenc=db_get_string(&orden_sql); printf("%s\n",ordenc); //Ejecutar (crear la tabla, hacer un índice y dar permisos) printf("conexión: %s %s\n",driver_nombre,db_nombre); printf("Ejecuta: %d\n",db_execute_immediate (driver, &orden_sql)); printf("Indice: %d\n",db_create_index2(driver, output->answer, "cat" )); printf("GRANT: %d\n",db_grant_on_table (driver, output->answer, DB_PRIV_SELECT, DB_GROUP|DB_PUBLIC )); //db_commit_transaction(driver);db_close_database(driver); //db_close_database(driver); // db_shutdown_driver(driver); // Abre el mapa vectorial de entrada name = input->answer; mapset = G_find_file ("vector", name, ""); if (mapset == NULL) { sprintf (err_msg, "Could not find vector file [%s]", name); G_fatal_error (err_msg); } printf("HOLA\n"); lev=Vect_open_old (&map, name, mapset); if (0 > lev) { sprintf (err_msg, "Could not open vector file [%s]\n", name); G_fatal_error (err_msg); } n_lines=Vect_get_num_lines(&map); n_nodes=Vect_get_num_nodes(&map); printf("Mapa %s abierto en nivel %d.\n",name,lev); printf("%d areas, %d lineas, %d nodos %d islas %d atts\n",Vect_get_num_areas(&map),n_lines,n_nodes,Vect_get_num_islands(&map)); if (criterio!=3){ //Abre el mapa vectorial de salida name2 = output->answer; lev=Vect_open_new (&map2, name2,0); if (0 > lev) { sprintf (err_msg, "Could not open vector file [%s]\n", name2); G_fatal_error (err_msg); } printf("Mapa %s abierto en nivel %d.\n",name2,lev); } printf("HOLA\n"); //Inicialización de estructuras y arrays cats=Vect_new_cats_struct(); linea=Vect_new_line_struct(); linea1=Vect_new_line_struct(); linea2=Vect_new_line_struct(); if (!(tocado=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 1\n"); if (!(orden=G_calloc(n_nodes+1,sizeof(int)))) printf("ERROR 2\n"); if (!(coming=G_calloc(n_nodes+1,sizeof(int)))) printf("ERROR 3\n"); if (!(longit=G_calloc(n_lines+1,sizeof(double)))) printf("ERROR 4\n"); if (!(sigue1=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 5\n"); if (!(sigue2=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 6\n"); if (!(recta=G_calloc(n_lines+1,sizeof(double)))) printf("ERROR 7\n"); if (!(sinuosidad=G_calloc(n_lines+1,sizeof(double)))) printf("ERROR 8\n"); if (!(n_lineas=G_calloc(n_nodes+1,sizeof(int)))) printf("ERROR 9\n"); if (!(fuente1=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 10\n"); if (!(fuente2=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 11\n"); if (!(fuente3=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 12\n"); if (!(desem=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 13\n"); if (!(att=G_calloc(n_lines+1,sizeof(int)))) printf("ERROR 14\n"); for (l=1;l<=n_lines;l++){ Vect_read_line(&map,linea,cats,l); longit[l]=longitud(linea); recta[l]=lonrec(linea); sinuosidad[l]=longit[l]/recta[l]; orden[l]=0; sigue1[l]=-99;sigue2[l]=-99; // printf("linea %d, %f m\n",l,longit[l]); } //Busqueda de lineas con orden 1 for (i=1;i<=n_nodes;i++){ n_lineas[i]=(map.plus.Node[i])->n_lines; printf("%d: %d",i,n_lineas[i]); if (n_lineas[i]==1){ l=abs(*((map.plus.Node[i])->lines)); printf(" %d ",l);if (l==desemb){printf("desembocadura\n");} else { orden[l]=1;printf("%d orden 1\n",l); Vect_read_line(&map,linea,cats,l); pintalinea(linea,orden[l]); R_stabilize(); } } else{printf("\n");} } busc=1; if (busc==1){ //printf("BUSQUEDA DE ORDEN >1\n"); fin=-1; while (fin!=0){ fin=0; for (i=1;i<=n_nodes;i++){ //Para todos los nodos if (n_lineas[i]==3){ //Si es un nodo con 3 lineas (caso normal) k=0; l1=abs(*((map.plus.Node[i])->lines)); l2=abs(*((map.plus.Node[i])->lines+1)); l3=abs(*((map.plus.Node[i])->lines+2)); if (orden[l1]!=0) k++;if (orden[l2]!=0) k++;if (orden[l3]!=0) k++; if (k==2){ ll=ordena(l1,&(orden[l1]),l2,&(orden[l2]),l3,&(orden[l3]),criterio);fin++; prev=0; if (orden[ll]==orden[l1] && l1!=ll) prev=l1; if (orden[ll]==orden[l2] && l2!=ll) prev=l2; if (orden[ll]==orden[l3] && l3!=ll) prev=l3; if (ll==l1){fuente1[ll]=l2;fuente2[ll]=l3;desem[l2]=ll;desem[l3]=ll;} if (ll==l2){fuente1[ll]=l1;fuente2[ll]=l3;desem[l1]=ll;desem[l3]=ll;} if (ll==l3){fuente1[ll]=l1;fuente2[ll]=l2;desem[l1]=ll;desem[l2]=ll;} if (prev!=0){ if (sigue1[prev]==-99) sigue1[prev]=ll; else sigue2[prev]=ll; if (sigue1[ll]==-99) sigue1[ll]=prev; else sigue2[ll]=prev; } //printf("el nodo %d tiene %d lineas\n",i,n_lineas[i]); //printf("%d %d %d %d %d %d \n",l1,orden[l1],l2,orden[l2],l3,orden[l3]); Vect_read_line(&map,linea,cats,ll); pintalinea(linea,orden[ll]); R_stabilize(); } } } } } printf("\nEscritura de los ficheros\n"); //if(db_open_database(driver,&handle)!=DB_OK){exit(ERROR);} for (i=1;i<=n_lines;i++){ if (criterio!=3){ if (sigue1[i]!=-99 && sigue2[i]==-99 && tocado[i]==0){ printf("Procesando linea %d\n",i); p=sigue1[i];linea1=Vect_new_line_struct(); Vect_read_line(&map,linea1,cats,i);n1=linea1->n_points; fin=0;ii=i; while(fin==0){ tocado[p]=1;tocado[ii]=1; linea2=Vect_new_line_struct();Vect_read_line(&map,linea2,cats,p); n1=linea1->n_points;n2=linea2->n_points; printf("l1=%d n1=%d l2=%d n2=%d\n",i,n1,p,n2); linea=Vect_new_line_struct();une_tramos(linea1,linea2,linea); if (sigue1[p]==ii && sigue2[p]==-99){ fin=1; Vect_write_line(&map2,2,linea,cats); long2=longitud(linea);recta2=lonrec(linea);sinu2=long2/recta2; //Añadir linea en la base de datos if(orden[p]==1) strcpy(colorin,"0:0:255"); if(orden[p]==2) strcpy(colorin,"255:255:0"); if(orden[p]==3) strcpy(colorin,"0:255:0"); if(orden[p]==4) strcpy(colorin,"255:0:0"); if(orden[p]==5) strcpy(colorin,"0:255:255"); if(orden[p]==6) strcpy(colorin,"255:0:255"); if(orden[p]>6) strcpy(colorin,"0:0:0"); sprintf(ordenc,"insert into %s values(%d,%d,%f,'%s');",name2,p,orden[p],long2,colorin); printf("%s\n",ordenc); db_set_string(&orden_sql,ordenc); printf("Ejecuta: %d\n",db_execute_immediate(driver, &orden_sql)); Vect_destroy_line_struct(linea); Vect_destroy_line_struct(linea1); Vect_destroy_line_struct(linea2); } else{ if (sigue1[p]==ii){ii=p;p=sigue2[p];} if (sigue2[p]==ii){ii=p;p=sigue1[p];} copia_tramos(linea,linea1); Vect_destroy_line_struct(linea); } } } if (sigue1[i]==-99 && sigue2[i]==-99){ linea=Vect_new_line_struct(); Vect_read_line(&map,linea,cats,i); printf("linea %d tal cual\n",i); Vect_write_line(&map2,2,linea,cats); if(orden[i]==1) strcpy(colorin,"0:0:255"); if(orden[i]==2) strcpy(colorin,"255:255:0"); if(orden[i]==3) strcpy(colorin,"0:255:0"); if(orden[i]==4) strcpy(colorin,"255:0:0"); if(orden[i]==5) strcpy(colorin,"0:255:255"); if(orden[i]==6) strcpy(colorin,"255:0:255"); if(orden[i]>6) strcpy(colorin,"0:0:0"); sprintf(ordenc,"insert into %s values(%d,%d,%f,'%s');",name2,i,orden[i],longit[i],colorin); printf("%s\n",ordenc); db_set_string(&orden_sql,ordenc); printf("Ejecuta: %d\n",db_execute_immediate(driver, &orden_sql)); Vect_destroy_line_struct(linea); } } } //Enlace mapa-tabla Vect_map_add_dblink(&map2,1,NULL,name2,"cat",db_nombre,driver_nombre); Vect_build(&map2, stdout); //Estamos cerrando R_close_driver(); Vect_close(&map); Vect_close(&map2); if (driver){ //db_commit_transaction(driver); //Creo que garantiza que no queden operaciones a medio db_close_database(driver); db_shutdown_driver(driver); } }